Skip to content

Commit

Permalink
Merge pull request #299 from TonyKim9401/main
Browse files Browse the repository at this point in the history
[TONY] WEEK 01 solutions
  • Loading branch information
leokim0922 authored Aug 16, 2024
2 parents 1f2b1c2 + 5739f6e commit 80129c6
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
17 changes: 17 additions & 0 deletions contains-duplicate/TonyKim9401.java
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;
}
}
18 changes: 18 additions & 0 deletions kth-smallest-element-in-a-bst/TonyKim9401.java
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
}
21 changes: 21 additions & 0 deletions number-of-1-bits/TonyKim9401.java
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;
*/
}
}
52 changes: 52 additions & 0 deletions palindromic-substrings/TonyKim9401.java
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;
}
}
32 changes: 32 additions & 0 deletions top-k-frequent-elements/TonyKim9401.java
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;
}
}

0 comments on commit 80129c6

Please sign in to comment.