10000 refactor 358 · arnav1996/Leetcode-1@859adb3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 859adb3

Browse files
refactor 358
1 parent 98ee158 commit 859adb3

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

src/main/java/com/fishercoder/solutions/_358.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,36 @@
3030
*/
3131
public class _358 {
3232

33-
public String rearrangeString(String s, int k) {
34-
Map<Character, Integer> count = new HashMap<>();
35-
for (char c : s.toCharArray()) {
36-
count.put(c, count.getOrDefault(c, 0) + 1);
37-
}
33+
public static class Solution1 {
34+
public String rearrangeString(String s, int k) {
35+
Map<Character, Integer> count = new HashMap<>();
36+
for (char c : s.toCharArray()) {
37+
count.put(c, count.getOrDefault(c, 0) + 1);
38+
}
3839

39-
PriorityQueue<Map.Entry<Character, Integer>> heap = new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
40-
heap.addAll(count.entrySet());
40+
PriorityQueue<Map.Entry<Character, Integer>> heap =
41+
new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
42+
heap.addAll(count.entrySet());
4143

42-
Queue<Map.Entry<Character, Integer>> waitQueue = new LinkedList<>();
44+
Queue<Map.Entry<Character, Integer>> waitQueue = new LinkedList<>();
4345

44-
StringBuilder stringBuilder = new StringBuilder();
45-
while (!heap.isEmpty()) {
46-
Map.Entry<Character, Integer> entry = heap.poll();
47-
stringBuilder.append(entry.getKey());
48-
entry.setValue(entry.getValue() - 1);
49-
waitQueue.offer(entry);
50-
if (waitQueue.size() < k) {
51-
continue; //there's only k-1 chars in the waitHeap, not full yet
52-
}
53-
Map.Entry<Character, Integer> front = waitQueue.poll();
54-
if (front.getValue() > 0) {
55-
heap.offer(front);
46+
StringBuilder stringBuilder = new StringBuilder();
47+
while (!heap.isEmpty()) {
48+
Map.Entry<Character, Integer> entry = heap.poll();
49+
stringBuilder.append(entry.getKey());
50+
entry.setValue(entry.getValue() - 1);
51+
waitQueue.offer(entry);
52+
if (waitQueue.size() < k) {
53+
continue; //there's only k-1 chars in the waitHeap, not full yet
54+
}
55+
Map.Entry<Character, Integer> front = waitQueue.poll();
56+
if (front.getValue() > 0) {
57+
heap.offer(front);
58+
}
5659
}
57-
}
5860

59-
return stringBuilder.length() == s.length() ? stringBuilder.toString() : "";
61+
return stringBuilder.length() == s.length() ? stringBuilder.toString() : "";
62+
}
6063
}
6164

6265
}

src/test/java/com/fishercoder/_358Test.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@
99
*/
1010
public class _358Test {
1111

12-
private static _358 test;
12+
private static _358.Solution1 solution1;
1313

14-
@BeforeClass
15-
public static void setup() {
16-
test = new _358();
17-
}
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _358.Solution1();
17+
}
1818

19-
@Test
20-
public void test1() {
21-
System.out.println(test.rearrangeString("aabbcc", 3));
22-
}
19+
@Test
20+
public void test1() {
21+
System.out.println(solution1.rearrangeString("aabbcc", 3));
22+
}
2323

24-
@Test
25-
public void test2() {
26-
System.out.println(test.rearrangeString("aaabc", 3));
27-
}
24+
@Test
25+
public void test2() {
26+
System.out.println(solution1.rearrangeString("aaabc", 3));
27+
}
2828

29-
@Test
30-
public void test3() {
31-
System.out.println(test.rearrangeString("aaadbbcc", 2));
32-
}
29+
@Test
30+
public void test3() {
31+
System.out.println(solution1.rearrangeString("aaadbbcc", 2));
32+
}
3333
}

0 commit comments

Comments
 (0)
0