Repository navigation
Conversation
|
๐ค AI ๋ฆฌ๋ทฐ(์๊ณ ๋ฆฌ์ฆ ํจํด, ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋, ํ์ต ํํฉ) ๊ฒฐ๊ณผ๋ Checks ํญ์์ ๋ณผ ์ ์์ด์. |
Yu-Jaeyoung
left a comment
There was a problem hiding this comment.
์๋ ํ์ธ์? ํ์ TypeScript๋ฅผ ์ข์ํด์ comments ๋จ๊ธฐ๊ฒ ๋์์ต๋๋ค.
์์ง ํ์ด๋ฅผ valid-anagram๊น์ง ๋ฐ์ ์ํ ์ํ๋ผ, ์ด์ ๋ํ comment๋ง ์งง๊ฒ ๋จ๊ฒจ๋ณด์์ต๋๋ค.
๋งค์ฃผ ์ฐพ์์์ ์ด์ฌํ comment ๋จ๊ธฐ๊ณ , ์ ๋ ๊ฐ์ธ์ ์ผ๋ก TS ํ์ด ์งํํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
์ข์ ํ๋ฃจ๋ณด๋ด์ธ์ ๐๐ป
| return true | ||
| } | ||
|
|
||
| // ๊ธฐ์กด ํ์ด/์ ์ถ ๋ด์ญ. |
There was a problem hiding this comment.
ํ์ดํ ํ๋ฆ์ ๋จ๊ฒจ๋๋๊ฒ ์ ๋ง ์ข์๊ฒ ๊ฐ์ต๋๋ค ๐
์ฐธ๊ณ ํ๊ฒ ์ต๋๋ค !!
There was a problem hiding this comment.
AI Reviewer๋ '์ฃผ์ ์์ผ๋ฉด ๊ฐ๋
์ฑ ๋จ์ด์ง๋ ์ง์ฐ๋ผ'๊ณ ํ์ง๋ง,
๊ทธ๋๋ ์ค์ค๋ก ํ์ตํ ๋ถ๋ถ์ ๋ ๋จ๊ธฐ๊ณ ์ถ์ด์ง๋๋ผ๊ณ ์ ใ
ใ
| @@ -0,0 +1,151 @@ | |||
| /* | |||
| ์๊ฐ ๋ณต์ก๋(TC): O(n) | |||
| ๊ณต๊ฐ ๋ณต์ก๋(SC): O(n) | |||
There was a problem hiding this comment.
3ํธ์์ ๊ณต๊ฐ๋ณต์ก๋๋ฅผ O(1)๊น์ง ์ค์ธ๊ฒ์ผ๋ก ํ์ธํ์ต๋๋ค!
์์ ์ด ํ์ํด๋ณด์ ๋๋ค ๐
There was a problem hiding this comment.
ํ๊ธฐ๋ฅผ ํท๊ฐ๋ ธ๋ค์. ๊ณ ์ณค์ต๋๋ค!
| const isAnagram = (s: string, t: string): boolean => { | ||
| if(s.length !== t.length) return false | ||
| const anagramMap = new Map<string, number>() |
There was a problem hiding this comment.
Map์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ๊ณต๊ฐ ๋ณต์ก๋๋ ๋์ง๋ง,
TypeScript ๋ต๊ฒ ์์ฑ๋ ๋ฌธ๋ฒ์ธ๊ฒ ๊ฐ๋ค์ ๐
There was a problem hiding this comment.
๋ง์์, ํ์ ์ด ๋๊ฒ ๊น๊นํ๋๋ผ๊ณ ์..!
| if (s.length !== t.length) return false | ||
|
|
||
| const BASE_CHAR_CODE = "a".charCodeAt(0) | ||
| const countArr = new Array(26).fill(0) |
There was a problem hiding this comment.
new Array<number>(26).fill(0) ์ผ๋ก ์์ฑํด์ ํ์
์ ๋ช
ํํ๊ฒ ํ๋๊ฑด ์ด๋จ๊น์?
์ฌ๋์ด ์์ฑํ๋ค ๋ณด๋ฉด ๋ค์์ ์ค์ ํ ์๋ ์์ ๊ฒ ๊ฐ์ ์๊ฒฌ ๋จ๊ฒจ๋ด
๋๋ค !
There was a problem hiding this comment.
์ค, ํ์คํ ์ด ๋ถ๋ถ any[]๋ก ๋น๋๋ค..!
const countArr: number[]๋ก๋ ๊ฐ๋ฅํ๊ฒ ์ง๋ง, ๋ก Generic์ ์ด์ฉํด์๋ ๊ณ ์น ์ ์์๊ตฐ์.
ํ๋ ๋ฐฐ์๊ฐ๋๋ค!
| return false | ||
| } | ||
|
|
||
| countArr[code - BASE_CHAR_CODE]-- |
There was a problem hiding this comment.
์๋
ํ์ธ์ @novacat25 ๋ :)
์ ๋ s, t ๋ฐฐ์ด์ ๊ฐ๊ฐ ๋ง๋ค์ด ๋ง์ง๋ง์ ๋น๊ตํ๋๋ฐ, ๋ฐฐ์ด ํ๋์์ t๋ฅผ ๋นผ๋ค๊ฐ 0 ์ดํ๊ฐ ๋๋ ์๊ฐ ๋ฐ๋ก false๋ฅผ ๋ฐํํด์ ๋๊น์ง loop๋ฅผ ๋์ง ์์๋ ๋๋ ์ ์ด ์ธ์์ ์
๋๋ค ๐
13์ค์ ๊ธธ์ด ๋น๊ต๊ฐ ์์๋ค๋ฉด ๋ง์ง๋ง์ ์ ๋ถ 0์ธ์ง ํ์ธํ๋ ์ค์ด ํ์ํ์ ํ
๋ฐ, ๊ธธ์ด ๋น๊ต ๋๋ถ์ ๊ทธ ๊ณผ์ ์ ์๋ตํ ์ ์๋ ๊ตฌ์กฐ๋ผ ๊น๋ํ์ต๋๋ค.
ํ์ด์ฌ์ ord(-)๋ก ๋ฌธ์ ์ฝ๋๋ฅผ ๊ตฌํ๋ ๋ถ๋ถ์ด TS์์๋ -.charCodeAt(0)์ ๋์๋๋๊ตฐ์!
์ฌ์ํ์ง๋ง code - BASE_CHAR_CODE๊ฐ 25์ค๊ณผ 31์ค์์ ๋ ๋ฒ ๊ณ์ฐ๋์ด์, idx ๊ฐ์ ๋ณ์๋ก ํ ๋ฒ๋ง ๊ตฌํด ๋๋ฉด ๋ ๊น๋ํ ๊ฒ ๊ฐ์์.
์ ํ์ด์ ๋์ ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค!
There was a problem hiding this comment.
๋์์ด ๋์
จ๋ค๋ ๋คํ์
๋๋ค!
์ด์ชฝ์ ์ฐธ๊ณ ๋ก ์์ ์ ์ญ์ผ๋ก ๋นผ๊ณ , getCodeIndex ๋ถ๋ถ์ ๋ฉ์๋ํ๋ก ๋๋ ์ด์.
Reusability๋ฅผ ์กฐ๊ธ ๋ ์ค์ํ๊ธด ํ์ต๋๋ค.
const BASE_CHAR_CODE = "a".charCodeAt(0)
const getCodeIndex = (code: number) => code - BASE_CHAR_CODEThere was a problem hiding this comment.
์ํ, getCodeIndex๋ก ๋ถ๋ฆฌํด์ ์ ์ญ ํจ์๋ก ๋๋ ์ฌ์ฌ์ฉํ๊ธฐ ๋ ์ฉ์ดํ ๊ฒ ๊ฐ๋ค์!
์ค๋ช
๊ฐ์ฌํฉ๋๋ค ์๊ณ ํ์
จ์ต๋๋ค!
| /* 2ํธ: Memoisation ํ์ฉ(Top-down DP) | ||
| const countRecur = (n: number, memo: number[]) => { | ||
| // Base case | ||
| if (n < 0) | ||
| return 0 | ||
| if (n === 0) | ||
| return 1 | ||
| // If value is memoised | ||
| if (memo[n] !== -1) { | ||
| return memo[n] | ||
| } | ||
| // Memoise the state (dp[n] = dp[n-1] + dp[n-2]) | ||
| memo[n] = countRecur(n - 1, memo) + | ||
| countRecur(n - 2, memo) | ||
| return memo[n] | ||
| } | ||
| const memoisedClimbStairs = (n: number) => { | ||
| const memo = Array(n + 1).fill(-1) | ||
| return countRecur(n, memo) | ||
| } | ||
| */ |
There was a problem hiding this comment.
์ ๋ ํ์ด ํ์คํ ๋ฆฌ๊ฐ ๊ต์ฅํ ๋น์ทํ๋ค์..! ์ฌ๋ด์ด์ง๋ง ์ ๋ 2ํธ์ Top-down DP๋ก ํ์์์ต๋๋ค...
์ ๋ 0๋ถํฐ ์์ํ๋ฉด ๊ณ๋จ ์์ ๋งค์นญ์ด ์ ๋์ด์ 1์นธ, 2์นธ์ ๋ต์ ์ง์ ๋ฃ๊ณ ์์ํ๋๋ฐ,
nova๋๊ป์๋ ๋จ์ ๊ณ๋จ ์๊ฐ 0์ด๋ฉด 1, ์์๋ฉด ์ด๋ฏธ ๋์ฐฉ ์ง์ ์ ๋์ด๊ฐ ๊ฒ์ด๋ผ 0์ผ๋ก ์ฒ๋ฆฌํ์
์, ์ ์ฒ๋ผ 1์นธ, 2์นธ์ ๋ฐ๋ก ์ธํ
ํ ํ์๊ฐ ์๊ฒ ๋ค์!
๊ทธ๋ฆฌ๊ณ ์ ๋ ๋ฉ๋ชจ(์ ํ์ด์์๋ dp์ ๋์)๋ฅผ ๋ชจ๋ 0์ผ๋ก ์ด๊ธฐํํ๋๋ฐ, -1๋ก ์ด๊ธฐํํ๋ฉด ๋ ์์ ํ ๊ฒ ๊ฐ๋ค์ ๐
There was a problem hiding this comment.
๋ฐฐ์ด๋ก์ '๋ฐฉ๋ฌธํ์ง ์์ ๊ฐ'์ด ํ์คํด์ผ ํ๊ธฐ ๋๋ฌธ์, -1์ด ํ์คํ ์ ์ผ ์์ ํ๊ธด ํ์ง์.
๊ทธ๋๋ Bottom-up ๋ฐฉ์์ด๋ Tabulation ๋ฐฉ์ ์ ๋ถ n < 0์ธ ๊ฒฝ์ฐ(์ ํจํ์ง ์์ ์์น) 0์ผ๋ก ์ด๊ธฐํํ๊ธฐ ๋๋ฌธ์,
if(memo[n] !== 0)๋ ์ถฉ๋ถํ ์์ ๋ฒํ ๋ก์ง์ด์ง ์๋ ์ถ์ต๋๋ค.
dp[0] = 1(์๋ฌด ์ด๋ ํ์ง ์๊ณ ์ถ๋ฐ์ ),
dp[1] = 1(์ผ๋จ ํ ์นธ ์ด๋)
๊น์ง๋ ๊ฑฐ์ ๋ค ๊ฐ์ผ๋๊น์.
๊ทธ๋ฆฌ๊ณ ์ ํ์ ํน์ง ์ n-1, n-2 ๊ฐ์ ๋ฏธ๋ฆฌ ๊ณ์ฐํด๋๊ณ
i = 3๋ถํฐ iteration ๋๋ ๋ฐฉ๋ฒ๋ ์๊ณ ๋ฌ๋ ๋งํฌ์๋ ์๊ฐ๋์ด ์๋ค ๋ณด๋, ์์ฃผ ๋ฏธ์ธํ ๋ํ
์ผ์ฐจ ์๋๊น ์ถ๋ค์ ใ
ใ
There was a problem hiding this comment.
๋ง์ ๋ฃ๊ณ ๋ณด๋ ๊ทธ๋ ๋ค์..!
๋ณดํธ์ ์ผ๋ก๋ -1์ ๊ธฐ์ค์ผ๋ก ๋๋ ํธ์ด์ง๋ง, ์ด ๋ฌธ์ ์ฒ๋ผ n < 0์ 0์ผ๋ก ์ฒ๋ฆฌํ๋ ๊ตฌ์กฐ๋ผ๋ฉด 0์ผ๋ก ๋์ด๋ ์ถฉ๋ถํ ์ฑ๋ฆฝํ๊ฒ ๋ค์.
์๊ณ ๋ฌ๋ ํ์ด๋ ๊ฐ์ด ํ์ธํด์ ์ ๊ฒํด๋ด์ผ๊ฒ ์ต๋๋ค
๊ฐ์ฌํฉ๋๋ค~
| for (let i = 2; i <= n; i++) { | ||
| const current = prev1 + prev2 | ||
|
|
||
| prev2 = prev1 | ||
| prev1 = current | ||
| } | ||
|
|
There was a problem hiding this comment.
์ต์ข
์ ์ถํ์ ํ์ด๋ ์ ์ ๊ตฌ์กฐ๋ ๋์ผํ ๊ฒ ๊ฐ์ต๋๋ค
์ ๋ ํ์ด์ฌ์ด๋ค๋ณด๋ first, second = second, first + second์ฒ๋ผ ํํ๋ก ํํํ๋๋ฐ,
๊ถ๊ธํด์ ์ฐพ์๋ณด๋ ts์์๋ ์ผํ๋ง ์ฐ๊ฑฐ๋(์๊ดํธ ์ง์๋ ์ ๋๋ค์!), [prev2, prev1] = [prev1, current]์ฒ๋ผ ๋๊ดํธ๋ก ์จ์ผ ์๋ํ๋ค์..
29~32์ค์ current ์์ด [prev2, prev1] = [prev1, prev1 + prev2] ํ ์ค๋ก ์ค์ผ ์๋ ์์ ๊ฒ ๊ฐ๋ค์:)
There was a problem hiding this comment.
for (let i = 2; i <= n; i++) {
;[prev2, prev1] = [prev1, prev1 + prev2]
}
๋ค, ์ด ๋ถ๋ถ์ ๋ง์๋๋ก current๊ฐ ์์ด์ง๋ ๋งํผ ํ์ค๋ก ์ค์ฌ์ง๋ค์.
Prettier๊ฐ ์ฝ๋๋ฅผ ์ ์ฅํ ๋๋ง๋ค ์ฝ๋ ์คํ์ผ์ ์ก์์คฌ๋๋ฐ, ์์ ;๊ฐ ๋ถ์ ๊ฒ ์ธ์์ ์ด์์ต๋๋ค.
๋ค๋ง, ์ด ๋ถ๋ถ์ ํ๋ต์ด ํ๋ค ๊ฒ ๊ฐ๋ค์..
prev2์๊ฒ prev1 ๊ฐ์ ๋ฎ๊ณ , prev1์ด current๋ฅผ ๋ฐ๋๋ค๋ ๋ช
์์ ์ธ ์๋ฏธ ์ ๋ฌ์ ์์ด์ ์ง๊ธ ๋ฐฉ์์ด ์กฐ๊ธ ๋ ํ์คํ๋ค๊ณ ๋๊ผ๊ฑฐ๋ ์!
๊ทธ๋์ ์ด ๋ถ๋ถ์ ์กฐ๊ธ ๋ ๊ณ ๋ฏผ์ด ํ์ํ ๊ฒ ๊ฐ์ต๋๋ค.
๊ทธ๋๋ ๋์์ด ํ๋ ์๊ฒจ์ ๋ฐฐ์๊ฐ๋ค์.
There was a problem hiding this comment.
๋ง์์ ๋ฃ๊ณ ๋ณด๋ ๋ฌด์กฐ๊ฑด ์ค์ ์ค์ด๋ ๊ฒ๋ณด๋ค, ๋ฌด์์ ๋ฐ๋์ง ๋ช ์์ ์ผ๋ก ๋๋ฌ๋๋ ๊ฒ์ด ๋ ์ข์ ๋๋ ์๋ ๊ฒ ๊ฐ์ต๋๋ค..! ์ด๋ฒ ์ฝ๋๋ฆฌ๋ทฐ๋ฅผ ํตํด ๋ง์ ๊ฒ์ ๋ฐฐ์๊ฐ๋ค์:)
| if (i === START_IDX) { | ||
| resultArr[i] = 1 | ||
| continue | ||
| } |
There was a problem hiding this comment.
์ ๋ ์ผ์ชฝ ๋์ ๊ณฑ์ i = 0์ ์ ์ธํ๊ณ i = 1๋ถํฐ ์์ํ๊ณ , ์ค๋ฅธ์ชฝ์ ๋ง์ง๋ง ์นธ์๋ 1์ ๊ณฑํ๋ ๋ฐฉ์์ผ๋ก ํ๋๋ฐ, ๊ณฑํ ์์๊ฐ ์๋ ์ ๋์์ continue๋ก ๊ฑด๋๋ฐ์ จ๋ค์!
There was a problem hiding this comment.
์, ์ด ๋ถ๋ถ์ ์์ฑ ๋น์์ ์กฐ๊ธ ๋ ์๋ฏธ๋ฅผ ํ์คํ๊ฒ ํ๊ณ ์ถ์ด์ ์์ธ์ฒ๋ฆฌ๋ฅผ ์ถ๊ฐํ ๊ฑฐ๊ธด ํด์!
๋ง์๋๋ก i = 1๋ถํฐ ์์, ๋ง์ง๋ง ์นธ์๋ 1๋ก ๊ณฑํ๊ธฐ ๋ฑ์ ๋ฐฉ๋ฒ๋ ์ถฉ๋ถํ ์ ํจํ๋ค๊ณ ๋ด
๋๋ค.
๊ทธ๋ฌ๊ณ ๋ณด๋, ์ด๋ฏธ 1๋ก fill์ด ๋์๋๋ฐ ๋ resultArr[0] = 1๋ก ์ด๊ธฐํํ๊ณ ์์๋ค์.
์ด ๋ถ๋ถ ์กฐ๊ธ ๋ ๋ค๋ฌ์ด ๋ณผ ์ฌ์ง๊ฐ ์๊ฒ ์ด์.
There was a problem hiding this comment.
์ ์ ์ฃผ์ ๋ถ๋ถ,์ฌ๋ฌ๋ชจ๋ก ๊ฒํ ํด ๋ณด์์ต๋๋ค๋ง ์ญ์ continue๋ก ๋ถํ์ํ ์์ธ์ฒ๋ฆฌ๊ฐ ๋ค์ด๊ฐ ์๋ ๊ฒ ๊ฐ์ ์์ ๋ฏ์ด๊ณ ์ณค์ต๋๋ค..ใ ใ
const lastIdx = nums.length - 1
for (let i = 1; i <= lastIdx; i++) {
leftProductedVal *= nums[i - 1]
resultArr[i] = leftProductedVal
}
for (let i = lastIdx - 1; i >= 0; i--) {
rightProductedVal *= nums[i + 1]
resultArr[i] *= rightProductedVal
}์ ๊ฐ ์ด ์ฝ๋ ๋ง๊ณ ๋ฐฐ์ด์ ์ฐ๋ ๋ค๋ฅธ ์ฝ๋๋ค๋ ์ญ ํ์ด ๋ณด์๋๋ฐ... START_IDX๋ MAX_INDEX์ฒ๋ผ ๋ฐฐ์ด ์ธ๋ฑ์ค์ ์์ ์ฒ๋ฆฌ๊ฐ ๋ง๋? ๋ ์๋ฌธ์ด ๋ค๋๋ผ๊ณ ์.
๊ด๋ก ์์ผ๋ก ๋ณด์๋ const PI = 3.14 ๊ฐ์ด ๊ฐ์ด ์ผ๊ด์ ์ธ ๊ฒฝ์ฐ๋ผ๋ฉด ๋ชจ๋ฅผ๊น, ๋ฐฐ์ด ์ธ๋ฑ์ค ๊ฐ์ ๊ฒฝ์ฐ์๋ ์ผ๋ฐ์ ์ธ camelCase ๋ณ์๋ก ๋๋ ๊ฒ ๋ ๋์ ๋ณด์ด๋๋ผ๊ณ ์.
๋ฐ๋ผ์, ์์ ๋ถ๋ถ์ 0์ผ๋ก ์ฒ๋ฆฌ, ๋ฐฐ์ด top ๋ถ๋ถ์ lastIdx๋ก ์ค์ ํ์ต๋๋ค!
There was a problem hiding this comment.
์ธ์ด๋ณ๋ก๋ ๋ณ์๋ช
๊ด๋ก๊ฐ ์์ดํ ๊ฒ ๊ฐ๋ค์..
์ ๋ ์ฃผ๋ก lowercase๋ก๋ง ๋ณ์๋ช
์ ์์ฑํ๋๋ฐ ๊ณต๋ถํ๋ฉด ํ ์๋ก ์๋กญ๋ค์ ใ
ใ
continue๋ฅผ ์ ์ธํ๊ณ ๋ฐ๋ณต ๋ฒ์๋ก ์ฒ๋ฆฌํ๋ ๋ ๊น๋ํด์ง ๊ฒ ๊ฐ๋ค์
์ ๋ continue ์ธ ์๊ฐ์ ๋ชปํ์ด์ ๋ฆฌ๋ทฐ๋ฅผ ๋จ๊ธด๊ฑด๋ฐ ๋๋ถ์ ๋ ๋ค๋ฅธ ํ์ด๊ฐ ๋์จ ๊ฒ ๊ฐ์ ๋ฐฐ์๊ฐ๋๋ค๐
| leftProductedVal *= nums[i - 1] | ||
| resultArr[i] = leftProductedVal |
There was a problem hiding this comment.
์ ๋ answer[i - 1]์ nums[i - 1]์ ๊ณฑํด answer[i]์ ์ ์ฅํ๋ ์์ผ๋ก ํ๋๋ฐ, leftProductedVal ๋ณ์์ ๋ฐ๋ก ๋์ ํ๋ ๋ ๋ช
์์ ์ธ ๊ฒ ๊ฐ๋ค์..! ํ์ด๊ฐ ๋น์ทํ๋ฉด์๋ ์ ์๋ฐฉ์์ด ๋ฌ๋ผ ์ฝ๋ ์ฝ๋ ์ฌ๋ฏธ๊ฐ ์๋ค์ ใ
ใ
์๊ณ ํ์
จ์ต๋๋ค!
| while (j < k && nums[j] === nums[j - 1]) j++ | ||
| while (j < k && nums[k] === nums[k + 1]) k-- |
There was a problem hiding this comment.
์ ๋ ์ค๋ณต ์ ๊ฑฐ๋ set์ผ๋ก ์ค์ !์ด๋ผ๋ ๋๋ฆ ์ ๋ง์ ๊ท์น์ ์ ํด๋๊ณ ์ฝ๋๋ฅผ ์ง๋๋ฐ,
res๋ฅผ ์ผ๋ฐ ๋ฐฐ์ด๋ก ๋๊ณ ํ์ ๊ณผ์ ์์ ์ค๋ณต๊ฐ์ ์ ์ธํ๋ ๋ฐฉ๋ฒ๋ ์๊ฐ๋ณต์ก๋๋ ๋น์ทํด์ ๋ฌธ์ ํ์ด์ ํ๋ฆ์ ํ์
ํ๊ธฐ์ ๋ ์ฉ์ดํ ๊ฒ ๊ฐ์ต๋๋ค.
์ ์ ๋ต์ ์ฐพ์ ๋ค left, right๋ฅผ ํ ์นธ์ฉ๋ง ์ด๋ํด์ ์ค๋ณต ์กฐํฉ์ด ๋ฐ์ํ ์ ์๋๋ฐ, ์ด ๋ถ๋ถ์์ ๊ฐ์ ๊ฐ์ ๊ฑด๋๋ฐ๋ฉด ๊ตณ์ด set์ ์ฐ์ง ์์๋ ๋ ๊ฒ ๊ฐ๋ค์ ๐
There was a problem hiding this comment.
๋ค, ํ์ ์ค์ ์ค๋ณต์ ์์ ์น์๋ฒ๋ฆฌ๋ฉด ๋์ค์ tuple์ด๋ set ๋ฑ์ ์ฒ๋ฆฌ๊ฐ ๋ค์ด๊ฐ์ง ์์๋ ๊ทธ๋งํผ ์ด๋์ด๊ธด ํ๋๊น์!
There was a problem hiding this comment.
์ด๋ฒ์ ๋ฐฑ์ค ์ฌ์คํํ๊น์ https://acmicpc.net/problem/3273
ํฌ ํฌ์ธํฐ ๊ธฐ๋ฒ์ผ๋ก ๋ค์ ํ์ด๋ดค๋๋ฐ, leetcode๋ฌธ์ ๋ณด๋ค๋ ๋ ํฉ์ด๋ผ์ ๋ ๋์ด๋๋ ์ฝ์ง๋ง
ํ์คํ ์ ์ฉ๋ฐฉ๋ฒ์ ์๊ฒ๋์ด์ ์ข๋ค์~!
๊ฐ์ฌํฉ๋๋ค:)
| const BASE_CHAR_CODE = "a".charCodeAt(0) | ||
|
|
||
| const getCodeIndex = (code: number) => code - BASE_CHAR_CODE |
There was a problem hiding this comment.
๐ ์ ๋์ฝ๋์ ์ํ๋ฒณ ์ซ์์ ์๋ก ์ ํํด๋ ํ์ด ๋ฐฉ๋ฒ์ด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
There was a problem hiding this comment.
์ฒ์์๋ ์ ๋ ฌ์ด๋ Map ๋ฑ์ ์๊ฐํ๋๋ฐ,
๋ฌธ์ ์กฐ๊ฑด์ผ๋ก ํํธ๋ฅผ ๋ณด๊ณ ๋์ ์ด๋ฐ ๋ฐฉ๋ฒ์ด ์๋๋ผ๊ณ ์..!
์ ๋ ๊ทธ๋์ ๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ด๋ ๊ฒ ์๋ ์ ์์๊ตฌ๋..ํ๊ณ ๋ฐฐ์ ์ต๋๋ค.
|
|
||
| const dfs = (node: TreeNode | null, low: number, high: number): boolean => { | ||
| if (!node) return true // Null์ธ Node๋ BST ์กฐ๊ฑด์ ๋ง์กฑํ๋ค๊ณ ๊ฐ์ฃผํด ์ฌ๊ท์ ์ผ๋ก True๋ฅผ ๋ฐํํ๋ค. | ||
| if (!(low < node.val && node.val < high)) return false // ํ์ฌ Node์ ๊ฐ์ด ํ์ฉ ๋ฒ์์ ์ํ์ง ์์ผ๋ฉด False๋ฅผ ๋ฐํํ๋ค. | ||
| return dfs(node.left, low, node.val) && dfs(node.right, node.val, high) // ์ผ์ชฝ ์๋ธํธ๋ฆฌ์ ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ๋ฅผ ์ฌ๊ท์ ์ผ๋ก ํ์ธํ๋ฉฐ, ํ์ฉ ๋ฒ์๋ฅผ ๊ฐฑ์ ํ๋ค. | ||
| } |
There was a problem hiding this comment.
ํ๋ฉด์๋ ์ด ๋ฌธ์ ๋ ํ์ด๊ฐ ์ ๋ง ๋ค์ํ ๊ฒ์ด๋ผ๊ณ ์๊ฐ์ด ๋ค์๋๋ฐ์
์ ๋ ์ด ๋ฌธ์ ๊ต์ฅํ ๊ณ ์ ํ๋ฉด์ ํ์์ด์ ๋ค๋ฅธ ๋ถ๋ค ํ์ด๋ฅผ ๋ณด๊ณ ๋ง์ด ๋ฐฐ์๊ฐ๊ณ ์ถ์๋ค์ ใ
ใ
์ ๋ ์ค์์ํ๋ก ํ์๋๋ฐ left--> ์์ ๋
ธ๋--> right์ด๋ ๊ฒ ํด์ ๊ฐ์ด ์ค๋ฆ์ฐจ์์ด ๋์ค๋์ง๋ฅผ ์ฝ๋๋ก ๊ตฌํํ์๋๋ฐ
low, high๋ก ๋ฒ์๋ฅผ ๋๊ณ ๋
ธ๋๊ฐ์ ํฌํจ๋ฒ์๋ฅผ ์ขํ ๋๊ฐ๋ ๋ฐฉ์๋ ๊ต์ฅํ ๊น๋ํ๋ค์!
There was a problem hiding this comment.
๋ค, left->Parent-> child, Parent์์ left-child ์ ๊ฐ ๋ฑ ๊ฐ์ ํ์ด ๋ฐฉ์์ด ์๋ ๊ฒ ๊ฐ๋๋ผ๊ณ ์..!
์ ๋ Parent์์๋ left/right๋ฅผ ์ฌ๊ท์ ์ผ๋ก ๋ถ๋ฌ ํด๊ฒฐํ์ต๋๋ค.
| } | ||
|
|
||
| const dfs = (node: TreeNode | null, low: number, high: number): boolean => { | ||
| if (!node) return true // Null์ธ Node๋ BST ์กฐ๊ฑด์ ๋ง์กฑํ๋ค๊ณ ๊ฐ์ฃผํด ์ฌ๊ท์ ์ผ๋ก True๋ฅผ ๋ฐํํ๋ค. |
There was a problem hiding this comment.
!node๋ก ๋น ๋ ธ๋๋ ๋ฐ๋ก ๊ฑด๋๋ฐ๋๊ฑด์
There was a problem hiding this comment.
๋ค! child๋ก node.left / node.right๋ฅผ ๋ถ๋ฅผ ๋, ์ ์กฐ๊ฑด๋ฌธ์ ์ถฉ์กฑ์์ผ ๊ฒ์ฌ๋ฅผ ์์ ๋น ์ง๋๋ค.
|
|
||
| const dfs = (node: TreeNode | null, low: number, high: number): boolean => { | ||
| if (!node) return true // Null์ธ Node๋ BST ์กฐ๊ฑด์ ๋ง์กฑํ๋ค๊ณ ๊ฐ์ฃผํด ์ฌ๊ท์ ์ผ๋ก True๋ฅผ ๋ฐํํ๋ค. | ||
| if (!(low < node.val && node.val < high)) return false // ํ์ฌ Node์ ๊ฐ์ด ํ์ฉ ๋ฒ์์ ์ํ์ง ์์ผ๋ฉด False๋ฅผ ๋ฐํํ๋ค. |
There was a problem hiding this comment.
์ง๋ฌธ์ด ์์ต๋๋ค..!
ํ์ด์ฌ์์๋ low < node.val < high์ฒ๋ผ ๋ถ๋ฑ์์ ํ๋ฒ์ ์ธ ์ ์๋๋ฐ, TS์์๋ ์ด๋ ๊ฒ ์ฐ๋ฉด ์ ๋๋ ๊ฑด๊ฐ์?
There was a problem hiding this comment.
์ค ts์์๋ operator ์ฐ์ฐ์๋ฅผ boolean๊ณผ number ์ฌ์ด์๋ ์ธ ์ ์๋ค๋ ๋ด์ฉ ์๋กญ๊ฒ ์๊ณ ๊ฐ๋๋ค!
๋๋ถ์ด ํ์ด์ฌ์ and ๋์ &&๋ก ์ฐ๋ ํ๊ธฐ๋ฒ๋ ๋ฐฐ์ฐ๊ณ ๊ฐ๋๋ค~
| } | ||
| } | ||
|
|
||
| const dfs = (node: TreeNode | null, low: number, high: number): boolean => { |
There was a problem hiding this comment.
ts์์๋ ๋ฐํํ์
(์ฌ๊ธฐ์๋ boolean)์ ํญ์ ๋ช
์ํด์ค์ผํ๋์ง ๊ถ๊ธํฉ๋๋ค..!
์ด ๋ฌธ์ ์์๋ dfs ํจ์๊ฐ true/false๋ฅผ returnํด์ boolean์ ๋ฐ๋ก ์จ ์ฃผ์ ๊ฑด์ง ๊ถ๊ธํ์ต๋๋ค ๐
There was a problem hiding this comment.
๋ณดํต์ TS์์ return value ๊ฐ์ ๋ณด๊ณ ์ถ๋ก ์ ํด ์ฃผ๊ธฐ ๋๋ฌธ์ ์๋ตํด๋ ๋ฌธ์ ๋ ์์ต๋๋ค.
ํ์
์ถ๋ก ์ด ํ์คํ๊ณ ์ฝ๋๋ก ์์์ฑ ์ ์๋ค๋ฉด ์๋ ๊ฒ ๋์ ์ ์์ผ๋ฉฐ,
(๋์ ์์)
const name = "John"
const age = 20
const names = ["John", "Terry"]
TypeScript๊ฐ ์ผ๋ฐ์ ์ผ๋ก ์ถ๋ก ํ๊ธฐ ํ๋ ๊ฒฝ์ฐ(Generic, API response ๋ฑ)๊ฑฐ๋
const users: User[] = []
const [users, setUsers] = useState<User[]>([])
ํ์ ์ด ์ฝ๋์ ์ค๊ณ ์๋๋ฅผ ๋ํ๋ธ๋ค๋ฉด
type UserStatus = "active" | "inactive" | "pending"
const getUserStatus = (user: User): UserStatus => {
// ๋ณต์กํ ๋น์ฆ๋์ค ๋ก์ง
}
์ด๋ฐ ์์ผ๋ก ๋ช ์ํด์ฃผ๋ ๊ฒ ์ข๋ค๊ณ ํ๋ค์.
ํ์ง๋ง, ์ด๋ฒ dfs ์ฝ๋ ๊ฐ์ ๊ฒฝ์ฐ์๋ ์ฌ๊ทํจ์๊ฐ ๋ถ๋ฌ์ ธ ์๋ ์ด์ ์ธ์ง ์๋ ฅ์ผ๋ก Type Check๊ฐ ๋ถ๊ฐ๋ฅํ ์ํฉ์ด์์ผ๋ฉฐ,
ํ์
๋ช
์๋ฅผ ํตํด ๋ถ๋ช
ํ๊ฒ ํด์ค์ผ ํ์ต๋๋ค.
const dfs = (node: TreeNode | null, low: number, high: number) => {
if (!node) return true // Null์ธ Node๋ BST ์กฐ๊ฑด์ ๋ง์กฑํ๋ค๊ณ ๊ฐ์ฃผํด ์ฌ๊ท์ ์ผ๋ก True๋ฅผ ๋ฐํํ๋ค.
if (!(low < node.val && node.val < high)) return false // ํ์ฌ Node์ ๊ฐ์ด ํ์ฉ ๋ฒ์์ ์ํ์ง ์์ผ๋ฉด False๋ฅผ ๋ฐํํ๋ค.
return dfs(node.left, low, node.val) && dfs(node.right, node.val, high) // ์ผ์ชฝ ์๋ธํธ๋ฆฌ์ ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ๋ฅผ ์ฌ๊ท์ ์ผ๋ก ํ์ธํ๋ฉฐ, ํ์ฉ ๋ฒ์๋ฅผ ๊ฐฑ์ ํ๋ค.
}
'dfs' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
There was a problem hiding this comment.
์์ธํ ์์๊น์ง ๋ค์ด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค
์ถ๋ก ์ด ํ์คํ ๊ฒฝ์ฐ์ ์๋ตํ๋ ๊ฒ ๋ ๊น๋ํ๊ณ , ๋ฐ๋์ ์ผ์ด์ค๋ ํ์
์ด ์ค๊ณ ์๋๋ฅผ ๋๋ฌ๋ผ ๋๋ ๋ช
์ํ๋ ๊ฒ ์ข๊ตฐ์
dfs์ฒ๋ผ ๋ฐํ์์์ ์๊ธฐ ์์ ์ ํธ์ถํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ ์ฌ๊ท ํจ์๋ ๋ฐํ ํ์
์ ๋ช
์ํด ์ค์ผ ํ๋ค๋ ์ ๋ ์๊ณ ๊ฐ๋๋ค!


๋ต์ ์ ์ถ ๋ฌธ์
์์ฑ์ ์ฒดํฌ ๋ฆฌ์คํธ
In Review๋ก ์ค์ ํด์ฃผ์ธ์.๊ฒํ ์ ์ฒดํฌ ๋ฆฌ์คํธ
Important
๋ณธ์ธ ๋ต์ ์ ์ถ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ ๋ถ PR ํ๋ ์ด์์ ๋ฐ๋์ ๊ฒํ ๋ฅผ ํด์ฃผ์ ์ผ ํฉ๋๋ค!