diff --git a/contains-duplicate/minji-go.java b/contains-duplicate/minji-go.java new file mode 100644 index 000000000..7a5eb0276 --- /dev/null +++ b/contains-duplicate/minji-go.java @@ -0,0 +1,24 @@ +/* + Problem: https://leetcode.com/problems/contains-duplicate/ + Description: return true if any value appears at least twice in the array + Concept: Array, Hash Table, Sorting + Time Complexity: O(n), Runtime: 10ms + Space Complexity: O(n), Memory: 58.6MB +*/ +import java.util.HashSet; +import java.util.Set; + +class Solution { + public boolean containsDuplicate(int[] nums) { + Set count = new HashSet<>(); + boolean answer = false; + for(int num : nums){ + if(count.contains(num)) { + answer = true; + break; + } + count.add(num); + } + return answer; + } +} diff --git a/house-robber/minji-go.java b/house-robber/minji-go.java new file mode 100644 index 000000000..a98bc2461 --- /dev/null +++ b/house-robber/minji-go.java @@ -0,0 +1,19 @@ +/* + Problem: https://leetcode.com/problems/house-robber/ + Description: the maximum amount of money you can rob if you cannot rob two adjacent houses + Concept: Array, Dynamic Programming + Time Complexity: O(n), Runtime: 0ms + Space Complexity: O(1), Memory: 41.42MB +*/ +class Solution { + public int rob(int[] nums) { + int sum1 = nums[0]; + int sum2 = nums.length>1? Math.max(nums[0], nums[1]) : nums[0]; + for(int i=2; i set = new HashSet<>(); + for(int num: nums) { + set.add(num); + } + + int answer = 0; + for(int num: nums){ + if(set.contains(num-1)){ //contains(): O(1) + continue; + } + int length = 1; + while (set.contains(num+length)){ + length++; + } + answer = Math.max(answer, length); + } + + return answer; + } +} diff --git a/top-k-frequent-elements/minji-go.java b/top-k-frequent-elements/minji-go.java new file mode 100644 index 000000000..b10cd1231 --- /dev/null +++ b/top-k-frequent-elements/minji-go.java @@ -0,0 +1,29 @@ +/* + Problem: https://leetcode.com/problems/top-k-frequent-elements/ + Description: return the k most frequent elements + Concept: Array, Hash Table, Divide and Conquer, Sorting, Heap (Priority Queue), Bucket Sort, Counting, Quickselect + Time Complexity: O(n log k), Runtime: 15ms + Space Complexity: O(n), Memory: 48.64MB +*/ +import java.util.*; + +class Solution { + public int[] topKFrequent(int[] nums, int k) { + Map count = new HashMap<>(); + for(int num : nums){ + count.put(num, count.getOrDefault(num, 0)+1); + } + + PriorityQueue pq = new PriorityQueue<>(Comparator.comparingInt(count::get)); + for (int num : count.keySet()) { + pq.offer(num); + if (pq.size() > k) pq.poll(); + } + + int[] answer = new int[k]; + for(int i=0; i