-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaximumPointsYouCanObtainFromCards.java
More file actions
81 lines (63 loc) · 2.79 KB
/
MaximumPointsYouCanObtainFromCards.java
File metadata and controls
81 lines (63 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 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.
// 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.
// Your score is the sum of the points of the cards you have taken.
// Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
// See: https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/
package leetcode.others;
import java.util.HashMap;
import java.util.Map;
public class MaximumPointsYouCanObtainFromCards {
/**
* Sliding window solution - accepted.
*/
public int maxScore(int[] cardPoints, int k) {
int ans = 0;
int[] cSum = new int[cardPoints.length + 1];
for (int i = 1; i < cSum.length; i++)
cSum[i] = cSum[i - 1] + cardPoints[i - 1];
int windowSize = cardPoints.length - k;
int sum = cSum[cSum.length - 1];
for (int i = 0; i <= k; i++)
ans = Math.max(ans, sum - (cSum[i + windowSize] - cSum[i]));
return ans;
}
/**
* Recursion with memorization - still not accepted(TLE)
*/
public int maxScore_memo(int[] cardPoints, int k) {
return score(cardPoints, k, 0, cardPoints.length - 1, new HashMap<>());
}
private int score(int[] points, int k, int l, int r, Map<String, Integer> memo) {
if (k == 0 || l > r)
return 0;
String k1 = (l + 1) + "," + r;
String k2 = l + "," + (r - 1);
if (!memo.containsKey(k1))
memo.put(k1, score(points, k - 1, l + 1, r, memo));
if (!memo.containsKey(k2))
memo.put(k2, score(points, k - 1, l, r - 1, memo));
return Math.max(points[l] + memo.get(k1), points[r] + memo.get(k2));
}
/**
* Clean recursion - not accepted(TLE)
*/
public int maxScore_recur(int[] cardPoints, int k) {
return calc(cardPoints, k, 0, cardPoints.length - 1);
}
private int calc(int[] points, int k, int l, int r) {
if (k == 0 || l > r)
return 0;
return Math.max(points[l] + calc(points, k - 1, l + 1, r),
points[r] + calc(points, k - 1, l, r - 1));
}
public static void main(String[] args) {
MaximumPointsYouCanObtainFromCards sln = new MaximumPointsYouCanObtainFromCards();
System.out.println(sln.maxScore(new int[] { 1, 79, 80, 1, 1, 1, 200, 1 }, 3));
System.out.println(sln.maxScore(new int[] { 1, 1000, 1 }, 1));
System.out.println(sln.maxScore(new int[] { 9, 7, 7, 9, 7, 7, 9 }, 7));
System.out.println(sln.maxScore(new int[] { 2, 2, 2 }, 2));
System.out.println(sln.maxScore(new int[] { 1, 2, 3, 4, 5, 6, 1 }, 3));
}
}