Skip to content

[wonYeong] Week 1 #631

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 11 commits into from
Dec 13, 2024
11 changes: 11 additions & 0 deletions contains-duplicate/dalpang81.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//시간복잡도 : O(nlogn)
class Solution {
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
for(int i = 0; i < nums.length - 1; i++) {
if(nums[i] == nums[i+1])
return true;
}
return false;
}
}
19 changes: 19 additions & 0 deletions house-robber/dalpang81.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//시간복잡도 O(n)
class Solution {
public int rob(int[] nums) {
if (nums.length == 1) {
return nums[0];
}

int temp2 = nums[0];
int temp1 = Math.max(nums[0], nums[1]);

for (int i = 2; i < nums.length; i++) {
int current = Math.max(temp1, nums[i] + temp2);
temp2 = temp1;
temp1 = current;
}

return temp1;
}
}
33 changes: 33 additions & 0 deletions longest-consecutive-sequence/dalpang81.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//시간복잡도: O(n)
import java.util.*;

class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> numSet = new HashSet<>();
for (int num : nums) {
numSet.add(num);
}

int longNum = 0;

// 각 숫자에 대해 시퀀스 시작 여부를 확인
for (int num : numSet) {
// num-1이 없는 경우에만 시퀀스를 시작
if (!numSet.contains(num - 1)) {
int currentNum = num;
int currentLong = 1;

// 연속된 숫자를 탐색
while (numSet.contains(currentNum + 1)) {
currentNum++;
currentLong++;
}

// 가장 긴 시퀀스를 갱신
longNum = Math.max(longNum, currentLong);
}
}

return longNum;
}
}
31 changes: 31 additions & 0 deletions top-k-frequent-elements/dalpang81.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//시간복잡도: O(n + mlogk)
import java.util.*;
class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();


for(int i : nums) {
map.put(i, map.getOrDefault(i,0) + 1);
}

PriorityQueue<Map.Entry<Integer, Integer>> pq =
new PriorityQueue<>(Comparator.comparingInt(Map.Entry::getValue));

for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
pq.offer(entry);
if (pq.size() > k) {
pq.poll(); // Remove the least frequent element
}
}

// Step 3: Extract the elements from the heap
int[] result = new int[k];
for (int i = k - 1; i >= 0; i--) {
result[i] = pq.poll().getKey();
}

return result;

}
}
13 changes: 13 additions & 0 deletions valid-palindrome/dalpang81.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//시간복잡도: O(n)
class Solution {
public boolean isPalindrome(String s) {
s = s.toLowerCase().trim();
s = s.replaceAll("[^a-z0-9]", "");

StringBuffer sb = new StringBuffer(s);
String reverse = sb.reverse().toString();

return(s.equals(reverse));

}
}
Loading