Skip to content

Commit 90cb0fa

Browse files
committed
2019-11-24
1 parent 413ec73 commit 90cb0fa

File tree

8 files changed

+170
-0
lines changed

8 files changed

+170
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def lexicalOrder(self, n):
3+
"""
4+
:type n: int
5+
:rtype: List[int]
6+
"""
7+
return sorted(range(1, n + 1), key = str)
8+
# res = []
9+
10+
# def dfs(k):
11+
# if k > n:
12+
# return
13+
# res.append(k)
14+
15+
# for i in range(10):
16+
# dfs(10 * k + i)
17+
18+
# for i in range(1, 10):
19+
# dfs(i)
20+
# return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def maxLevelSum(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
from collections import deque
15+
queue = deque([root])
16+
17+
layer = 1
18+
res = 1
19+
max_values = 0
20+
while queue:
21+
values = 0
22+
for _ in range(len(queue)):
23+
node = queue.popleft()
24+
if node:
25+
values += node.val
26+
queue.append(node.left)
27+
queue.append(node.right)
28+
if values > max_values:
29+
max_values = values
30+
res = layer
31+
layer += 1
32+
return res
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution(object):
2+
def maxNumberOfApples(self, arr):
3+
"""
4+
:type arr: List[int]
5+
:rtype: int
6+
"""
7+
arr.sort()
8+
s = 0
9+
for i, num in enumerate(arr):
10+
if s + num > 5000:
11+
return i
12+
s += num
13+
return len(arr)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution(object):
2+
def smallestCommonElement(self, mat):
3+
"""
4+
:type mat: List[List[int]]
5+
:rtype: int
6+
"""
7+
from collections import Counter
8+
flatten = sum(mat,[])
9+
dic = Counter(flatten)
10+
for num in mat[0]:
11+
if dic[num] == len(mat):
12+
return num
13+
return -1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def minTimeToVisitAllPoints(self, points):
3+
"""
4+
:type points: List[List[int]]
5+
:rtype: int
6+
"""
7+
def helper(pair1, pair2):
8+
x0, y0 = pair1[0], pair1[1]
9+
x1, y1 = pair2[0], pair2[1]
10+
11+
return max(abs(y1 - y0), abs(x1 - x0))
12+
13+
res = 0
14+
for i, point in enumerate(points):
15+
if i > 0:
16+
res += helper(point, points[i - 1])
17+
18+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution(object):
2+
def countServers(self, grid):
3+
"""
4+
:type grid: List[List[int]]
5+
:rtype: int
6+
"""
7+
8+
m, n = len(grid), len(grid[0])
9+
res = 0
10+
for i in range(m):
11+
for j in range(n):
12+
if grid[i][j] == 1:
13+
# horizontal
14+
for col in range(n):
15+
if col != j:
16+
if grid[i][col] in [1, 2]:
17+
grid[i][j] = 2
18+
grid[i][col] = 2
19+
20+
# vertical
21+
for row in range(m):
22+
if row != i:
23+
if grid[row][j] in [1, 2]:
24+
grid[i][j] = 2
25+
grid[row][j] = 2
26+
if grid[i][j] == 2:
27+
res += 1
28+
# print grid
29+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def suggestedProducts(self, products, searchWord):
3+
"""
4+
:type products: List[str]
5+
:type searchWord: str
6+
:rtype: List[List[str]]
7+
"""
8+
products.sort()
9+
res = []
10+
prefix = ""
11+
for char in searchWord:
12+
tmp = []
13+
prefix += char
14+
idx = bisect.bisect_left(products, prefix) # 找当前prefix 起点,因为有序所以二分
15+
for word in products[idx:]:
16+
if len(tmp) >= 3 or word[:len(prefix)] > prefix:
17+
break
18+
if word[:len(prefix)] == prefix:
19+
tmp.append(word)
20+
res.append(tmp)
21+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
def numWays(self, steps, arrLen):
3+
"""
4+
:type steps: int
5+
:type arrLen: int
6+
:rtype: int
7+
"""
8+
# dp[i][j] 代表走 i 步,到位置 j 的解
9+
# dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] + dp[i - 1][j + 1]
10+
n = min(steps, arrLen)
11+
dp = [[0 for _ in range(n)] for _ in range(steps + 1)]
12+
mod = 10 ** 9 + 7
13+
14+
dp[0][0] = 1
15+
for i in range(1, steps + 1):
16+
for j in range(n):
17+
if j == 0:
18+
dp[i][j] += (dp[i - 1][0] + dp[i - 1][1]) % mod
19+
elif j == n - 1:
20+
dp[i][j] += (dp[i - 1][j] + dp[i - 1][j - 1]) % mod
21+
else:
22+
dp[i][j] += (dp[i - 1][j - 1] + dp[i - 1][j] + dp[i - 1][j + 1]) % mod
23+
return dp[steps][0]

0 commit comments

Comments
 (0)