Skip to content

Commit 20fff9d

Browse files
add 621
1 parent 6ae2059 commit 20fff9d

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Your ideas/fixes/algorithms are more than welcome!
2323
|625|[Minimum Factorization](https://leetcode.com/problems/minimum-factorization/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_625.java) | O(?) |O(?) | Medium |
2424
|624|[Maximum Distance in Arrays](https://leetcode.com/problems/maximum-distance-in-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_624.java) | O(nlogn) |O(1) | Easy | Sort, Array
2525
|623|[Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_623.java) | O(n) |O(h) | Medium | Tree
26+
|621|[Task Scheduler](https://leetcode.com/problems/task-scheduler/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_621.java) | O(n) |O(26) | Medium | Greedy, Queue
2627
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_617.java) | O(n) |O(h) | Easy | Tree, Recursion
2728
|616|[Add Bold Tag in String](https://leetcode.com/problems/add-bold-tag-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_616.java) | O(n*k) (n is length of string, k is size of dict) |O(n) | Medium | String
2829
|611|[Valid Triangle Number](https://leetcode.com/problems/valid-triangle-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_611.java) | O(n^2logn) |O(logn) | Medium | Binary Search
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.*;
4+
5+
/**
6+
* 621. Task Scheduler
7+
*
8+
* Given a char array representing tasks CPU need to do.
9+
* It contains capital letters A to Z where different letters represent different tasks.
10+
* Tasks could be done without original order.
11+
* Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.
12+
* However, there is a non-negative cooling interval n that means between two same tasks,
13+
* there must be at least n intervals that CPU are doing different tasks or just be idle.
14+
* You need to return the least number of intervals the CPU will take to finish all the given tasks.
15+
16+
Example 1:
17+
Input: tasks = ['A','A','A','B','B','B'], n = 2
18+
Output: 8
19+
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
20+
21+
Note:
22+
The number of tasks is in the range [1, 10000].
23+
The integer n is in the range [0, 100].
24+
*/
25+
public class _621 {
26+
27+
/**Could be simplified just using an array to record the frequencies of each letter, like this one:
28+
* https://leetcode.com/articles/task-scheduler/#approach-2-using-priority-queue-accepted*/
29+
public int leastInterval(char[] tasks, int n) {
30+
Map<Character, Integer> map = new HashMap<>();
31+
for (char c : tasks) {
32+
map.put(c, map.getOrDefault(c, 0) + 1);
33+
}
34+
PriorityQueue<Task> maxHeap = new PriorityQueue<>((a, b) -> b.total - a.total);
35+
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
36+
maxHeap.offer(new Task(entry.getValue(), entry.getKey()));
37+
}
38+
int times = 0;
39+
while (!maxHeap.isEmpty()) {
40+
int i = 0;
41+
List<Task> temp = new ArrayList<>();
42+
while (i <= n) {
43+
if (!maxHeap.isEmpty()) {
44+
if (maxHeap.peek().total > 1) {
45+
Task curr = maxHeap.poll();
46+
temp.add(new Task(curr.total-1, curr.character));
47+
} else {
48+
maxHeap.poll();
49+
}
50+
}
51+
times++;
52+
if (maxHeap.isEmpty() && temp.size() == 0) break;
53+
i++;
54+
}
55+
for (Task task : temp) {
56+
maxHeap.offer(task);
57+
}
58+
}
59+
return times;
60+
}
61+
62+
class Task {
63+
int total;
64+
char character;
65+
public Task(int total, char character) {
66+
this.total = total;
67+
this.character = character;
68+
}
69+
}
70+
71+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._621;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
/**
10+
* Created by stevesun on 6/19/17.
11+
*/
12+
public class _621Test {
13+
private static _621 test;
14+
private static char[] tasks;
15+
16+
@BeforeClass
17+
public static void setup(){
18+
test = new _621();
19+
}
20+
21+
@Test
22+
public void test1(){
23+
tasks = new char[]{'A','A','A','B','B','B'};
24+
assertEquals(8, test.leastInterval(tasks, 2));
25+
}
26+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy