@@ -349,7 +349,8 @@ public int findContentChildren(int[] g, int[] s) {
349
349
Arrays . sort(s);
350
350
int gIndex = 0 , sIndex = 0 ;
351
351
while (gIndex < g. length && sIndex < s. length) {
352
- if (g[gIndex] <= s[sIndex]) gIndex++ ;
352
+ if (g[gIndex] <= s[sIndex])
353
+ gIndex++ ;
353
354
sIndex++ ;
354
355
}
355
356
return gIndex;
@@ -367,11 +368,10 @@ public int findContentChildren(int[] g, int[] s) {
367
368
``` java
368
369
public int maxProfit(int [] prices) {
369
370
int profit = 0 ;
370
- for (int i = 1 ; i < prices. length; i++ ) {
371
- if (prices[i] > prices[i - 1 ]) {
371
+ for (int i = 1 ; i < prices. length; i++ )
372
+ if (prices[i] > prices[i - 1 ])
372
373
profit += (prices[i] - prices[i - 1 ]);
373
- }
374
- }
374
+
375
375
return profit;
376
376
}
377
377
```
@@ -389,11 +389,13 @@ Output: True
389
389
390
390
``` java
391
391
public boolean canPlaceFlowers(int [] flowerbed, int n) {
392
+ int len = flowerbed. length;
392
393
int cnt = 0 ;
393
- for (int i = 0 ; i < flowerbed. length; i++ ) {
394
- if (flowerbed[i] == 1 ) continue ;
394
+ for (int i = 0 ; i < len; i++ ) {
395
+ if (flowerbed[i] == 1 )
396
+ continue ;
395
397
int pre = i == 0 ? 0 : flowerbed[i - 1 ];
396
- int next = i == flowerbed . length - 1 ? 0 : flowerbed[i + 1 ];
398
+ int next = i == len - 1 ? 0 : flowerbed[i + 1 ];
397
399
if (pre == 0 && next == 0 ) {
398
400
cnt++ ;
399
401
flowerbed[i] = 1 ;
@@ -415,17 +417,19 @@ Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
415
417
416
418
题目描述:判断一个数组能不能只修改一个数就成为非递减数组。
417
419
418
- 在出现 nums[ i] < nums[ i - 1] 时,需要考虑的是应该修改数组的哪个数,使得本次修改能使 i 之前的数组成为非递减数组,并且 ** 不影响后续的操作** 。优先考虑令 nums[ i - 1] = nums[ i] ,因为如果修改 nums[ i] = nums[ i - 1] 的话,那么 nums[ i] 这个数会变大,就有可能比 nums[ i + 1] 大,从而影响了后续操作。还有一个比较特别的情况就是 nums[ i] < nums[ i - 2] ,只修改 nums[ i - 1] = nums[ i] 不能令数组成为非递减,只能通过修改 nums[ i] = nums[ i - 1] 才行 。
420
+ 在出现 nums[ i] < nums[ i - 1] 时,需要考虑的是应该修改数组的哪个数,使得本次修改能使 i 之前的数组成为非递减数组,并且 ** 不影响后续的操作** 。优先考虑令 nums[ i - 1] = nums[ i] ,因为如果修改 nums[ i] = nums[ i - 1] 的话,那么 nums[ i] 这个数会变大,就有可能比 nums[ i + 1] 大,从而影响了后续操作。还有一个比较特别的情况就是 nums[ i] < nums[ i - 2] ,只修改 nums[ i - 1] = nums[ i] 不能使数组成为非递减数组,只能修改 nums[ i] = nums[ i - 1] 。
419
421
420
422
``` java
421
423
public boolean checkPossibility(int [] nums) {
422
424
int cnt = 0 ;
423
- for (int i = 1 ; i < nums. length; i++ ) {
424
- if (nums[i] < nums[i - 1 ]) {
425
- cnt++ ;
426
- if (i - 2 >= 0 && nums[i - 2 ] > nums[i]) nums[i] = nums[i - 1 ];
427
- else nums[i - 1 ] = nums[i];
428
- }
425
+ for (int i = 1 ; i < nums. length && cnt < 2 ; i++ ) {
426
+ if (nums[i] >= nums[i - 1 ])
427
+ continue ;
428
+ cnt++ ;
429
+ if (i - 2 >= 0 && nums[i - 2 ] > nums[i])
430
+ nums[i] = nums[i - 1 ];
431
+ else
432
+ nums[i - 1 ] = nums[i];
429
433
}
430
434
return cnt <= 1 ;
431
435
}
@@ -442,15 +446,61 @@ Return true.
442
446
443
447
``` java
444
448
public boolean isSubsequence(String s, String t) {
445
- int pos = - 1 ;
449
+ int index = - 1 ;
446
450
for (char c : s. toCharArray()) {
447
- pos = t. indexOf(c, pos + 1 );
448
- if (pos == - 1 ) return false ;
451
+ index = t. indexOf(c, index + 1 );
452
+ if (index == - 1 )
453
+ return false ;
449
454
}
450
455
return true ;
451
456
}
452
457
```
453
458
459
+ ** 不重叠的区间个数**
460
+
461
+ [ 435. Non-overlapping Intervals (Medium)] ( https://leetcode.com/problems/non-overlapping-intervals/description/ )
462
+
463
+ ``` html
464
+ Input: [ [1,2], [1,2], [1,2] ]
465
+
466
+ Output: 2
467
+
468
+ Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.
469
+ ```
470
+
471
+ ``` html
472
+ Input: [ [1,2], [2,3] ]
473
+
474
+ Output: 0
475
+
476
+ Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
477
+ ```
478
+
479
+ 题目描述:计算让一组区间不重叠所需要移除的区间个数。
480
+
481
+ 直接计算最多能组成的不重叠区间个数即可。
482
+
483
+ 在每次选择中,区间的结尾最为重要,选择的区间结尾越小,留给后面的区间的空间越大,那么后面能够选择的区间个数也就越大。
484
+
485
+ 按区间的结尾进行排序,每次选择结尾最小,并且和前一个区间不重叠的区间。
486
+
487
+ ``` java
488
+ public int eraseOverlapIntervals(Interval [] intervals) {
489
+ if (intervals. length == 0 )
490
+ return 0 ;
491
+ Arrays . sort(intervals, Comparator . comparingInt(o - > o. end));
492
+ int cnt = 1 ;
493
+ int end = intervals[0 ]. end;
494
+ for (int i = 1 ; i < intervals. length; i++ ) {
495
+ if (intervals[i]. start < end)
496
+ continue ;
497
+ end = intervals[i]. end;
498
+ cnt++ ;
499
+ }
500
+ return intervals. length - cnt;
501
+ }
502
+ ```
503
+
454
504
** 投飞镖刺破气球**
455
505
456
506
[ 452. Minimum Number of Arrows to Burst Balloons (Medium)] ( https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/ )
@@ -465,31 +515,27 @@ Output:
465
515
466
516
题目描述:气球在一个水平数轴上摆放,可以重叠,飞镖垂直投向坐标轴,使得路径上的气球都会刺破。求解最小的投飞镖次数使所有气球都被刺破。
467
517
468
- 对气球按末尾位置进行排序,得到:
469
-
470
- ``` html
471
- [[1,6], [2,8], [7,12], [10,16]]
472
- ```
473
-
474
- 如果让飞镖投向 6 这个位置,那么 [ 1,6] 和 [ 2,8] 这两个气球都会被刺破,这种方式下刺破这两个气球的投飞镖次数最少,并且后面两个气球依然可以使用这种方式来刺破。
518
+ 也是计算不重叠的区间个数,不过和 Non-overlapping Intervals 的区别在于,[ 1, 2] 和 [ 2, 3] 在本题中算是重叠区间。
475
519
476
520
``` java
477
521
public int findMinArrowShots(int [][] points) {
478
- if (points. length == 0 ) return 0 ;
479
- Arrays . sort(points, (a, b) - > (a[1 ] - b[1 ]));
480
- int curPos = points[0 ][1 ];
481
- int shots = 1 ;
522
+ if (points. length == 0 )
523
+ return 0 ;
524
+
525
+ Arrays . sort(points, Comparator . comparingInt(o - > o[1 ]));
526
+
527
+ int cnt = 1 , end = points[0 ][1 ];
482
528
for (int i = 1 ; i < points. length; i++ ) {
483
- if (points[i][0 ] <= curPos) {
529
+ if (points[i][0 ] <= end) // [1,2] 和 [2,3] 算重叠
484
530
continue ;
485
- }
486
- curPos = points[i][1 ];
487
- shots++ ;
531
+ cnt++ ;
532
+ end = points[i][1 ];
488
533
}
489
- return shots ;
534
+ return cnt ;
490
535
}
491
536
```
492
537
538
+
493
539
** 分隔字符串使同种字符出现在一起**
494
540
495
541
[ 763. Partition Labels (Medium)] ( https://leetcode.com/problems/partition-labels/description/ )
@@ -504,25 +550,25 @@ A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits
504
550
```
505
551
506
552
``` java
507
- public List<Integer > partitionLabels(String S ) {
508
- List< Integer > partitions = new ArrayList<> () ;
509
- int [] lastIndexs = new int [ 26 ];
510
- for ( int i = 0 ; i < S . length(); i ++ ) {
511
- lastIndexs[ S . charAt(i) - ' a ' ] = i;
512
- }
513
- int firstIndex = 0 ;
514
- while (firstIndex < S . length()) {
515
- int lastIndex = firstIndex;
516
- for (int i = firstIndex; i < S . length() && i <= lastIndex; i++ ) {
517
- int index = lastIndexs[S . charAt(i) - ' a' ];
518
- if (index == i) continue ;
519
- if (index > lastIndex) lastIndex = index;
520
- }
521
- partitions . add(lastIndex - firstIndex + 1 );
522
- firstIndex = lastIndex + 1 ;
523
- }
524
- return partitions ;
525
- }
553
+ public List<Integer > partitionLabels(String S ) {
554
+ int [] lastIndexs = new int [ 26 ] ;
555
+ for ( int i = 0 ; i < S . length(); i ++ )
556
+ lastIndexs[ S . charAt(i) - ' a ' ] = i;
557
+
558
+ List< Integer > ret = new ArrayList<> ();
559
+ int firstIndex = 0 ;
560
+ while (firstIndex < S . length()) {
561
+ int lastIndex = firstIndex;
562
+ for (int i = firstIndex; i < S . length() && i <= lastIndex; i++ ) {
563
+ int index = lastIndexs[S . charAt(i) - ' a' ];
564
+ if (index > lastIndex)
565
+ lastIndex = index;
566
+ }
567
+ ret . add(lastIndex - firstIndex + 1 );
568
+ firstIndex = lastIndex + 1 ;
569
+ }
570
+ return ret ;
571
+ }
526
572
```
527
573
528
574
** 根据身高和序号重组队列**
@@ -545,25 +591,17 @@ Output:
545
591
546
592
``` java
547
593
public int [][] reconstructQueue(int [][] people) {
548
- if (people == null || people. length == 0 || people[0 ]. length == 0 ) return new int [0 ][0 ];
549
- Arrays . sort(people, (a, b) - > {
550
- if (a[0 ] == b[0 ]) return a[1 ] - b[1 ];
551
- return b[0 ] - a[0 ];
552
- });
553
- int N = people. length;
554
- List<int[]> tmp = new ArrayList<> ();
555
- for (int i = 0 ; i < N ; i++ ) {
556
- int index = people[i][1 ];
557
- int [] p = new int []{people[i][0 ], people[i][1 ]};
558
- tmp. add(index, p);
559
- }
560
-
561
- int [][] ret = new int [N ][2 ];
562
- for (int i = 0 ; i < N ; i++ ) {
563
- ret[i][0 ] = tmp. get(i)[0 ];
564
- ret[i][1 ] = tmp. get(i)[1 ];
565
- }
566
- return ret;
594
+ if (people == null || people. length == 0 || people[0 ]. length == 0 )
595
+ return new int [0 ][0 ];
596
+
597
+ Arrays . sort(people, (a, b) - > (a[0 ] == b[0 ] ? a[1 ] - b[1 ] : b[0 ] - a[0 ]));
598
+
599
+ List<int[]> queue = new ArrayList<> ();
600
+
601
+ for (int [] p : people)
602
+ queue. add(p[1 ], p);
603
+
604
+ return queue. toArray(new int [queue. size()][]);
567
605
}
568
606
```
569
607
0 commit comments