leetcode第157场周赛总结
1-Play with Chips-easy。数组
There are some chips, and the i-th chip is at position chips[i].
You can perform any of the two following types of moves any number of times (possibly zero) on any chip:
Move the i-th chip by 2 units to the left or to the right with a cost of 0.
Move the i-th chip by 1 unit to the left or to the right with a cost of 1.
There can be two or more chips at the same position initially.
Return the minimum cost needed to move all the chips to the same position (any position).
Example 1:
Input: chips = [1,2,3]
Output: 1
Explanation: Second chip will be moved to positon 3 with cost 1. First chip will be moved to position 3 with cost 0. Total cost is 1.
Example 2:
Input: chips = [2,2,2,3,3]
Output: 2
Explanation: Both fourth and fifth chip will be moved to position two with cost 1. Total minimum cost will be 2.
Constraints:
1 <= chips.length <= 100
1 <= chips[i] <= 10^9
看chips长度,暴力应该能接受.
优化的解法是统计数组里的奇偶数的个数,奇数个数代表选定偶数坐标为终点时的cost,因为此时偶数之间transfer是没有cost的。偶数vice versa。巧妙
暴力
class Solution:
def minCostToMoveChips(self, chips: List[int]) -> int:
if not chips:
return 0
n = len(chips)
res = n+1
for i in chips:
cur = 0
for j in chips:
if abs(i-j) & 1:
cur += 1
res = min(res, cur)
return res
# 优化解法
class Solution:
def minCostToMoveChips(self, chips: List[int]) -> int:
if not chips:
return 0
n = len(chips)
l = r = 0
for i in chips:
if i & 1:
l += 1
else:
r += 1
return min(l, r)
2-Longest Arithmetic Subsequence of Given Difference-medium。哈希表、DP
Given an integer array arr and an integer difference, return the length of the longest subsequence in arr which is an arithmetic sequence such that the difference between adjacent elements in the subsequence equals difference.
Example 1:
Input: arr = [1,2,3,4], difference = 1
Output: 4
Explanation: The longest arithmetic subsequence is [1,2,3,4].
Example 2:
Input: arr = [1,3,5,7], difference = 1
Output: 1
Explanation: The longest arithmetic subsequence is any single element.
Example 3:
Input: arr = [1,5,7,8,5,3,4,2,1], difference = -2
Output: 4
Explanation: The longest arithmetic subsequence is [7,5,3,1].
Constraints:
1 <= arr.length <= 10^5
-10^4 <= arr[i], difference <= 10^4
看大神的解法是用字典记录每个元素,上一个valid的值对应的长度,很妙,因为这里是等差的,所以可以顺利索引到上一个值是什么,很巧妙。
class Solution:
def longestSubsequence(self, arr: List[int], difference: int) -> int:
if not arr:
return 0
n = len(arr)
dic = {}
res = 1
for n in arr:
dic[n] = dic.get(n-difference, 0) + 1
res = max(res, dic[n])
return res
3-Path with Maximum Gold-medium。BFS
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.
Return the maximum amount of gold you can collect under the conditions:
Every time you are located in a cell you will collect all the gold in that cell.
From your position you can walk one step to the left, right, up or down.
You can’t visit the same cell more than once.
Never visit a cell with 0 gold.
You can start and stop collecting gold from any position in the grid that has some gold.
Example 1:
Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
There are at most 25 cells containing gold.
比较典型的BFS题,和以往不同的是这里1是没有起始点,从任何一个非0点开始都可以也就意味着有多重拾金子的路径,2是没有设定目标点也就是没有结束判定,所以要自己设定结束判定,用典型的回溯法就能解决。
class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
ans = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] != 0:
ans = max(ans, self.dfs(i, j, grid))
return ans
def dfs(self, i, j, grid):
res = grid[i][j]
r = 0
temp = grid[i][j]
grid[i][j] = 0
direct = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for x, y in direct:
if 0 <= i + x < len(grid) and 0 <= j + y < len(grid[0]) and grid[i+x][j+y] != 0:
r = max(r, self.dfs(i+x, j+y, grid))
grid[i][j] = temp
return res + r
4-Count Vowels Permutation-hard。DP、BFS
Given an integer n, your task is to count how many strings of length n can be formed under the following rules:
Each character is a lower case vowel (‘a’, ‘e’, ‘i’, ‘o’, ‘u’)
Each vowel ‘a’ may only be followed by an ‘e’.
Each vowel ‘e’ may only be followed by an ‘a’ or an ‘i’.
Each vowel ‘i’ may not be followed by another ‘i’.
Each vowel ‘o’ may only be followed by an ‘i’ or a ‘u’.
Each vowel ‘u’ may only be followed by an ‘a’.
Since the answer may be too large, return it modulo 10^9 + 7.
Example 1:
Input: n = 1
Output: 5
Explanation: All possible strings are: “a”, “e”, “i” , “o” and “u”.
Example 2:
Input: n = 2
Output: 10
Explanation: All possible strings are: “ae”, “ea”, “ei”, “ia”, “ie”, “io”, “iu”, “oi”, “ou” and “ua”.
Example 3:
Input: n = 5
Output: 68
Constraints:
1 <= n <= 2 * 10^4
看看大神的做法,简洁精彩。其实是用动态规划的方法,在上一步的基础上不重不漏的列举每种排列,计算长度新增1时,共有多少总排列,一步步迭代即可求除最终答案。
class Solution:
def countVowelPermutation(self, n: int) -> int:
if n < 1:
return 0
mod = 10**9 + 7
a, e, i, o, u = 1, 1, 1, 1, 1
for _ in range(n-1):
a_ = e + i + u
e_ = a + i
i_ = e + o
o_ = i
u_ = o + i
a, e, i, o, u = a_, e_, i_, o_, u_
res = (a%mod + e%mod + i%mod + o%mod + u%mod) % mod
return res