[LeetCode] 1641.count-sorted-vowel-strings

gomanboยท2023๋…„ 8์›” 20์ผ
0

โญโญ ๋ฌธ์ œ

ttps://leetcode.com/problems/count-sorted-vowel-strings/ ๐Ÿ’ฃ


๐Ÿ“Œ ๋ฌธ์ œ ์„ค๋ช…

  • ์ฃผ์–ด์ง„ ์ •์ˆ˜ n์— ๋Œ€ํ•ด, ๊ธธ์ด๊ฐ€ n์ด๊ณ  ๋ชจ์Œ ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ
  • Input: n = 1
    Output: 5
    Explanation: The 5 sorted strings that consist of vowels only are ["a","e","i","o","u"]
  • Input: n = 2
    Output: 15
    Explanation: The 15 sorted strings that consist of vowels only are
    ["aa","ae","ai","ao","au","ee","ei","eo","eu","ii","io","iu","oo","ou","uu"].
    Note that "ea" is not a valid string since 'e' comes after 'a' in the alphabet.

๐Ÿ“Œ ๋ฌธ์ œ ํ’€์ด

dp ์ ํ™”์‹์œผ๋กœ ํ‘ธ๋Š” ๋ฌธ์ œ
1.dp = [1,1,1,1,1,1] ์ดˆ๊ธฐํ™” ํ•ด์ค€๋‹ค. (n=1)
2. i : n = 2๋ถ€ํ„ฐ n์˜ ๊ธธ์ด ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ
3. j : j = 3๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์ด์ „ ํ•ฉ์„ ๋”ํ•ด์ค€๋‹ค

/**
 * @param {number} n
 * @return {number}
 */
var countVowelStrings = function (n) {
  let dp = [1, 1, 1, 1, 1];

  for (let i = 2; i <= n; i++) {
    for (let j = 3; j >= 0; j--) {
      dp[j] += dp[j + 1];
    }
  }
  return dp.reduce((a, b) => a + b);
};
profile
ใ…Ž.ใ…Ž

0๊ฐœ์˜ ๋Œ“๊ธ€