There are several cards arranged in a row, and each has an associated number of points. The points are given in the integer array cardPoints of size N.
In one step, you can take one card from 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 its size N and the integer k, return the maximum score you can obtain.
Example 1:
Input:
N = 7
k = 3
cardPoints[ ] = {1, 2, 3, 4, 5, 6, 1}
Output: 12
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 cards on the right, giving a final score of 1 + 6 + 5 = 12.
 
Example 2:
Input:
N = 5
k = 5
arr[ ] = {8, 6, 2, 4, 5}
Output: 25
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxScore() which takes the array of integers cardPoints , an integer N and k as parameters and returns a maximum score.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ k ≤ N
^{1 }≤ cardPoints_{i  }≤ 10^{5}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Data structures

Amortized analysis

Starter code: #User function Template for python3



class Solution:

    def maxScore(self, cardPoints, N, k):

        # Code here

Solutions:
class Solution:

	def maxScore(self, cardPoints, N, k):
		startpoint = N - 1
		endpoint = k - 1
		rollingsum = sum(cardPoints[0:endpoint + 1])
		maxsum = rollingsum
		for i in range(k):
			rollingsum = rollingsum - cardPoints[endpoint] + cardPoints[startpoint]
			startpoint -= 1
			endpoint -= 1
			maxsum = max(maxsum, rollingsum)
		return maxsum


class Solution:

	def maxScore(self, cardPoints, N, k):
		window = N - k
		if window == 0:
			return sum(cardPoints)
		window_sum = sum(cardPoints[:window - 1])
		total_sum = sum(cardPoints)
		max_sum = 0
		for i in range(window - 1, N):
			window_sum += cardPoints[i]
			max_sum = max(max_sum, total_sum - window_sum)
			window_sum -= cardPoints[i - window + 1]
		return max_sum


class Solution:

	def maxScore(self, cardPoints, n, k):
		par_ans = 0
		for i in range(k):
			par_ans += cardPoints[i]
		ans = par_ans
		i = k - 1
		j = n - 1
		while i >= 0:
			par_ans -= cardPoints[i]
			par_ans += cardPoints[j]
			ans = max(ans, par_ans)
			i -= 1
			j -= 1
		return ans


class Solution:

	def maxScore(self, arr, n, k):
		if k == n:
			return sum(arr)
		left_sum = 0
		right_sum = 0
		for i in range(k):
			left_sum += arr[i]
		ll = 0
		lr = k - 1
		rr = n - 1
		rl = n - k
		count = left_sum
		suma = left_sum
		for i in range(k):
			left_sum -= arr[lr]
			right_sum += arr[rr]
			suma = suma + arr[rr] - arr[lr]
			rr -= 1
			lr -= 1
			count = max(count, suma)
		return count


class Solution:

	def maxScore(self, cardPoints, N, k):
		(i, j, ans) = (0, 0, float('inf'))
		state = 0
		window = N - k
		count = 0
		for j in range(N):
			state += cardPoints[j]
			count += 1
			if count > window:
				state -= cardPoints[i]
				i += 1
				count -= 1
			if count == window:
				ans = min(ans, state)
		return sum(cardPoints) - ans


class Solution:

	def maxScore(self, cardPoints, n, k):
		n = len(cardPoints)
		res = sumi = sum(cardPoints[:k])
		left = k - 1
		right = n - 1
		while left >= 0:
			sumi -= cardPoints[left]
			sumi += cardPoints[right]
			res = max(res, sumi)
			left -= 1
			right -= 1
		return res


class Solution:

	def maxScore(self, cardPoints, N, k):
		(left, right) = (0, len(cardPoints) - k)
		total = sum(cardPoints[right:])
		res = total
		while right < len(cardPoints):
			total += cardPoints[left] - cardPoints[right]
			res = max(total, res)
			left += 1
			right += 1
		return res


class Solution:

	def maxScore(self, cardPoints, N, k):
		n = len(cardPoints)
		(left, right) = (0, n - k)
		total_sum = sum(cardPoints[right:])
		ans_sum = total_sum
		for i in range(k):
			total_sum += cardPoints[left] - cardPoints[right]
			ans_sum = max(ans_sum, total_sum)
			left += 1
			right += 1
		return ans_sum


class Solution:

	def maxScore(self, cardPoints, N, k):
		tot = sum(cardPoints)
		ans = 0
		res = 0
		for i in range(N - k):
			res += cardPoints[i]
		ans = tot - res
		for i in range(N - k, N):
			res += cardPoints[i] - cardPoints[i - (N - k)]
			ans = max(ans, tot - res)
		return ans


