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.
Example 1:
Example 2:
1 <= n <= 50
1 <= limit <= 50
/**
* @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;
};
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.
1 <= n <= 50
/**
* @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;
};
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:
nums
comprise of n
elements. If n == 1
, end the process. Otherwise, create a new 0-indexed integer array newNums
of length n - 1
.i
, where 0 <= i < n - 1
, assign the value of newNums[i]
as (nums[i] + nums[i+1]) % 10
, where %
denotes modulo operator.nums
with newNums
.Return the triangular sum of nums.
Example 1:
Example 2:
1 <= nums.length <= 1000
0 <= nums[i] <= 9
/**
* @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);
};