随笔分类 -  算法 / 贪心

摘要:738.单调递增的数字 class Solution: def monotoneIncreasingDigits(self, n: int) -> int: # 主要思路当前数字比前面数字小时。前面数字 -1,当前数字变2为 9 str_n = str(n) for i in range(len(s 阅读全文
posted @ 2023-11-17 23:30 忆象峰飞 阅读(2) 评论(0) 推荐(0) 编辑
摘要:435. 无重叠区间 class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: intervals.sort(key=lambda x: x[0]) count = 0 for i in r 阅读全文
posted @ 2023-11-15 19:16 忆象峰飞 阅读(9) 评论(0) 推荐(0) 编辑
摘要:860.柠檬水找零 class Solution: def lemonadeChange(self, bills: List[int]) -> bool: five, ten, twenty = 0, 0, 0 for bill in bills: if bill == 5: five += 1 e 阅读全文
posted @ 2023-11-14 13:01 忆象峰飞 阅读(7) 评论(0) 推荐(0) 编辑
摘要:1005.K次取反后最大化的数组和 class Solution: def largestSumAfterKNegations(self, nums: List[int], k: int) -> int: nums.sort(key=lambda x:abs(x), reverse=True) fo 阅读全文
posted @ 2023-11-13 17:37 忆象峰飞 阅读(1) 评论(0) 推荐(0) 编辑
摘要:122.买卖股票的最佳时机 II 1、贪心 class Solution: def maxProfit(self, prices: List[int]) -> int: res = 0 for i in range(1, len(prices)): res += max(prices[i]-pric 阅读全文
posted @ 2023-11-13 16:03 忆象峰飞 阅读(3) 评论(0) 推荐(0) 编辑
摘要:455.分发饼干 1、优先大饼干 class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() index = len(s) - 1 # 最后一块饼干 res = 阅读全文
posted @ 2023-11-10 21:08 忆象峰飞 阅读(10) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示