-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #800 from Yjason-K/main
[gomgom22] Week3
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* ์ธ ์์ ํฉ์ด 0์ด ๋๋ ๋ชจ๋ ๊ณ ์ ํ ์กฐํฉ์ ์ฐพ๋ ํจ์ | ||
* | ||
* @param {number[]} nums - ์ ์ ๋ฐฐ์ด | ||
* @returns {number[][]} - ์ธ ์์ ํฉ์ด 0์ด ๋๋ ์กฐํฉ ๋ฐฐ์ด | ||
* | ||
* 1. ์ ๋ ฅ ๋ฐฐ์ด `nums`๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ. | ||
* 2. ์ด์ค ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ์ฌ ๊ฐ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก `ํฌ ํฌ์ธํฐ(two-pointer)`๋ฅผ ์ด์ฉํด ์กฐํฉ์ ํ์. | ||
* 3. ์ค๋ณต ์กฐํฉ์ ๋ฐฉ์งํ๊ธฐ ์ํด `Set`์ ์ฌ์ฉํ์ฌ ๊ฒฐ๊ณผ ์กฐํฉ์ ๋ฌธ์์ด์ ์ ์ฅ. | ||
* 4. ์กฐ๊ฑด์ ๋ง๋ ์กฐํฉ์ `result` ๋ฐฐ์ด์ ์ถ๊ฐํฉ๋๋ค. | ||
* | ||
* ์๊ฐ ๋ณต์ก๋: | ||
* - ์ ๋ ฌ: O(n log n) | ||
* - ์ด์ค ๋ฐ๋ณต๋ฌธ ๋ฐ ํฌ ํฌ์ธํฐ: O(n^2) | ||
* - ์ ์ฒด ์๊ฐ ๋ณต์ก๋: O(n^2) | ||
* | ||
* ๊ณต๊ฐ ๋ณต์ก๋: | ||
* - `Set` ๋ฐ `result` ๋ฐฐ์ด์ ์ ์ฅ๋๋ ๊ณ ์ ์กฐํฉ: O(k), k๋ ๊ณ ์ ํ ์กฐํฉ์ ์ | ||
* - ์ ์ฒด ๊ณต๊ฐ ๋ณต์ก๋: O(n + k) | ||
*/ | ||
function threeSum(nums: number[]): number[][] { | ||
const sumSet = new Set<string>(); | ||
const result: number[][] = []; | ||
nums.sort((a, b) => a - b); | ||
|
||
// ์ฒซ ๋ฒ์งธ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฐ๋ณต๋ฌธ ์ํ | ||
for (let i = 0; i < nums.length - 2; i++) { | ||
// ์ ๋ ฌ ๋ ์ํ์ด๊ธฐ ๋๋ฌธ์ ์์์ ์ ๊ธฐ์ค์ผ๋ก ๋ค์ ๊ฐ ์ค๋ณต ๋น๊ต | ||
if (i > 0 && nums[i] === nums[i - 1]) continue; | ||
|
||
let start = i + 1, end = nums.length - 1; | ||
while (start < end) { | ||
const sum = nums[i] + nums[start] + nums[end]; | ||
if (sum > 0) { | ||
end--; | ||
} else if (sum < 0) { | ||
start++; | ||
} else { | ||
const triplet = [nums[i], nums[start], nums[end]]; | ||
const key = triplet.toString(); | ||
if (!sumSet.has(key)) { | ||
sumSet.add(key); | ||
result.push(triplet); | ||
} | ||
start++; | ||
end--; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* ์ฃผ์ด์ง ๋ฐฐ์ด์์ ์์ ์ ์ธ๋ฑ์ค๋ฅผ ์ ์ธํ ๋๋จธ์ง ์์๋ค์ ๊ณฑ์ ๊ณ์ฐํ๋ ํจ์ | ||
* | ||
* @param {number[]} nums - ์ ์ ๋ฐฐ์ด | ||
* @returns {number[]} - ๊ฐ ์ธ๋ฑ์ค์ ์์๋ฅผ ์ ์ธํ ๋๋จธ์ง ์์๋ค์ ๊ณฑ์ ๊ตฌํ ๋ฐฐ์ด | ||
* | ||
* 1. ๊ฒฐ๊ณผ ๋ฐฐ์ด `result`๋ฅผ 1๋ก ์ด๊ธฐํ. | ||
* 2. ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก ์ํํ๋ฉฐ `left` ๊ฐ์ ์ด์ฉํด ๊ธฐ์ค idx ์ด์ ์ ๊ฐ์ ๊ณ์ฐํ์ฌ `result`์ ์ ์ฅ. | ||
* 3. ์ค๋ฅธ์ชฝ์์ ์ผ์ชฝ์ผ๋ก ์ํํ๋ฉฐ `right` ๊ฐ์ ์ด์ฉํด ์ ๋ฏธ ๊ธฐ์ค idx ์ดํ์ ๊ฐ๋ค์ ๊ณ์ฐ ํ์ผ `result`์ ๊ณฑํจ. | ||
* 4. ๊ฒฐ๊ณผ ๋ฐฐ์ด `result`๋ฅผ ๋ฐํ. | ||
* | ||
* ์๊ฐ ๋ณต์ก๋: | ||
* - ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ ์ํ: O(n) | ||
* - ์ค๋ฅธ์ชฝ์์ ์ผ์ชฝ ์ํ: O(n) | ||
* - ์ ์ฒด ์๊ฐ ๋ณต์ก๋: O(n) | ||
* | ||
* ๊ณต๊ฐ ๋ณต์ก๋: | ||
* - ์ถ๊ฐ ๋ฐฐ์ด ์์ด ์์ ๊ณต๊ฐ ์ฌ์ฉ (result๋ ๋ฌธ์ ์ ์๊ตฌ ์กฐ๊ฑด์ ํฌํจ๋์ง ์์). | ||
* - ์ ์ฒด ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
*/ | ||
function productExceptSelf(nums: number[]): number[] { | ||
const numLength = nums.length; | ||
const result = new Array(numLength).fill(1); | ||
|
||
let left = 1; | ||
for (let i = 0; i < numLength; i++) { | ||
result[i] *= left; | ||
left *= nums[i]; | ||
} | ||
|
||
let right = 1; | ||
for (let i = numLength; i >= 0; i--) { | ||
result[i] *= right; | ||
right *= nums[i]; | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* ์ฃผ์ด์ง ์ ์๋ฅผ 32๋นํธ๋ก ๋ณํํ๊ณ ๋ฐ์ ์์ผ ๊ทธ๋์ ์ ์๋ฅผ ๋ฐํํ๋ ๋ฌธ์ . | ||
* | ||
* @param {number} n - ์ ์ (32๋นํธ)) | ||
* @returns {number} - 2์ง์ ๋ณํ ๋ฐ ๋ฐ์ ํ์ฌ ์ ์ ๋ณํ. | ||
* | ||
* ๋ด์ฅ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ 32๋นํธ 2์ง์ ๋ณํ ํ, reverseํ์ฌ ๋ค์ ์ ์๋ก ๋ณํ. | ||
* | ||
* ์๊ฐ ๋ณต์ก๋: O(32) | ||
* - 32๋นํธ ์ ์์ ๋นํธ๋ฅผ ์ฒ๋ฆฌํ๋ฏ๋ก ๊ณ ์ ๋ ์์ ์๊ฐ. | ||
* | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(32) | ||
* - 2์ง์ ๋ฌธ์์ด์ ์์ฑํ๊ณ ๋ฐ์ ๋ ๋ฌธ์์ด์ ์ ์ฅํ๋ฏ๋ก ๊ณ ์ ๋ ํฌ๊ธฐ์ ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ๊ฐ ํ์. | ||
*/ | ||
function reverseBits(n: number): number { | ||
// ์ซ์๋ฅผ 32๋นํธ 2์ง์ ๋ฌธ์์ด๋ก ๋ณํ (์์ 0 ์ฑ์ฐ๊ธฐ) | ||
const binaryStr = n.toString(2).padStart(32, '0'); | ||
// 2์ง์ ๋ฌธ์์ด์ ๋ค์ง๊ธฐ | ||
const reversedBinaryStr = binaryStr.split('').reverse().join(''); | ||
// ๋ค์งํ 2์ง์ ๋ฌธ์์ด์ ๋ค์ ์ซ์๋ก ๋ณํ | ||
return parseInt(reversedBinaryStr, 2); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* ์ฃผ์ด์ง ๋ฐฐ์ด์์ ๋ ์ซ์์ ํฉ์ด target์ด ๋๋ idx ์์ ๋ฐํํ๋ ํจ์ | ||
* - ์๊ฐ ๋ณต์ก๋: O(n) | ||
* - ํ๋ฒ์ ๋ฐฐ์ด์ ์ํํ๋ฉฐ Map์ ๊ฐ์ ์ ์ฅํ๊ณ , Map์์ ๊ฐ์ ์ฐพ์ | ||
* - ๊ณต๊ฐ ๋ณต์ก๋: O(n) | ||
* - ์ซ์์ ๊ทธ๋์ idx๋ฅผ ์์ผ๋กํ๋ Map | ||
* | ||
* @param {number[]} nums - ์ซ์ ๋ฐฐ์ด | ||
* @param {number} target - ํ๊ฒ ํฉ | ||
* @returns {number[]} - ํฉ์ ๋ง๋ค ์ ์๋ idx ๋ฐฐ์ด | ||
*/ | ||
function twoSum(nums: number[], target: number): number[] { | ||
const map = new Map<number, number>(); | ||
for (let i = 0; i < nums.length; i++) { | ||
const cur = nums[i]; // ํ์ฌ ๊ฐ | ||
const reamin = target - cur; // ๋๋จธ์ง | ||
if (map.has(reamin)) { | ||
// Non-null assertion operator(!)๋ฅผ ์ฌ์ฉํ์ฌ undefined๊ฐ ์๋์ ๋จ์ธ | ||
return [map.get(reamin)!, i]; | ||
} | ||
// ๋๋จธ์ง๋ฅผ ์ฐพ์ง ๋ชปํ ๊ฒฝ์ฐ ํ์ฌ ๊ฐ์ ์ ์ฅ | ||
map.set(cur, i); | ||
} | ||
return []; // ๋ชฉํ ํฉ์ ๋ง๋ค ์ ์๋ ์ซ์๊ฐ ์๋ ๊ฒฝ์ฐ ๋น ๋ฐฐ์ด ๋ฐํ | ||
} |