-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Zioq] Week3 #772
[Zioq] Week3 #772
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<nums.length; i++ ) { | ||
result[i] *= left; | ||
left *= nums[i]; | ||
} | ||
|
||
// Iterate right to left based on the result arr above | ||
let right = 1; | ||
for( let i=nums.length -1; 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])) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제가 js를 잘 몰라서 그러는데, >>>를 하고 안 하고 차이가 뭔가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 이번 과제는 이해가 잘 되지 않아서 스터디 목적으로 제출한 코드였습니다 :)
The >>> operator in JavaScript is the unsigned right shift operator. It shifts the bits of a number to the right by a specified number of positions and fills the leftmost bits with zeros, regardless of the sign of the original number. Key Points: |
||
}; | ||
/* | ||
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. | ||
*/ | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<nums.length++; i++ ) { | ||
let remain = target - nums[i]; // Calculate reamined value to check | ||
|
||
// If remained value found in the object, return current index and object's key | ||
if( remain_with_index_obj.hasOwnProperty(remain) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 보통 in을 많이 썼는데, 실무에서는 이 메소드를 더 많이 쓰나요? ㅎㅎㅎ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 종종 프로퍼티만을 검사하고 싶을 때 실무에서도 자주 쓰는 메소드 입니다 :) 저도 여러번 사용하기두 하구요 ㅎㅎ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @obzva @Zioq 제가 예전에 자바스크립트 객체에 특정 속성이 있는지 확인하는 방법을 정리한 포스팅이 있어서 추천드립니다. TLDR:
|
||
return [i, remain_with_index_obj[remain]] | ||
} | ||
|
||
// Save the valu as key | ||
remain_with_index_obj[nums[i]] = i; | ||
} | ||
return null | ||
}; | ||
|
||
/* | ||
Time Complexity: O(n) | ||
Space Complexity: O(n) | ||
*/ | ||
|
||
console.log(twoSum([2,7,11,15], 9)); | ||
console.log(twoSum([3,2,4], 6)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
복잡도 분석을 위한 자리만 만들어두시고 기입을 누락하신 것 같습니다.