-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #299 from TonyKim9401/main
[TONY] WEEK 01 solutions
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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; | ||
} | ||
} |
This file contains 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,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 | ||
} |
This file contains 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,21 @@ | ||
class Solution { | ||
public int hammingWeight(int n) { | ||
/* | ||
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; | ||
*/ | ||
} | ||
} |
This file contains 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 Solution { | ||
public int countSubstrings(String s) { | ||
/** | ||
* time complexity: O(n); | ||
* space complexity: O(1); | ||
*/ | ||
|
||
int output = 0; | ||
// ex) abc | ||
// 1. abc | ||
// 2. bc | ||
// 3. c | ||
for (int i = 0; i < s.length(); i++) { | ||
output += palindromicCheck(s.substring(i, s.length())); | ||
} | ||
return output; | ||
} | ||
|
||
public int palindromicCheck(String str) { | ||
int result = 0; | ||
/** | ||
* ex) abc | ||
* 1. a | ||
* 2. ab | ||
* 3. abc | ||
*/ | ||
for (int i = 0; i < str.length(); i++) { | ||
String candidate = str.substring(0, i+1); | ||
if (palindromic(candidate)) { | ||
result += 1; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public boolean palindromic(String candidate) { | ||
int start = 0; | ||
int end = candidate.length() - 1; | ||
|
||
/** ex)abc | ||
* 1. a -> true | ||
* 2. ab -> false | ||
* 3. abc -> false | ||
*/ | ||
while (start < end) { | ||
if (candidate.charAt(start) != candidate.charAt(end)) return false; | ||
start += 1; | ||
end -= 1; | ||
} | ||
return true; | ||
} | ||
} |
This file contains 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,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))); | ||
|
||
|
||
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; | ||
} | ||
} |