-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #699 from imsosleepy/main
[ackku] Week 1
- Loading branch information
Showing
5 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// 중복제거를 위해 set을 적극적으로 활용해야할 듯... | ||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
Set<Integer> numSet = new HashSet<>(); | ||
|
||
for (int num : nums) { | ||
if (numSet.contains(num)) { | ||
return true; | ||
} | ||
numSet.add(num); | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// 공간복잡도를 줄이는법. 배열로 관리 안하기 | ||
class Solution { | ||
public int rob(int[] nums) { | ||
if (nums.length == 1) return nums[0]; | ||
|
||
int prev2 = nums[0]; // dp[i-2] | ||
int prev1 = Math.max(nums[0], nums[1]); // dp[i-1] | ||
for (int i = 2; i < nums.length; i++) { | ||
int current = Math.max(nums[i] + prev2, prev1); | ||
prev2 = prev1; | ||
prev1 = current; | ||
} | ||
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// 중복여부만 제거하고 포함여부로 판단 O(N) | ||
class Solution { | ||
public int longestConsecutive(int[] nums) { | ||
if (nums == null || nums.length == 0) { | ||
return 0; | ||
} | ||
|
||
HashSet<Integer> set = new HashSet<>(); | ||
for (int num : nums) { | ||
set.add(num); | ||
} | ||
|
||
int maxLength = 0; | ||
|
||
for (int num : set) { | ||
if (!set.contains(num - 1)) { | ||
int currentNum = num; | ||
int count = 1; | ||
while (set.contains(currentNum + 1)) { | ||
currentNum++; | ||
count++; | ||
} | ||
|
||
maxLength = Math.max(maxLength, count); | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution { | ||
public int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer, Integer> countMap = new HashMap<>(); | ||
for (int num : nums) { | ||
countMap.put(num, countMap.getOrDefault(num, 0) + 1); | ||
} | ||
|
||
PriorityQueue<Integer> pq = new PriorityQueue<>( | ||
Comparator.comparingInt(countMap::get).reversed() | ||
); | ||
|
||
pq.addAll(countMap.keySet()); | ||
|
||
int[] result = new int[k]; | ||
for (int i = 0; i < k; i++) { | ||
result[i] = pq.poll(); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 정규표현식으로 풀기엔 재미없어보여서 Character.isLetterOrDigit을 이용함 | ||
class Solution { | ||
public boolean isPalindrome(String s) { | ||
int left = 0 | ||
int right = s.length() - 1; | ||
|
||
while (left < right) { | ||
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) left++; | ||
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) right--; | ||
|
||
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) { | ||
return false; | ||
} | ||
left++; | ||
right--; | ||
} | ||
|
||
return true; | ||
} | ||
} |