Skip to content

Commit 81ae169

Browse files
committed
add 767 java
1 parent e1caac5 commit 81ae169

File tree

5 files changed

+352
-11
lines changed

5 files changed

+352
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@
11341134
659 | [Split Array into Consecutive Subsequences](https://leetcode.com/problems/split-array-into-consecutive-subsequences/) | [Python](./leetcode_python/Greedy/split-array-into-consecutive-subsequences.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/SplitArrayIntoConsecutiveSubsequences.java) | _O(n)_ | _O(1)_ | Medium | map, pq, greedy, dp, `google`| AGAIN***** (1)
11351135
738 | [Monotone Increasing Digits](https://leetcode.com/problems/monotone-increasing-digits/) | [Python](./leetcode_python/Greedy/monotone-increasing-digits.py) | _O(1)_ | _O(1)_ | Medium | good trick, greedy, string, trick, `Amazon`| AGAIN********* (4)
11361136
763 | [Partition Labels](https://leetcode.com/problems/partition-labels/) | [Python](./leetcode_python/Greedy/partition-labels.py) | _O(n)_ | _O(n)_ | Medium |greedy,trick,dict,index,`amazon`| AGAIN******* (5) (AGAIN)
1137-
767 | [Reorganize String](https://leetcode.com/problems/reorganize-string/) | [Python](./leetcode_python/Greedy/reorganize-string.py) | _O(n)_ | _O(1)_ | Medium |`greedy`,`counter`,`good trick`,`amazon`,`fb`| AGAIN**************** (7) (MUST)
1137+
767 | [Reorganize String](https://leetcode.com/problems/reorganize-string/) | [Python](./leetcode_python/Greedy/reorganize-string.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/ReorganizeString.java) | _O(n)_ | _O(1)_ | Medium |`greedy`,`counter`,`good trick`,`amazon`,`fb`, google| AGAIN**************** (8) (MUST)
11381138
861 | [Score After Flipping Matrix](https://leetcode.com/problems/score-after-flipping-matrix/) | [Python](./leetcode_python/Greedy/score-after-flipping-matrix.py) | _O(r * c)_ | _O(1)_ | Medium || AGAIN (not start*)
11391139
870 | [Advantage Shuffle](https://leetcode.com/problems/advantage-shuffle/) | [Python](./leetcode_python/Greedy/advantage-shuffle.py) | _O(nlogn)_ | _O(n)_ | Medium || OK*
11401140
881 | [Boats to Save People](https://leetcode.com/problems/boats-to-save-people/)|[Python](./leetcode_python/Greedy/boats-to-save-people.py) | _O(nlogn)_ | _O(n)_ | Medium |`good trick`| OK*

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241130: 34
1+
20241130: 34,767
22
20241126: 722,380
33
20241125: 33,81
44
20241124: 153

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
2025-01-24 -> ['34']
1+
2025-01-24 -> ['34,767']
22
2025-01-20 -> ['722,380']
33
2025-01-19 -> ['33,81']
44
2025-01-17 -> ['253']
@@ -8,7 +8,7 @@
88
2025-01-11 -> ['394']
99
2025-01-10 -> ['833,950']
1010
2025-01-04 -> ['53,210,207']
11-
2025-01-03 -> ['34', '444']
11+
2025-01-03 -> ['34,767', '444']
1212
2025-01-02 -> ['1188,130,855(again)']
1313
2024-12-30 -> ['722,380']
1414
2024-12-29 -> ['33,81']
@@ -17,24 +17,24 @@
1717
2024-12-26 -> ['776,31']
1818
2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)']
1919
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
20-
2024-12-21 -> ['34', '394', '855,846']
20+
2024-12-21 -> ['34,767', '394', '855,846']
2121
2024-12-20 -> ['833,950', '932']
2222
2024-12-18 -> ['951,792']
2323
2024-12-17 -> ['722,380']
2424
2024-12-16 -> ['33,81']
2525
2024-12-14 -> ['253', '53,210,207', '163,1048']
26-
2024-12-13 -> ['34', '776,31', '444', '298,729']
26+
2024-12-13 -> ['34,767', '776,31', '444', '298,729']
2727
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2828
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2929
2024-12-09 -> ['722,380']
30-
2024-12-08 -> ['34', '33,81', '394', '737']
30+
2024-12-08 -> ['34,767', '33,81', '394', '737']
3131
2024-12-07 -> ['833,950', '900', '686,734,737']
3232
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
33-
2024-12-05 -> ['34', '776,31', '528,334']
33+
2024-12-05 -> ['34,767', '776,31', '528,334']
3434
2024-12-04 -> ['722,380', '004(todo),34(todo),162(todo),275(todo)']
35-
2024-12-03 -> ['34', '33,81', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
36-
2024-12-02 -> ['34']
37-
2024-12-01 -> ['34', '722,380', '253', '53,210,207']
35+
2024-12-03 -> ['34,767', '33,81', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
36+
2024-12-02 -> ['34,767']
37+
2024-12-01 -> ['34,767', '722,380', '253', '53,210,207']
3838
2024-11-30 -> ['33,81', '776,31', '394', '444', '855,846', '1145,1219']
3939
2024-11-29 -> ['722,380', '004(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
4040
2024-11-28 -> ['722,380', '33,81', '253', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
package LeetCodeJava.Greedy;
2+
3+
// https://leetcode.com/problems/reorganize-string/description/
4+
5+
import java.util.*;
6+
import java.util.concurrent.ConcurrentHashMap;
7+
8+
/**
9+
* 767. Reorganize String
10+
* Solved
11+
* Medium
12+
* Topics
13+
* Companies
14+
* Hint
15+
* Given a string s, rearrange the characters of s so that any two adjacent characters are not the same.
16+
*
17+
* Return any possible rearrangement of s or return "" if not possible.
18+
*
19+
*
20+
*
21+
* Example 1:
22+
*
23+
* Input: s = "aab"
24+
* Output: "aba"
25+
* Example 2:
26+
*
27+
* Input: s = "aaab"
28+
* Output: ""
29+
*
30+
*
31+
* Constraints:
32+
*
33+
* 1 <= s.length <= 500
34+
* s consists of lowercase English letters.
35+
*
36+
*
37+
*/
38+
public class ReorganizeString {
39+
40+
// V0
41+
// IDEA : HASHMAP
42+
// TODO : fix below
43+
// public String reorganizeString(String s) {
44+
//
45+
// if (s.length() == 1){
46+
// return s;
47+
// }
48+
//
49+
// ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
50+
// for (String x : s.split("")){
51+
// map.put(x, map.getOrDefault(x, 0)+1);
52+
// }
53+
//
54+
// System.out.println(">>> map = " + map);
55+
// StringBuilder sb = new StringBuilder();
56+
// String prev = null;
57+
// //String res = "";
58+
//
59+
// while(!map.isEmpty()){
60+
// for(String k : map.keySet()){
61+
// System.out.println(">>> k = " + k + ", keySet = " + map.keySet() + ", prev = " + prev);
62+
// if (prev != null && prev.equals(k)){
63+
// return "";
64+
// }
65+
// sb.append(k);
66+
// prev = k;
67+
// if (map.get(k) - 1 == 0){
68+
// map.remove(k);
69+
// }else{
70+
// map.put(k, map.get(k)-1);
71+
// }
72+
// }
73+
// }
74+
//
75+
// return sb.toString();
76+
// }
77+
78+
// V1
79+
// IDEA : HASHMAP + PriorityQueue
80+
// https://leetcode.com/problems/reorganize-string/solutions/943268/heap-fetch-2-at-once-with-very-detail-explanations/
81+
public String reorganizeString_1(String S) {
82+
83+
// step 1:
84+
// build a hashmap to store characters and its frequencies:
85+
Map<Character, Integer> freq_map = new HashMap<>();
86+
for (char c : S.toCharArray()) {
87+
freq_map.put(c, freq_map.getOrDefault(c, 0) + 1);
88+
}
89+
// step 2:
90+
// put the char of freq_map into the maxheap with sorting the frequencies by
91+
// large->small
92+
/** NOTE !!!!
93+
*
94+
* make PQ as descending order
95+
*
96+
* (a, b) -> freq_map.get(b) - freq_map.get(a)
97+
*/
98+
PriorityQueue<Character> maxheap = new PriorityQueue<>(
99+
(a, b) -> freq_map.get(b) - freq_map.get(a)
100+
);
101+
// addAll() is adding more then one element to heap
102+
maxheap.addAll(freq_map.keySet());
103+
104+
// now maxheap has the most frequent character on the top
105+
106+
// step 3:
107+
// obtain the character 2 by 2 from the maxheap to put in the result sb
108+
// until there is only one element(character) left in the maxheap
109+
// create a stringbuilder to build the result result
110+
StringBuilder sb = new StringBuilder();
111+
while (maxheap.size() > 1) {
112+
char first = maxheap.poll();
113+
char second = maxheap.poll();
114+
sb.append(first);
115+
sb.append(second);
116+
freq_map.put(first, freq_map.get(first) - 1);
117+
freq_map.put(second, freq_map.get(second) - 1);
118+
119+
// insert the character back to the freq_map if the count in
120+
// hashmap of these two character are still > 0
121+
if (freq_map.get(first) > 0) {
122+
maxheap.offer(first);
123+
}
124+
if (freq_map.get(second) > 0) {
125+
maxheap.offer(second);
126+
}
127+
}
128+
129+
if (!maxheap.isEmpty()) {
130+
// when there is only 1 element left in the maxheap
131+
// check the count, it should not be greater than 1
132+
// otherwise it would be impossible and should return ""
133+
if (freq_map.get(maxheap.peek()) > 1) {
134+
return "";
135+
} else {
136+
sb.append(maxheap.poll());
137+
}
138+
}
139+
140+
return sb.toString();
141+
}
142+
143+
// V2
144+
// https://leetcode.com/problems/reorganize-string/solutions/3948228/100-fast-priorityqueue-with-explanation-c-java-python-c/
145+
public String reorganizeString_2(String s) {
146+
int[] f = new int[26];
147+
int n = s.length();
148+
149+
for (int i = 0; i < n; i++) {
150+
f[s.charAt(i) - 'a']++;
151+
if (f[s.charAt(i) - 'a'] > (n + 1) / 2) {
152+
return "";
153+
}
154+
}
155+
156+
PriorityQueue<Pair> p = new PriorityQueue<>((a, b) -> b.freq - a.freq);
157+
for (int i = 0; i < 26; i++) {
158+
if (f[i] != 0) {
159+
p.offer(new Pair(f[i], (char) (i + 'a')));
160+
}
161+
}
162+
163+
StringBuilder ans = new StringBuilder();
164+
while (p.size() >= 2) {
165+
Pair p1 = p.poll();
166+
Pair p2 = p.poll();
167+
ans.append(p1.ch);
168+
ans.append(p2.ch);
169+
if (p1.freq > 1) {
170+
p.offer(new Pair(p1.freq - 1, p1.ch));
171+
}
172+
if (p2.freq > 1) {
173+
p.offer(new Pair(p2.freq - 1, p2.ch));
174+
}
175+
}
176+
177+
if (!p.isEmpty()) {
178+
ans.append(p.poll().ch);
179+
}
180+
181+
return ans.toString();
182+
}
183+
184+
class Pair {
185+
int freq;
186+
char ch;
187+
188+
Pair(int freq, char ch) {
189+
this.freq = freq;
190+
this.ch = ch;
191+
}
192+
}
193+
194+
195+
// V3
196+
// https://leetcode.com/problems/reorganize-string/solutions/3948110/easy-solution-python3-c-c-java-python-with-image/
197+
public String reorganizeString_3(String s) {
198+
Map<Character, Integer> count = new HashMap<>();
199+
for (char c : s.toCharArray()) {
200+
count.put(c, count.getOrDefault(c, 0) + 1);
201+
}
202+
203+
List<int[]> maxHeap = new ArrayList<>();
204+
for (Map.Entry<Character, Integer> entry : count.entrySet()) {
205+
maxHeap.add(new int[]{-entry.getValue(), entry.getKey()});
206+
}
207+
heapify(maxHeap);
208+
209+
int[] prev = null;
210+
StringBuilder res = new StringBuilder();
211+
while (!maxHeap.isEmpty() || prev != null) {
212+
if (prev != null && maxHeap.isEmpty()) {
213+
return "";
214+
}
215+
216+
int[] top = heapPop(maxHeap);
217+
res.append((char) top[1]);
218+
top[0]++;
219+
220+
if (prev != null) {
221+
heapPush(maxHeap, prev);
222+
prev = null;
223+
}
224+
225+
if (top[0] != 0) {
226+
prev = top;
227+
}
228+
}
229+
230+
return res.toString();
231+
}
232+
233+
private void heapify(List<int[]> heap) {
234+
int n = heap.size();
235+
for (int i = n / 2 - 1; i >= 0; i--) {
236+
heapifyDown(heap, i);
237+
}
238+
}
239+
240+
private void heapifyDown(List<int[]> heap, int index) {
241+
int n = heap.size();
242+
int left = 2 * index + 1;
243+
int right = 2 * index + 2;
244+
int largest = index;
245+
246+
if (left < n && heap.get(left)[0] < heap.get(largest)[0]) {
247+
largest = left;
248+
}
249+
if (right < n && heap.get(right)[0] < heap.get(largest)[0]) {
250+
largest = right;
251+
}
252+
253+
if (largest != index) {
254+
Collections.swap(heap, index, largest);
255+
heapifyDown(heap, largest);
256+
}
257+
}
258+
259+
private int[] heapPop(List<int[]> heap) {
260+
int n = heap.size();
261+
int[] top = heap.get(0);
262+
heap.set(0, heap.get(n - 1));
263+
heap.remove(n - 1);
264+
heapifyDown(heap, 0);
265+
return top;
266+
}
267+
268+
private void heapPush(List<int[]> heap, int[] element) {
269+
heap.add(element);
270+
heapifyUp(heap, heap.size() - 1);
271+
}
272+
273+
private void heapifyUp(List<int[]> heap, int index) {
274+
while (index > 0) {
275+
int parent = (index - 1) / 2;
276+
if (heap.get(index)[0] >= heap.get(parent)[0]) {
277+
break;
278+
}
279+
Collections.swap(heap, index, parent);
280+
index = parent;
281+
}
282+
}
283+
284+
// V3
285+
}

0 commit comments

Comments
 (0)