435. Non-overlapping Intervals
Link: https://leetcode.com/problems/non-overlapping-intervals/
Description
Given an array of intervals intervals
where intervals[i] = [starti, endi]
, return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Approach
- Sort the
intervals
in ascending order accroding to the start. - Initialize
cnt
to store the intervals needed to be removed. - Loop through the sorted
intervals
from the second interval:- If the start of the current interval smaller than the end of the previous interval, increment
cnt
and save the smller value of end of the two intervals to the end of the current interval.
- If the start of the current interval smaller than the end of the previous interval, increment
- Return
cnt
.
Solution
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
int cnt = 0;
for (int i = 1; i < intervals.length; i++) {
if (intervals[i][0] < intervals[i - 1][1]) {
cnt++;
intervals[i][1] = Math.min(intervals[i][1], intervals[i - 1][1]);
}
}
return cnt;
}
}
763. Partition Labels
Link: https://leetcode.com/problems/partition-labels/
Description
You are given a string s
. We want to partition the string into as many parts as possible so that each letter appears in at most one part.
Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s
.
Return a list of integers representing the size of these parts.
Approach
- Loop throught
s
to find the last appearance of each letter and save them toedge
array. - Initialize
left
andright
as 0. - Loop through
s
from the first character:- Save the maximum value between
right
and the the value ofedge
of the current index toright
. - If right equals to the current index, save
right - left + 1
to the result list and changeleft
to the next index of the current index.
- Save the maximum value between
Solution
class Solution {
public List<Integer> partitionLabels(String s) {
List<Integer> list = new ArrayList<>();
int[] edge = new int[26];
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
edge[chars[i] - 'a'] = i;
}
int right = 0;
int left = 0;
for (int i = 0 ; i < chars.length; i++) {
right = Math.max(right, edge[chars[i] - 'a']);
if (right == i) {
list.add(right - left + 1);
left = right + 1;
}
}
return list;
}
}
56. Merge Intervals
Link: https://leetcode.com/problems/merge-intervals/
Description
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.
Approach
- Sort the
intervals
in ascending order accroding to the start. - Initialize a list to store the intervals and add the first element to the list. Save the first element as
last
(used to save the ). - Loop through the sorted
intervals
from the second interval:- If the start of the current interval larger than the end of
last
. Save it to the list andlast
. - Else, compare the start of
last
and the start of the current interval, save the smaller to one tolast
. Then compare the end oflast
and the end of the current interval, save the larger one tolast
.
- If the start of the current interval larger than the end of
- Return the array of
list
.
Solution
class Solution {
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
List<int[]> list = new ArrayList<>();
list.add(intervals[0]);
int[] last = intervals[0];
for (int i = 1; i < intervals.length; i++) {
if (intervals[i][0] > last[1]) {
list.add(intervals[i]);
last = intervals[i];
}
else {
list.remove(list.size() - 1);
last[0] = last[0] < intervals[i][0] ? last[0] : intervals[i][0];
last[1] = last[1] > intervals[i][1] ? last[1] : intervals[i][1];
list.add(last);
}
}
return list.toArray(new int[list.size()][]);
}
}