-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jaejeong1] WEEK 01 solutions #326
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a8d6265
contains duplicate solution
wad-jangjaejeong 6a12988
number of 1 bits solution
wad-jangjaejeong bcd0ee8
top k frequent elements solution
wad-jangjaejeong 6b783a7
Kth Smallest Element in a BST solution
wad-jangjaejeong a230922
Panlindromic substrings solution
wad-jangjaejeong 3baa1d5
피드백 반영
wad-jangjaejeong bb2f2d3
피드백 반영
wad-jangjaejeong 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,16 @@ | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
class SolutionJaeJeong1 { | ||
public boolean containsDuplicate(int[] nums) { | ||
// 해시맵 사용해서 같은 값의 카운트가 1보다 크면 true 반환 | ||
// 끝까지 다 돌면 false 반환 | ||
// 또는 해시셋 사용해서 모두 해시셋에 넣고 | ||
// 길이 비교해서 같으면 false, 다르면 true 반환 | ||
// 시간복잡도: O(N), 공간복잡도: O(N) | ||
|
||
Set<Integer> set = Arrays.stream(nums).collect(HashSet::new, Set::add, Set::addAll); | ||
return set.size() != nums.length; | ||
} | ||
} |
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,36 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
// Definition for a binary tree node. | ||
class TreeNode { | ||
int val; | ||
TreeNode left; | ||
TreeNode right; | ||
TreeNode() {} | ||
TreeNode(int val) { this.val = val; } | ||
TreeNode(int val, TreeNode left, TreeNode right) { | ||
this.val = val; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
} | ||
class SolutionKthSmallest { | ||
public int kthSmallest(TreeNode root, int k) { | ||
// 이진 검색 트리의 루트와 정수 k가 주어지면 트리에 있는 모든 노드 값 중 k번째로 작은 값(1-인덱스)을 반환합니다. | ||
// 이진 검색 트리의 특성을 이용해 중위 순회를 하면 노드 값이 오름차순으로 정렬된다. | ||
// 정렬 후 k번째 값을 반환한다. | ||
// 시간복잡도: O(N), 공간복잡도: O(N) | ||
List<Integer> list = new ArrayList<>(); | ||
inorder(root, list); | ||
return list.get(k - 1); | ||
} | ||
|
||
private void inorder(TreeNode root, List<Integer> list) { | ||
if (root == null) { | ||
return; | ||
} | ||
inorder(root.left, list); | ||
list.add(root.val); | ||
inorder(root.right, list); | ||
} | ||
} |
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,14 @@ | ||
class SolutionJaejeong1 { | ||
public int hammingWeight(int n) { | ||
// 주어진 양의 정수를 이진수로 변환하고, 1로 설정된 자릿수의 개수를 반환 | ||
// 시간복잡도: O(1), 공간복잡도: O(1) | ||
int num = 0; | ||
for (int i=0; i<32; i++) { | ||
if((n & 1) == 1) { | ||
num++; | ||
} | ||
n = n >> 1; | ||
} | ||
return num; | ||
} | ||
} |
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,52 @@ | ||
class SolutionPalindromicSubstrings { | ||
// 1번쨰 풀이: 구현해야할 로직을 팰린드롬 여부 검사와 검사할 대상 문자열을 구하는 로직 둘로 나눈다 | ||
// 팰린드롬 여부 검사: 투포인터 사용, lt=0, rt=length-1 로 시작해 동등성 여부를 검사 | ||
// 시간복잡도: O(N), 공간복잡도: O(1) | ||
// 대상 문자열 구하기: 투포인터 사용. rt가 length보다 같거나 작을떄까지 계속 증가시키고, | ||
// rt가 끝에 도달하면 lt를 증가시키고, rt를 lt+1로 만든다. 모든 과정에서 팰린드롬 여부를 검사한다 | ||
// 시간복잡도: O(N), 공간복잡도: O(1) | ||
// 결과 | ||
// 시간복잡도: O(N^2), 공간복잡도: O(1) | ||
|
||
public int countSubstrings(String s) { | ||
var subStrings = s.toCharArray(); | ||
|
||
if (subStrings.length == 0) return 0; | ||
if (subStrings.length == 1) return 1; | ||
|
||
var answer = 0; | ||
|
||
var lt = 0; | ||
var rt = 1; | ||
while(lt < subStrings.length){ | ||
if (isPalindrome(s.substring(lt, rt))) { | ||
answer++; | ||
} | ||
|
||
if (rt <= subStrings.length-1){ | ||
rt++; | ||
} else { | ||
lt++; | ||
rt = lt+1; | ||
} | ||
} | ||
|
||
return answer; | ||
} | ||
|
||
private boolean isPalindrome(String s) { | ||
var chars = s.toCharArray(); | ||
var lt = 0; | ||
var rt = chars.length - 1; | ||
|
||
while(lt < rt) { | ||
if (chars[lt] != chars[rt]) { | ||
return false; | ||
} | ||
lt++; | ||
rt--; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,24 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class SolutionTopKFrequentElements { | ||
public int[] topKFrequent(int[] nums, int k) { | ||
// 빈도순으로 k개 반환 | ||
// 빈도 체크: 해시맵으로 카운트. 시간복잡도 O(N), 공간복잡도 O(N) | ||
// 빈도순 정렬: sorting, 시간복잡도 O(N log N), 공간복잡도 O(N) | ||
// 합산: 시간복잡도 O(N log N), 공간복잡도 O(N) | ||
|
||
// 빈도 체크 | ||
Map<Integer, Integer> freq = new HashMap<>(); | ||
for (int num : nums) { | ||
freq.put(num, freq.getOrDefault(num, 0) + 1); | ||
} | ||
|
||
// 빈도순 정렬 | ||
return freq.keySet().stream() | ||
.sorted((a, b) -> freq.get(b) - freq.get(a)) | ||
.mapToInt(i -> i) | ||
.limit(k) // 배열에서 상위 k개만 반환 | ||
.toArray(); | ||
} | ||
} |
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.
Stream 사용하니까 엄청 직관적으로 정렬 시킬 수 있네요.. 👍
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.
네 맞습니다!! 스트림의 장점인것 같아요 ㅎㅎ