Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions contains-duplicate/seungyeonchoi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> duplicates = new HashSet<>();
for (int i=0; i<nums.length;i++) {
// duplicates.add(nums[i]);
// if (duplicates.size() != i+1) {
// return true;
// }
if (!duplicates.add(nums[i])) {
return true;
}
}
return false;
}

/*
complexity
- time: O(n) using a for loop
- space: O(n) declaring a set
*/

}
61 changes: 61 additions & 0 deletions top-k-frequent-elements/seungyeonchoi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Solution {
public int[] topKFrequent(int[] nums, int k) {
/*
Approach 1
1. using map, key -> num, value -> count
2. sort
*/

Map<Integer, Integer> count = new HashMap<>();
for (int num: nums) { // O(n)
count.put(num, count.getOrDefault(num,0)+1);
}

// return count.entrySet().stream()
// .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
// .map(Map.Entry::getKey)
// .limit(k)
// .mapToInt(Integer::intValue)
// .toArray();
// /*
// Complexity
// - time: O(nlogn) // sort
// - space: O(n) // declare map?
// */


/*
Approach 2
1. using map, key -> num, value -> count
2. Initialize a 2D dynamic array (buckets) and group numbers by their frequency
*/
List<List<Integer>> buckets = new ArrayList<>();
for (int i = 0; i <= nums.length; i++) {
buckets.add(new ArrayList<>());
}
for (int key: count.keySet()) {
buckets.get(count.get(key)).add(key);
}

int[] result = new int[k];
int idx = 0;
for (int i = buckets.size()-1;i>=0;i--) {
for (int j = 0; j < buckets.get(i).size(); j++) {
if (idx<k) {
result[idx] = buckets.get(i).get(j);
idx++;
} else {
return result;
}

}
}

return result;
/**
Complexity
- time: O(n) -> o(n) + o(n) + o (n+1+ n) -> o(4n+1)
- space: O(n) -> dynamic 2d array (buckets)
*/
}
}
Loading