-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution_400.py
158 lines (136 loc) · 4.27 KB
/
solution_400.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
class Solution(object):
### 400. Nth Digit ###
# @param {integer} n
# @return {integer}
def findNthDigit(self, n):
if not n: return 0
cnt, size = 9, 1
while cnt*size < n:
n -= cnt*size
cnt *= 10
size += 1
num = 10 ** (size-1) + (n-1) // size
return int(str(num)[(n-1)%size])
### 401. Binary Watch ###
# @param {integer} num
# @return {string[]}
def readBinaryWatch(self, num):
def dfs(cur, left, num, val, end, rec, up):
if cur == end or left == 0:
if left == 0 and val <= up: rec.append(val)
return
dfs(cur+1, left-1, num, val|(1<<cur), end, rec, up)
dfs(cur+1, left, num, val, end, rec, up)
res = []
for i in range(min(num+1,4)):
hour = []
dfs(0, i, i, 0, 4, hour, 11)
for x in hour:
if num-i >= 6: continue
minute = []
dfs(0, num-i, num-i, 0, 6, minute, 59)
for y in minute:
res.append("%d:%02d" % (x, y))
return res
### 402. Remove K Digits ###
# @param {string} num
# @param {integer} k
# @return {string}
def removeKdigits(self, num, k):
if not k: return num
if k >= len(num): return '0'
stack = []
for c in num:
while stack and stack[-1] > c and k:
stack.pop()
k -= 1
if stack or c != '0': stack.append(c)
while k and stack:
stack.pop()
k -= 1
return ''.join(stack) or '0'
### 403. Frog Jump ###
# @param {integer[]} stones
# @return {boolean}
def canCross(self, stones):
if not stones: return True
dp, rec = [[] for _ in range(len(stones))], {}
dp[0].append(0)
for i in range(len(stones)): rec[stones[i]] = i
for i in range(len(stones)):
for j in dp[i]:
for k in (j+1,j,j-1):
if k > 0 and stones[i]+k in rec and (not dp[rec[stones[i]+k]] or dp[rec[stones[i]+k]][-1] > k):
dp[rec[stones[i]+k]].append(k)
if rec[stones[i]+k] == len(stones)-1: return True
return False
### 404. Sum of Left Leaves ###
# @param {TreeNode} param
# @return {integer}
def sumOfLeftLeaves(self, root):
if not root: return 0
if not root.left and not root.right: return 0
if root.left and not root.left.left and not root.left.right: return root.left.val + self.sumOfLeftLeaves(root.right)
return self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)
### 405. Convert a Number to Hexadecimal ###
# @param {integer} num
# @return {string}
def toHex(self, num):
if not num: return '0'
if num < 0: num += 2**32
res = ''
while num:
cur = num & 15
if cur == 0: res = '0'+res
elif cur < 10: res = chr(cur+48)+res
else: res = chr(cur%10+97)+res
num >>= 4
return res
### 406. Queue Reconstruction by Height ###
# @param {integer[][]} people
# @return {integer[][]}
def reconstructQueue(self, people):
if not people: return []
res = []
for p in sorted(people, key=lambda x: (-x[0], x[1])):
res.insert(p[1], p)
return res
### 407. Trapping Rain Water II ###
# @param {integer[][]} heightMap
# @return {integer}
def trapRainWater(self, heightMap):
if not heightMap: return 0
import heapq
m, n = len(heightMap), len(heightMap[0])
queue, vis, res = [], [[False]*n for _ in range(m)], 0
for i in range(m):
queue.append((heightMap[i][0], (i,0)))
queue.append((heightMap[i][n-1], (i,n-1)))
vis[i][0] = vis[i][n-1] = True
for i in range(1,n-1):
queue.append((heightMap[0][i], (0,i)))
queue.append((heightMap[m-1][i], (m-1,i)))
vis[0][i] = vis[m-1][i] = True
heapq.heapify(queue)
while queue:
val, loc = heapq.heappop(queue)
res += val - heightMap[loc[0]][loc[1]]
for d in [[-1,0],[0,1],[1,0],[0,-1]]:
tx, ty = loc[0]+d[0], loc[1]+d[1]
if 0 < tx < m and 0 < ty < n and not vis[tx][ty]:
vis[tx][ty] = True
heapq.heappush(queue, (max(val, heightMap[tx][ty]), (tx, ty)))
return res
### 409. Longest Palindrome ###
# @param {string} s
# @return {integer}
def longestPalindrome(self, s):
if not s: return 0
cnt, odd, rec = 0, 0, {}
for c in s:
if c not in rec: rec[c] = 0
rec[c] += 1
for _, v in rec.items():
cnt += v // 2
if v & 1: odd = 1
return (cnt << 1) + odd