Skip to content

Commit e884ecd

Browse files
committed
feat : week01
1 parent 64e75e1 commit e884ecd

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

contains-duplicate/ackku.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// 중복제거를 위해 set을 적극적으로 활용해야할 듯...
2+
class Solution {
3+
public boolean containsDuplicate(int[] nums) {
4+
Set<Integer> numSet = new HashSet<>();
5+
6+
for (int num : nums) {
7+
if (numSet.contains(num)) {
8+
return true;
9+
}
10+
numSet.add(num);
11+
}
12+
13+
return false;
14+
}
15+
}

house-robber/ackku.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// 공간복잡도를 줄이는법. 배열로 관리 안하기
2+
class Solution {
3+
public int rob(int[] nums) {
4+
if (nums.length == 1) return nums[0];
5+
6+
int prev2 = nums[0]; // dp[i-2]
7+
int prev1 = Math.max(nums[0], nums[1]); // dp[i-1]
8+
for (int i = 2; i < nums.length; i++) {
9+
int current = Math.max(nums[i] + prev2, prev1);
10+
prev2 = prev1;
11+
prev1 = current;
12+
}
13+
return prev1;
14+
}
15+
}
16+
17+
class Solution {
18+
public int rob(int[] nums) {
19+
// 점화식의 최대값을 구하는 방법
20+
// 1. 현재 위치의 최대 값은 한칸 전 집까지만 털었던가(두칸 연속 겹치면 안된다는 룰을 지키면서)
21+
// 2. 두칸 전 집까지 털고 + 현재집을 털었을 때다
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+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// 중복여부만 제거하고 포함여부로 판단 O(N)
2+
class Solution {
3+
public int longestConsecutive(int[] nums) {
4+
if (nums == null || nums.length == 0) {
5+
return 0;
6+
}
7+
8+
HashSet<Integer> set = new HashSet<>();
9+
for (int num : nums) {
10+
set.add(num);
11+
}
12+
13+
int maxLength = 0;
14+
15+
for (int num : set) {
16+
if (!set.contains(num - 1)) {
17+
int currentNum = num;
18+
int count = 1;
19+
while (set.contains(currentNum + 1)) {
20+
currentNum++;
21+
count++;
22+
}
23+
24+
maxLength = Math.max(maxLength, count);
25+
}
26+
}
27+
28+
return maxLength;
29+
}
30+
}
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+
}

top-k-frequent-elements/ackku.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int[] topKFrequent(int[] nums, int k) {
3+
Map<Integer, Integer> countMap = new HashMap<>();
4+
for (int num : nums) {
5+
countMap.put(num, countMap.getOrDefault(num, 0) + 1);
6+
}
7+
8+
PriorityQueue<Integer> pq = new PriorityQueue<>(
9+
Comparator.comparingInt(countMap::get).reversed()
10+
);
11+
12+
pq.addAll(countMap.keySet());
13+
14+
int[] result = new int[k];
15+
for (int i = 0; i < k; i++) {
16+
result[i] = pq.poll();
17+
}
18+
return result;
19+
}
20+
}

valid-palindrome/ackku.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// 정규표현식으로 풀기엔 재미없어보여서 Character.isLetterOrDigit을 이용함
2+
class Solution {
3+
public boolean isPalindrome(String s) {
4+
int left = 0
5+
int right = s.length() - 1;
6+
7+
while (left < right) {
8+
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) left++;
9+
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) right--;
10+
11+
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
12+
return false;
13+
}
14+
left++;
15+
right--;
16+
}
17+
18+
return true;
19+
}
20+
}

0 commit comments

Comments
 (0)