Skip to content

[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 4 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions contains-duplicate/imsosleepy.java
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;
}
}
33 changes: 33 additions & 0 deletions house-robber/imsosleepy.java
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];
}
}
79 changes: 79 additions & 0 deletions longest-consecutive-sequence/imsosleepy.java
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);
}
}
20 changes: 20 additions & 0 deletions top-k-frequent-elements/imsosleepy.java
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;
}
}
20 changes: 20 additions & 0 deletions valid-palindrome/imsosleepy.java
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--;
}
Comment on lines +7 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드가 깔끔하고 이해하기 쉽게 작성된 것 같습니다.
정규표현식이 재미없어 isLetterOrDigit() 메서드를 사용하신 것도 좋아보입니다~


return true;
}
}
Loading