From cff5ec5477807ad6e97b31178a8ae173fc8bb2c8 Mon Sep 17 00:00:00 2001 From: Alexander Olenev Date: Sat, 15 Apr 2023 22:56:18 +0300 Subject: [PATCH] maximum-value-of-k-coins-from-piles --- array/maximum-value-of-k-coins-from-piles.js | 64 ++++++++++++++++++++ string/palindrome-number.js | 47 ++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 array/maximum-value-of-k-coins-from-piles.js create mode 100644 string/palindrome-number.js diff --git a/array/maximum-value-of-k-coins-from-piles.js b/array/maximum-value-of-k-coins-from-piles.js new file mode 100644 index 0000000..ab15141 --- /dev/null +++ b/array/maximum-value-of-k-coins-from-piles.js @@ -0,0 +1,64 @@ +/** +2218. Maximum Value of K Coins From Piles + +There are n piles of coins on a table. Each pile consists of a positive number of coins of assorted denominations. + +In one move, you can choose any coin on top of any pile, remove it, and add it to your wallet. + +Given a list piles, where piles[i] is a list of integers denoting the composition of the ith pile from top to bottom, and a positive integer k, return the maximum total value of coins you can have in your wallet if you choose exactly k coins optimally. + +Example 1: +Input: piles = [[1,100,3],[7,8,9]], k = 2 +Output: 101 +Explanation: +The above diagram shows the different ways we can choose k coins. +The maximum total we can obtain is 101. + +Example 2: +Input: piles = [[100],[100],[100],[100],[100],[100],[1,1,1,1,1,1,700]], k = 7 +Output: 706 +Explanation: +The maximum total can be obtained if we choose all coins from the last pile. + +Constraints: +n == piles.length +1 <= n <= 1000 +1 <= piles[i][j] <= 105 +1 <= k <= sum(piles[i].length) <= 2000 +*/ + +/** + * @param {number[][]} piles + * @param {number} k + * @return {number} + */ +var maxValueOfCoins = function (piles, k) { + const n = piles.length; + const cache = {}; + + const getMaxValue = (i, k) => { + if (i >= n || k <= 0) { + return 0; + } + + const key = i + "," + k; + + if (cache[key]) { + return cache[key]; + } + + let result = getMaxValue(i + 1, k); + + let sum = 0; + + for (let j = 0; j < piles[i].length && j < k; j++) { + sum += piles[i][j]; + + result = Math.max(result, sum + getMaxValue(i + 1, k - j - 1)); + } + + return (cache[key] = result); + }; + + return getMaxValue(0, k); +}; diff --git a/string/palindrome-number.js b/string/palindrome-number.js new file mode 100644 index 0000000..568a333 --- /dev/null +++ b/string/palindrome-number.js @@ -0,0 +1,47 @@ +/** +9. Palindrome Number + +Given an integer x, return true if x is a +palindrome, and false otherwise. + +Example 1: +Input: x = 121 +Output: true +Explanation: 121 reads as 121 from left to right and from right to left. + +Example 2: +Input: x = -121 +Output: false +Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. + +Example 3: +Input: x = 10 +Output: false +Explanation: Reads 01 from right to left. Therefore it is not a palindrome. + +Constraints: +-231 <= x <= 231 - 1 + +Follow up: Could you solve it without converting the integer to a string? +*/ + +/** + * @param {number} x + * @return {boolean} + */ +var isPalindrome = function (x) { + const str = `${x}`; + let left = 0; + let right = str.length - 1; + + while (left < right) { + if (str[left] !== str[right]) { + return false; + } + + left++; + right--; + } + + return true; +};