Skip to content

[Gotprgmer] week 1 #624

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 7 commits into from
Dec 10, 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
30 changes: 30 additions & 0 deletions contains-duplicate/Gotprgmer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.HashSet;
import java.util.Set;

class SolutionGotprgmer {
// 해당 문제는 어느 한 숫자가 2개이상 존재할 경우 true를 그렇지 않을 경우, false를 반환하는 문제이다.
// set을 사용해서 set에 이미 값이 존재한다면 개수가 2 이상이므로 true 그렇지 않으면 false를 출력한다.

// 각 숫자들을 저장해서 set으로 관리 -> distinctNums
// nums의 각 숫자인 checkNum을 distinctNums에 넣어준다.
// 만약 checkNum이 이미 distinctNums에 존재한다면 ans를 true로 만들어주고 답을 출력한다.


// 시간복잡도 -> O(n)
// 공간복잡도 -> O(n)
static Set<Integer> distinctNums;
public boolean containsDuplicate(int[] nums) {
distinctNums = new HashSet<>();
boolean ans = false;
for (int checkNum : nums) {
if (distinctNums.contains(checkNum)) {
ans = true;
break;
};
distinctNums.add(checkNum);
}
return ans;
}


}
28 changes: 28 additions & 0 deletions top-k-frequent-elements/Gotprgmer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 각 수의 개수를 카운트 하고 카운트 한 값을 기준으로 정렬하여 우선순위 큐로
// k개를 추출하여 result 리스트에 담는다.

// 시간복잡도 : O(NlogN)
// 공간복잡도 : O(N)


public class SolutionGotprgmer {

public int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for(int num:nums){
map.put(num,map.getOrDefault(num,0)+1);
}
PriorityQueue<Map.Entry<Integer, Integer>> pq = new PriorityQueue<>(
(e1, e2) -> e2.getValue().compareTo(e1.getValue())
);
for (Map.Entry<Integer,Integer> entry : map.entrySet()){
pq.offer(entry);
}

int[] result = new int[k];
for(int ansIdx=0;ansIdx < k; ansIdx++){
result[ansIdx] = pq.poll().getKey();
}
return result;
}
}
24 changes: 24 additions & 0 deletions valid-palindrome/Gotprgmer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 입력된 문자 중 알파벳이나 숫자가 아닌 값들을 하나의 문자열로 만든다.
// 이때 모든 문자들을 소문자로 바꾼다.
// 역정렬한 값과 원래 값을 비교해 팰린드롬 유무를 출력한다.

// 알파벳 혹은 숫자가 아닌지 검사하는 문자 -> charToCheck

// 시간복잡도 : O(n)
// 공간복잡도 : O(n)

class Solution_Gotprgmer {
public boolean validPalindrome(String s) {
StringBuilder sb = new StringBuilder();
for(char charToCheck : s.toCharArray()){
if(!Character.isLetterOrDigit(charToCheck)){
continue;
}
sb.append(Character.toLowerCase(charToCheck));
}
String originalDirection = sb.toString();
String reDirection = sb.reverse().toString();

return originalDirection.equals(reDirection);
}
}
Loading