diff --git a/contains-duplicate/wogha95.js b/contains-duplicate/wogha95.js new file mode 100644 index 000000000..6dbd43f01 --- /dev/null +++ b/contains-duplicate/wogha95.js @@ -0,0 +1,10 @@ +// TC: O(N) +// SC: O(N) + +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function (nums) { + return nums.length !== new Set(nums).size; +}; diff --git a/kth-smallest-element-in-a-bst/wogha95.js b/kth-smallest-element-in-a-bst/wogha95.js new file mode 100644 index 000000000..8bbd2f7be --- /dev/null +++ b/kth-smallest-element-in-a-bst/wogha95.js @@ -0,0 +1,41 @@ +// TC: O(N) +// SC: O(N) + +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @param {number} k + * @return {number} + */ +var kthSmallest = function (root, k) { + let result = null; + + dfs(root); + + return result; + + function dfs(current) { + if (result !== null) { + return; + } + if (!current) { + return; + } + + dfs(current.left); + if (current.val >= 0) { + if (k === 1) { + result = current.val; + } + k -= 1; + } + dfs(current.right); + } +}; diff --git a/number-of-1-bits/wogha95.js b/number-of-1-bits/wogha95.js new file mode 100644 index 000000000..03b332ace --- /dev/null +++ b/number-of-1-bits/wogha95.js @@ -0,0 +1,13 @@ +// TC: O(log n) +// SC: O(1) + +/** + * @param {number} n + * @return {number} + */ +var hammingWeight = function (n) { + return n + .toString(2) + .split("") + .filter((s) => s === "1").length; +}; diff --git a/palindromic-substrings/wogha95.js b/palindromic-substrings/wogha95.js new file mode 100644 index 000000000..eae4f8376 --- /dev/null +++ b/palindromic-substrings/wogha95.js @@ -0,0 +1,32 @@ +// TC: O(n^3) +// SC: O(1) + +/** + * @param {string} s + * @return {number} + */ +var countSubstrings = function(s) { + let count = 0; + + for (let left = 0; left < s.length; left++) { + for (let right = left; right < s.length; right++) { + if(checkIsPalinDrome(left, right)) { + count += 1; + } + } + } + + return count; + + function checkIsPalinDrome(left, right) { + while (left < right) { + if (s[left] !== s[right]) { + return false; + } + left += 1; + right -= 1; + } + + return true; + } +}; \ No newline at end of file diff --git a/top-k-frequent-elements/wogha95.js b/top-k-frequent-elements/wogha95.js new file mode 100644 index 000000000..9aa119139 --- /dev/null +++ b/top-k-frequent-elements/wogha95.js @@ -0,0 +1,19 @@ +// TC: O(n logn) +// SC: O(N) + +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var topKFrequent = function (nums, k) { + const numAndCountBoard = nums.reduce((result, current) => { + result[current] = result.hasOwnProperty(current) ? result[current] + 1 : 1; + return result; + }, {}); + + return Object.entries(numAndCountBoard) + .sort((a, b) => b[1] - a[1]) + .slice(0, k) + .map((element) => element[0]); +};