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 longestSubsequence(self, arr: List[int], difference: int) -> int: | |
opt = [1] * (len(arr) + 1) | |
for i in range(1, len(arr)): | |
maxn = 0 | |
for j in range(0,i): | |
if arr[i] - arr[j] == difference: | |
if opt[j] > maxn: | |
maxn = opt[j] |
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 isMonotonic(self, A: List[int]) -> bool: | |
i, j = 0, len(A) - 1 | |
state_i, state_j = None, None | |
while i < len(A)-1 and j > 0: | |
current_state_i, current_state_j = None, None | |
if A[i] < A[i+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 Solution(object): | |
def peakIndexInMountainArray(self, A): | |
""" | |
:type A: List[int] | |
:rtype: int | |
""" | |
l = 0 | |
r = len(A) - 1 | |
peak = -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 Solution(object): | |
def findMedianSortedArrays(self, nums1, nums2): | |
""" | |
:type nums1: List[int] | |
:type nums2: List[int] | |
:rtype: float | |
""" | |
# 1 4 6 | |
# 2 3 5 | |
# 1 2 3 4 5 6 |
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(object): | |
def numRollsToTarget(self, d, f, target): | |
""" | |
:type d: int | |
:type f: int | |
:type target: int | |
:rtype: int | |
""" | |
ways = [0] | |
self.backtrackit(d, f, 0, target, ways) |
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 random import randrange | |
class RandomizedSet(object): | |
def __init__(self): | |
""" | |
Initialize your data structure here. | |
""" | |
self.list = list() | |
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 random import randrange | |
class RandomizedSet(object): | |
def __init__(self): | |
""" | |
Initialize your data structure here. | |
""" | |
self.list, self.pos = [], {} | |
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(object): | |
def permute(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: List[List[int]] | |
""" | |
res = [] | |
self.pahmute(nums,res, list(), 0) | |
return res | |
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 MaxHeap(object): | |
def __init__(self,items): | |
self.heap = [] | |
for item in items: | |
self.heappush(item) | |
def heappush(self,item): | |
heapq.heappush(self.heap, item * -1) | |
def heappop(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 graph_traverse(self, root, k): | |
parent = collections.deafultdict(set) | |
queue = [] | |
queue.append(root) | |
parent[root].add(None) | |
while queue: | |
cnode = queue.pop(0) | |
for node in cnode.adjacent(): | |
queue.append(node) | |
parent[node].add(node) |
NewerOlder