Skip to content

Commit db5c314

Browse files
committed
fix : 코드 원복
1 parent 495b9d2 commit db5c314

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

house-robber/imsosleepy.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// 점화식의 최대값을 구하는 방법
2-
// 1. 현재 위치의 최대 값은 한칸 전 집까지만 털었던가(두칸 연속 겹치면 안된다는 룰을 지키면서)
3-
// 2. 두칸 전 집까지 털고 + 현재집을 털었을 때다
41
// 공간복잡도를 줄이는법. 배열로 관리 안하기
52
class Solution {
63
public int rob(int[] nums) {
@@ -16,3 +13,21 @@ public int rob(int[] nums) {
1613
return prev1;
1714
}
1815
}
16+
17+
// 점화식의 최대값을 구하는 방법
18+
// 1. 현재 위치의 최대 값은 한칸 전 집까지만 털었던가(두칸 연속 겹치면 안된다는 룰을 지키면서)
19+
// 2. 두칸 전 집까지 털고 + 현재집을 털었을 때다
20+
class Solution {
21+
public int rob(int[] nums) {
22+
if (nums.length == 1) {
23+
return nums[0];
24+
}
25+
int[] dp = new int[nums.length];
26+
dp[0] = nums[0];
27+
dp[1] = Math.max(nums[0], nums[1]);
28+
for (int i = 2; i < nums.length; i++) {
29+
dp[i] = Math.max(nums[i] + dp[i - 2], dp[i - 1]);
30+
}
31+
return dp[nums.length - 1];
32+
}
33+
}

longest-consecutive-sequence/imsosleepy.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// 이중 변환 필요 없음
2-
// 정렬이 들어가면 O(nlogn) 아래로 줄일 수 없음
31
// 중복여부만 제거하고 포함여부로 판단 O(N)
42
class Solution {
53
public int longestConsecutive(int[] nums) {
@@ -30,3 +28,52 @@ public int longestConsecutive(int[] nums) {
3028
return maxLength;
3129
}
3230
}
31+
// 정렬이 들어가면 O(nlogn) 아래로 줄일 수 없음
32+
class Solution {
33+
public int longestConsecutive(int[] nums) {
34+
if(nums.length == 0) return 0;
35+
TreeSet<Integer> set = new TreeSet<>();
36+
for (int num : nums) {
37+
set.add(num);
38+
}
39+
int max = 1;
40+
int consecutiveCount = 1;
41+
int prev = set.pollFirst();
42+
while(!set.isEmpty()) {
43+
int next = set.pollFirst();
44+
if (next - prev == 1) {
45+
consecutiveCount++;
46+
} else {
47+
max = Math.max(consecutiveCount, max);
48+
consecutiveCount = 1;
49+
}
50+
prev = next;
51+
}
52+
return Math.max(max, consecutiveCount);
53+
}
54+
}
55+
// 이중 변환 필요 없음
56+
class Solution {
57+
public int longestConsecutive(int[] nums) {
58+
if(nums.length == 0) return 0;
59+
HashSet<Integer> set = new HashSet<>();
60+
for (int num : nums) {
61+
set.add(num);
62+
}
63+
PriorityQueue<Integer> pq = new PriorityQueue<>(set);
64+
int max = 1;
65+
int consecutiveCount = 1;
66+
int prev = pq.poll();
67+
while(!pq.isEmpty()) {
68+
int next = pq.poll();
69+
if (next - prev == 1) {
70+
consecutiveCount++;
71+
} else {
72+
max = Math.max(consecutiveCount, max);
73+
consecutiveCount = 1;
74+
}
75+
prev = next;
76+
}
77+
return Math.max(max, consecutiveCount);
78+
}
79+
}

0 commit comments

Comments
 (0)