Fork me on GitHub

力扣 1620. 网络信号最好的坐标

解法:暴力

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def bestCoordinate(towers, radius):
# 暴力枚举
dp = [[0] * 110 for _ in range(110)]
x, y, power = 0, 0, 0
for dx, dy, dpo in towers:
for i in range(max(0, dx - radius), dx + radius + 1):
for j in range(max(0, dy - radius), dy + radius + 1):
d = math.sqrt((dx - i) ** 2 + (dy - j) ** 2)
if d > radius:
continue
cur = math.floor(dpo / (1 + d))
dp[i][j] += cur
if dp[i][j] > power:
x, y = i, j
power = dp[i][j]
elif dp[i][j] == power and (i < x or (i == x and j < y)):
x, y = i, j
return [x, y]

力扣 1662. 检查两个字符串数组是否相等

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def arrayStringsAreEqual(word1, word2):
j, c_j, cur2, w2_num = 0, 0, word2[0], len(word2)
for i in range(len(word1)):
for c in word1[i]:
if c_j >= len(cur2):
j += 1
if j >= w2_num:
return False
cur2 = word2[j]
c_j = 0
# print(c, cur2[c_j])
if c != cur2[c_j]:
return False
c_j += 1
if j + 1 < w2_num or c_j < len(cur2):
return False
return True

力扣 481. 神奇字符串

解法: 暴力

1
2
3
4
5
6
7
8
9
10
11
12
def magicalString(n):
s = "122"
if n <= 3:
return 1
ret, i, add = 1, 2, "1"
while i < n:
if s[i] == "1":
ret += 1
s += add * int(s[i])
add = "1" if add == "2" else "2"
i += 1
return ret

力扣 784. 字母大小写全排列

解法:回溯

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
def letterCasePermutation(s):
# 回溯
ret, cur = [], []
def fun(i):
if i >= s_num:
ret.append("".join(cur))
return
if s[i].isdigit():
cur.append(s[i])
fun(i + 1)
cur.pop()
elif 65 <= ord(s[i]) <= 90: # 大写字母
cur.append(s[i])
fun(i + 1)
cur.pop()
cur.append(chr(ord(s[i]) + 32))
fun(i + 1)
cur.pop()
else: # 小写字母
cur.append(s[i])
fun(i + 1)
cur.pop()
cur.append(chr(ord(s[i]) - 32))
fun(i + 1)
cur.pop()
s_num = len(s)
fun(0)
return ret

力扣 347. 前K个高频元素

解法: 堆

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
def topKFrequent(nums, k):
def heap_up(i):
cur, parent = i, (i - 1) >> 1
while cur > 0 and heap[cur][1] >= heap[parent][1]:
heap[cur], heap[parent] = heap[parent], heap[cur]
heap_down(cur)
cur = parent
parent = (cur - 1) >> 1

def heap_down(i):
cur, left, right = i, 2 * i + 1, 2 * i + 2
while left < heap_num :
x, nex = heap[cur][1], cur
if left < heap_num and x < heap[left][1]:
nex = left
x = heap[left][1]
if right < heap_num and x < heap[right][1]:
nex = right
if cur != nex:
heap[cur], heap[nex] = heap[nex], heap[cur]
cur = nex
left, right = 2 * cur + 1, 2 * cur + 2
else:
break


# 堆排序
heap = []
heap_num = 0
for num in nums:
i = 0
while i < heap_num and heap[i][0] != num: i += 1
if i >= heap_num:
heap.append([num, 1])
heap_num += 1
else:
heap[i][1] += 1
# 上浮堆元素
heap_up(i)
# print(heap)
# 输出结果
ret = []
for i in range(k):
ret.append(heap[0][0])
heap[0], heap[-1] = heap[-1], heap[0]
heap.pop()
heap_num -= 1
heap_down(0)
# print(ret[-1], heap)
# print(ret)
return ret

力扣 907. 子数组的最小值之和

解法1: 贡献法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def sumSubarrayMins(arr):
# 单调栈贡献法
n = len(arr)
left, right= [-1] * n, [n] * n
stack = []
for i in range(n):
while stack and arr[stack[-1]] >= arr[i]:
right[stack.pop()] = i
stack.append(i)
stack = []
for i in range(n - 1 ,-1, -1):
while stack and arr[stack[-1]] > arr[i]:
left[stack.pop()] = i
stack.append(i)
ret = 0
for i in range(n):
ret += (i - left[i]) * (right[i] - i) * arr[i]
return ret % (10 ** 9 + 7)

力扣 337. 打家劫舍III

前置

1
2
3
4
5
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def rob(root):
# 后序遍历
"""
0: 代表没偷
1: 代表偷了
"""
def fun(node):
if not node: return 0, 0
l_pre_dp0, l_pre_dp1 = fun(node.left)
r_pre_dp0, r_pre_dp1 = fun(node.right)
dp0 = max(l_pre_dp0, l_pre_dp1) + max(r_pre_dp0, r_pre_dp1)
dp1 = node.val + l_pre_dp0 + r_pre_dp0
return dp0, dp1

return max(fun(root))
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信