当前位置: 代码迷 >> 综合 >> leetcode刷题记录1211-1220 python版
  详细解决方案

leetcode刷题记录1211-1220 python版

热度:22   发布时间:2024-03-08 08:32:27.0

前言

继续leetcode刷题生涯
这里记录的都是笔者觉得有点意思的做法
参考了好几位大佬的题解,感谢各位大佬

1217. 玩筹码

class Solution:def minCostToMoveChips(self, position: List[int]) -> int:res = sum(i%2 for i in position)return min(res, len(position)-res)

1218. 最长定差子序列

class Solution:def longestSubsequence(self, arr: List[int], difference: int) -> int:dp = collections.defaultdict(int)for num in arr:dp[num] = max(dp[num], dp[num-difference] + 1)return max(dp.values())

1219. 黄金矿工

class Solution:def getMaximumGold(self, grid: List[List[int]]) -> int:self.res = 0m, n = len(grid), len(grid[0]) def backtrack(x, y, tmp):tmp += grid[x][y]self.res = max(self.res, tmp)cur, grid[x][y] = grid[x][y], 0for nx,ny in [[x+1,y],[x-1,y],[x,y+1],[x,y-1]]:if 0 <= nx < m and 0 <= ny < n and grid[nx][ny] != 0:backtrack(nx, ny, tmp)grid[x][y] = curfor i in range(m):for j in range(n):if grid[i][j] != 0:backtrack(i, j, 0)return self.res

1220. 统计元音字母序列的数目

class Solution:def countVowelPermutation(self, n: int) -> int:if n == 1: return 5a, e, i, o, u = 1, 1, 1, 1, 1for _ in range(2, n+1):a, e, i, o, u = (e+i+u) % 1000000007, (a+i) % 1000000007, (e+o) % 1000000007, i % 1000000007, (i+o) % 1000000007return (a + e + i + o + u) % 1000000007