Algorithm - LeetCode Problems 23

이소라·2024년 2월 19일
0

Algorithm

목록 보기
77/77
post-custom-banner

Problem 2928. Distribute Candies Among Children I

  • You are given two positive integers n and limit.

  • Return the total number of ways to distribute n candies among 3 children such that no child gets more than limit candies.

Examples

  • Example 1:

    • Input: n = 5, limit = 2
    • Output: 3
    • Explanation: There are 3 ways to distribute 5 candies such that no child gets more than 2 candies: (1, 2, 2), (2, 1, 2) and (2, 2, 1).
  • Example 2:

    • Input: n = 3, limit = 3
    • Output: 10
    • Explanation: There are 10 ways to distribute 3 candies such that no child gets more than 3 candies: (0, 0, 3), (0, 1, 2), (0, 2, 1), (0, 3, 0), (1, 0, 2), (1, 1, 1), (1, 2, 0), (2, 0, 1), (2, 1, 0) and (3, 0, 0).

Constraints

  • 1 <= n <= 50
  • 1 <= limit <= 50

Solution

/**
 * @param {number} n
 * @param {number} limit
 * @return {number}
 */
var distributeCandies = function(n, limit) {
    let ways = 0;
    
    for (let i = 0; i <= limit; i++) {
        for (let j = 0; j <= limit; j++) {
            const k = n - i - j;
            if (k >= 0 && k <= limit) {
                ways++;
            }
        }
    }

    return ways;
};



Problem 1641. Count Sorted Vowel Strings

Given an integer n, return the number of strings of length n that consist only of vowels (a, e, i, o, u) and are lexicographically sorted.

A string s is lexicographically sorted if for all valid i, s[i] is the same as or comes before s[i+1] in the alphabet.

Examples

  • Example 1:
    • Input: n = 1
    • Output: 5
    • Explanation: The 5 sorted strings that consist of vowels only are ["a","e","i","o","u"].
  • Example 2:
    • 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.

Constraints

  • 1 <= n <= 50

Solution

/**
 * @param {number} n
 * @return {number}
 */
var countVowelStrings = function(n) {
    const vowels = ['a', 'e', 'i', 'o', 'u'];
    const answer = [];

    const getCombination = (startIndex, rest) => {
        if (rest.length === n) {
            answer.push(rest.join(''));
            return;
        }

        for (let i = startIndex; i < vowels.length; i++) {
            rest.push(vowels[i]);
            getCombination(i, rest);
            rest.pop();
        }
    };

    getCombination(0, []);
    return answer.length;
    
};



Problem 2221. Find Triangular Sum of an Array

  • You are given a 0-indexed integer array nums, where nums[i] is a digit between 0 and 9 (inclusive).

  • The triangular sum of nums is the value of the only element present in nums after the following process terminates:

    1. Let nums comprise of n elements. If n == 1, end the process. Otherwise, create a new 0-indexed integer array newNums of length n - 1.
    2. For each index i, where 0 <= i < n - 1, assign the value of newNums[i] as (nums[i] + nums[i+1]) % 10, where % denotes modulo operator.
    3. Replace the array nums with newNums.
    4. Repeat the entire process starting from step 1
  • Return the triangular sum of nums.

Examples

  • Example 1:

    • Input: nums = [1,2,3,4,5]
    • Output: 8
    • Explanation:
      • The above diagram depicts the process from which we obtain the triangular sum of the array.
  • Example 2:

    • Input: nums = [5]
    • Output: 5
    • Explanation:
      • Since there is only one element in nums, the triangular sum is the value of that element itself.

Constraints

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 9

Solution

/**
 * @param {number[]} nums
 * @return {number}
 */
var triangularSum = function (nums) {
  const getTriangularSums = array => {
    if (array.length === 1) {
      return array[0];
    }
    const newNums = [];

    for (let i = 0; i < array.length - 1; i++) {
      newNums[i] = (array[i] + array[i + 1]) % 10;
    }

    return getTriangularSums(newNums);
  };

  return getTriangularSums(nums);
};
post-custom-banner

0개의 댓글