diff --git a/contains-duplicate/minji-go.java b/contains-duplicate/minji-go.java index 7a5eb0276..8622d24cb 100644 --- a/contains-duplicate/minji-go.java +++ b/contains-duplicate/minji-go.java @@ -1,24 +1,15 @@ -/* - 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; +/** + week01-1.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: 11ms
  • +
  • Space Complexity: O(n), Memory: 59.27MB
  • + */ class Solution { + Set set = new HashSet<>(); + 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; + return !Arrays.stream(nums).allMatch(set::add); } } diff --git a/house-robber/minji-go.java b/house-robber/minji-go.java index a98bc2461..43c85bc07 100644 --- a/house-robber/minji-go.java +++ b/house-robber/minji-go.java @@ -1,19 +1,26 @@ -/* - 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 -*/ +/** + * week01-5.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.1MB
  • + */ + 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 money = new ArrayList<>(); + money.add(nums[0]); + money.add(Math.max(nums[0], nums[1])); + + for(int i=2; iweek01-4.longest-consecutive-sequence + *
  • Description: return the length of the longest consecutive elements sequence
  • + *
  • Concept: Array, Hash Table, Union Find
  • + *
  • Time Complexity: O(n), Runtime: 60ms
  • + *
  • Space Complexity: O(n), Memory: 55.7MB
  • + */ class Solution { + private Set set; + public int longestConsecutive(int[] nums) { - Set set = new HashSet<>(); - for(int num: nums) { - set.add(num); - } + set = Arrays.stream(nums) + .boxed() + .collect(Collectors.toSet()); - 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 set.stream() + .filter(num -> !set.contains(num-1)) + .map(this::calculateLength) + .max(Integer::compareTo) + .orElse(0); + } - return answer; + public int calculateLength(int num) { + return (int) IntStream.iterate(num, n->n+1) + .takeWhile(set::contains) + .count(); } } diff --git a/top-k-frequent-elements/minji-go.java b/top-k-frequent-elements/minji-go.java index b10cd1231..e8c9b507a 100644 --- a/top-k-frequent-elements/minji-go.java +++ b/top-k-frequent-elements/minji-go.java @@ -1,29 +1,23 @@ -/* - 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.*; +/** + * week01-3.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: 49.4MB
  • + */ 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(); - } + Map map = Arrays.stream(nums) + .boxed() + .collect(Collectors.toMap(num -> num, num -> 1, (cnt, cnt2) -> cnt + 1)); - int[] answer = new int[k]; - for(int i=0; iweek01-2.two-sum + *
  • Description: return indices of the two numbers such that they add up to target. not use the same element twice.
  • + *
  • Topics: Array, Hash Table
  • + *
  • Time Complexity: O(N), Runtime 2ms
  • + *
  • Space Complexity: O(N), Memory 45.4MB
  • + */ + class Solution { public int[] twoSum(int[] nums, int target) { - Map numIndex = new HashMap<>(); - for(int secondIndex=0; secondIndex seen = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + int num = target - nums[i]; + + if (seen.containsKey(num)) + return new int[]{seen.get(num), i}; + + seen.put(nums[i], i); } - return new int[]{}; + + throw new IllegalArgumentException(); } }