随笔分类 - 算法 / 贪心
摘要:738.单调递增的数字 class Solution: def monotoneIncreasingDigits(self, n: int) -> int: # 主要思路当前数字比前面数字小时。前面数字 -1,当前数字变2为 9 str_n = str(n) for i in range(len(s
阅读全文
摘要:435. 无重叠区间 class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: intervals.sort(key=lambda x: x[0]) count = 0 for i in r
阅读全文
摘要: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
阅读全文
摘要:1005.K次取反后最大化的数组和 class Solution: def largestSumAfterKNegations(self, nums: List[int], k: int) -> int: nums.sort(key=lambda x:abs(x), reverse=True) fo
阅读全文
摘要: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
阅读全文
摘要:455.分发饼干 1、优先大饼干 class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() index = len(s) - 1 # 最后一块饼干 res =
阅读全文