Skip to content

Commit 8c73a5c

Browse files
author
yennj12
committed
add 692 java
1 parent 67978f6 commit 8c73a5c

File tree

6 files changed

+304
-14
lines changed

6 files changed

+304
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@
770770
347| [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [Python](./leetcode_python/Sort/top-k-frequent-elements.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/HashTable/TopKFrequentElements.java) | _O(n)_ | _O(n)_| Medium | Curated Top 75 , HashMap, Heap, Sort,`Yelp`,`amazon`,`fb`, GOOD basic| AGAIN******** (4)
771771
406| [Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/) | [Python](./leetcode_python/Sort/queue-reconstruction-by-height.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Greedy/QueueReconstructionByHeight.java) | _O(n * sqrt(n))_ | _O(n)_ | Medium | `basic`,`trick`, `sort by key`, `insert`, `google` | AGAIN********* (5)
772772
451| [Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/) | [Python](./leetcode_python/Sort/sort-characters-by-frequency.py)| _O(n)_ | _O(n)_| Medium |`collections.Counter(s).most_common`,`sorted(count_dict.items() with lambda`,good basic, `amazon`| OK* (4)
773-
692| [Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/) | [Python](./leetcode_python/Sort/top-k-frequent-words.py) | _O(n + klogk)_ on average | _O(n)_ | Medium | `good basic`,Quick Select, Heap, Bucket Sort, `heapq`, `yelp`, `UBER`,`amazon`,`fb` | OK***** (5)
773+
692| [Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/) | [Python](./leetcode_python/Sort/top-k-frequent-words.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Sort/TopKFrequentWords.java) | _O(n + klogk)_ on average | _O(n)_ | Medium | `good basic`,Quick Select, Heap, Bucket Sort, `heapq`, `yelp`, `UBER`,`amazon`,`fb` | OK***** (5)
774774
937| [Reorder Data in Log Files](https://leetcode.com/problems/reorder-data-in-log-files/) | [Python](./leetcode_python/Sort/reorder-data-in-log-files.py) | _O(nlogn * l)_ | _O(l)_ | Medium | good basic, sort, `string`, `amazon`| OK**** (3)(again!!!)
775775
969| [Pancake Sorting](https://leetcode.com/problems/pancake-sorting/) | [Python](./leetcode_python/Sort/pancake-sorting.py) | _O(n^2)_ | _O(l)_ | Medium |`fb`, sorting| AGAIN********** (4)
776776
973| [K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/) | [Python](./leetcode_python/Sort/k-closest-points-to-origin.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Sort/KClosestPointsToOrigin.java) | _O(n)_ on average | _O(1)_ | Medium | sort with lambda, Quick Select, Heap, `amazon`| OK* (3)

data/to_review.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2025-03-04 -> ['852,692']
12
2025-03-03 -> ['247']
23
2025-03-01 -> ['399,1091']
34
2025-02-28 -> ['1031,369']
@@ -9,6 +10,7 @@
910
2025-02-15 -> ['369,311']
1011
2025-02-14 -> ['370']
1112
2025-02-13 -> ['815,871,593,1109']
13+
2025-02-11 -> ['852,692']
1214
2025-02-10 -> ['247']
1315
2025-02-08 -> ['399,1091']
1416
2025-02-07 -> ['1031,369', '560,523']
@@ -17,25 +19,26 @@
1719
2025-02-01 -> ['498(again),934', '304,853,325']
1820
2025-01-31 -> ['379,173']
1921
2025-01-30 -> ['079,212(todo),362,849']
22+
2025-01-29 -> ['852,692']
2023
2025-01-28 -> ['247']
2124
2025-01-26 -> ['399,1091', '370(todo)']
2225
2025-01-25 -> ['1031,369', '369,311']
2326
2025-01-24 -> ['370', '34,767']
2427
2025-01-23 -> ['815,871,593,1109']
2528
2025-01-22 -> ['729,731']
26-
2025-01-21 -> ['769,817,855(again)']
29+
2025-01-21 -> ['852,692', '769,817,855(again)']
2730
2025-01-20 -> ['247', '722,380']
2831
2025-01-19 -> ['498(again),934', '33,81']
2932
2025-01-18 -> ['399,1091', '379,173']
3033
2025-01-17 -> ['1031,369', '079,212(todo),362,849', '560,523', '253']
31-
2025-01-16 -> ['776,31']
34+
2025-01-16 -> ['852,692', '776,31']
3235
2025-01-15 -> ['247', '004(todo),34(todo),162(todo),275(todo)']
3336
2025-01-14 -> ['729,731', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
34-
2025-01-13 -> ['399,1091', '769,817,855(again)']
37+
2025-01-13 -> ['852,692', '399,1091', '769,817,855(again)']
3538
2025-01-12 -> ['247', '1031,369', '369,311']
36-
2025-01-11 -> ['498(again),934', '370', '304,853,325', '394']
37-
2025-01-10 -> ['247', '399,1091', '379,173', '815,871,593,1109', '833,950']
38-
2025-01-09 -> ['247', '1031,369', '729,731', '079,212(todo),362,849']
39+
2025-01-11 -> ['852,692', '498(again),934', '370', '304,853,325', '394']
40+
2025-01-10 -> ['852,692', '247', '399,1091', '379,173', '815,871,593,1109', '833,950']
41+
2025-01-09 -> ['852,692', '247', '1031,369', '729,731', '079,212(todo),362,849']
3942
2025-01-08 -> ['247', '399,1091', '769,817,855(again)']
4043
2025-01-07 -> ['399,1091', '1031,369']
4144
2025-01-06 -> ['399,1091', '1031,369', '729,731', '498(again),934']
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package LeetCodeJava.Sort;
2+
3+
// https://leetcode.com/problems/top-k-frequent-words/description/
4+
5+
import java.util.*;
6+
7+
/**
8+
* 692. Top K Frequent Words
9+
* Solved
10+
* Medium
11+
* Topics
12+
* Companies
13+
* Given an array of strings words and an integer k, return the k most frequent strings.
14+
*
15+
* Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.
16+
*
17+
*
18+
*
19+
* Example 1:
20+
*
21+
* Input: words = ["i","love","leetcode","i","love","coding"], k = 2
22+
* Output: ["i","love"]
23+
* Explanation: "i" and "love" are the two most frequent words.
24+
* Note that "i" comes before "love" due to a lower alphabetical order.
25+
* Example 2:
26+
*
27+
* Input: words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
28+
* Output: ["the","is","sunny","day"]
29+
* Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.
30+
*
31+
*
32+
* Constraints:
33+
*
34+
* 1 <= words.length <= 500
35+
* 1 <= words[i].length <= 10
36+
* words[i] consists of lowercase English letters.
37+
* k is in the range [1, The number of unique words[i]]
38+
*
39+
*
40+
* Follow-up: Could you solve it in O(n log(k)) time and O(n) extra space?
41+
*
42+
*/
43+
public class TopKFrequentWords {
44+
45+
// V0
46+
// IDEA: sort map on value and key length (gpt)
47+
// TODO: validate & fix
48+
// public List<String> topKFrequent(String[] words, int k) {
49+
//
50+
// // edge
51+
// if (words.length == 0) {
52+
// return new ArrayList<>();
53+
// }
54+
//
55+
// List<String> res = new ArrayList<>();
56+
// Map<String, Integer> map = new HashMap<>();
57+
// // biggest queue
58+
// // Queue<Integer> cntQueue = new LinkedList<>();
59+
//
60+
// for (String x : words) {
61+
//
62+
// map.putIfAbsent(x, 1);
63+
// Integer cur = map.get(x);
64+
// map.put(x, cur + 1);
65+
// }
66+
//
67+
// System.out.println(">>> (before sort) map = " + map);
68+
//
69+
// // sort map by value and key lenth
70+
// // Convert the map to a list of entries
71+
// List<Map.Entry<String, Integer>> entryList = new ArrayList<>(map.entrySet());
72+
//
73+
// // Sort the entry list
74+
// Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() {
75+
// @Override
76+
// public int compare(Map.Entry<String, Integer> entry1, Map.Entry<String, Integer> entry2) {
77+
// // First, compare by value in decreasing order
78+
// int valueCompare = entry2.getValue().compareTo(entry1.getValue());
79+
//
80+
// // If values are equal, compare by key length in increasing order
81+
// if (valueCompare == 0) {
82+
// return entry1.getKey().length() - entry2.getKey().length();
83+
// }
84+
//
85+
// return valueCompare;
86+
// }
87+
// });
88+
//
89+
// System.out.println(">>> (after sort) map = " + map);
90+
// for (Map.Entry<String, Integer> x : entryList) {
91+
// if (k == 0){
92+
// break;
93+
// }
94+
// res.add((String) x.getKey());
95+
// k -= 1;
96+
// }
97+
//
98+
// return res;
99+
// }
100+
101+
// V1
102+
// V2
103+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package dev;
2+
3+
import java.util.*;
4+
5+
6+
public class SortMapByValueAndKeyLength {
7+
public static void main(String[] args) {
8+
9+
Map<String, Integer> map = new HashMap<>();
10+
map.put("apple", 3);
11+
map.put("banana", 2);
12+
map.put("grape", 3);
13+
map.put("kiwi", 5);
14+
map.put("peach", 3);
15+
16+
System.out.println("Original map: " + map);
17+
18+
Map<String, Integer> sortedMap = sortMapByValueAndKeyLength(map);
19+
System.out.println("Sorted map: " + sortedMap);
20+
21+
22+
}
23+
24+
25+
public static Map<String, Integer> sortMapByValueAndKeyLength(Map<String, Integer> map) {
26+
// Convert the map to a list of entries
27+
List<Map.Entry<String, Integer>> entryList = new ArrayList<>(map.entrySet());
28+
29+
// Sort the entry list
30+
Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() {
31+
@Override
32+
public int compare(Map.Entry<String, Integer> entry1, Map.Entry<String, Integer> entry2) {
33+
// First, compare by value in decreasing order
34+
int valueCompare = entry2.getValue().compareTo(entry1.getValue());
35+
36+
// If values are equal, compare by key length in increasing order
37+
if (valueCompare == 0) {
38+
return entry1.getKey().length() - entry2.getKey().length();
39+
}
40+
41+
return valueCompare;
42+
}
43+
});
44+
45+
// Convert the sorted list back to a linked map to preserve the order
46+
Map<String, Integer> sortedMap = new LinkedHashMap<>();
47+
for (Map.Entry<String, Integer> entry : entryList) {
48+
sortedMap.put(entry.getKey(), entry.getValue());
49+
}
50+
51+
return sortedMap;
52+
}
53+
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import LeetCodeJava.DataStructure.ListNode;
55
import LeetCodeJava.DataStructure.TreeNode;
6-
import jdk.javadoc.internal.doclets.toolkit.util.Utils;
6+
//import jdk.javadoc.internal.doclets.toolkit.util.Utils;
77

88
import java.util.*;
99

@@ -2326,4 +2326,124 @@ else if (arr[mid] >= arr[mid+1] && arr[mid] >= arr[r]){
23262326
return mid;
23272327
}
23282328

2329+
// LC 692
2330+
/**
2331+
* Given an array of strings words and an integer k, return the k most frequent strings.
2332+
*
2333+
* Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.
2334+
*
2335+
*
2336+
*
2337+
* Example 1:
2338+
*
2339+
* Input: words = ["i","love","leetcode","i","love","coding"], k = 2
2340+
* Output: ["i","love"]
2341+
* Explanation: "i" and "love" are the two most frequent words.
2342+
* Note that "i" comes before "love" due to a lower alphabetical order.
2343+
* Example 2:
2344+
*
2345+
* Input: words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
2346+
* Output: ["the","is","sunny","day"]
2347+
* Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.
2348+
*
2349+
*
2350+
*/
2351+
/**
2352+
* IDEA 1) treeMap ?? (map with sorting feature)
2353+
* IDEA 2) map + sorting with val, and get key
2354+
* IDEA 3) map + PQ ??
2355+
*
2356+
*
2357+
* -> use IDEA 2) first
2358+
*/
2359+
public List<String> topKFrequent(String[] words, int k) {
2360+
2361+
// edge
2362+
if (words.length == 0) {
2363+
return new ArrayList<>();
2364+
}
2365+
2366+
List<String> res = new ArrayList<>();
2367+
Map<String, Integer> map = new HashMap<>();
2368+
// biggest queue
2369+
// Queue<Integer> cntQueue = new LinkedList<>();
2370+
2371+
for (String x : words) {
2372+
2373+
map.putIfAbsent(x, 1);
2374+
Integer cur = map.get(x);
2375+
map.put(x, cur + 1);
2376+
}
2377+
2378+
System.out.println(">>> (before sort) map = " + map);
2379+
2380+
// sort map by value and key lenth
2381+
// Convert the map to a list of entries
2382+
List<Map.Entry<String, Integer>> entryList = new ArrayList<>(map.entrySet());
2383+
2384+
// Sort the entry list
2385+
Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() {
2386+
@Override
2387+
public int compare(Map.Entry<String, Integer> entry1, Map.Entry<String, Integer> entry2) {
2388+
// First, compare by value in decreasing order
2389+
int valueCompare = entry2.getValue().compareTo(entry1.getValue());
2390+
2391+
// If values are equal, compare by key length in increasing order
2392+
if (valueCompare == 0) {
2393+
return entry1.getKey().length() - entry2.getKey().length();
2394+
}
2395+
2396+
return valueCompare;
2397+
}
2398+
});
2399+
2400+
System.out.println(">>> (after sort) map = " + map);
2401+
for (Map.Entry<String, Integer> x : entryList) {
2402+
if (k == 0){
2403+
break;
2404+
}
2405+
res.add((String) x.getKey());
2406+
k -= 1;
2407+
}
2408+
2409+
return res;
2410+
}
2411+
2412+
// public List<String> topKFrequent(String[] words, int k) {
2413+
//
2414+
// // edge
2415+
// if (words.length == 0){
2416+
// return new ArrayList<>();
2417+
// }
2418+
//
2419+
// List<String> res = new ArrayList<>();
2420+
// Map<String, Integer> map = new HashMap<>();
2421+
// // biggest queue
2422+
// Queue<Integer> cntQueue = new LinkedList<>();
2423+
//
2424+
// for(String x : words){
2425+
//
2426+
// map.putIfAbsent(x, 1);
2427+
// Integer cur = map.get(x);
2428+
// map.put(x, cur+1);
2429+
// }
2430+
//
2431+
// // save top K values in queue
2432+
// for(Integer val: map.values()){
2433+
// if(cntQueue.size() > k){
2434+
// cntQueue.poll();
2435+
// }
2436+
// cntQueue.add(val);
2437+
// }
2438+
//
2439+
// // prepare result
2440+
// // {"the": 1, "appl" : 2, "cos" : 3} ...
2441+
// //List<>
2442+
// while(!cntQueue.isEmpty()){
2443+
// // res.add(cntQueue.poll());
2444+
// }
2445+
//
2446+
// return res;
2447+
// }
2448+
23292449
}

leetcode_java/src/main/java/dev/workspace7.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package dev;
22

3-
import java.util.ArrayList;
4-
import java.util.Arrays;
5-
import java.util.List;
3+
import java.util.*;
64

75
public class workspace7 {
86
public static void main(String[] args) {
@@ -51,9 +49,22 @@ public static void main(String[] args) {
5149
* • Variable Name:
5250
* • Ensure the loop variable is consistently referenced. Mixing i and I results in a compilation error due to case sensitivity in Java.
5351
*/
54-
for (int i = 0; i < 5; ++i) {
55-
System.out.println(i);
56-
}
52+
// for (int i = 0; i < 5; ++i) {
53+
// System.out.println(i);
54+
// }
55+
56+
// ------------------- TEST 6 : map put, get
57+
Map<String, Integer> map = new HashMap<>();
58+
map.put("a", 1);
59+
map.put("b", 2);
60+
map.put("c", 3);
61+
62+
System.out.println(">>> (before) map = " + map);
63+
map.put("a", 10);
64+
//map.put("c", 4);
65+
66+
System.out.println(">>> (after) map = " + map);
67+
5768
}
5869

5970

0 commit comments

Comments
 (0)