题目python
注意是子序列而不是子串!算法
因此只有三种可能的答案:数组
0:空串app
1:整个字符串是回文串rest
2:不是回文串code
class Solution: def removePalindromeSub(self, s: str) -> int: if not s: return 0 def is_p(i, j): while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True if is_p(0, len(s)-1): return 1 return 2
题目ci
简单leetcode
class Solution: def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]: # restaurants[i] = [idi, ratingi, veganFriendlyi, pricei, distancei] ans = [] for r in restaurants: if veganFriendly == 1 and r[2] == 0: continue if r[3] > maxPrice or r[4] > maxDistance: continue ans.append(r) ans.sort(key=lambda x: x[1]*1e5+x[0], reverse=True) return [x[0] for x in ans]
题目rem
Floyd(弗洛伊德)算法字符串
class Solution: def findTheCity(self, n: int, edges: List[List[int]], distanceThreshold: int) -> int: to = {} for i in range(n): to[i] = {} for e in edges: if e[2] <= distanceThreshold: if e[1] in to[e[0]]: to[e[0]][e[1]] = min(to[e[0]][e[1]], e[2]) else: to[e[0]][e[1]] = e[2] if e[0] in to[e[1]]: to[e[1]][e[0]] = min(to[e[1]][e[0]], e[2]) else: to[e[1]][e[0]] = e[2] flag = True while flag: flag = False for c in to: tl = list(to[c].items()) for d1, v1 in tl: for d2, v2 in to[d1].items(): if d2 == c: continue if v1 + v2 <= distanceThreshold: if d2 in to[c]: if v2 + v1 < to[c][d2]: flag = True to[c][d2] = v1 + v2 else: to[c][d2] = v1 + v2 flag = True mc = n + 1 mid = -1 for c in to: if len(to[c]) == mc and c > mid or len(to[c]) < mc: mc = len(to[c]) mid = c return mid
n 个工做,d 天,天天完成一个或多个工做,每日工做量是当日工做量最大的一个工做的值。工做必须顺序执行。
求:d 天完成全部工做最小工做量的和
二维数组 dp[i][j] 表明使用 i 天完成 j 个工做的最小工做量。(注意 dp 数组的第一列和第一行,也就是下标 0,是没有用的,也没有意义)
首先只有 j >= i 的时候才有解,不然返回 -1
因此要求的是 i 天里工做数从 i 到 n。
i 天作 j 个工做。能够是前 i - 1 天作了前 k 个工做,最后一天作了 剩余全部的工做(最后一天的工做量是:k 到 j 的最大值),因此
dp[i][j] = dp[i-1][j-1] + jobDifficulty[j-1] # 初始化成前 i-1 天作了 j-1 个工做,最后一天作最后一个工做 work = jobDifficulty[j-1] # work 是 k 到 j 的最大值 for k in range(j-2, i-2, -1): work = max(jobDifficulty[k], work) if dp[i-1][k] + work < dp[i][j]: # i-1 天作前 k 个工做,最后一天作 k 到 j 的工做。 dp[i][j] = dp[i-1][k] + work
初始化 1 天完成 1 个任务,2 个任务 ..... n 个任务。工做量天然就是这些任务中的最大值。
for i in range(2, jc+1): dp[1][i] = max(dp[1][i-1], jobDifficulty[i-1])
class Solution: def minDifficulty(self, jobDifficulty: List[int], d: int) -> int: jc = len(jobDifficulty) if d > jc: return -1 dp = [[-1 for i in range(jc+1)] for i in range(d+1)] dp[1][1] = jobDifficulty[0] for i in range(2, jc+1): dp[1][i] = max(dp[1][i-1], jobDifficulty[i-1]) for i in range(2, d+1): for j in range(i, jc+1): dp[i][j] = dp[i-1][j-1] + jobDifficulty[j-1] work = jobDifficulty[j-1] for k in range(j-2, i-2, -1): work = max(jobDifficulty[k], work) if dp[i-1][k] + work < dp[i][j]: dp[i][j] = dp[i-1][k] + work return dp[d][jc]
欢迎来个人博客: https://codeplot.top/
个人博客刷题分类:https://codeplot.top/categori...