day31-greedy-part05-8.2
创始人
2024-11-14 15:04:59

tasks for today

1. 56.合并区间

2. 738.单调递增的数字

3. 968.监控二叉树(optional)

----------------------------------------------------------------

1. 56.合并区间 In this practice, the mindset is similar to the practices in yesterday's tasks, but the difference is, in this practice, we do not aim to find the intersection, instead, we aim to find the combination, this is reflected on the operation of "min & max"
class Solution:     def merge(self, intervals: List[List[int]]) -> List[List[int]]:         intervals.sort(key = lambda x: (x[0], x[1]))         result = []         sl, sr = intervals[0][0], intervals[0][1]         for i in intervals:             if i[0] > sr:                 result.append([sl,sr])                 sl, sr = i[0], i[1]             else:                 sl = min(sl, i[0])                 sr = max(sr, i[1])                  result.append([sl, sr])                  return result

2. 738.单调递增的数字

In this practice, the key mindset is: if the digits[i] < digits[i-1], the digits[i-1] will be deduct 1, and the all rest digits from i on should be changed to 9.

class Solution:     def monotoneIncreasingDigits(self, n: int) -> int:         if n == 0: return 0         digits = [int(i) for i in str(n)]         for i in range(len(digits) - 1, 0, -1):             if digits[i] < digits[i - 1]:                 digits[i - 1] -= 1                 for k in range(i, len(digits)):                     digits[k] = 9         result = 0         for i in digits:             result = 10*result + i          return result

相关内容

热门资讯

裸辞做“一人公司”,我后悔了 去年这个时候,一位以色列程序员正在东南亚旅行。他顺手把一个在脑子里转了很久的想法做成了产品,一个让任...
南京建成国内首个Pre-6G试... 4月21日,2026全球6G技术与产业生态大会在南京开幕。全息互动技术展台前,一名远在北京的工作人员...
超梵求职受邀参加“2025抖音... 超梵求职受邀参加“2025抖音巨量引擎成人教育行业生态大会”,探讨分享优质内容传播,服务万千学员。 ...
摩托罗拉Razr 2026(R... IT之家 4 月 22 日消息,摩托罗拉宣布新一代 Razr 折叠手机将于 4 月 29 日在美国发...
库克卸任,特纳斯领航:苹果新纪... 苹果首席执行官蒂姆·库克将卸任,硬件工程主管约翰·特纳斯将接任,苹果公司今天宣布此事。 库克将在夏季...