Skip to content

Commit 80129c6

Browse files
authored
Merge pull request #299 from TonyKim9401/main
[TONY] WEEK 01 solutions
2 parents 1f2b1c2 + 5739f6e commit 80129c6

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public boolean containsDuplicate(int[] nums) {
3+
// HashSet O(n)
4+
/*
5+
Set<Integer> set = new HashSet();
6+
for (int num : nums) set.add(num);
7+
return set.size() != nums.length;
8+
*/
9+
10+
// dupl value O(n log n)
11+
Arrays.sort(nums);
12+
for (int i = 0; i < nums.length - 1; i++) {
13+
if (nums[i] == nums[i + 1]) return true;
14+
}
15+
return false;
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
private List<Integer> nums = new ArrayList<>();
3+
public int kthSmallest(TreeNode root, int k) {
4+
visitTreeNode(root);
5+
return nums.get(k-1);
6+
}
7+
8+
public void visitTreeNode(TreeNode node) {
9+
if (node == null) return;
10+
11+
// left < right
12+
visitTreeNode(node.left);
13+
nums.add(node.val);
14+
visitTreeNode(node.right);
15+
}
16+
// time complexity: O(n), visit all nodes once
17+
// space complexity: O(1), used an array list
18+
}

number-of-1-bits/TonyKim9401.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int hammingWeight(int n) {
3+
/*
4+
Time complexity: O(n)
5+
Space complexity: O(1)
6+
*/
7+
return Integer.bitCount(n);
8+
9+
/*
10+
Time complexity: O(n)
11+
Space complexity: O(1)
12+
13+
int output = 0;
14+
while (n > 0) {
15+
if ((n & 1) == 1) output += 1;
16+
n >>= 1;
17+
}
18+
return output;
19+
*/
20+
}
21+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Solution {
2+
public int countSubstrings(String s) {
3+
/**
4+
* time complexity: O(n);
5+
* space complexity: O(1);
6+
*/
7+
8+
int output = 0;
9+
// ex) abc
10+
// 1. abc
11+
// 2. bc
12+
// 3. c
13+
for (int i = 0; i < s.length(); i++) {
14+
output += palindromicCheck(s.substring(i, s.length()));
15+
}
16+
return output;
17+
}
18+
19+
public int palindromicCheck(String str) {
20+
int result = 0;
21+
/**
22+
* ex) abc
23+
* 1. a
24+
* 2. ab
25+
* 3. abc
26+
*/
27+
for (int i = 0; i < str.length(); i++) {
28+
String candidate = str.substring(0, i+1);
29+
if (palindromic(candidate)) {
30+
result += 1;
31+
}
32+
}
33+
return result;
34+
}
35+
36+
public boolean palindromic(String candidate) {
37+
int start = 0;
38+
int end = candidate.length() - 1;
39+
40+
/** ex)abc
41+
* 1. a -> true
42+
* 2. ab -> false
43+
* 3. abc -> false
44+
*/
45+
while (start < end) {
46+
if (candidate.charAt(start) != candidate.charAt(end)) return false;
47+
start += 1;
48+
end -= 1;
49+
}
50+
return true;
51+
}
52+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public int[] topKFrequent(int[] nums, int k) {
3+
4+
// declare hashmap
5+
// key: each element, value: appered count
6+
Map<Integer, Integer> map = new HashMap<>();
7+
8+
// if map contains the element, increase its value by one.
9+
// else put the element and 1 for initializing
10+
for (int num : nums) {
11+
if (map.containsKey(num)) {
12+
map.put(num, map.getOrDefault(num, 0) + 1);
13+
} else {
14+
map.put(num, 1);
15+
}
16+
}
17+
18+
// keyList only has key values of the hashmap
19+
// using their value count sort keys by descending order
20+
List<Integer> keyList = new ArrayList<>(map.keySet());
21+
Collections.sort(keyList, (o1, o2) -> map.get(o2).compareTo(map.get(o1)));
22+
23+
24+
int[] output = new int[k];
25+
int idx = 0;
26+
27+
// retreive keys k times and set output
28+
while (idx < k) output[idx] = keyList.get(idx++);
29+
30+
return output;
31+
}
32+
}

0 commit comments

Comments
 (0)