Skip to content

[minji-go] Week 1 #649

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 8 commits into from
Dec 15, 2024
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
24 changes: 24 additions & 0 deletions contains-duplicate/minji-go.java
Original file line number Diff line number Diff line change
@@ -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<Integer> count = new HashSet<>();
boolean answer = false;
for(int num : nums){
if(count.contains(num)) {
answer = true;
break;
}
count.add(num);
}
return answer;
}
}
19 changes: 19 additions & 0 deletions house-robber/minji-go.java
Original file line number Diff line number Diff line change
@@ -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<nums.length; i++){
int sum3=Math.max(nums[i]+sum1,sum2);
sum1=sum2;
sum2=sum3;
}
return sum2;
}
}
32 changes: 32 additions & 0 deletions longest-consecutive-sequence/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
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;

class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> 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;
}
}
29 changes: 29 additions & 0 deletions top-k-frequent-elements/minji-go.java
Original file line number Diff line number Diff line change
@@ -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<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();
}

int[] answer = new int[k];
for(int i=0; i<k; i++) {
answer[i] = pq.poll();
}
return answer;
}
}
22 changes: 22 additions & 0 deletions valid-palindrome/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Problem: https://leetcode.com/problems/valid-palindrome/
Description: return true if it is a palindrome, alphanumeric characters(letters and numbers) reads the same forward and backward
Concept: Two Pointers, String
Time Complexity: O(n), Runtime: 10ms
Space Complexity: O(n), Memory: 58.6MB
*/
class Solution {
public boolean isPalindrome(String s) {
String regex ="[^A-Za-z0-9]";
String palindrome = s.replaceAll(regex,"").toLowerCase(); //replaceAll(), toLowerCase(): O(n)

boolean answer = true;
for(int i=0; i<palindrome.length()/2; i++){
if(palindrome.charAt(i) != palindrome.charAt(palindrome.length()-1-i)) {
answer = false;
break;
}
}
return answer;
}
}
Loading