Skip to content

Commit 1cb6732

Browse files
committed
fd
1 parent 5e9b951 commit 1cb6732

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@
379379
|560|[Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/)| [Java](leetcode/solution/src/SubarraySumEqualsK.java)|70||
380380
|606|[Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree/)|[Java](leetcode/solution/src/ConstructStringFromBinaryTree.java)|90||
381381
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)|[Java](leetcode/solution/src/MergeTwoBinaryTrees.java)|100|很简单|
382+
|621|[Task Scheduler](https://leetcode.com/problems/task-scheduler/)|[Java](leetcode/solution/src/TaskScheduler.java)|70||
382383
|637|[Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/#/description)| [Java](leetcode/solution/src/AverageOfLevelsInBinaryTree.java)|100||
383384
|652|[Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/)| [Java](leetcode/solution/src/FindDuplicateSubtrees.java)|70|开始还没思路|
384385
|653|[Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)| [Java](leetcode/solution/src/TwoSumIV.java)|90||
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.Arrays;
2+
3+
public class TaskScheduler {
4+
5+
/**
6+
* 这题关键是先按频率排序,最高频的优先占座,中间隔着stub,然后依次插次低频的
7+
* 有两种情况,
8+
* 一种是stub不够用,元素不但给stub替换完了,还要多塞元素,如AAAABBBBCCCCDDDD, n=2,这种情况结果是tasks.length
9+
* 第二种是stub太多了,元素替换不完,中间留了不少空,如AABB, n=3,这种情况结果是(count[25] - 1) * n + count[25] + 24 - i
10+
* 这里(count[25] - 1) * n是插入的stub总数,24 - i是除了count[25]外和count[25]同样频率的数的个数
11+
*/
12+
public int leastInterval(char[] tasks, int n) {
13+
int[] count = new int[26];
14+
for (char c : tasks) {
15+
count[c - 'A']++;
16+
}
17+
Arrays.sort(count);
18+
int i = 25;
19+
for (; i >= 0 && count[i] == count[25]; i--) ;
20+
// (count[25] - 1) * n + count[25] + 24 - i = (count[25] - 1) * (n + 1) + 25 - i
21+
return Math.max(tasks.length, (count[25] - 1) * (n + 1) + 25 - i);
22+
}
23+
}

leetcode/src/Main.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ public class Main {
44

55
public static class Solution {
66

7-
public int subarraySum(int[] nums, int k) {
8-
HashMap<Integer, Integer> map = new HashMap<>();
9-
map.put(0, 1);
10-
int count = 0;
11-
for (int i = 0, sum = 0; i < nums.length; i++) {
12-
sum += nums[i];
13-
map.put(sum, map.getOrDefault(sum, 0) + 1);
14-
count += map.getOrDefault(sum - k, 0);
7+
public int leastInterval(char[] tasks, int n) {
8+
int[] count = new int[26];
9+
for (char c : tasks) {
10+
count[c - 'A']++;
1511
}
16-
return count;
12+
Arrays.sort(count);
13+
int i = 25;
14+
for ( ; i >= 0 && count[i] == count[25]; i--);
15+
return Math.max(tasks.length, (count[25] - 1) * (n + 1) + 25 - i);
1716
}
1817
}
1918

0 commit comments

Comments
 (0)