-
-
Notifications
You must be signed in to change notification settings - Fork 195
[TONY] WEEK 01 solutions #299
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
Changes from 4 commits
a7576f3
4002eb2
e714695
2a3dfd8
5739f6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
// HashSet O(n) | ||
/* | ||
Set<Integer> set = new HashSet(); | ||
for (int num : nums) set.add(num); | ||
return set.size() != nums.length; | ||
*/ | ||
|
||
// dupl value O(n log n) | ||
Arrays.sort(nums); | ||
for (int i = 0; i < nums.length - 1; i++) { | ||
if (nums[i] == nums[i + 1]) return true; | ||
} | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution { | ||
private List<Integer> nums = new ArrayList<>(); | ||
public int kthSmallest(TreeNode root, int k) { | ||
visitTreeNode(root); | ||
return nums.get(k-1); | ||
} | ||
|
||
public void visitTreeNode(TreeNode node) { | ||
if (node == null) return; | ||
|
||
// left < right | ||
visitTreeNode(node.left); | ||
nums.add(node.val); | ||
visitTreeNode(node.right); | ||
} | ||
// time complexity: O(n), visit all nodes once | ||
// space complexity: O(1), used an array list | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 감사합니다! 잘못 생각했네요ㅜ |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution { | ||
public int hammingWeight(int n) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 주석으로 처리하신 public static int bitCount(int i) {
// HD, Figure 5-2
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
} 단순 연산으로 처리하고 있어서 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 리뷰 주셔서 감사합니다! Integer.bitCount(n) : 시간 복잡도 O(1), 공간 복잡도 O(1)
while 반복문: 시간 복잡도 O(log n), 공간 복잡도 O(1) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
오 그렇군요 ㅎㅎ 확인해주셔서 감사합니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @TonyKim9401 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @DaleSeo 위에 jdalma 님께서 올려주신 코드가 내부적으로 사용하는 코드이네요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네네, 덕분에 저도 배웠네요! 이런 코드가 실제 코딩 테스트에서 유리할 지 불리할 지에 대해서 모임 때 같이 얘기해보면 좋을 것 같습니다. |
||
/* | ||
Time complexity: O(n) | ||
Space complexity: O(1) | ||
*/ | ||
return Integer.bitCount(n); | ||
|
||
/* | ||
Time complexity: O(n) | ||
Space complexity: O(1) | ||
|
||
int output = 0; | ||
while (n > 0) { | ||
if ((n & 1) == 1) output += 1; | ||
n >>= 1; | ||
} | ||
return output; | ||
*/ | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Solution { | ||
public int[] topKFrequent(int[] nums, int k) { | ||
|
||
// declare hashmap | ||
// key: each element, value: appered count | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
|
||
// if map contains the element, increase its value by one. | ||
// else put the element and 1 for initializing | ||
for (int num : nums) { | ||
if (map.containsKey(num)) { | ||
map.put(num, map.getOrDefault(num, 0) + 1); | ||
} else { | ||
map.put(num, 1); | ||
} | ||
} | ||
|
||
// keyList only has key values of the hashmap | ||
// using their value count sort keys by descending order | ||
List<Integer> keyList = new ArrayList<>(map.keySet()); | ||
Collections.sort(keyList, (o1, o2) -> map.get(o2).compareTo(map.get(o1))); | ||
Comment on lines
+18
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분이 얼마나 시간이 걸릴지 분석을 한 번 해보시면 어떨까요? 그 분석을 바탕으로 좀 더 효율적인 알고리즘을 고민해보셔도 좋을 것 같습니다. 이미 문제를 다 푸셔서 시간이 좀 있으실 것 같아서 제안드립니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @DaleSeo 이 부분은... GPT에 물어 본 결과였구요 ㅜ 답변 공유해드리는걸로 대체해도 괜찮을까요?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 위 내용은 #299 (comment) 에 대한 답변이 아닐까요? 제가 여쭤본 내용에 대한 답변이 아닌 것 같습니다. |
||
|
||
|
||
int[] output = new int[k]; | ||
int idx = 0; | ||
|
||
// retreive keys k times and set output | ||
while (idx < k) output[idx] = keyList.get(idx++); | ||
|
||
return output; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.