Skip to content
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

Merged
merged 4 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions combination-sum/Zioq.js
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;
};

/*



*/
Comment on lines +36 to +40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

복잡도 분석을 위한 자리만 만들어두시고 기입을 누락하신 것 같습니다.


console.log(combinationSum([2,3,6,7], 7))


36 changes: 36 additions & 0 deletions product-of-array-except-self/Zioq.js
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]))


19 changes: 19 additions & 0 deletions reverse-bits/Zioq.js
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 js를 잘 몰라서 그러는데, >>>를 하고 안 하고 차이가 뭔가요?

Copy link
Contributor Author

@Zioq Zioq Dec 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 이번 과제는 이해가 잘 되지 않아서 스터디 목적으로 제출한 코드였습니다 :)

>>> 는 JS에서 이런 목적으로 쓰이는 오퍼레이션 이라고 하네요

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:
It treats the number as an unsigned 32-bit integer.
The result is always non-negative, even if the original number was negative.

};
/*
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.
*/



30 changes: 30 additions & 0 deletions two-sum/Zioq.js
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) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 보통 in을 많이 썼는데, 실무에서는 이 메소드를 더 많이 쓰나요? ㅎㅎㅎ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

종종 프로퍼티만을 검사하고 싶을 때 실무에서도 자주 쓰는 메소드 입니다 :) 저도 여러번 사용하기두 하구요 ㅎㅎ

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@obzva @Zioq 제가 예전에 자바스크립트 객체에 특정 속성이 있는지 확인하는 방법을 정리한 포스팅이 있어서 추천드립니다.

TLDR:

객체 자신의 속성 뿐만 아니라 상위 클래스로 부터 상속받은 속성까지 고려하고 싶다면 in 연산자를 사용해야합니다. 상위 클래스로 부터 상속받은 속성을 제외하고 객체 자신이 특정 속성을 가지고 있는지 알고 싶다면 Object.hasOwn() 메서드를 사용하면 됩니다. Object.prototype.hasOwnProperty() 메서드는 몇 가지 문제점이 있어서 사용하는 것을 피해야합니다.

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));
Loading