Skip to content

[minji-go] week 01 solutions #1170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 5, 2025
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
29 changes: 10 additions & 19 deletions contains-duplicate/minji-go.java
Original file line number Diff line number Diff line change
@@ -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;
/**
<a href="https://leetcode.com/problems/contains-duplicate/">week01-1.contains-duplicate</a>
<li> Description: return true if any value appears at least twice in the array </li>
<li> Concept: Array, Hash Table, Sorting </li>
<li> Time Complexity: O(n), Runtime: 11ms </li>
<li> Space Complexity: O(n), Memory: 59.27MB </li>
*/

class Solution {
Set<Integer> set = new HashSet<>();

public boolean containsDuplicate(int[] nums) {
Set<Integer> 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);
}
}
35 changes: 21 additions & 14 deletions house-robber/minji-go.java
Original file line number Diff line number Diff line change
@@ -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
*/
/**
* <a href="https://leetcode.com/problems/house-robber/">week01-5.house-robber</a>
* <li> Description: the maximum amount of money you can rob if you cannot rob two adjacent houses </li>
* <li> Concept: Array, Dynamic Programming </li>
* <li> Time Complexity: O(n), Runtime: 0ms </li>
* <li> Space Complexity: O(1), Memory: 41.1MB </li>
*/

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<nums.length; i++){
int sum3=Math.max(nums[i]+sum1,sum2);
sum1=sum2;
sum2=sum3;
if(nums.length==1) {
return nums[0];
}
return sum2;

List<Integer> money = new ArrayList<>();
money.add(nums[0]);
money.add(Math.max(nums[0], nums[1]));

for(int i=2; i<nums.length; i++) {
money.set(0, Math.max(money.get(0)+nums[i], money.get(1)));
Collections.swap(money, 0 , 1);
}

return money.get(1);
}
}
47 changes: 22 additions & 25 deletions longest-consecutive-sequence/minji-go.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
/*
Problem: https://leetcode.com/problems/longest-consecutive-sequence/
Description: return the length of the longest consecutive elements sequence
Concept: Array, Hash Table, Union Find
Time Complexity: O(n), Runtime: 1141ms
Space Complexity: O(n), Memory: 66.74MB
*/
import java.util.HashSet;
import java.util.Set;
/**
* <a href="https://leetcode.com/problems/longest-consecutive-sequence/">week01-4.longest-consecutive-sequence</a>
* <li> Description: return the length of the longest consecutive elements sequence </li>
* <li> Concept: Array, Hash Table, Union Find </li>
* <li> Time Complexity: O(n), Runtime: 60ms </li>
* <li> Space Complexity: O(n), Memory: 55.7MB </li>
*/

class Solution {
private Set<Integer> set;

public int longestConsecutive(int[] nums) {
Set<Integer> 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();
}
}
38 changes: 16 additions & 22 deletions top-k-frequent-elements/minji-go.java
Original file line number Diff line number Diff line change
@@ -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.*;
/**
* <a href="https://leetcode.com/problems/top-k-frequent-elements/">week01-3.top-k-frequent-elements</a>
* <li> Description: return the k most frequent elements </li>
* <li> Concept: Array, Hash Table, Divide and Conquer, Sorting, Heap (Priority Queue), Bucket Sort, Counting, Quickselect </li>
* <li> Time Complexity: O(n log k), Runtime: 15ms </li>
* <li> Space Complexity: O(n), Memory: 49.4MB </li>
*/

class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> count = new HashMap<>();
for(int num : nums){
count.put(num, count.getOrDefault(num, 0)+1);
}

PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.comparingInt(count::get));
for (int num : count.keySet()) {
pq.offer(num);
if (pq.size() > k) pq.poll();
}
Map<Integer, Integer> 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; i<k; i++) {
answer[i] = pq.poll();
}
return answer;
return map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Collections.reverseOrder()))
.limit(k)
.map(Map.Entry::getKey)
.mapToInt(Integer::intValue)
.toArray();
}
}
35 changes: 19 additions & 16 deletions two-sum/minji-go.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
/*
Problem: https://leetcode.com/problems/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.1MB
*/
/**
* <a href="https://leetcode.com/problems/two-sum/">week01-2.two-sum</a>
* <li> Description: return indices of the two numbers such that they add up to target. not use the same element twice. </li>
* <li> Topics: Array, Hash Table </li>
* <li> Time Complexity: O(N), Runtime 2ms </li>
* <li> Space Complexity: O(N), Memory 45.4MB </li>
*/

class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> numIndex = new HashMap<>();
for(int secondIndex=0; secondIndex<nums.length; secondIndex++){
if(numIndex.containsKey(target-nums[secondIndex])){
int firstIndex = numIndex.get(target-nums[secondIndex]);
return new int[]{firstIndex, secondIndex};
} else {
numIndex.put(nums[secondIndex], secondIndex);
}
Map<Integer, Integer> 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();
}
}