File tree Expand file tree Collapse file tree 2 files changed +37
-32
lines changed Expand file tree Collapse file tree 2 files changed +37
-32
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int [] findPermutation (String s ) {
3
+ int [] ans = new int [s .length () + 1 ];
4
+ Stack <Integer > stack = new Stack <>();
5
+ int idx = 0 ;
6
+ for (int i = 1 ; i <= s .length (); i ++) {
7
+ if (s .charAt (i - 1 ) == 'I' ) {
8
+ stack .push (i );
9
+ while (!stack .isEmpty ()) {
10
+ ans [idx ++] = stack .pop ();
11
+ }
12
+ }
13
+ else {
14
+ stack .push (i );
15
+ }
16
+ }
17
+ stack .push (s .length () + 1 );
18
+ while (!stack .isEmpty ()) {
19
+ ans [idx ++] = stack .pop ();
20
+ }
21
+ return ans ;
22
+ }
23
+ }
Original file line number Diff line number Diff line change 1
- /**
2
- * Definition for an interval.
3
- * public class Interval {
4
- * int start;
5
- * int end;
6
- * Interval() { start = 0; end = 0; }
7
- * Interval(int s, int e) { start = s; end = e; }
8
- * }
9
- */
10
1
class Solution {
11
- public static int eraseOverlapIntervals (Interval [] intervals ) {
12
- if (intervals .length == 0 ) {
13
- return 0 ;
14
- }
15
-
16
- Arrays .sort (intervals , new Comparator <Interval >() {
17
- @ Override
18
- public int compare (Interval o1 , Interval o2 ) {
19
- return o1 .end - o2 .end ;
20
- }
21
- });
22
-
23
- int count = 1 ;
24
- int end = intervals [0 ].end ;
25
-
26
- for (int i =1 ; i <intervals .length ; i ++) {
27
- if (intervals [i ].start >= end ) {
28
- end = intervals [i ].end ;
29
- count ++;
30
- }
31
- }
32
-
33
- return intervals .length - count ;
2
+ public int eraseOverlapIntervals (int [][] intervals ) {
3
+ if (intervals .length == 0 ) {
4
+ return 0 ;
34
5
}
6
+ Arrays .sort (intervals , Comparator .comparingInt (o -> o [1 ]));
7
+ int notRemoved = 1 ;
8
+ int end = intervals [0 ][1 ];
9
+ for (int i = 1 ; i < intervals .length ; i ++) {
10
+ if (intervals [i ][0 ] >= end ) {
11
+ notRemoved ++;
12
+ end = intervals [i ][1 ];
13
+ }
14
+ }
15
+ return intervals .length - notRemoved ;
16
+ }
35
17
}
You can’t perform that action at this time.
0 commit comments