Skip to content

Commit 4a2b8b5

Browse files
authored
Merge pull request DaleStudy#649 from minji-go/main
[minji-go] Week 1
2 parents 2399863 + 7fbc074 commit 4a2b8b5

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

contains-duplicate/minji-go.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Problem: https://leetcode.com/problems/contains-duplicate/
3+
Description: return true if any value appears at least twice in the array
4+
Concept: Array, Hash Table, Sorting
5+
Time Complexity: O(n), Runtime: 10ms
6+
Space Complexity: O(n), Memory: 58.6MB
7+
*/
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
11+
class Solution {
12+
public boolean containsDuplicate(int[] nums) {
13+
Set<Integer> count = new HashSet<>();
14+
boolean answer = false;
15+
for(int num : nums){
16+
if(count.contains(num)) {
17+
answer = true;
18+
break;
19+
}
20+
count.add(num);
21+
}
22+
return answer;
23+
}
24+
}

house-robber/minji-go.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Problem: https://leetcode.com/problems/house-robber/
3+
Description: the maximum amount of money you can rob if you cannot rob two adjacent houses
4+
Concept: Array, Dynamic Programming
5+
Time Complexity: O(n), Runtime: 0ms
6+
Space Complexity: O(1), Memory: 41.42MB
7+
*/
8+
class Solution {
9+
public int rob(int[] nums) {
10+
int sum1 = nums[0];
11+
int sum2 = nums.length>1? Math.max(nums[0], nums[1]) : nums[0];
12+
for(int i=2; i<nums.length; i++){
13+
int sum3=Math.max(nums[i]+sum1,sum2);
14+
sum1=sum2;
15+
sum2=sum3;
16+
}
17+
return sum2;
18+
}
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Problem: https://leetcode.com/problems/longest-consecutive-sequence/
3+
Description: return the length of the longest consecutive elements sequence
4+
Concept: Array, Hash Table, Union Find
5+
Time Complexity: O(n), Runtime: 1141ms
6+
Space Complexity: O(n), Memory: 66.74MB
7+
*/
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
11+
class Solution {
12+
public int longestConsecutive(int[] nums) {
13+
Set<Integer> set = new HashSet<>();
14+
for(int num: nums) {
15+
set.add(num);
16+
}
17+
18+
int answer = 0;
19+
for(int num: nums){
20+
if(set.contains(num-1)){ //contains(): O(1)
21+
continue;
22+
}
23+
int length = 1;
24+
while (set.contains(num+length)){
25+
length++;
26+
}
27+
answer = Math.max(answer, length);
28+
}
29+
30+
return answer;
31+
}
32+
}

top-k-frequent-elements/minji-go.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Problem: https://leetcode.com/problems/top-k-frequent-elements/
3+
Description: return the k most frequent elements
4+
Concept: Array, Hash Table, Divide and Conquer, Sorting, Heap (Priority Queue), Bucket Sort, Counting, Quickselect
5+
Time Complexity: O(n log k), Runtime: 15ms
6+
Space Complexity: O(n), Memory: 48.64MB
7+
*/
8+
import java.util.*;
9+
10+
class Solution {
11+
public int[] topKFrequent(int[] nums, int k) {
12+
Map<Integer, Integer> count = new HashMap<>();
13+
for(int num : nums){
14+
count.put(num, count.getOrDefault(num, 0)+1);
15+
}
16+
17+
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.comparingInt(count::get));
18+
for (int num : count.keySet()) {
19+
pq.offer(num);
20+
if (pq.size() > k) pq.poll();
21+
}
22+
23+
int[] answer = new int[k];
24+
for(int i=0; i<k; i++) {
25+
answer[i] = pq.poll();
26+
}
27+
return answer;
28+
}
29+
}

valid-palindrome/minji-go.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Problem: https://leetcode.com/problems/valid-palindrome/
3+
Description: return true if it is a palindrome, alphanumeric characters(letters and numbers) reads the same forward and backward
4+
Concept: Two Pointers, String
5+
Time Complexity: O(n), Runtime: 10ms
6+
Space Complexity: O(n), Memory: 58.6MB
7+
*/
8+
class Solution {
9+
public boolean isPalindrome(String s) {
10+
String regex ="[^A-Za-z0-9]";
11+
String palindrome = s.replaceAll(regex,"").toLowerCase(); //replaceAll(), toLowerCase(): O(n)
12+
13+
boolean answer = true;
14+
for(int i=0; i<palindrome.length()/2; i++){
15+
if(palindrome.charAt(i) != palindrome.charAt(palindrome.length()-1-i)) {
16+
answer = false;
17+
break;
18+
}
19+
}
20+
return answer;
21+
}
22+
}

0 commit comments

Comments
 (0)