Skip to content

Commit 2a997a3

Browse files
authored
Merge pull request #2426 from grapefruit13/main
[grapefruit13] WEEK 02 solutions
2 parents a3c63bf + c231618 commit 2a997a3

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @description 1์นธ ๋˜๋Š” 2์นธ์”ฉ ์˜ฌ๋ผ๊ฐˆ ์ˆ˜ ์žˆ์„ ๋•Œ n๋ฒˆ์งธ ๊ณ„๋‹จ์— ๋„๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜
3+
* ์ ํ™”์‹: ways(n) = ways(n-1) + ways(n-2)
4+
* @param {number} n - ๊ณ„๋‹จ์˜ ์ด ๊ฐœ์ˆ˜
5+
* @returns {number} n๋ฒˆ์งธ ๊ณ„๋‹จ๊นŒ์ง€ ์˜ฌ๋ผ๊ฐ€๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์˜ ์ˆ˜
6+
*/
7+
function climbStairs(n: number): number {
8+
// base case
9+
// n์ด 1์ด๋ฉด 1๊ฐ€์ง€
10+
// n์ด 2์ด๋ฉด (1+1, 2) ์ด 2๊ฐ€์ง€
11+
if (n <= 2) return n;
12+
13+
// a = ways(n-2)
14+
// b = ways(n-1)
15+
let a = 1;
16+
let b = 2;
17+
18+
// 3๋ฒˆ์งธ ๊ณ„๋‹จ๋ถ€ํ„ฐ n๋ฒˆ์งธ ๊ณ„๋‹จ๊นŒ์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐ
19+
for (let i = 3; i <= n; i++) {
20+
// ํ˜„์žฌ ๊ณ„๋‹จ(i)์— ๋„๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜
21+
// = ๋ฐ”๋กœ ์ „ ๊ณ„๋‹จ(i-1) + ๋‘ ์นธ ์ „ ๊ณ„๋‹จ(i-2)
22+
const c = a + b;
23+
24+
// ๋‹ค์Œ ๊ณ„์‚ฐ์„ ์œ„ํ•ด ๊ฐ’ ์ด๋™
25+
// a โ† ์ด์ „ ways(i-1)
26+
// b โ† ํ˜„์žฌ ways(i)
27+
a = b;
28+
b = c;
29+
}
30+
31+
return b;
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @description Return an array where each element is the product of all numbers in nums excep nums
3+
* @param {number[]} nums
4+
* @returns {number[]}
5+
*/
6+
function productExceptSelf(nums: number[]): number[] {
7+
// result array (stores prefix products first)
8+
const answer = new Array(nums.length);
9+
10+
// prefix product: product of all elements to the left of i
11+
answer[0] = 1;
12+
for (let i = 1; i < nums.length; i++) {
13+
// multiply previous prefix with the previous number
14+
answer[i] = answer[i - 1] * nums[i - 1];
15+
}
16+
17+
// suffix product: product of all elements to the right of i
18+
let rightProduct = 1;
19+
for (let i = nums.length - 1; i >= 0; i--) {
20+
// multiply prefix product and suffix product
21+
answer[i] *= rightProduct;
22+
// update suffix product for next iteration
23+
rightProduct *= nums[i];
24+
}
25+
26+
return answer;
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @description ๋ฌธ์ž์˜ ์ข…๋ฅ˜์™€ ๊ฐœ์ˆ˜๊ฐ€ ์™„์ „ํžˆ ๊ฐ™์€ ์• ๋„ˆ๊ทธ๋žจ์ธ์ง€ ํŒ๋ณ„
3+
* @param {string} s ๋ฌธ์ž์—ด 1
4+
* @param {string} t ๋ฌธ์ž์—ด 2
5+
* @returns {boolean} ์• ๋„ˆ๊ทธ๋žจ ์—ฌ๋ถ€
6+
*/
7+
function isAnagram(s: string, t: string): boolean {
8+
// ๋ฌธ์ž์—ด๋“ค์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋ฉด ์• ๋„ˆ๊ทธ๋žจ ์•„๋‹˜
9+
if (s.length !== t.length) return false;
10+
11+
// ์˜์–ด ์†Œ๋ฌธ์ž ๊ฐœ์ˆ˜ "a"~"z" = 26๊ฐœ
12+
const LOWER_ALPHABET_COUNT = 26;
13+
// ์•ŒํŒŒ๋ฒณ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด
14+
// index 0 -> 'a'
15+
// index 1 -> 'b'
16+
const count = new Array(LOWER_ALPHABET_COUNT).fill(0);
17+
// a์˜ ์•„์Šคํ‚ค์ฝ”๋“œ
18+
const BASE = "a".charCodeAt(0);
19+
20+
for (let i = 0; i < s.length; i++) {
21+
count[s.charCodeAt(i) - BASE]++;
22+
count[t.charCodeAt(i) - BASE]--;
23+
}
24+
25+
// ๋ชจ๋“  ๋ฌธ์ž์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๊ฐ€ 0์ด๋ฉด
26+
// s์™€ t์˜ ๋ฌธ์ž ์ข…๋ฅ˜์™€ ๊ฐœ์ˆ˜๊ฐ€ ์™„์ „ํžˆ ๋™์ผ
27+
return count.every((value) => value === 0);
28+
}

0 commit comments

Comments
ย (0)