1423. Maximum Points You Can Obtain from Cards Medium
1/**
2 * [1423] Maximum Points You Can Obtain from Cards
3 *
4 * There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array cardPoints.
5 * In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
6 * Your score is the sum of the points of the cards you have taken.
7 * Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
8 *
9 * Example 1:
10 *
11 * Input: cardPoints = [1,2,3,4,5,6,1], k = 3
12 * Output: 12
13 * Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
14 *
15 * Example 2:
16 *
17 * Input: cardPoints = [2,2,2], k = 2
18 * Output: 4
19 * Explanation: Regardless of which two cards you take, your score will always be 4.
20 *
21 * Example 3:
22 *
23 * Input: cardPoints = [9,7,7,9,7,7,9], k = 7
24 * Output: 55
25 * Explanation: You have to take all the cards. Your score is the sum of points of all cards.
26 *
27 *
28 * Constraints:
29 *
30 * 1 <= cardPoints.length <= 10^5
31 * 1 <= cardPoints[i] <= 10^4
32 * 1 <= k <= cardPoints.length
33 *
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/
38// discuss: https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43 pub fn max_score(card_points: Vec<i32>, k: i32) -> i32 {
44 0
45 }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_1423() {
56 }
57}
58
Back
© 2025 bowen.ge All Rights Reserved.