Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f707631

Browse files
committed
Add solution #1423
1 parent 90426f2 commit f707631

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,296 LeetCode solutions in JavaScript
1+
# 1,297 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1088,6 +1088,7 @@
10881088
1419|[Minimum Number of Frogs Croaking](./solutions/1419-minimum-number-of-frogs-croaking.js)|Medium|
10891089
1420|[Build Array Where You Can Find The Maximum Exactly K Comparisons](./solutions/1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons.js)|Hard|
10901090
1422|[Maximum Score After Splitting a String](./solutions/1422-maximum-score-after-splitting-a-string.js)|Easy|
1091+
1423|[Maximum Points You Can Obtain from Cards](./solutions/1423-maximum-points-you-can-obtain-from-cards.js)|Medium|
10911092
1431|[Kids With the Greatest Number of Candies](./solutions/1431-kids-with-the-greatest-number-of-candies.js)|Easy|
10921093
1436|[Destination City](./solutions/1436-destination-city.js)|Easy|
10931094
1437|[Check If All 1's Are at Least Length K Places Away](./solutions/1437-check-if-all-1s-are-at-least-length-k-places-away.js)|Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 1423. Maximum Points You Can Obtain from Cards
3+
* https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/
4+
* Difficulty: Medium
5+
*
6+
* There are several cards arranged in a row, and each card has an associated number of
7+
* points. The points are given in the integer array cardPoints.
8+
*
9+
* In one step, you can take one card from the beginning or from the end of the row.
10+
* You have to take exactly k cards.
11+
*
12+
* Your score is the sum of the points of the cards you have taken.
13+
*
14+
* Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
15+
*/
16+
17+
/**
18+
* @param {number[]} cardPoints
19+
* @param {number} k
20+
* @return {number}
21+
*/
22+
var maxScore = function(cardPoints, k) {
23+
const totalLength = cardPoints.length;
24+
const windowSize = totalLength - k;
25+
let windowSum = cardPoints.slice(0, windowSize).reduce((sum, num) => sum + num, 0);
26+
let minWindowSum = windowSum;
27+
const totalSum = cardPoints.reduce((sum, num) => sum + num, 0);
28+
29+
for (let i = windowSize; i < totalLength; i++) {
30+
windowSum = windowSum + cardPoints[i] - cardPoints[i - windowSize];
31+
minWindowSum = Math.min(minWindowSum, windowSum);
32+
}
33+
34+
return totalSum - minWindowSum;
35+
};

0 commit comments

Comments
 (0)