-
Notifications
You must be signed in to change notification settings - Fork 2
/
56.merge-intervals.java
113 lines (98 loc) · 2.57 KB
/
56.merge-intervals.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* @lc app=leetcode id=56 lang=java
*
* [56] Merge Intervals
*
* https://leetcode.com/problems/merge-intervals/description/
*
* algorithms
* Medium (39.42%)
* Likes: 5228
* Dislikes: 323
* Total Accepted: 684.5K
* Total Submissions: 1.7M
* Testcase Example: '[[1,3],[2,6],[8,10],[15,18]]'
*
* Given a collection of intervals, merge all overlapping intervals.
*
* Example 1:
*
*
* 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] overlaps, merge them into
* [1,6].
*
*
* Example 2:
*
*
* Input: intervals = [[1,4],[4,5]]
* Output: [[1,5]]
* Explanation: Intervals [1,4] and [4,5] are considered overlapping.
*
* NOTE: input types have been changed on April 15, 2019. Please reset to
* default code definition to get new method signature.
*
*
* Constraints:
*
*
* intervals[i][0] <= intervals[i][1]
*
*
*/
// @lc code=start
class Solution {
public int[][] merge(int[][] intervals) {
// if(intervals.length < 2)
// return intervals;
// Arrays.sort(intervals,new Comparator<int[]>(){
// public int compare(int[] a1,int[] a2){
// return a1[0]-a2[0];
// }
// });
// ArrayList<int[]> result = new ArrayList<>();
// int s1 = intervals[0][0];
// int e1 = intervals[0][1];
// for(int i=1;i<intervals.length;i++){
// int s2 = intervals[i][0];
// int e2 = intervals[i][1];
// if(s2>e1){
// result.add(new int[]{s1,e1});
// s1=s2;
// e1=e2;
// }
// else{
// e1 = Math.max(e1,e2);
// }
// }
// result.add(new int[]{s1,e1});
// int[][] r = new int[result.size()][];
// for(int i=0 ; i<result.size();i++){
// r[i] = result.get(i);
// }
// return r;
if (intervals.length <= 1)
return intervals;
Arrays.sort(intervals,new Comparator<int[]>(){
public int compare(int[] a1,int[] a2){
return a1[0]-a2[0];
}
});
ArrayList<int[]> result = new ArrayList<>();
int[] newInterval = intervals[0];
result.add(newInterval);
for(int[] interval : intervals){
if(interval[0]<=newInterval[1]){//Overlapping interval move the end:
newInterval[1] = Math.max(interval[1],newInterval[1]);
}
else{
newInterval = interval;
result.add(newInterval);
}
}
return result.toArray(new int[result.size()][]);
}
}
// @lc code=end