Skip to content

Commit 78e93bd

Browse files
committed
410
410
1 parent 3e4a6bf commit 78e93bd

File tree

9 files changed

+374
-14
lines changed

9 files changed

+374
-14
lines changed

Python3/401.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
11
__________________________________________________________________________________________________
2-
2+
sample 24 ms submission
3+
class Solution:
4+
def readBinaryWatch(self, num: int) -> List[str]:
5+
out = []
6+
for i in range(12):
7+
for j in range(60):
8+
if bin(j).count('1') == num - bin(i).count('1'):
9+
out.append("{}:{:02d}".format(i, j))
10+
return out
311
__________________________________________________________________________________________________
4-
12+
sample 12940 kb submission
13+
class Solution:
14+
def readBinaryWatch(self, num: int) -> List[str]:
15+
16+
ans = []
17+
18+
def bitCount( n ):
19+
# compute the number of 1s in the binary representation of n
20+
21+
count = 0
22+
while n > 0:
23+
if n%2 == 1:
24+
count += 1
25+
n = n >> 1
26+
27+
return count
28+
29+
def convert(H, M):
30+
# convert to string time 0<= H < 12
31+
return str(H) + ":" + ( "0" if M < 10 else "" ) + str(M)
32+
33+
34+
for H in range(0, 12):
35+
for M in range(0, 60):
36+
if bitCount(H) + bitCount(M) == num:
37+
ans.append( convert(H, M) )
38+
39+
return ans
540
__________________________________________________________________________________________________

Python3/402.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
__________________________________________________________________________________________________
2-
2+
sample 32 ms submission
3+
#
4+
# @lc app=leetcode id=402 lang=python3
5+
#
6+
# [402] Remove K Digits
7+
#
8+
class Solution:
9+
def removeKdigits(self, num: str, k: int) -> str:
10+
out = []
11+
for c in num:
12+
while k > 0 and out and out[-1] > c:
13+
out.pop()
14+
k -= 1
15+
out.append(c)
16+
return ''.join(out[:-k or None]).lstrip('0') or '0'
317
__________________________________________________________________________________________________
4-
18+
sample 13060 kb submission
19+
class Solution:
20+
def removeKdigits(self, num: str, k: int) -> str:
21+
if k >= len(num):
22+
return '0'
23+
24+
changed = False
25+
while k > 0:
26+
for i in range(len(num) - 1):
27+
if num[i] > num[i+1]:
28+
num = num[:i] + num[i+1:]
29+
changed = True
30+
break
31+
32+
if not changed:
33+
num = num[:-1]
34+
k -= 1
35+
changed = False
36+
37+
i = 0
38+
while i < len(num) - 1 and num[i] == '0':
39+
i += 1
40+
41+
return num[i:]
542
__________________________________________________________________________________________________

Python3/403.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
__________________________________________________________________________________________________
2-
2+
sample 72 ms submission
3+
from collections import deque
4+
class Solution:
5+
def canCross(self, s: List[int]) -> bool:
6+
stones = set(s)
7+
step = 1
8+
for i in range(len(s) - 1):
9+
if s[i + 1] - s[i] > step: return False
10+
step += 1
11+
def helper(start, end, step):
12+
if start == end: return True
13+
if start not in stones: return False
14+
if helper(start + step + 1, end, step + 1): return True
15+
if helper(start + step, end, step): return True
16+
if step > 1 and helper(start + step - 1, end, step - 1): return True
17+
return False
18+
return helper(1, s[-1], 1)
319
__________________________________________________________________________________________________
4-
20+
sample 13376 kb submission
21+
class Solution:
22+
def canCross(self, stones: List[int]) -> bool:
23+
if (len(stones) - 1 > stones[-1]
24+
or stones[-1] > (len(stones) - 1)*(len(stones))/2):
25+
return False
26+
27+
last_stone = stones[-1] # keep the value of the last stone
28+
stones = set(stones) # convert the stones positions into set, to have O(1) check using "in"
29+
jumps_stack = [(1, 1)] # we are at the second stone with k = 1
30+
31+
while jumps_stack:
32+
current_stone = jumps_stack.pop() # pop the last element from stack
33+
pos = current_stone[0]
34+
k = current_stone[1]
35+
for dk in [-1,0,1]:
36+
candidate_stone = pos + k + dk
37+
if candidate_stone in stones:
38+
if pos + k + dk == last_stone: # if we have reached the last stone
39+
return True
40+
if k + dk > 0: # avoid situations when k = 1 and dk = -1, since sum is 0
41+
jumps_stack.append((candidate_stone, k + dk))
42+
return False
543
__________________________________________________________________________________________________

