๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/leetcode

LeetCode 230 - Kth Smallest Element in a BST (Medium)

by HandHand 2021. 3. 2.

๋ฌธ์ œ

LeetCode - 230๋ฒˆ

ํ’€์ด ๊ณผ์ •

์ด์ง„ ๊ฒ€์ƒ‰ ํŠธ๋ฆฌ ์—์„œ k๋ฒˆ์งธ ์›์†Œ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์ด์ง„ ๊ฒ€์ƒ‰ ํŠธ๋ฆฌ ๋Š” ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ํ•ด๋‹น ๋…ธ๋“œ์˜ ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ๋ณด๋‹ค ํฌ๊ณ  ์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ๋ณด๋‹ค ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ์ด๋ฅผ ์ค‘์œ„ ์ˆœํšŒ ํ•  ๊ฒฝ์šฐ ํ‚ค ๊ฐ’์ด ์ •๋ ฌ๋œ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ค‘์œ„ ์ˆœํšŒ๋ฅผ ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  k ๋ฒˆ์งธ ๋…ธ๋“œ์— ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ๋ฅผ ๊ธฐ๋กํ•˜์—ฌ ๋‹ต์„ ๊ตฌํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ

/**
 * @param {number[]} nums
 * @return {number}
 */
var rob = function (nums) {
  const memo = new Array(nums.length).fill(-1);

  function dp(i) {
    if (i >= nums.length) return 0;
    if (i === nums.length - 1) return nums[i];

    if (memo[i] !== -1) return memo[i];

    memo[i] = Math.max(dp(i + 1), dp(i + 2) + nums[i]);
    return memo[i];
  }

  return dp(0);
};
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > leetcode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

LeetCode 417 - Pacific Atlantic Water Flow (Medium)  (0) 2021.03.02
LeetCode 17 - Letter Combinations of a Phone Number (Medium)  (0) 2021.03.02
LeetCode 198 - House Robber (Easy)  (0) 2021.03.02
LeetCode 22 - Generate Parentheses (Medium)  (0) 2021.03.02
LeetCode 62 - Unique Paths (Medium)  (0) 2021.03.02

๐Ÿ’ฌ ๋Œ“๊ธ€