From b57c526f2a9a278d066f414b1a0d66e8e5bf6378 Mon Sep 17 00:00:00 2001 From: Jaehyeon Robert Han Date: Mon, 23 Dec 2024 16:53:15 -0800 Subject: [PATCH 1/4] two-sum solution --- two-sum/Zioq.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 two-sum/Zioq.js diff --git a/two-sum/Zioq.js b/two-sum/Zioq.js new file mode 100644 index 000000000..592da9b53 --- /dev/null +++ b/two-sum/Zioq.js @@ -0,0 +1,30 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function(nums, target) { + // Initialize object to save remained value with index + let remain_with_index_obj = {} + + for ( let i =0; i Date: Tue, 24 Dec 2024 14:18:41 -0800 Subject: [PATCH 2/4] reverse-bit solution --- reverse-bits/Zioq.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 reverse-bits/Zioq.js diff --git a/reverse-bits/Zioq.js b/reverse-bits/Zioq.js new file mode 100644 index 000000000..20f3d5ae6 --- /dev/null +++ b/reverse-bits/Zioq.js @@ -0,0 +1,19 @@ +/** + * @param {number} n - a positive integer + * @return {number} - a positive integer + */ +var reverseBits = function(n) { + let result = 0; //Initial value + for (let i=0; i < 32; i++) { //The loop iterates 32 times, as the input n is a 32-bit unsigned integer + result = (result << 1) | (n & 1); // Shift the result to the left by 1 bit OR it with the least significant bit of n. + n >>= 1; // Shifts the bits of n one place to the right, effectively "removing" the processed LSB. + } + return result >>> 0; +}; +/* + Time Complexity: O(1), because we always loop exactly 32 times, regardless of the input. + Space Complexity: O(1), because we use a constant amount of space. +*/ + + + From 205f7a97d3f7ce0e47f09b779de3c8fcb6604b4b Mon Sep 17 00:00:00 2001 From: Jaehyeon Robert Han Date: Wed, 25 Dec 2024 14:29:17 -0800 Subject: [PATCH 3/4] product-of-array-except-self solution --- product-of-array-except-self/Zioq.js | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 product-of-array-except-self/Zioq.js diff --git a/product-of-array-except-self/Zioq.js b/product-of-array-except-self/Zioq.js new file mode 100644 index 000000000..ccb304bbd --- /dev/null +++ b/product-of-array-except-self/Zioq.js @@ -0,0 +1,36 @@ +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function(nums) { + let result = Array.from({length: nums.length}, () => 1) // Initialize return array + + // Iterate left to right + let left = 1; + for( let i =0 ; i=0; i-- ) { + result[i] *= right; + right *= nums[i]; + } + + // console.log(result) + return result +}; + +/* + Time Complexity: O(n): Loop the nth nums array length + Space Complexity: O(1) +*/ + + + +console.log(productExceptSelf([1,2,3,4])) +console.log(productExceptSelf([-1,1,0,-3,3])) + + From 8b91fc33e5d7170b5e07d0ec8ec7e9153fd55cb1 Mon Sep 17 00:00:00 2001 From: Jaehyeon Robert Han Date: Thu, 26 Dec 2024 15:26:28 -0800 Subject: [PATCH 4/4] combination-sum solution --- combination-sum/Zioq.js | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 combination-sum/Zioq.js diff --git a/combination-sum/Zioq.js b/combination-sum/Zioq.js new file mode 100644 index 000000000..cb019a87f --- /dev/null +++ b/combination-sum/Zioq.js @@ -0,0 +1,44 @@ +/** + * @param {number[]} candidates + * @param {number} target + * @return {number[][]} + */ +var combinationSum = function(candidates, target) { + let result = []; + + function find_combination(index, target, current) { + if (target === 0) { + result.push([...current]); + return; + } + + for (let i = index; i < candidates.length; i++) { + // Only proceed if current number doesn't exceed target + if (candidates[i] <= target) { + // Include current number in combination + current.push(candidates[i]); + + // Recursive call with: + // - same index i (allowing reuse of same number) + // - reduced target by current number + find_combination(i, target - candidates[i], current); + + // Backtrack: remove the last added number to try other combinations + current.pop(); + } + } + } + + find_combination(0, target, []); + return result; +}; + +/* + + + +*/ + +console.log(combinationSum([2,3,6,7], 7)) + +