This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
def maxSlidingWindow(nums, window_size): | |
start, ans = 0, [] | |
q = deque(); | |
for end in range(len(nums)): | |
while q and nums[end] > nums[q[-1]]: q.pop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def getSum(self, nums_list, limit): | |
# accepts nums_list, array of numbers in reversed order eg, [ [1, 3], [3, 2] ] == 31+23 | |
res = [] | |
iters = [ iter(nums) for nums in nums_list]; | |
carry = 0 | |
for i in range(limit): | |
val = carry | |
for _iter in iters: val += next(_iter, 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getClosest(nums, k): | |
if k < nums[0]: return nums[0] | |
if k > nums[len(nums)-1]: return nums[len(nums)-1] | |
left, right = 0 , len(nums)-1 | |
while left <= right: | |
mid = left + (right-left)//2 | |
if nums[mid] > k: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def myPow(self, x: float, n: int) -> float: | |
ans, power = 1, abs(n) | |
while power: | |
if power % 2 == 0: | |
x *= x | |
power //= 2 | |
else: | |
ans *= x | |
power -= 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The rand7() API is already defined for you. | |
# def rand7(): | |
# @return a random integer in the range 1 to 7 | |
class Solution: | |
def generate(self): | |
row, col = rand7(), rand7() | |
return ((row-1) * 7 ) + col | |
def rand10(self): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sum(x, y){ | |
if(y === 0) return x | |
return sum( x^y, (x&y)<<1 ) | |
} | |
function multiply(x, y){ | |
if(x ===0 || y ===0) return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def sortColors(self, nums: List[int]) -> None: | |
""" | |
Do not return anything, modify nums in-place instead. | |
""" | |
start, end = 0, len(nums)-1 | |
i = start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self, val=None): | |
self.val = val | |
self.children = {} | |
self.count = 0. # number of times words with this prefix | |
self.end = 0 # number of times this word has been inserted | |
class Trie: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self, value): | |
self.value = value | |
self.rank = 1 | |
self.parent = self | |
class DisjointSet: | |
def __init__(self): | |
self.mapping = {} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getListMiddle(head){ | |
let slow = head; | |
let fast = head; | |
let moveBoth = false; | |
// to get second element, in case of two mid-nodes, use 'fast' instead of 'fast.next' | |
while(fast.next){ | |
if(moveBoth){ | |
slow = slow.next; | |
fast = fast.next; |