Skip to content

Commit 0196e44

Browse files
committed
added problems
1 parent 0a5f3b2 commit 0196e44

File tree

6 files changed

+135
-0
lines changed

6 files changed

+135
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from heapq import heappush, heappop
2+
3+
class MedianFinder:
4+
5+
def __init__(self):
6+
"""
7+
initialize your data structure here.
8+
"""
9+
self.max_heap = []
10+
self.min_heap = []
11+
12+
def addNum(self, num: int) -> None:
13+
if not self.max_heap or -self.max_heap[0] >= num:
14+
heappush(self.max_heap, -num)
15+
else:
16+
heappush(self.min_heap, num)
17+
18+
if len(self.max_heap) > len(self.min_heap) + 1:
19+
heappush(self.min_heap, -heappop(self.max_heap))
20+
elif len(self.max_heap) < len(self.min_heap):
21+
heappush(self.max_heap, -heappop(self.min_heap))
22+
23+
def findMedian(self) -> float:
24+
if len(self.max_heap) == len(self.min_heap):
25+
return (-self.max_heap[0] + self.min_heap[0]) / 2.0
26+
return -self.max_heap[0]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def subsets(self, nums):
3+
subsets = [[]]
4+
for num in nums:
5+
n = len(subsets)
6+
for i in range(n):
7+
subsets.append(subsets[i].copy() + [num])
8+
return subsets
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def subsetsWithDup(self, nums):
3+
nums.sort()
4+
subsets = [[]]
5+
endIdx = 0
6+
7+
for i in range(len(nums)):
8+
startIdx = 0
9+
if i > 0 and nums[i] == nums[i-1]:
10+
startIdx = endIdx + 1
11+
12+
endIdx = len(subsets) - 1
13+
for j in range(startIdx, endIdx+1):
14+
subset = subsets[j][:]
15+
subset.append(nums[i])
16+
subsets.append(subset)
17+
18+
return subsets
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def permute(self, nums):
3+
self.res = []
4+
self.backtrack(nums, [])
5+
return self.res
6+
7+
def backtrack(self, nums, path):
8+
if len(nums) == len(path):
9+
self.res.append(path[:])
10+
return
11+
12+
for num in nums:
13+
if num in path:
14+
continue
15+
16+
path.append(num)
17+
self.backtrack(nums, path)
18+
path.pop()
19+
20+
class Solution2:
21+
def permute(self, nums):
22+
self.res = []
23+
self.backtrack(nums, [])
24+
return self.res
25+
26+
def backtrack(self, nums, path):
27+
if not nums:
28+
self.res.append(path)
29+
return
30+
31+
for i in range(len(nums)):
32+
self.backtrack(nums[:i] + nums[i+1:], path + [nums[i]])
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def letterCasePermutation(self, S):
3+
res = [S]
4+
for i in range(len(S)):
5+
if S[i].isalpha():
6+
n = len(res)
7+
for j in range(n):
8+
string = res[j]
9+
res.append(string[:i] + string[i].swapcase() + string[i+1:])
10+
return res
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from collections import deque
2+
3+
class Solution:
4+
def generateParenthesis(self, n):
5+
res = []
6+
q = deque()
7+
q.append({'s': '', 'open': 0, 'close': 0})
8+
while q:
9+
cur = q.popleft()
10+
if cur['open'] == n and cur['close'] == n:
11+
res.append(cur['s'])
12+
else:
13+
if cur['open'] < n:
14+
new_s = cur['s'] + '('
15+
new_open_c = cur['open'] + 1
16+
q.append({'s': new_s,
17+
'open': new_open_c,
18+
'close': cur['close']})
19+
if cur['open'] > cur['close']:
20+
new_s = cur['s'] + ')'
21+
new_close_c = cur['close'] + 1
22+
q.append({'s': new_s,
23+
'open': cur['open'],
24+
'close': new_close_c})
25+
return res
26+
27+
class Solution2:
28+
def generateParenthesis(self, n):
29+
self.res = []
30+
self.n = n
31+
self.backtrack('', 0, 0)
32+
return self.res
33+
34+
def backtrack(self, s, left, right):
35+
if len(s) == self.n * 2:
36+
self.res.append(s)
37+
return
38+
if left < self.n:
39+
self.backtrack(s + '(', left+1, right)
40+
if left > right:
41+
self.backtrack(s + ')', left, right+1)

0 commit comments

Comments
 (0)