Skip to content

Commit 226a07a

Browse files
authored
Merge pull request #321 from kim-young/main
[kimyoung] WEEK 01 Solutions
2 parents bdc484b + c60723c commit 226a07a

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

contains-duplicate/kimyoung.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var containsDuplicate = function (nums) {
2+
let set = new Set(); // create a set to keep track of items within the nums array
3+
for (const el of nums) set.add(el); // add items to the set, duplicates will automatically be ignored (set vs map)
4+
return set.size !== nums.length; // compare the length of nums array and the size of the set, which shows if there's a duplicate or not
5+
};
6+
7+
// test cases
8+
console.log(containsDuplicate([])); // false
9+
console.log(containsDuplicate([1, 2, 3, 1])); // true
10+
console.log(containsDuplicate([1, 2, 3, 4])); // false
11+
12+
// space - O(n) - creating a set to store elements
13+
// time - O(n) - traverse through the array
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var kthSmallest = function (root, k) {
2+
const nums = [];
3+
function helper(node) { // helper method to traverse the binary tree to map the node values to nums array
4+
if (!node) return;
5+
nums.push(node.val);
6+
if (node.left) helper(node.left); // recursive call to left node if it exists
7+
if (node.right) helper(node.right); // recursive call to right node if it exists
8+
}
9+
helper(root);
10+
const sorted = nums.sort((a, b) => a - b); // sort the nums array
11+
return sorted[k - 1]; // return kth smallest val
12+
};
13+
14+
// space - O(n) - mapping node values into nums array
15+
// time - O(nlogn) - sort

number-of-1-bits/kimyoung.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var hammingWeight = function (n) {
2+
let bitVal = n.toString(2); // convert input value to bit
3+
let setBits = 0;
4+
for (const char of bitVal) { // iterate through the string bit value to check the number of "1"s
5+
if (char === "1") setBits++; // increment if char === "1"
6+
}
7+
return setBits;
8+
};
9+
10+
// test cases
11+
console.log(hammingWeight(11)); // 3
12+
console.log(hammingWeight(128)); // 1
13+
console.log(hammingWeight(2147483645)); // 30
14+
15+
// space - O(1) - created only constant variables
16+
// time - O(n) - iterate through the bitVal string (larger the number the longer the string)

palindromic-substrings/kimyoung.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var countSubstrings = function (s) {
2+
let result = 0;
3+
for (let i = 0; i < s.length; i++) {
4+
let left = i,
5+
right = i; // odd length substrings
6+
helper(s, left, right);
7+
8+
(left = i), (right = i + 1); // even length substrings
9+
helper(s, left, right);
10+
}
11+
function helper(s, l, r) {
12+
// increment result and keep expanding left and right, while left and right indexes are within range and they're equal
13+
while (l >= 0 && r <= s.length && s[l] === s[r]) {
14+
result++;
15+
l--;
16+
r++;
17+
}
18+
}
19+
return result;
20+
};
21+
22+
// test cases
23+
console.log(countSubstrings("abc")); // 3
24+
console.log(countSubstrings("aaa")); // 6
25+
console.log(countSubstrings("a")); // 1
26+
console.log(countSubstrings("")); // 0
27+
28+
// space - O(1) - constant variable `result`
29+
// time - O(n^2) - iterating through the string and expanding both ways

top-k-frequent-elements/kimyoung.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var topKFrequent = function (nums, k) {
2+
let obj = {};
3+
for (const num of nums) {
4+
obj[num] ? obj[num]++ : (obj[num] = 1);
5+
}
6+
let sorted = Object.entries(obj).sort((a, b) => b[1] - a[1]);
7+
let result = [];
8+
for (let i = 0; i < k; i++) {
9+
result.push(sorted[i][0]);
10+
}
11+
return result;
12+
};
13+
14+
// test cases
15+
console.log(topKFrequent([1, 1, 1, 2, 2, 3], 2)); // [1, 2]
16+
console.log(topKFrequent([1], 1)); // [1]
17+
18+
// space - O(n) - mapping the object in [key, freq]
19+
// time - O(nlogn) - sorting the mapped objects in the order of frequency

0 commit comments

Comments
 (0)