Skip to content

Commit

Permalink
fix : 코드 원복
Browse files Browse the repository at this point in the history
  • Loading branch information
imsosleepy committed Dec 14, 2024
1 parent 495b9d2 commit db5c314
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
21 changes: 18 additions & 3 deletions house-robber/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// 점화식의 최대값을 구하는 방법
// 1. 현재 위치의 최대 값은 한칸 전 집까지만 털었던가(두칸 연속 겹치면 안된다는 룰을 지키면서)
// 2. 두칸 전 집까지 털고 + 현재집을 털었을 때다
// 공간복잡도를 줄이는법. 배열로 관리 안하기
class Solution {
public int rob(int[] nums) {
Expand All @@ -16,3 +13,21 @@ public int rob(int[] nums) {
return prev1;
}
}

// 점화식의 최대값을 구하는 방법
// 1. 현재 위치의 최대 값은 한칸 전 집까지만 털었던가(두칸 연속 겹치면 안된다는 룰을 지키면서)
// 2. 두칸 전 집까지 털고 + 현재집을 털었을 때다
class Solution {
public int rob(int[] nums) {
if (nums.length == 1) {
return nums[0];
}
int[] dp = new int[nums.length];
dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);
for (int i = 2; i < nums.length; i++) {
dp[i] = Math.max(nums[i] + dp[i - 2], dp[i - 1]);
}
return dp[nums.length - 1];
}
}
51 changes: 49 additions & 2 deletions longest-consecutive-sequence/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// 이중 변환 필요 없음
// 정렬이 들어가면 O(nlogn) 아래로 줄일 수 없음
// 중복여부만 제거하고 포함여부로 판단 O(N)
class Solution {
public int longestConsecutive(int[] nums) {
Expand Down Expand Up @@ -30,3 +28,52 @@ public int longestConsecutive(int[] nums) {
return maxLength;
}
}
// 정렬이 들어가면 O(nlogn) 아래로 줄일 수 없음
class Solution {
public int longestConsecutive(int[] nums) {
if(nums.length == 0) return 0;
TreeSet<Integer> set = new TreeSet<>();
for (int num : nums) {
set.add(num);
}
int max = 1;
int consecutiveCount = 1;
int prev = set.pollFirst();
while(!set.isEmpty()) {
int next = set.pollFirst();
if (next - prev == 1) {
consecutiveCount++;
} else {
max = Math.max(consecutiveCount, max);
consecutiveCount = 1;
}
prev = next;
}
return Math.max(max, consecutiveCount);
}
}
// 이중 변환 필요 없음
class Solution {
public int longestConsecutive(int[] nums) {
if(nums.length == 0) return 0;
HashSet<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
PriorityQueue<Integer> pq = new PriorityQueue<>(set);
int max = 1;
int consecutiveCount = 1;
int prev = pq.poll();
while(!pq.isEmpty()) {
int next = pq.poll();
if (next - prev == 1) {
consecutiveCount++;
} else {
max = Math.max(consecutiveCount, max);
consecutiveCount = 1;
}
prev = next;
}
return Math.max(max, consecutiveCount);
}
}

0 comments on commit db5c314

Please sign in to comment.