diff --git a/contains-duplicate/TonyKim9401.java b/contains-duplicate/TonyKim9401.java new file mode 100644 index 000000000..ef9037c49 --- /dev/null +++ b/contains-duplicate/TonyKim9401.java @@ -0,0 +1,17 @@ +class Solution { + public boolean containsDuplicate(int[] nums) { + // HashSet O(n) + /* + Set 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; + } +} \ No newline at end of file diff --git a/kth-smallest-element-in-a-bst/TonyKim9401.java b/kth-smallest-element-in-a-bst/TonyKim9401.java new file mode 100644 index 000000000..ac759e459 --- /dev/null +++ b/kth-smallest-element-in-a-bst/TonyKim9401.java @@ -0,0 +1,18 @@ +class Solution { + private List 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 +} \ No newline at end of file diff --git a/number-of-1-bits/TonyKim9401.java b/number-of-1-bits/TonyKim9401.java new file mode 100644 index 000000000..8cf89e55f --- /dev/null +++ b/number-of-1-bits/TonyKim9401.java @@ -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; + */ + } +} \ No newline at end of file diff --git a/palindromic-substrings/TonyKim9401.java b/palindromic-substrings/TonyKim9401.java new file mode 100644 index 000000000..a35717567 --- /dev/null +++ b/palindromic-substrings/TonyKim9401.java @@ -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; + } +} \ No newline at end of file diff --git a/top-k-frequent-elements/TonyKim9401.java b/top-k-frequent-elements/TonyKim9401.java new file mode 100644 index 000000000..4688be0cf --- /dev/null +++ b/top-k-frequent-elements/TonyKim9401.java @@ -0,0 +1,32 @@ +class Solution { + public int[] topKFrequent(int[] nums, int k) { + + // declare hashmap + // key: each element, value: appered count + Map 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 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; + } +} \ No newline at end of file