Python3/404.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,51 @@
11
__________________________________________________________________________________________________
2+
sample 20 ms submission
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, x):
6+
# self.val = x
7+
# self.left = None
8+
# self.right = None
29

10+
class Solution:
11+
def sumOfLeftLeaves(self, root):
12+
self.sum = 0
13+
def dfs(node):
14+
if not node:
15+
return
16+
if node.left:
17+
if not node.left.left and not node.left.right:
18+
self.sum += node.left.val
19+
else:
20+
dfs(node.left)
21+
if node.right:
22+
dfs(node.right)
23+
return
24+
dfs(root)
25+
return self.sum
326
__________________________________________________________________________________________________
27+
sample 13296 kb submission
28+
# Definition for a binary tree node.
29+
# class TreeNode:
30+
# def __init__(self, x):
31+
# self.val = x
32+
# self.left = None
33+
# self.right = None
434

35+
class Solution:
36+
def sumOfLeftLeaves(self, root: TreeNode) -> int:
37+
if not root:
38+
return 0
39+
ans = 0
40+
stack = [(root,False)]
41+
while stack:
42+
cur, side = stack.pop()
43+
if not cur.left and not cur.right and side:
44+
ans += cur.val
45+
continue
46+
if cur.left:
47+
stack.append((cur.left,True))
48+
if cur.right:
49+
stack.append((cur.right,False))
50+
return ans
551
__________________________________________________________________________________________________

Python3/405.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
__________________________________________________________________________________________________
2-
2+
sample 16 ms submission
3+
class Solution:
4+
def toHex(self, num: int) -> str:
5+
if num == 0: return '0'
6+
x = num.to_bytes(4, byteorder='big', signed=True).hex()
7+
count = 0
8+
for i in x:
9+
if i == '0':
10+
count += 1
11+
else:
12+
break
13+
return x[count:]
314
__________________________________________________________________________________________________
4-
15+
sample 13152 kb submission
16+
class Solution:
17+
def toHex(self, num: int) -> str:
18+
ans = ''
19+
if num < 0:
20+
num = 0x100000000 + num
21+
while num > 0:
22+
digit = num % 16
23+
if digit < 10:
24+
ans = chr(ord('0') + digit) + ans
25+
else:
26+
ans = chr(ord('a') + digit - 10) + ans
27+
num //= 16
28+
29+
if len(ans) == 0:
30+
return '0'
31+
else:
32+
return ans
533
__________________________________________________________________________________________________

Python3/406.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
__________________________________________________________________________________________________
2-
2+
sample 96 ms submission
3+
class Solution:
4+
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
5+
people.sort(key=lambda p: (-p[0], p[1]))
6+
7+
queue = []
8+
for person in people:
9+
queue.insert(person[1], person)
10+
11+
return queue
312
__________________________________________________________________________________________________
13+
sample 13332 kb submission
14+
from collections import deque
15+
class Solution:
16+
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
17+
order = deque()
18+
people.sort(key = lambda x: (-x[0], x[1]))
19+
for pair in people:
20+
order.insert(pair[1], pair)
421

22+
return list(order)
523
__________________________________________________________________________________________________

