Skip to content

Commit fe2f86d

Browse files
committed
refactor: complexity 추가, 공간복잡도 개선
1 parent ec729e1 commit fe2f86d

File tree

5 files changed

+59
-13
lines changed

5 files changed

+59
-13
lines changed

contains-duplicate/minji-go.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
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+
111
class Solution {
212
public boolean containsDuplicate(int[] nums) {
313
Set<Integer> count = new HashSet<>();

house-robber/minji-go.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +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+
*/
18
class Solution {
29
public int rob(int[] nums) {
3-
int[] sum = new int[nums.length+1];
4-
sum[0] = nums[0];
5-
if(nums.length>1) sum[1] = Math.max(nums[0], nums[1]);
10+
int sum1 = nums[0];
11+
int sum2 = nums.length>1? Math.max(nums[0], nums[1]) : nums[0];
612
for(int i=2; i<nums.length; i++){
7-
sum[i]=Math.max(nums[i]+sum[i-2],sum[i-1]);
13+
int sum3=Math.max(nums[i]+sum1,sum2);
14+
sum1=sum2;
15+
sum2=sum3;
816
}
9-
return sum[nums.length-1];
17+
return sum2;
1018
}
11-
}
19+
}

longest-consecutive-sequence/minji-go.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
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+
111
class Solution {
212
public int longestConsecutive(int[] nums) {
313
Set<Integer> set = new HashSet<>();
@@ -7,7 +17,7 @@ public int longestConsecutive(int[] nums) {
717

818
int answer = 0;
919
for(int num: nums){
10-
if(set.contains(num-1)){
20+
if(set.contains(num-1)){ //contains(): O(1)
1121
continue;
1222
}
1323
int length = 1;

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
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+
110
class Solution {
211
public int[] topKFrequent(int[] nums, int k) {
312
Map<Integer, Integer> count = new HashMap<>();
413
for(int num : nums){
514
count.put(num, count.getOrDefault(num, 0)+1);
615
}
7-
List<Integer> tops = count.keySet().stream()
8-
.sorted((i1, i2) -> count.get(i2)-count.get(i1))
9-
.limit(k)
10-
.toList();
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+
}
1122

1223
int[] answer = new int[k];
1324
for(int i=0; i<k; i++) {
14-
answer[i] = tops.get(i);
25+
answer[i] = pq.poll();
1526
}
1627
return answer;
1728
}

valid-palindrome/minji-go.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
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+
*/
18
class Solution {
29
public boolean isPalindrome(String s) {
310
String regex ="[^A-Za-z0-9]";
4-
String palindrome = s.replaceAll(regex,"").toLowerCase();
11+
String palindrome = s.replaceAll(regex,"").toLowerCase(); //replaceAll(), toLowerCase(): O(n)
512

613
boolean answer = true;
714
for(int i=0; i<palindrome.length()/2; i++){

0 commit comments

Comments
 (0)