Skip to content

Commit

Permalink
maximum-value-of-k-coins-from-piles
Browse files Browse the repository at this point in the history
  • Loading branch information
aolenevme authored Apr 15, 2023
1 parent 4053861 commit cff5ec5
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
64 changes: 64 additions & 0 deletions array/maximum-value-of-k-coins-from-piles.js
Original file line number Diff line number Diff line change
@@ -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);
};
47 changes: 47 additions & 0 deletions string/palindrome-number.js
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit cff5ec5

Please sign in to comment.