Python3/407.py

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,73 @@
11
__________________________________________________________________________________________________
2-
2+
sample 164 ms submission
3+
class Solution:
4+
def trapRainWater(self, heightMap: List[List[int]]) -> int:
5+
if not heightMap or not heightMap[0]:
6+
return 0
7+
m = len(heightMap)
8+
n = len(heightMap[0])
9+
10+
heap = []
11+
seen = [[False] * n for _ in range(m)]
12+
13+
# Puts the edges on the heap
14+
for i in range(m):
15+
heap.append( (heightMap[i][0], i, 0) )
16+
heap.append( (heightMap[i][n - 1], i, n - 1) )
17+
seen[i][0] = True
18+
seen[i][n - 1] = True
19+
for j in range(n):
20+
heap.append( (heightMap[0][j], 0, j) )
21+
heap.append( (heightMap[m - 1][j], m - 1, j) )
22+
seen[0][j] = True
23+
seen[m - 1][j] = True
24+
heapq.heapify(heap)
25+
26+
area = 0
27+
while heap:
28+
h, i, j = heapq.heappop(heap)
29+
if h > heightMap[i][j]:
30+
area += h - heightMap[i][j]
31+
32+
if i > 0 and not seen[i - 1][j]:
33+
heapq.heappush(heap, (max(h, heightMap[i - 1][j]), i - 1, j))
34+
seen[i - 1][j] = True
35+
if i + 1 < m and not seen[i + 1][j]:
36+
heapq.heappush(heap, (max(h, heightMap[i + 1][j]), i + 1, j))
37+
seen[i + 1][j] = True
38+
if j > 0 and not seen[i][j - 1]:
39+
heapq.heappush(heap, (max(h, heightMap[i][j - 1]), i, j - 1))
40+
seen[i][j - 1] = True
41+
if j + 1 < n and not seen[i][j + 1]:
42+
heapq.heappush(heap, (max(h, heightMap[i][j + 1]), i, j + 1))
43+
seen[i][j + 1] = True
44+
45+
return area
346
__________________________________________________________________________________________________
4-
47+
sample 14168 kb submission
48+
class Solution:
49+
def trapRainWater(self, heightMap):
50+
"""
51+
:type heightMap: List[List[int]]
52+
:rtype: int
53+
"""
54+
if not any(heightMap):
55+
return 0
56+
m, n = len(heightMap), len(heightMap[0])
57+
boundaries = []
58+
for i, row in enumerate(heightMap):
59+
for j, val in enumerate(row):
60+
if i in (0, m - 1) or j in (0, n - 1):
61+
boundaries.append((val, i, j))
62+
heightMap[i][j] = -1
63+
heapq.heapify(boundaries)
64+
ans = 0
65+
while boundaries:
66+
height, i, j = heapq.heappop(boundaries)
67+
for newi, newj in (i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1):
68+
if 0 <= newi < m and 0 <= newj < n and heightMap[newi][newj] != -1:
69+
ans += max(0, height - heightMap[newi][newj])
70+
heapq.heappush(boundaries, (max(height, heightMap[newi][newj]), newi, newj))
71+
heightMap[newi][newj] = -1
72+
return ans
573
__________________________________________________________________________________________________

Python3/409.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
__________________________________________________________________________________________________
2-
2+
sample 24 ms submission
3+
class Solution:
4+
def longestPalindrome(self, s: str) -> int:
5+
6+
counter = {}
7+
for c in s:
8+
if c in counter:
9+
counter[c] += 1
10+
else:
11+
counter[c] = 1
12+
13+
has_isolated = False
14+
15+
l = 0
16+
for _, v in counter.items():
17+
if v % 2 == 1:
18+
has_isolated = True
19+
l += v - 1
20+
else:
21+
l += v
22+
23+
if has_isolated:
24+
l += 1
25+
return l
326
__________________________________________________________________________________________________
27+
sample 13004 kb submission
28+
from collections import defaultdict
429

30+
class Solution:
31+
def longestPalindrome(self, s: str) -> int:
32+
d = defaultdict(int)
33+
for c in s:
34+
d[c] += 1
35+
36+
count = 0
37+
for v in d.values():
38+
n = v // 2 * 2
39+
count += n
40+
41+
if len(s) > count:
42+
count += 1
43+
44+
return count
545
__________________________________________________________________________________________________

0 commit comments

Comments
 (0)