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

[jj7779607] Week3 #777

Merged
merged 3 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
27 changes: 27 additions & 0 deletions product-of-array-except-self/limlimjo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function (nums) {
const result = new Array(nums.length).fill(1); // 결과값

// 왼쪽 곱 계산
let leftProduct = 1;
for (let i = 0; i < nums.length; i++) {
result[i] = leftProduct;
leftProduct *= nums[i];
}

// 오른쪽 곱 계산해서 왼쪽 곱 계산한거 곱해주기
let rightProduct = 1;
for (let i = nums.length - 1; i >= 0; i--) {
result[i] *= rightProduct;
rightProduct *= nums[i];
}

return result;
};

// 조건에 O(n)이라고 있음
// 시간 복잡도: O(n)
// 공간 복잡도: O(n)
29 changes: 29 additions & 0 deletions reverse-bits/limlimjo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @param {number} n - a positive integer
* @return {number} - a positive integer
*/
var reverseBits = function (n) {
// 문자열로 변환
let nString = n.toString(2).padStart(32, "0");
//console.log(nString);

// 스택 생성 (스택은 나중에 들어온게 먼저 나가므로)
let stack = [];

// nString 스택에 넣기
for (let i = 0; i < nString.length; i++) {
stack.push(nString[i]);
}
Comment on lines +7 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let nString = n.toString(2).padStart(32, "0");
//console.log(nString);
// 스택 생성 (스택은 나중에 들어온게 먼저 나가므로)
let stack = [];
// nString 스택에 넣기
for (let i = 0; i < nString.length; i++) {
stack.push(nString[i]);
}
const stack = n.toString(2).padStart(32, "0").split('');

이렇게 하는 건 어떨까요?

Copy link
Contributor Author

@limlimjo limlimjo Dec 26, 2024

Choose a reason for hiding this comment

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

훨씬 간결하네요. 감사합니다!


// pop하여 뒤집힌 문자열 만들기
let reverseNString = "";
for (let i = 0; i < nString.length; i++) {
reverseNString += stack.pop();
}

// 뒤집힌 문자열을 정수로 변환
return parseInt(reverseNString, 2);
};

// 시간 복잡도: O(1)
// 공간 복잡도: O(1)
55 changes: 55 additions & 0 deletions two-sum/limlimjo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
for (let i = 0; i < nums.length; i++) {
for (let j = 0; j < nums.length; j++) {
if (i !== j) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}
}
}
};

// 처음에 풀었던 방법 -> 시간 복잡도가 O(n^2)로 nums 배열에 있는 값이 늘어날수록 성능상 좋지 못함
// 시간 복잡도: O(n^2)
// 공간 복잡도: O(1)

// 두 번째 푼 방법 -> 이전에 threeSum 문제 풀 때 정렬 + 포인터 이용한 것처럼 이 문제도 그런식으로 품
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
const numsIndex = nums.map((num, i) => ({ num, i })); // 원래 인덱스 저장
//console.log(numsIndex);

numsIndex.sort((a, b) => a.num - b.num); // 오름차순 정렬
//console.log(numsIndex);

// left와 right 포인터 이용해 target값과 동일한 것 찾기
let left = 0;
let right = numsIndex.length - 1;

while (left < right) {
const sum = numsIndex[left].num + numsIndex[right].num;

if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
return [numsIndex[left].i, numsIndex[right].i];
}
}
return null;
};

// 첫 번째 푼 방법보다 공간 복잡도가 늘어났지만 시간 복잡도는 줄어듦
Copy link
Contributor

Choose a reason for hiding this comment

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

👍👍

// 시간 복잡도: O(n log n)
// 공간 복잡도: O(n)
Loading