A string is a valid parentheses string (denoted VPS) if it meets one of the following:
""
, or a single character not equal to "("
or ")"
,AB
(A
concatenated with B
), where A
and B
are VPS's, or(A)
, where A
is a VPS.We can similarly define the nesting depth depth(S)
of any VPS S
as follows:
depth("") = 0
depth(C) = 0
, where C
is a string with a single character not equal to "("
or ")"
.depth(A + B) = max(depth(A), depth(B))
, where A
and B
are VPS's.depth("(" + A + ")") = 1 + depth(A)
, where A
is a VPS.For example, ""
, "()()"
, and "()(()())"
are VPS's (with nesting depths 0, 1, and 2), and ")("
and "(()"
are not VPS's.
Given a VPS represented as string s
, return the nesting depth of s
.
Example 1:
Example 2:
1 <= s.length <= 100
s
consists of digits 0-9
and characters '+'
, '-'
, '*'
, '/'
, '('
, and ')'
.s
is a VPS./**
* @param {string} s
* @return {number}
*/
var maxDepth = function(s) {
const openBracket = '(';
const closeBracket = ')';
let bracketCount = 0;
let maxDepth = 0;
for (const letter of s) {
if (letter === openBracket) {
bracketCount++;
}
if (letter === closeBracket) {
maxDepth = Math.max(maxDepth, bracketCount);
bracketCount--;
}
}
return maxDepth;
};
Given an integer array nums
of unique elements, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
Example 1:
Example 2:
1 <= nums.length <= 10
-10 <= nums[i] <= 10
nums
are unique./**
* @param {number[]} nums
* @return {number[][]}
*/
var subsets = function(nums) {
const dfs = (index, subArr, result, visited) => {
if (subArr.length > nums.length || index > nums.length) {
return;
}
result.push([...subArr]);
for (let i = index; i < nums.length; i++) {
const number = nums[i];
if (!visited[number]) {
subArr.push(number);
visited[number] = true;
dfs(i, subArr, result, visited);
subArr.pop();
visited[number] = false;
}
}
return result;
};
return dfs(0, [], [], []);
};
nums
of 2n
integers, group these integers into n
pairs (a1, b1), (a2, b2), ..., (an, bn)
such that the sum of min(ai, bi)
for all i
is maximized. Return the maximized sum.Example 1:
Example 2:
1 <= n <= 10^4
nums.length == 2 * n
-10^4 <= nums[i] <= 10^4
/**
* @param {number[]} nums
* @return {number}
*/
var arrayPairSum = function(nums) {
nums.sort((a, b) => b - a);
let sum = 0;
for (let i = 0; i < nums.length - 1; i += 2) {
const minNumber = Math.min(nums[i], nums[i+1]);
sum += minNumber;
}
return sum;
};
There are 3n
piles of coins of varying size, you and your friends will take piles of coins as follows:
3
piles of coins (not necessarily consecutive).Given an array of integers piles
where piles[i]
is the number of coins in the ith
pile.
Return the maximum number of coins that you can have.
Example 1:
Example 2:
Example 3:
3 <= piles.length <= 10^5
piles.length % 3 == 0
1 <= piles[i] <= 10^4
/**
* @param {number[]} piles
* @return {number}
*/
var maxCoins = function(piles) {
piles.sort((a, b) => b - a);
const length = piles.length;
const quotient = length / 3;
let sum = 0;
for (let i = 1; i < length - quotient; i += 2) {
sum += piles[i];
}
return sum;
};