Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions contains-duplicate/minji-go.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*
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<>();
Expand Down
20 changes: 14 additions & 6 deletions house-robber/minji-go.java
Original file line number Diff line number Diff line change
@@ -1,11 +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[] sum = new int[nums.length+1];
sum[0] = nums[0];
if(nums.length>1) sum[1] = Math.max(nums[0], nums[1]);
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++){
sum[i]=Math.max(nums[i]+sum[i-2],sum[i-1]);
int sum3=Math.max(nums[i]+sum1,sum2);
sum1=sum2;
sum2=sum3;
}
return sum[nums.length-1];
return sum2;
}
}
}
12 changes: 11 additions & 1 deletion longest-consecutive-sequence/minji-go.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*
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<>();
Expand All @@ -7,7 +17,7 @@ public int longestConsecutive(int[] nums) {

int answer = 0;
for(int num: nums){
if(set.contains(num-1)){
if(set.contains(num-1)){ //contains(): O(1)
continue;
}
int length = 1;
Expand Down
21 changes: 16 additions & 5 deletions top-k-frequent-elements/minji-go.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
/*
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);
}
List<Integer> tops = count.keySet().stream()
.sorted((i1, i2) -> count.get(i2)-count.get(i1))
.limit(k)
.toList();

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] = tops.get(i);
answer[i] = pq.poll();
}
return answer;
}
Expand Down
9 changes: 8 additions & 1 deletion valid-palindrome/minji-go.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/*
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();
String palindrome = s.replaceAll(regex,"").toLowerCase(); //replaceAll(), toLowerCase(): O(n)

boolean answer = true;
for(int i=0; i<palindrome.length()/2; i++){
Expand Down
Loading