class Solution:

	def maxScore(self, cardPoints, N, k):
		sum1 = 0
		ans = 0
		i = 0
		j = N - 1
		sum1 = 0
		while i < k:
			sum1 = sum1 + cardPoints[i]
			ans = max(ans, sum1)
			i = i + 1
		i = i - 1
		while i >= 0 and j >= 0:
			sum1 = sum1 + cardPoints[j] - cardPoints[i]
			ans = max(sum1, ans)
			i = i - 1
			j = j - 1
		return ans


class Solution:

	def maxScore(self, cardPoints, N, k):
		sum = 0
		res = 0
		Sum = []
		Score = 0
		n = len(cardPoints)
		for i in range(n):
			sum += cardPoints[i]
			Sum.append(sum)
		for i in range(k + 1):
			j = i + n - k - 1
			if i == 0:
				res = Sum[j]
			else:
				res = Sum[j] - Sum[i - 1]
			Score = max(Score, sum - res)
		return Score


class Solution:

	def maxScore(self, cardPoints, N, k):
		ans = 0
		for i in range(k):
			ans += cardPoints[i]
		cur = ans
		for i in range(k - 1, -1, -1):
			cur -= cardPoints[i]
			cur += cardPoints[N - k + i]
			ans = max(cur, ans)
		return ans


class Solution:

	def maxScore(self, cardPoints, N, k):
		l = 0
		r = N - k
		total = sum(cardPoints[r:])
		res = total
		while r < N:
			total += cardPoints[l] - cardPoints[r]
			res = max(res, total)
			l += 1
			r += 1
		return res


class Solution:

	def maxScore(self, cardPoints, N, k):
		maxScore = 0
		sumTotal = sum(cardPoints[:k])
		maxScore = max(maxScore, sumTotal)
		for i in range(k):
			sumTotal = sumTotal - cardPoints[k - 1 - i] + cardPoints[N - 1 - i]
			maxScore = max(maxScore, sumTotal)
		return maxScore


class Solution:

	def maxScore(self, a, n, k):
		sumf = sum(a[:k])
		sumb = sum(a[n - k:n])
		bf = 0
		ef = k - 1
		bb = n - 1
		eb = n - k
		ans = 0
		x = 1
		while x <= k:
			if sumf >= sumb:
				ans += a[bf]
				sumf -= a[bf]
				sumb -= a[eb]
				bf += 1
				eb += 1
			elif sumf < sumb:
				ans += a[bb]
				sumf -= a[ef]
				sumb -= a[bb]
				bb -= 1
				ef -= 1
			x += 1
		return ans


class Solution:

	def maxScore(self, cardPoints, N, k):
		(i, j) = (0, len(cardPoints) - k)
		summ = sum(cardPoints[j:])
		res = summ
		while j < len(cardPoints):
			summ = summ - cardPoints[j] + cardPoints[i]
			res = max(res, summ)
			i += 1
			j += 1
		return res


class Solution:

	def maxScore(self, arr, N, k):
		i = 0
		j = N - k
		total = sum(arr[j:])
		ans = total
		while j < N:
			total = total + arr[i] - arr[j]
			ans = max(ans, total)
			i = i + 1
			j = j + 1
		return ans


class Solution:

	def maxScore(self, cardPoints, N, k):
		s = sum(cardPoints)
		remi = N - k
		sub_tot = sum(cardPoints[:remi])
		su = sub_tot
		for i in range(remi, N):
			sub_tot += cardPoints[i]
			sub_tot -= cardPoints[i - remi]
			su = min(su, sub_tot)
		return s - su


class Solution:

	def maxScore(self, cardPoints, N, k):
		summa = 0
		for i in range(N - k):
			summa += cardPoints[i]
		mini = summa
		for i in range(k):
			summa -= cardPoints[i]
			summa += cardPoints[i + N - k]
			mini = min(mini, summa)
		return sum(cardPoints) - mini


class Solution:

	def maxScore(self, cardPoints, N, k):
		arr = cardPoints
		n = len(cardPoints)
		t = sum(cardPoints)
		r = n - k
		sub = sum(arr[:r])
		min_s = sub
		for i in range(r, n):
			sub += arr[i]
			sub -= arr[i - r]
			min_s = min(min_s, sub)
		return t - min_s


class Solution:

	def maxScore(self, cardPoints, N, k):
		cardPoints = cardPoints[N - k:] + cardPoints[:k]
		(i, j) = (0, 0)
		maxi = 0
		sum_ = 0
		count = 0
		N = len(cardPoints)
		while j < N:
			sum_ += cardPoints[j]
			count += 1
			if count < k:
				j += 1
			elif count == k:
				maxi = max(maxi, sum_)
				sum_ -= cardPoints[i]
				i += 1
				j += 1
				count -= 1
		return maxi


