-
-
Notifications
You must be signed in to change notification settings - Fork 195
[ackku] Week 1 #699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[ackku] Week 1 #699
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코드가 깔끔하고 이해하기 쉽게 작성된 것 같습니다.
정규표현식이 재미없어 isLetterOrDigit() 메서드를 사용하신 것도 좋아보입니다~