Skip to content
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
12 changes: 12 additions & 0 deletions contains-duplicate/essaysir.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set
  • 설명: 이 코드는 중복 여부를 빠르게 확인하기 위해 HashSet을 사용하여 각 요소를 저장하며, 이미 존재하는지 여부를 체크하는 방식입니다. 따라서 해시 맵 또는 해시 세트 패턴에 속합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: Set에 모든 원소를 저장하며, 중복 검사 시 add 실패 여부로 판단한다. 시간과 공간 모두 입력 크기에 비례한다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import java.util.*;

class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> sets = new HashSet<>();
for ( int i = 0; i < nums.length; i++){
boolean added = sets.add(nums[i]);
if (!added) return true;
}
return false;
}
}
15 changes: 15 additions & 0 deletions house-robber/essaysir.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming
  • 설명: 이 코드는 각 집을 털었을 때와 안 털었을 때의 최대 금액을 저장하며 최적의 선택을 반복하는 방식으로, DP 패턴에 속합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(1)

피드백: 한 번의 반복으로 이전 상태를 갱신하며, 공간은 상수만 사용한다. 시간은 입력 크기에 비례한다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int rob(int[] nums) {
int prev = 0; // dp[i-2]: 두 칸 전까지의 최대 금액
int curr = 0; // dp[i-1]: 한 칸 전까지의 최대 금액

for (int num : nums) {
// 현재 집을 털 경우(prev + num) vs 안 털 경우(curr) 중 큰 값
int temp = Math.max(curr, prev + num);
prev = curr;
curr = temp;
}

return curr;
}
}
23 changes: 23 additions & 0 deletions longest-consecutive-sequence/essaysir.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Sorting
  • 설명: 이 코드는 배열을 정렬한 후 연속된 수의 길이를 찾기 위해 순차적으로 탐색하는 방식으로, 정렬이 핵심 알고리즘입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n log n)
Space O(1)

피드백: 배열 정렬이 시간 복잡도를 결정하며, 정렬 후 한 번 순회하여 최대 연속 길이를 찾는다.

개선 제안: 정렬 대신 해시셋을 이용하면 시간 복잡도를 O(n)으로 개선 가능하다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.*;

class Solution {
// TC: O(n)
// SC: O(n)
public int longestConsecutive(int[] nums) {
// 연속적인 수의 개수를 구한다.
Arrays.sort(nums);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arrays.sort()의 시간복잡도는 O(nlogn) 이라서, 결국 전체 시간 복잡도는 O(nlogn)으로 봐야할 것 같습니다!

int maxCount = 0;
int cur = 1;
for ( int i = 1 ; i < nums.length; i ++){
if (nums[i] == nums[i - 1]) {
continue; // 중복 건너뛰기
} else if (nums[i - 1] + 1 == nums[i]) {
cur += 1;
} else {
cur = 1;
}
maxCount = Math.max(maxCount, cur); // 매 스텝마다 갱신
}
return maxCount;
}
}
27 changes: 27 additions & 0 deletions top-k-frequent-elements/essaysir.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set, Heap / Priority Queue
  • 설명: 이 코드는 각 숫자의 빈도수를 HashMap으로 세고, 우선순위 큐(힙)를 사용해 상위 k개를 찾는 방식으로 구성되어 있습니다. 따라서 해시 맵과 힙 패턴이 적용됩니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(n log n) O(n log k)
Space O(n) O(n)

피드백: 빈도수 계산은 O(n), 힙 연산은 k개에 대해 수행되어 전체 시간은 O(n log k)이다. 공간은 해시맵과 힙에 비례한다.

개선 제안: 현재 구현이 적절해 보입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.*;

class Solution {
// TC: O(n log n)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

시간 복잡도를 수정해야할 것 같습니다.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 풀지는 못했는데, 버킷 정렬을 이용해서 최적화할 수 있는 문제더라구요. 버킷 정렬 방법도 고민해보면 좋을 것 같습니다.

// SC: O(n)
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> count = new HashMap<>();
for ( int i = 0; i < nums.length; i++){
count.put(nums[i], count.getOrDefault(nums[i],0)+ 1);
}

PriorityQueue<Integer> heap = new PriorityQueue<>((a,b) -> count.get(a) - count.get(b));

for ( int key : count.keySet()){
heap.offer(key);
if ( heap.size() > k ){
heap.poll();
}
}

int[] result = new int[k];
for ( int i = 0; i < k; i++){
result[i] = heap.poll();
}
return result;
}
}
17 changes: 17 additions & 0 deletions two-sum/essaysir.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Brute Force
  • 설명: 이 코드는 모든 가능한 쌍을 탐색하는 방식으로 문제를 해결하며, 명시적 패턴 목록에는 없지만 가장 기본적인 탐색 방법인 Brute Force에 해당합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n^2)
Space O(1)

피드백: 모든 쌍을 검사하는 방식으로, 시간 복잡도는 입력 크기의 제곱이다. 공간은 상수이다.

개선 제안: 해시맵을 이용한 방법으로 시간 복잡도를 O(n)으로 개선할 수 있다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.*;

class Solution {
public int[] twoSum(int[] nums, int target) {
// TWO Sum , 순서는 상관이 없음. NC2 를 계산 필요
int[] result = new int[2];
for ( int i = 0; i < nums.length; i++){
for ( int j = i + 1; j < nums.length; j++){
if ( target == nums[i] + nums[j]){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정답을 찾았을 경우엔, break를 줘서 불필요하게 for 문을 돌지 않게 해도 좋을 것 같습니다

result[0] = i;
result[1] = j;
}
}
}
return result;
}
}
Loading