Post

Leetcode Day 27 - Greedy: Advanced Interval & Sequence Problems

56 Merge Intervals | 738 Monotone Increasing Digits | 968 Binary Tree Cameras

Greedy Algorithm

Merge Intervals

Link to Leetcode question1

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

A detailed explaination of solution can be found here2.

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

DiffSimilar QuestionsPythonJava
Medium57 Insert Interval3  
Hard715 Range Module4  

Monotone Increasing Digits

Link to Leetcode question5

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

A detailed explaination of solution can be found here6.

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

DiffSimilar QuestionsPythonJava
Medium402 Remove K Digits[^dtmnoall]  

Binary Tree Cameras

Link to Leetcode question7

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

Desktop View

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

Desktop View

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

A detailed explaination of solution can be found here8.

Similar Questions

DiffSimilar QuestionsPythonJava
Medium979 Distribute Coins in Binary Tree9  

Reference

This post is licensed under CC BY 4.0 by the author.