File tree Expand file tree Collapse file tree 3 files changed +87
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 3 files changed +87
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You canโt perform that action at this time.
0 commit comments