Leetcode Day 27 - Greedy: Advanced Interval & Sequence Problems
56 Merge Intervals | 738 Monotone Increasing Digits | 968 Binary Tree Cameras
Greedy Algorithm
Diff | Problem | Python | Java |
---|---|---|---|
56 Merge Intervals | ✅ | ||
738 Monotone Increasing Digits | ✅ | ||
968 Binary Tree Cameras |
Merge Intervals
Given an array of intervals where intervals[i] = [starti, endi]
, merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example 1
1
2
3
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
Example 2
1
2
3
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
Solution
Python
1
2
3
4
5
6
7
8
9
10
11
12
class Solution(object):
def merge(self, intervals):
merged = []
for start, end in sorted(intervals, key=lambda i: i[0]):
if len(merged) != 0 and start <= merged[-1][1]:
merged[-1][1] = max(end, merged[-1][1])
else:
merged.append([start, end])
return merged
Similar Questions
Diff | Similar Questions | Python | Java |
---|---|---|---|
57 Insert Interval3 | |||
715 Range Module4 |
Monotone Increasing Digits
An integer has monotone increasing digits if and only if each pair of adjacent digits x
and y
satisfy x <= y
.
Given an integer n
, return the largest number that is less than or equal to n
with monotone increasing digits.
Example 1
1
2
Input: n = 10
Output: 9
Example 2
1
2
Input: n = 1234
Output: 1234
Example 3
1
2
Input: n = 332
Output: 299
Solution
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution(object):
def monotoneIncreasingDigits(self, n):
ln = list(str(n))
if ln == sorted(ln):
return n
for i in range(len(ln) - 1, 0, -1):
if ln[i] < ln[i - 1]:
ln[i - 1] = str(int(ln[i - 1]) - 1)
for j in range(i, len(ln)):
ln[j] = '9'
return int(''.join(ln))
Similar Questions
Diff | Similar Questions | Python | Java |
---|---|---|---|
402 Remove K Digits[^dtmnoall] |
Binary Tree Cameras
You are given the root
of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children.
Return the minimum number of cameras needed to monitor all nodes of the tree.
Example 1
1
2
3
Input: root = [0,0,null,0,0]
Output: 1
Explanation: One camera is enough to monitor all nodes if placed as shown.
Example 2
1
2
3
Input: root = [0,0,null,0,null,0,null,null,0]
Output: 2
Explanation: At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.
Solution
Similar Questions
Diff | Similar Questions | Python | Java |
---|---|---|---|
979 Distribute Coins in Binary Tree9 |
Reference
Leetcode-56 Merge Intervals: https://leetcode.com/problems/merge-intervals/description/. ↩︎
代码随想录-合并区间: https://programmercarl.com/0056.合并区间.html. ↩︎
Leetcode-57 Insert Interval: https://leetcode.com/problems/insert-interval/description/. ↩︎
Leetcode-715 Range Module: https://leetcode.com/problems/range-module/. ↩︎
Leetcode-738 Monotone Increasing Digits: https://leetcode.com/problems/monotone-increasing-digits/description/. ↩︎
代码随想录-单调递增的数字: https://programmercarl.com/0738.单调递增的数字.html. ↩︎
Leetcode-968 Binary Tree Cameras: https://leetcode.com/problems/binary-tree-cameras/description/. ↩︎
代码随想录-监控二叉树: https://programmercarl.com/0968.监控二叉树.html. ↩︎
Leetcode-979 Distribute Coins in Binary Tree: https://leetcode.com/problems/distribute-coins-in-binary-tree/. ↩︎