2
* @param {number[]} cardPoints7
// Obtaining k cards from the beginning or end of the row for the largest sum, meaning leaving the8
// array with n-k adjacent cards with the min sum9
// therefore, this transform the problem to finding the minSum of subarray of length n-k10
// we use slide window to calculate the minSubArraySum11
var maxScore = function (cardPoints, k) {12
const n = cardPoints.length,13
d = n - k; // d is the window length15
for (let i = 0; i < d; i++) {18
let minWindowSum = sum,21
for (let i = d; i < n; i++) {22
// the sum of the next window will the the sum of previous window + the next card (the end card of the next window) - the beginning card of the previous window23
sum += cardPoints[i] - cardPoints[i - d];24
minWindowSum = Math.min(minWindowSum, sum);25
totalSum += cardPoints[i];27
// the ans will be the sum of all cards - the sum of min subArray28
return totalSum - minWindowSum;