2022年6月29日

LeetCode198 打家劫舍

摘要: LeetCode198 打家劫舍 $dp[i]$ 表示前 $i$ 个房间带来的收益,这里不一定要选择第 $i$ 个房间 class Solution: def rob(self, nums: List[int]) -> int: l = len(nums) if l == 0: return 0 i 阅读全文

posted @ 2022-06-29 22:27 solvit 阅读(37) 评论(0) 推荐(0)

LeetCode357 统计各位数字都不同的数字个数

摘要: LeetCode357 统计各位数字都不同的数字个数 注意0的位置,分不同位数单独计算 class Solution: def countNumbersWithUniqueDigits(self, n: int) -> int: if n == 0: return 1 if n == 1: retu 阅读全文

posted @ 2022-06-29 21:08 solvit 阅读(27) 评论(0) 推荐(0)

LeetCode319 灯泡开关

摘要: LeetCode319 灯泡开关 等价于求小于等于n的平方数的个数 class Solution: def bulbSwitch(self, n: int) -> int: if n == 0: return 0 i, ans = 1, 0 while i * i <= n: ans, i = an 阅读全文

posted @ 2022-06-29 20:03 solvit 阅读(17) 评论(0) 推荐(0)

LeetCode233 数字 1 的个数

摘要: LeetCode233 数字 1 的个数 枚举每一个数位为1时存在多少种满足小于等于n的数字 当前数位大于1,不存在限制,存在数字个数为左边 $\times$ 右边 当前数位等于1,右边存在限制,当左边等于n的部分时,右边必须满足小于等于n的条件 当前数位等于0,右边存在限制,当左边等于n的部分时, 阅读全文

posted @ 2022-06-29 19:30 solvit 阅读(32) 评论(0) 推荐(0)

LeetCode188 买卖股票的最佳时机 IV(dp)

摘要: LeetCode188 买卖股票的最佳时机 IV 对于第 $i$ 只股票,可以进行的操作为作为第 $j$ 只买入或卖出的股票 $buy[i][j]$ 表示对于前 $i$ 只股票,第 $j$ 次买入股票之后的最大收益 $sell[i][j]$ 表示对于前 $i$ 只股票,第 $j$ 次卖出股票之后的最 阅读全文

posted @ 2022-06-29 16:16 solvit 阅读(31) 评论(0) 推荐(0)

LeetCode123 买卖股票的最佳时机 III(贪心)

摘要: LeetCode123 买卖股票的最佳时机 III 最多可以完成两笔交易,即分两段做LeetCode121,处理前缀后缀之后,遍历每一个分段节点所带来的收益取最大 class Solution: def maxProfit(self, prices: List[int]) -> int: pre_g 阅读全文

posted @ 2022-06-29 14:31 solvit 阅读(34) 评论(0) 推荐(0)

LeetCode122 买卖股票的最佳时机 II(贪心)

摘要: LeetCode122 买卖股票的最佳时机 II 贪心计算爬峰收益 class Solution: def maxProfit(self, prices: List[int]) -> int: ans, l = 0, len(prices) for i in range(1, l): ans += 阅读全文

posted @ 2022-06-29 13:29 solvit 阅读(24) 评论(0) 推荐(0)

LeetCode121 买卖股票的最佳时机(贪心)

摘要: LeetCode121 买卖股票的最佳时机 维护当前股票价格最小值 class Solution: def maxProfit(self, prices: List[int]) -> int: ans, cur_min, l = 0, prices[0], len(prices) for i in 阅读全文

posted @ 2022-06-29 13:08 solvit 阅读(25) 评论(0) 推荐(0)

LeetCode124 二叉树中的最大路径和(dfs)

摘要: LeetCode124 二叉树中的最大路径和 dfs左右子节点的最大收益 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self 阅读全文

posted @ 2022-06-29 12:54 solvit 阅读(34) 评论(0) 推荐(0)

LeetCode115 不同的子序列(dp)

摘要: LeetCode115 不同的子序列 $dp[i][j]$ 表示字符串 $s[:i]$ 中包子序列 $t[:j]$ 的数量 对于当前字符 $s[i]$ 与 $t[j]$: 如果 $s[i] == t[j]$,$dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]$ 如 阅读全文

posted @ 2022-06-29 12:35 solvit 阅读(31) 评论(0) 推荐(0)

LeetCode69 x的平方根 (二分)

摘要: LeetCode69 x的平方根 class Solution(object): def mySqrt(self, x): """ :type x: int :rtype: int """ l, r, ans = 0, x, -1 while l <= r: mid = (l + r) // 2 i 阅读全文

posted @ 2022-06-29 11:00 solvit 阅读(27) 评论(0) 推荐(0)

LeetCode72 编辑距离(dp)

摘要: LeetCode72 编辑距离 对单词A删除一个等价于对单词B插入一个字符,基于此我们仅考虑如下三种操作: 对单词A插入一个字符 对单词B插入一个字符 对单词A替换一个字符 class Solution(object): def minDistance(self, word1, word2): "" 阅读全文

posted @ 2022-06-29 10:50 solvit 阅读(31) 评论(0) 推荐(0)

LeetCode刷题记录

摘要: 贪心算法 LeetCode3 无重复字符的最长子串 LeetCode31 下一个排列 LeetCode53 最大子数组和 LeetCode55 跳跃游戏 LeetCode121 买卖股票的最佳时机 LeetCode122 买卖股票的最佳时机 II LeetCode123 买卖股票的最佳时机 III 阅读全文

posted @ 2022-06-29 10:36 solvit 阅读(49) 评论(0) 推荐(0)

LeetCode55 跳跃游戏(贪心)

摘要: LeetCode55 跳跃游戏 顺序维护当前位置可到达最远位置,判断其是否可到达最后一个下标。 class Solution: def canJump(self, nums: List[int]) -> bool: l, max_pos = len(nums), 0 for i in range(l 阅读全文

posted @ 2022-06-29 10:34 solvit 阅读(24) 评论(0) 推荐(0)

导航