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
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
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
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
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
def minSwaps(arr, N): | |
newArr = [ (arr[i], i) for i in range(len(arr)) ] | |
newArr.sort() | |
ans = 0 | |
for i in range(len(newArr)): | |
j = newArr[i][1] | |
while newArr[i][1] != newArr[j][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
# Definition for singly-linked list. | |
# class ListNode: | |
# def __init__(self, val=0, next=None): | |
# self.val = val | |
# self.next = next | |
class Solution: | |
def findMiddle(self, head): | |
slow, fast = head, head | |
while fast and fast.next and fast.next.next: |
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 compress(arr): | |
arr.sort(); | |
compress_map = {} | |
val = 1 | |
for el in arr: | |
if el not in compress_map: | |
compress_map[el] = val | |
val += 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
class Calendar { | |
constructor(selector){ | |
this.MONTHS= ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] | |
this.root = document.querySelector(selector); | |
this.activeDateTime = new Date(); | |
this.render(); | |
this.attachEvents(); | |
} | |
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 animate({ timing, duration, draw }){ | |
return new Promise((resolve, reject) => { | |
const start = performance.now(); | |
function animation(time){ | |
const progress = Math.min((time-start)/duration, 1); | |
const visualProgress = timing ? timing(progress): progress; | |
draw(visualProgress); | |
if(progress < 1) requestAnimationFrame(animation) |