class Solution:

	def maxScore(self, cardPoints, N, k):
		cur = sum(cardPoints[:k])
		maxs = cur
		x = len(cardPoints)
		for i in range(k):
			cur = cur + cardPoints[x - 1 - i]
			cur = cur - cardPoints[k - 1 - i]
			maxs = max(maxs, cur)
		return maxs


class Solution:

	def maxScore(self, cardPoints, N, k):
		start_sum = 0
		end_sum = sum(cardPoints[-k:])
		max_sum = end_sum
		for i in range(k):
			start_sum = start_sum + cardPoints[i]
			end_sum = end_sum - cardPoints[N - k + i]
			current_sum = start_sum + end_sum
			max_sum = max(max_sum, current_sum)
		return max_sum


class Solution:

	def maxScore(self, card, N, k):
		n = len(card)
		r = n - k
		total = sum(card[n - k:])
		res = total
		l = 0
		while r < n:
			total = total + card[l] - card[r]
			res = max(res, total)
			r += 1
			l += 1
		return res


class Solution:

	def maxScore(self, c, n, k):
		ans = 0
		res = 0
		for i in range(k):
			ans += c[i]
		res = ans
		for i in range(k - 1, -1, -1):
			res -= c[i]
			res += c[n - k + i]
			ans = max(ans, res)
		return ans


class Solution:

	def maxScore(self, a, n, k):
		ans = 0
		for i in range(k):
			ans += a[i]
		res = ans
		j = n - 1
		while i >= 0:
			ans = ans - a[i] + a[j]
			res = max(res, ans)
			i -= 1
			j -= 1
		return res


class Solution:

	def maxScore(self, a, N, k):
		s = sum(a[:k])
		e = 0
		m = max(s, e)
		j = len(a) - 1
		for i in range(k - 1, -1, -1):
			e += a[j]
			s -= a[i]
			m = max(m, s + e)
			j -= 1
			k -= 1
		return m


class Solution:

	def maxScore(self, cardPoints, N, k):
		o_s = sum(cardPoints[N - k:])
		m = o_s
		i = 0
		j = N - k - 1
		while j < N - 1:
			j += 1
			o_s = o_s - cardPoints[j] + cardPoints[i]
			i += 1
			m = max(m, o_s)
		return m


class Solution:

	def maxScore(self, arr, N, k):
		s = sum(arr)
		l = 0
		r = N - k
		ws = sum(arr[r:])
		ma = ws
		while r < N:
			ws += arr[l]
			ws -= arr[r]
			ma = max(ma, ws)
			l += 1
			r += 1
		return ma


class Solution:

	def maxScore(self, arr, n, k):
		(l, r) = (0, n - k)
		summ = max_sum = sum(arr[r:])
		for r in range(n - k, n):
			summ = summ - arr[r] + arr[l]
			l += 1
			if summ > max_sum:
				max_sum = summ
		return max_sum


class Solution:

	def maxScore(self, cardPoints, N, k):
		total_points = sum(cardPoints)
		if k == len(cardPoints):
			return total_points
		remain_size = N - k
		curr_sum = sum(cardPoints[:remain_size])
		mini_sum = curr_sum
		for i in range(remain_size, len(cardPoints)):
			curr_sum -= cardPoints[i - remain_size]
			curr_sum += cardPoints[i]
			mini_sum = min(curr_sum, mini_sum)
		return total_points - mini_sum


class Solution:

	def maxScore(self, A, N, k):
		req = len(A) - k
		summ = sum(A)
		partSum = sum(A[0:req])
		mini = partSum
		for i in range(req, N):
			old = i - req
			partSum -= A[old]
			partSum += A[i]
			mini = min(mini, partSum)
		return summ - mini


class Solution:

	def maxScore(self, cardPoints, N, k):
		s = 0
		for i in range(N - k, N):
			s += cardPoints[i]
		p = s
		for i in range(k):
			h = cardPoints[N - (k - i)]
			p = p - h + cardPoints[i]
			s = max(s, p)
		return s


class Solution:

	def maxScore(self, cardPoints, N, k):
		suff = [0]
		for el in cardPoints[::-1]:
			suff.append(suff[-1] + el)
		ans = 0
		temp = 0
		for i in range(k + 1):
			ans = max(ans, temp + suff[k - i])
			if i < len(cardPoints):
				temp += cardPoints[i]
		return ans