-
-
Notifications
You must be signed in to change notification settings - Fork 245
[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 3 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,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,27 @@ | ||
import java.util.Arrays; | ||
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); | ||
} | ||
|
||
// 빈도순 정렬 | ||
int[] sortedNums = freq.keySet().stream() | ||
.sorted((a, b) -> freq.get(b) - freq.get(a)) | ||
.mapToInt(i -> i) | ||
.toArray(); | ||
|
||
// 배열에서 상위 k개만 반환 | ||
return Arrays.copyOf(sortedNums, k); | ||
} | ||
} |
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.
재밌는 코드 잘 보았습니다 : )
풀이에 영향이 있는건 아니지만 궁금한 부분이 있어서 질문드립니다!
이런식으로 limit 을 걸어줘도 됐을 것 같은데
혹시
Arrays.copyOf
를 추가적으로 사용하신 이유가 있으신지 궁금합니다 : )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.
좋은 피드백 감사합니다. 두 방법 모두 결과는 같으나
copyOf 사용 시 새 배열 생성으로 인해 불필요한 메모리를 낭비 및 성능적 단점이 있어 보이네요 :)
제안주신 방법이 더 나은 방안으로 보여 피드백 적용해보겠습니다!!