inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
# sum of points from left most
# sum of points from right most
# sum of points from both left and right
# defualt max_sum
total = sum(cardPoints[:k])
# if k == len(cardPoitns), result is the total sum of cardPoints
if k == len(cardPoints):
return total
max_sum = total
print(max_sum)
# compute sum from left to right
for i in range(k - 1, -1, -1):
total = total + cardPoints[i - k] - cardPoints[i]
print((i-k, total, cardPoints[i - k], cardPoints[i]))
if total > max_sum:
max_sum = total
return max_sum
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
j = len(cardPoints) - 1
ms = 0
for i in range(k):
ms += cardPoints[j]
j -= 1
cand = ms
for i in range(k):
cand += cardPoints[i] - cardPoints[j+1]
j += 1
ms = max(cand, ms)
return ms
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, nums: List[int], k: int) -> int:
n = len(nums)
if k == n:
return sum(nums)
pre =[0]
post = []
for e in nums:
pre.append(pre[-1]+e)
post.append(e)
post.append(0)
for i in range(n-1,-1,-1):
post[i] = post[i+1] + nums[i]
res = 0
j = n - k
while j <= n:
res = max(res, pre[i] + post[j])
i+=1
j+=1
return res
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
if len(cardPoints) < k:
return -1
remain = len(cardPoints) - k
suum = sum(cardPoints[:remain])
min_suum = suum
for i in range(remain, len(cardPoints)):
suum = suum - cardPoints[i-remain] + cardPoints[i]
min_suum = min(min_suum, suum)
return sum(cardPoints) - min_suum
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints)
sums = [0] * (n+1)
for i in range(1, n+1):
sums[i] = sums[i-1] + cardPoints[i-1]
ans = float('inf')
for i in range(k+1):
ans = min(ans, sums[i+n-k] - sums[i])
return sums[-1] - ans
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, A: List[int], k: int) -> int:
su = sum(A[:k])
res = su
for i in range(k):
su -= A[k-i-1]
su += A[len(A)-i-1]
res = max(res, su)
return res
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints) - k
l, r, res, count = 0, 0, sum(cardPoints[:n]), 0
while r < len(cardPoints):
count += cardPoints[r]
if r >= n:
count -= cardPoints[l]
l += 1
res = min(res, count)
r += 1
return sum(cardPoints) - res
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
pre = [cardPoints[0]]
n = len(cardPoints)
for i in range(1, n):
pre.append(pre[-1] + cardPoints[i])
if k == n:
return pre[-1]
s = pre[-1]
cur_s = pre[n - k - 1]
l = 0
r = n - k + l - 1
ans = s - pre[n - k - 1]
while l != len(cardPoints) - (n - k) :
cur_s -= cardPoints[l]
l += 1
r += 1
cur_s += cardPoints[r]
ans = max(ans, s - cur_s)
return ans
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints) - k
current = 0
for i in range(n):
current += cardPoints[i]
result = current
for i in range(1, k+1):
current -= cardPoints[i-1]
current += cardPoints[i+n-1]
result = min(result, current)
return sum(cardPoints) - result
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
f, b = [0], [0]
for n in cardPoints:
f.append(f[-1] + n)
for n in cardPoints[::-1]:
b.append(b[-1] + n)
allCombo = [f[i] + b[k-i] for i in range(k+1)]
return max(allCombo)
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
points_L = []
points_R = []
for p in cardPoints:
if len(points_L): points_L.append(points_L[-1] + p)
else: points_L.append(p)
for p in cardPoints[::-1]:
if len(points_R): points_R.append(points_R[-1] + p)
else: points_R.append(p)
points_R = points_R[::-1]
return self.solution(points_L, points_R, k)
def solution(self, points_L, points_R, k):
max = 0
for i in range(k+1):
cmp1 = points_L[i-1] if i > 0 else 0
cmp2 = points_R[-(k-i)] if i < k else 0
score = cmp1 + cmp2
if score > max:
max = score
return max
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
left_sums = [0] * (k + 1)
right_sums = [0] * (k + 1)
for i in range(k):
left_sums[i+1] = left_sums[i] + cardPoints[i]
right_sums[i+1] = right_sums[i] + cardPoints[len(cardPoints)-i-1]
res = 0
for i in range(k+1): # we must include K as a choice
j = k-i
res = max(res, left_sums[i] + right_sums[j])
return res
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
from functools import lru_cache
import sys
from itertools import accumulate
sys.setrecursionlimit(10**5)
class Solution:
def maxScore(self, arr: List[int], k: int) -> int:
n = len(arr)
pre = list(accumulate(arr))
total = pre[-1]
# print(f'{pre=}')
if k == n:
return total
w = n-k
result = 0
# print(f'{w=}')
for i in range(w-1, n):
# print(f'{i=}')
sub_sum = total - (pre[i] - pre[i-w+1] + arr[i-w+1])
result = max(result, sub_sum)
return result
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
curr_max = sum(cardPoints[0:k])
curr_sum = curr_max
for i in range(1, k+1):
curr_sum = curr_sum - cardPoints[k-i] + cardPoints[-i]
curr_max = max(curr_max, curr_sum)
return curr_max
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
size = len(cardPoints)
maxRightSum = sum(cardPoints[size-k:])
ans = maxRightSum
currSum = maxRightSum
# sum with left
for i in range(k):
currSum = currSum - cardPoints[size-k+i] + cardPoints[i]
ans = max(ans, currSum)
return ans
# def maxScore(self, cardPoints: List[int], k: int) -> int:
# windowEnd = 0
# size = len(cardPoints) - k
# ans = float(\"inf\")
# currSum = 0
# for windowStart, point in enumerate(cardPoints):
# currSum += point
# if windowStart - windowEnd + 1 > size:
# currSum -= cardPoints[windowEnd]
# windowEnd += 1
# if windowStart - windowEnd + 1 == size:
# ans = min(ans, currSum)
# return sum(cardPoints) - ans
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints)
curr_max = sum(cardPoints[0:k])
curr_sum = curr_max
for i in range(1, k+1):
curr_sum = curr_sum - cardPoints[k-i] + cardPoints[-i]
curr_max = max(curr_max, curr_sum)
return curr_max
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
totalPoints, window = sum(cardPoints), len(cardPoints) - k
i, j, Sum, Min = 0, 0, 0, totalPoints
while j < len(cardPoints):
Sum += cardPoints[j]
if j - i + 1 > window:
Sum -= cardPoints[i]
i += 1
if j - i + 1 == window: Min = min(Min, Sum)
j += 1
# print(Min)
return totalPoints - Min
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
if len(cardPoints) < k:
return 0
if len(cardPoints) == k:
return sum(cardPoints)
n = len(cardPoints)
res, cur = sum(cardPoints[:k]), sum(cardPoints[:k])
for i in range(k):
cur += cardPoints[n-i-1]-cardPoints[k-1-i]
res = max(res, cur)
return res
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
if not cardPoints or k==0:
return 0
for i in range(1,len(cardPoints)):
cardPoints[i]+=cardPoints[i-1]
if k==len(cardPoints):
return cardPoints[-1]
ans=cardPoints[k-1]
for i in range(1,k+1):
print(i)
ans=max(ans,cardPoints[k-i]+cardPoints[-1]-cardPoints[-i])
print(cardPoints[k-i],cardPoints[-1]-cardPoints[-i])
return max(ans,cardPoints[-1]-cardPoints[-(k+1)])
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints)
left = n - k
mini = sum(cardPoints[:left])
cur = mini
for i in range(left, n):
cur = cur - cardPoints[i-left] + cardPoints[i]
mini = min(mini, cur)
return sum(cardPoints) - mini
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, card_points: List[int], k: int) -> int:
window_size = len(card_points) - k
current_min_sum, min_sum = 0, sys.maxsize
total_points = 0
left = 0
for right, value in enumerate(card_points):
total_points += value
current_min_sum += value
current_size = right - left + 1
if current_size < window_size:
continue
if current_size > window_size:
current_min_sum -= card_points[left]
left += 1
min_sum = min(min_sum, current_min_sum)
return total_points - min_sum
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
def max_score_using_subsequence(card_points, k):
l = len(card_points) - k
if l == 0:
return sum(card_points)
sum_subsequence = 0
for i in range(l):
sum_subsequence += card_points[i]
lowest = sum_subsequence
for i in range(len(card_points) - l):
sum_subsequence = sum_subsequence - card_points[i] + card_points[i+l]
if sum_subsequence < lowest:
lowest = sum_subsequence
return sum(card_points) - lowest
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
# return max_score_recursive(cardPoints, k, 0, len(cardPoints)-1, {})
return max_score_using_subsequence(cardPoints, k)
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
psum = sum(cardPoints[:k])
res = psum
n = len(cardPoints)
for i in range(k):
psum += cardPoints[n - i - 1] - cardPoints[k - i - 1]
res = max(res, psum)
return res
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
pre_sum = [] # i: sum of all items before i
temp_left = 0
for i, v in enumerate(cardPoints):
pre_sum.append(temp_left)
temp_left += v
pre_sum.append(temp_left)
post_sum = [] # i: sum of all items after i-1
temp_right = 0
temp_right = 0
for i in range(len(cardPoints)-1, -1, -1):
v = cardPoints[i]
post_sum.append(temp_right)
temp_right += v
post_sum.append(v)
post_sum.reverse()
# print(pre_sum)
# print(post_sum)
return max([pre_sum[i] + post_sum[-(k-i)-1] for i in range(k+1)])
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
from collections import deque
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints)
cards = n-k
minimum = 0
if (cards != 0):
sum1 = 0
q = deque()
for i in range(cards):
q.append(cardPoints[i])
sum1 += cardPoints[i]
minimum = sum1
for i in range(cards, n):
first = q.popleft()
q.append(cardPoints[i])
sum1 -=first
sum1 += cardPoints[i]
if (sum1 < minimum):
minimum = sum1
#else:
# minimum = 0
print(minimum)
return sum(cardPoints) - minimum
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, l: List[int], k: int) -> int:
length = len(l)
if k == length:
return sum(l)
elif k == 0:
return 0
k = length - k
v = curr_min = sum(l[:k])
for i in range(k,length):
v = v - l[i-k] + l[i]
curr_min = min(curr_min, v)
return sum(l) - curr_min
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
if k == len(cardPoints):
return sum(cardPoints)
size = len(cardPoints) - k
min_sum = float('inf')
left = 0
right = 0
window_sum = 0
while right < len(cardPoints):
window_sum += cardPoints[right]
right += 1
while right - left == size:
min_sum = min(min_sum, window_sum)
window_sum -= cardPoints[left]
left += 1
return sum(cardPoints) - min_sum
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, p: List[int], k: int) -> int:
res = sum(p[:k])
cur = res
for i in range(1, k+1):
cur += p[-i] - p[k-i]
res = max(cur, res)
return res
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
# Time: O(k)
# Space: O(1)
ans = win = 0
for i in range(-k, k):
win += cardPoints[i]
if i >= 0:
win -= cardPoints[i - k]
#print(i, i-k, ans, win, cardPoints[i] )
ans = max(win, ans)
return ans
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
# O(n) time and O(1) space
# alternative dp solution O(k) space: form two k sized arrays: 1 to k and n-k to n
# https://www.youtube.com/watch?v=t3JHx5J01F0 9mins06s
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
curr_max = sum(cardPoints[:k])
ans = curr_max
for i in range(1, k+1):
curr_max += cardPoints[-i] - cardPoints[k-i]
ans = max(ans, curr_max)
return ans
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints)
w = n - k
left, right = 0, w - 1
ans = float('-inf')
win_sum = sum(cardPoints[:w])
total = sum(cardPoints)
while right < n:
sub = total - win_sum
ans = max(sub, ans)
if left < n:
win_sum -= cardPoints[left]
left += 1
right += 1
if right < n:
win_sum += cardPoints[right]
return ans
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
# 我写的还是太复杂了
# if not cardPoints or not k:
# return 0
# if k==len(cardPoints):
# return sum(cardPoints)
# front={0:0}
# back={0:0}
# total=0
# for i in range(k):
# total+=cardPoints[i]
# front[i+1]=total
# total=0
# for i in range(-1,-k-1,-1):
# total+=cardPoints[i]
# back[-i]=total
# maxres=float('-inf')
# for i in range(k+1):
# maxres=max(maxres,front[i]+back[k-i])
# return maxres
s = sum(cardPoints[:k])
res = s
for i in range(1, k+1):
s += cardPoints[-i] - cardPoints[k-i]
res = max(res, s)
return res
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
left, right = [0], [0]
for i in range(k):
left.append(left[-1] + cardPoints[i])
right.append(right[-1] + cardPoints[len(cardPoints) - 1 - i])
# print(left, right)
maxSoFar = 0
for i in range(k + 1):
if left[i] + right[k - i] > maxSoFar:
maxSoFar = left[i] + right[k - i]
return maxSoFar
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, p: List[int], k: int) -> int:
s = sum(p)
if k == len(p): return s
n = len(p)
for i in range(1, len(p)): p[i] += p[i-1]
return s - min(p[n+i-k-1] - (p[i-1] if i else 0) for i in range(k+1))
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
score = 0
l = k
prefix = [0]
for i in range(len(cardPoints)):
prefix.append(prefix[-1] + cardPoints[i])
for i in range(k+1):
score = max(score, prefix[l] + prefix[-1] - prefix[-1-i])
l -= 1
return score
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
right_index = len(cardPoints)-k
curr_max = sum(cardPoints[right_index:])
curr_sum = curr_max
for left_index, right_index in zip(list(range(0, k)), list(range(len(cardPoints)-k, len(cardPoints)))):
print((left_index, right_index))
curr_sum -= cardPoints[right_index]
curr_sum += cardPoints[left_index]
curr_max = max(curr_max, curr_sum)
return curr_max
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
sub_len = len(cardPoints) - k
localSum = 0
localSum = sum(cardPoints[0: sub_len])
localMin = localSum
for i in range(sub_len, len(cardPoints)):
localSum += cardPoints[i]
localSum -= cardPoints[i - sub_len]
localMin = min(localSum, localMin)
return sum(cardPoints) - localMin
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
points = cardPoints[-k:] + cardPoints[:k]
curr = 0
max_ = 0
for ind, score in enumerate(points):
if ind >= k:
curr -= points[ind-k]
curr += score
max_ = max(max_, curr)
return max_
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
total = sum(cardPoints)
nk = len(cardPoints) - k
if nk == 0:
return total
current = sum(cardPoints[0:nk])
max_score = current
for i in range(1, k + 1):
current = current - cardPoints[i - 1] + cardPoints[nk + i - 1]
max_score = min(max_score, current)
return total - max_score
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
total = [0 for _ in range(len(cardPoints))]
total[0] = cardPoints[0]
for i in range(1, len(cardPoints)):
total[i] = total[i-1] + cardPoints[i]
max_sum = 0
for i in range(k+1):
left = total[k-i-1] if k-i > 0 else 0
right = total[-1] - total[len(cardPoints)-i-1]
if left + right > max_sum:
max_sum = left + right
return max_sum
|
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.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
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 three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
N = len(cardPoints)
res = float('inf')
run = 0
for i, p in enumerate(cardPoints):
if i >= N-k: run -= cardPoints[i-N+k]
run += p
if i >= N-k-1: res = min(res, run)
return sum(cardPoints) - res
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
import math
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
s=0
c=0
r=0
x=math.factorial(N)
while(True):
c=x*((N-r-K)**(L-K))*(-1)**(r)//(math.factorial(N-r-K)*math.factorial(r))
if(c!=0):
s=(s+c)%(10**9+7)
r+=1
else:
return s
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
import math
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
s=0
c=0
r=0
x=math.factorial(N)
while(True):
c=x*((N-r-K)**(L-K))*(-1)**(r)//(math.factorial(N-r-K)*math.factorial(r))
if(c!=0):
s=(s+c)%(10**9+7)
r+=1
else:
return s
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
MOD = 10 ** 9 + 7
@lru_cache(None)
def dp(i, j):
if i < j: return 0
if i == 0:
return 1 if j == 0 else 0
# if i == j:
# return math.factorial
a = dp(i - 1, j - 1) * (N - j + 1)
a += dp(i - 1, j) * (j - K if j > K else 0)
return a % MOD
return dp(L, N)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
mod = 10**9+7
def memo(f):
dic = {}
def f_alt(*args):
if args not in dic:
dic[args] = f(*args)
return dic[args]
return f_alt
@memo
def play(N, L):
if L == 0:
return 1 if N == 0 else 0
if N > L:
return 0
return (N*play(N-1, L-1) + max(0, N-K)*play(N, L-1))%mod
return play(N, L)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
dp = [[0] * (N + 1) for _ in range(L + 1)]
dp[0][0] = 1
for i in range(1, L + 1):
for j in range(1, N + 1):
dp[i][j] = dp[i - 1][j - 1] * (N - j + 1) #play new song
if j > K: #play old song
dp[i][j] += dp[i - 1][j] * (j - K)
return dp[-1][-1]%(10 ** 9 + 7)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
mod = 10 ** 9 + 7
dp = [[0] * (L + 1) for _ in range(N + 1)]
for i in range(1, N + 1):
for j in range(i, L + 1):
if i == K + 1:# or i == j:
dp[i][j] = math.factorial(i)
else:
dp[i][j] = dp[i - 1][j - 1] * i
if j > i:
dp[i][j] += dp[i][j - 1] * (i - K)
dp[i][j] %= mod
# print(dp)
return dp[N][L]
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
dp = [[0]*(N+1) for _ in range(L+1)]
dp[0][0] = 1
for i in range(1,L+1):
for j in range(1,N+1):
dp[i][j] = dp[i-1][j-1]*(N-j+1)%(10**9+7)
if j > K:
dp[i][j] = (dp[i][j] + dp[i-1][j] * (j-K))%(10**9+7)
return dp[L][N]
#T=O(NL) S=O(NL)
memo = {}
def DFS(i,j):
if i == 0:
return j==0
if (i,j) in memo:
return memo[(i,j)]
ans = DFS(i-1, j-1)*(N-j+1)
ans += DFS(i-1, j)* max(j-K,0)
memo[(i,j)] = ans%(10**9+7)
return memo[(i,j)]
return DFS(L,N)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
mod = 10 ** 9 + 7
dp = [[0] * (L + 1) for _ in range(N + 1)]
for i in range(1, N + 1):
for j in range(i, L + 1):
if i == K + 1 or i == j:
dp[i][j] = math.factorial(i)
else:
dp[i][j] = dp[i - 1][j - 1] * i
if j > i:
dp[i][j] += dp[i][j - 1] * (i - K)
dp[i][j] %= mod
# print(dp)
return dp[N][L]
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
dp = [[0 for _ in range(N+1)] for _ in range(L+1)]
dp[0][0] = 1
mod = 10**9 + 7
for i in range(1, L+1):
for j in range(1, N+1):
dp[i][j] = dp[i-1][j-1] * (N-j+1)
if j > K:
dp[i][j] += dp[i-1][j]*(j-K)
dp[i][j] %= mod
return dp[L][N]
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
dp = [1] * (L-N+1)
for p in range(2, N-K+1):
for i in range(1, L-N+1):
dp[i] += dp[i-1] * p
ans = dp[-1]
for k in range(2, N+1):
ans *= k
return ans % (10 ** 9 + 7)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
# 11:35
# pick k+1 songs out of N Songs
# k+1 factorial
# you can pick a new song or old song from this k+1 => basically you got N options now
mod=10**9+7
@lru_cache(None)
def helper(i,notplayed):
nonlocal mod
if i==L+1:
return 0 if notplayed!=0 else 1
ans=(max((N-notplayed)-K,0)*helper(i+1,notplayed))%mod
if notplayed!=0:
ans+=(notplayed)*helper(i+1,notplayed-1)
return ans%mod
return helper(1,N)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
BIG = 10**9+7
@lru_cache(None)
def dp(r,n):
if r == 0: return 1 if n == 0 else 0
return ( dp(r-1,n-1) * (N-(n-1)) + dp(r-1,n) * max(0, n-K) ) % BIG
return dp(L, N)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
@lru_cache(None)
def unique(length, uni):
if uni == 0:
return 0
if length == 1:
if uni == 1:
return N
else:
return 0
ret = unique(length - 1, uni - 1) * (N - uni + 1)
ret += unique(length -1, uni) * max(0, uni - K)
return ret % (10**9+7)
return unique(L, N)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
m = 10 ** 9 + 7
dp = [[0 for _ in range(N+1)] for _ in range(L+1)]
dp[0][0] = 1
for i in range(1,L+1):
for j in range(1,N+1):
dp[i][j] = (dp[i-1][j-1] * (N-j+1) + dp[i-1][j] * max(j-K,0))%m
return int(dp[L][N])
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
mod = 10 ** 9 + 7
@lru_cache(None)
def dp(l, n):
if not l:
return not n
return dp(l - 1, n - 1) * (N - n + 1) + dp(l - 1, n) * max(n - K, 0)
return dp(L, N) % mod
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
from functools import lru_cache
class Solution:
def numMusicPlaylists(self, N, L, K):
@lru_cache(None)
def dp(i, j):
if i == 0:
return +(j == 0)
ans = dp(i-1, j-1) * (N-j+1)
ans += dp(i-1, j) * max(j-K, 0)
return ans % (10**9+7)
return dp(L, N)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
dp = [[0 for i in range(L+1)] for j in range(N+1)]
for i in range(1, len(dp)):
for j in range(1, len(dp[0])):
if i == j:
dp[i][j] = math.factorial(i)
else:
dp[i][j] = dp[i-1][j-1]*i + dp[i][j-1]*max((i-K), 0)
print(dp)
return dp[N][L]%(10**9+7)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
dp = [0 for _ in range(L + 1)]
dp[0] = 1
for i in range(1, N + 1):
dp2 = [0 for _ in range (L + 1)]
for j in range(1, L + 1):
dp2[j] = dp[j - 1] * (N - i + 1)
dp2[j] += dp2[j - 1] * max(i - K, 0)
dp = dp2
return dp[L] % (10**9 + 7)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
import math
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
mod = 10**9+7
dp = [[0 for _ in range(L+1)] for _ in range(N+1)]
dp[0][0]=1
for i in range(1,N+1):
for j in range(1,L+1):
dp[i][j] = dp[i-1][j-1]*(N-i+1)%mod
dp[i][j] += dp[i][j-1]*max(i-K,0)%mod
return dp[-1][-1]%mod
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
memo = {}
def dp(i, j):
if i == 0:
return j == 0
if (i, j) in memo: return memo[i, j]
memo[i, j] = dp(i - 1, j - 1) * (N - j + 1) + dp(i - 1, j) * max(j - K, 0)
return memo[i, j]
return dp(L, N)%(10**9 + 7)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
# N = 10 L = 25 K = 4
# 1~10 24 3
# 2~9 23 2
# Unique : L - N + 1 Extra : N-K+1
dp = [1] * (L-N+1)
for p in range(2,N-K+1):
for i in range(1,L-N+1):
dp[i] += p*dp[i-1]
ans = dp[-1]
for k in range(2,N+1):
ans = ans * k
return ans% (10**9 + 7)
dp = [1] * (L-N+1)
print(dp)
for p in range(2, N-K+1):
for i in range(1, L-N+1):
dp[i] += dp[i-1] * p
print((p,i, dp))
# Multiply by N!
ans = dp[-1]
for k in range(2, N+1):
ans *= k
return ans % (10**9 + 7)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
cache = {}
def dp(i, j):
if i == 0:
return +(j == 0)
if (i,j) in cache:
return cache[(i,j)]
ans = dp(i-1, j-1) * (N-j+1)
ans += dp(i-1, j) * max(j-K, 0)
ans %= (10**9+7)
cache[(i,j)] = ans
return ans
return dp(L, N)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
@lru_cache(None)
def dp(i, j):
if i == 0:
return j == 0
return (dp(i-1, j) * max(0, j - K) + dp(i-1, j-1) * (N - j + 1)) % (10**9 + 7)
return dp(L, N)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
dp = [[0 for n in range(N+1) ] for l in range(L+1)]
dp[0][0] = 1
for l in range(1, L+1):
for n in range(1, N+1):
dp[l][n] += dp[l-1][n-1] * (N-n+1)
dp[l][n] += dp[l-1][n] * max(n-K, 0)
dp [l][n] = dp [l][n] % (10 **9+7)
return dp[L][N]
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
dp = [[0] * (N + 1) for _ in range(L + 1)]
dp[0][0] = 1;
for l in range(1, L + 1):
for n in range(1, N + 1):
dp[l][n] += dp[l - 1][n - 1] * (N - n + 1)
dp[l][n] += dp[l - 1][n] * max(n - K, 0)
dp[l][n] = dp[l][n] % (1000000007)
return dp[L][N]
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
mod = 10**9 + 7
memo = {}
# helper(i,j) returns the number of playlists of len i
# that have exactly j unique songs
def helper(i, j):
nonlocal N, K
if i==0:
if j==0:
# base case
# helper(0,0) returns 1
return 1
else:
return 0
if (i,j) in memo:
return memo[(i,j)]
ans = 0
# the jth song is unique,
# then the jth song has (N-(j-1)) possibilities
ans += helper(i-1, j-1)*(N-(j-1))
# the jth song is not unique
# it is the same as one of the previous songs
# then the jth song has max(0, j-K) possibilities
# since it can be the same as the previous K songs
ans += helper(i-1, j)*max(0, j-K)
memo[(i,j)]=ans%mod
return ans%mod
return helper(L, N)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
memo = {}
def dp(i, j):
if i == 0: return 1 if j == 0 else 0
if (i, j) in memo: return memo[(i, j)]
# non repeat
ans = dp(i - 1, j - 1) * (N - (j - 1))
# repeat
ans += dp(i - 1, j) * max(0, j - K)
memo[(i, j)] = ans % (10 ** 9 + 7)
return memo[(i, j)]
return dp(L, N)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
memo = {}
def dp(i, j):
if i == 0: return 1 if j == 0 else 0
if (i, j) in memo: return memo[(i, j)]
# non repeat
ans = dp(i - 1, j - 1) * (N - (j - 1))
# repeat
ans += dp(i - 1, j) * max(0, j - K)
memo[(i, j)] = ans % (10 ** 9 + 7)
return memo[(i, j)]
return dp(L, N)
# from functools import lru_cache
# class Solution:
# def numMusicPlaylists(self, N, L, K):
# @lru_cache(None)
# def dp(i, j):
# if i == 0:
# return +(j == 0)
# ans = dp(i-1, j-1) * (N-j+1)
# ans += dp(i-1, j) * max(j-K, 0)
# return ans % (10**9+7)
# return dp(L, N)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
@lru_cache(None)
def dp(i, j):
if i == 0:
return +(j == 0)
ans = dp(i-1, j-1) * (N-j+1)
ans += dp(i-1, j) * max(j-K, 0)
return ans % (10**9+7)
return dp(L, N)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
from functools import lru_cache
class Solution:
def numMusicPlaylists(self, N, L, K):
@lru_cache(None)
def dp(i, j):
if i == 0:
return +(j == 0)
ans = dp(i-1, j-1) * (N-j+1)
ans += dp(i-1, j) * (j-min(K, i-1))
return ans % (10**9+7)
return dp(L, N)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
from functools import lru_cache
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
# Dynamic Programming
# Let dp[i][j] be the number of playlists of length i that have exactly j unique songs.
# Time complexity: O(NL)
# Space complexity: O(NL)
@lru_cache(None)
def dp(i, j):
if i == 0:
return +(j == 0)
ans = dp(i - 1, j - 1) * (N - j + 1)
ans += dp(i - 1, j) * max(j - K, 0)
return ans % (10**9 + 7)
return dp(L, N)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
dp = [[0 for j in range(L+1)] for i in range(N+1)]
for i in range(K+1, N+1):
for j in range(i, L+1):
if i == j or i == K+1:
dp[i][j] = math.factorial(i)
else:
dp[i][j] = dp[i-1][j-1]*i + dp[i][j-1]*(i-K)
return dp[N][L]%(10**9+7)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
'''
construct a 2d dp[i][j] where i is i different songs and
j is the length of the playlist, also track the remaining songs r:
for dp update, we have two options:
if i <= k:
1. add a new song to the list, r -= 1
else:
if r > L-j
1. add a new song to the list, r -= 1
2. add an existing song
else:
1. add a new song
'''
@lru_cache(None)
def dp(unique, total, r):
if total == L:
return 1
if unique <= K:
return r * dp(unique+1, total+1, r-1)
else:
ans = 0
if r < L-total:
# add an existing song
ans += (unique-K) * dp(unique, total+1, r)
# add a new song
ans += r * dp(unique+1, total+1, r-1)
return ans
return dp(0, 0, N) % (10**9+7)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
dp = [[0 for i in range(L + 1)] for j in range(N + 1)]
for i in range(K + 1, N + 1):
for j in range(i, L + 1):
if i == j or i == K + 1:
dp[i][j] = math.factorial(i)
else:
dp[i][j] = dp[i - 1][j - 1] * i + dp[i][j - 1] * (i - K)
return dp[N][L] % (10**9 + 7)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
import math
from functools import lru_cache
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
MOD = 1000000007
@lru_cache(maxsize=None)
def rec(n, l):
if l < n or n <= K:
return 0
elif l == n:
return math.factorial(n) % MOD
return (n * rec(n - 1, l - 1) + (n - K) * rec(n, l - 1)) % MOD
return rec(N, L)
|
Your music player contains N different songs and she wants to listen to L (not necessarily different) songs during your trip. You create a playlist so that:
Every song is played at least once
A song can only be played again only if K other songs have been played
Return the number of possible playlists. As the answer can be very large, return it modulo 10^9 + 7.
Example 1:
Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].
Example 2:
Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]
Example 3:
Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]
Note:
0 <= K < N <= L <= 100
|
class Solution:
def numMusicPlaylists(self, N: int, L: int, K: int) -> int:
MOD = 10 ** 9 + 7
@lru_cache(None)
def dp(i, j):
if i < j: return 0
if i == 0:
return 1 if j == 0 else 0
if i == j:
return (math.factorial(N) // math.factorial(N - j)) % MOD
a = dp(i - 1, j - 1) * (N - j + 1)
a += dp(i - 1, j) * (j - K if j > K else 0)
return a % MOD
return dp(L, N)
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
return target in nums
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
l,r = 0, len(nums) -1
while l <= r:
m = l+(r-l)//2
if nums[m] == target: return True
while nums[m] == nums[l] and l < m:
l += 1
if nums[l] <= nums[m]:#[l,m] is sorted
if nums[l] <= target < nums[m]:
r = m - 1
else:
l = m+1
else:
if nums[m] < target <= nums[r]:
l = m + 1
else:
r = m - 1
return False
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
low = 0
high = len(nums)-1
while low<=high:
mid = (low+high)//2
if nums[mid]==target:
return True
if nums[low]<nums[mid]:
if nums[low]<=target and nums[mid]>target:
high = mid-1
else:
low= mid+1
elif nums[low]>nums[mid]:
if nums[mid]<target and nums[high]>=target:
low= mid+1
else:
high = mid -1
else:
low+=1
return False
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
left, right = 0, len(nums)-1
while left <= right:
mid = (left+right) // 2
if nums[mid] == target:
return True
elif nums[mid] > nums[left]:
if target >= nums[left] and target < nums[mid]:
right = mid
else:
left = mid + 1
elif nums[mid] < nums[left]:
if target > nums[mid] and target <= nums[right]:
left = mid + 1
else:
right = mid
else:
left = left + 1
return False
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
res = False
def searchR(self, nums, target, start, end):
if(start > end or self.res == True):
return self.res
else:
mid = int((start + end) / 2)
print("Mid index: %d, Mid val: %d, Target: %d" % (mid, nums[mid], target))
if(nums[mid] == target):
self.res = True
elif(nums[start] <= nums[mid]):
if(nums[mid] == nums[end]):
self.searchR(nums, target, start, mid - 1)
self.searchR(nums, target, mid+1, end)
elif (target >= nums[start] and target <= nums[mid]):
self.searchR(nums, target, start, mid - 1)
else:
self.searchR(nums, target, mid+1, end)
else:
print("Here")
if (target >= nums[mid] and target <= nums[end]):
self.searchR(nums, target, mid + 1, end)
else:
self.searchR(nums, target, start, mid - 1)
return self.res
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
res = self.searchR(nums, target, 0, (len(nums) - 1))
return res
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
return self.bsearch(nums, target, 0, len(nums)-1)
def bsearch(self, nums, target, left, right):
if left > right:
return False
mid = (left+right)//2
if nums[mid] == target:
return True
#print(nums[left], nums[mid], nums[right])
if nums[left] < nums[mid]: # left sorted
if nums[left] <= target <= nums[mid]:
return self.bsearch(nums, target, left, mid-1)
else:
return self.bsearch(nums, target, mid+1, right)
elif nums[mid] < nums[right]: # right sorted
if nums[mid] <= target <= nums[right]:
return self.bsearch(nums, target, mid+1, right)
else:
return self.bsearch(nums, target, left, mid-1)
elif nums[mid] == nums[left]:
return self.bsearch(nums, target, left+1, right)
elif nums[mid] == nums[right]:
return self.bsearch(nums, target, left, right-1)
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
if not nums:
return False
left, right = 0, len(nums) - 1
while nums[left] == nums[right] and left != right:
right -= 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return True
elif nums[mid] >= nums[left]:
if target >= nums[left] and target < nums[mid]:
right = mid - 1
else:
left = mid + 1
else:
if target > nums[mid] and target <= nums[right]:
left = mid + 1
else:
right = mid - 1
return False
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
start, end = 0, len(nums)-1
while start<=end:
mid = start+(end-start)//2
if nums[mid]==target:
return True
while start<=end and nums[start]==nums[end]:
if nums[start]==target:
return True
start += 1
end -= 1
if start>end:
return False
if nums[start]<nums[end]:
#normal
if target<nums[mid]:
end = mid-1
else:
start = mid+1
else:
if nums[mid]<nums[start]:
#peak between start and mid
if target<nums[mid] or target>=nums[start]:
end = mid-1
else:
start = mid+1
else:
#peak between mid and end
if target>nums[mid] or target<=nums[end]:
start = mid+1
else:
end = mid-1
return False
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
if not nums:
return False
l, r = 0, len(nums)-1
while l < r:
mid = (l + r) // 2
if nums[mid] == target:
return True
if nums[mid] < nums[r]:
if nums[mid] < target <= nums[r]:
l = mid + 1
else:
r = mid - 1
elif nums[mid] > nums[r]:
if nums[l] <= target < nums[mid]:
r = mid - 1
else:
l = mid + 1
else:
r -= 1
return nums[l] == target
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
if not nums:
return False
l = 0
r = len(nums) - 1
print((">", l, r))
while l < r and nums[l] == nums[r]:
l = l + 1
while l <= r:
mid = int((l + r + 1) / 2)
if target == nums[mid]:
return True
if target < nums[mid]:
if target == nums[l]:
return True
elif target > nums[l]:
r = mid - 1
elif target < nums[l]:
if nums[l] <= nums[mid]:
l = mid + 1
elif nums[l] > nums[mid]:
r = mid - 1
if target > nums[mid]:
if target == nums[r]:
return True
elif target > nums[r]:
if nums[mid] >= nums[l]:
l = mid + 1
elif nums[mid] < nums[l]:
r = mid - 1
elif target < nums[r]:
l = mid + 1
return False
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
def recursive(self, nums, left, right , target):
print((left, right))
if left > right:
return False
if left == right:
return nums[left] == target
if left + 1 == right:
return nums[left] == target or nums[right] == target
mid = (left + right) // 2
if nums[mid] == target:
return True
if nums[mid] == nums[left] and nums[mid] == nums[right]:
return self.recursive(nums, mid, right, target) or self.recursive(nums, left, mid, target)
else:
if nums[mid] < target:
if nums[right] >= target or (nums[left] <= nums[mid] and nums[mid] >= nums[right]):
return self.recursive(nums, mid, right, target)
else:
return self.recursive(nums, left, mid, target)
else:
if nums[left] <=target or (nums[right] >= nums[mid] and nums[mid] <= nums[left]):
return self.recursive(nums, left, mid, target)
else:
return self.recursive(nums, mid, right, target)
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
return self.recursive(nums, 0, len(nums) - 1, target)
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
if len(nums) == 0:
return False
def binary_search(leftIndex, rightIndex):
midIndex = int((rightIndex+leftIndex)/2)
if nums[midIndex] == target:
return True
if leftIndex > rightIndex:
return False
return binary_search(leftIndex, midIndex-1) or binary_search(midIndex+1, rightIndex)
return binary_search(0, len(nums)-1)
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
l, r = 0, len(nums) - 1
while l <= r:
mid = l + (r - l)//2
if nums[mid] == target:
return True
while nums[mid] == nums[r]:
r -= 1
mid = l + (r - l)//2
if r < 0:
return False
if nums[mid] == target:
return True
if nums[mid] <= nums[r]:
if target > nums[mid] and target <= nums[r]:
l = mid + 1
else:
r = mid - 1
else:
if target < nums[mid] and target >= nums[l]:
r = mid - 1
else:
l = mid + 1
return False
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
if (not nums):
return False
i = 0
while (i + 1 < len(nums) and nums[i] == nums[i + 1]):
i += 1
if (i + 1 == len(nums)):
# nums中所有元素相等
return nums[0] == target
# 找到最大元素的位置
l, r = i, len(nums) - 1
while (l + 1 < r):
mid = l + (r - l) // 2
if (nums[i] < nums[mid]):
l = mid
else:
r = mid - 1
highest_pos = l if (nums[l] > nums[r]) else r
if (target >= nums[0]):
return self._binarySearch(nums, 0, highest_pos, target)
else:
return self._binarySearch(nums, highest_pos + 1, len(nums) - 1, target)
def _binarySearch(self, nums, l, r, target):
while (l <= r):
mid = l + (r - l) // 2
if (nums[mid] < target):
l = mid + 1
elif (nums[mid] > target):
r = mid - 1
else:
return True
return False
|
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
Would this affect the run-time complexity? How and why?
|
class Solution:
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
for i in nums:
if i == target:
return True
return False
|
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases.
|
class Solution:
def superPow(self, a, b):
result = 1
fermatb = (int(''.join(map(str, b)))) % 570
while fermatb:
if fermatb & 1:
result = (result * a) % 1337
a = (a * a) % 1337
fermatb >>= 1
return result
|
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases.
|
class Solution:
def superPow(self, a, b):
return pow(a, int(''.join(map(str, b))), 1337)
|
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases.
|
class Solution:
def superPow(self, a, b):
"""
:type a: int
:type b: List[int]
:rtype: int
"""
res = a**b[0]
for i in range(1, len(b)):
res = res**10 % 1337 * a**b[i] % 1337 % 1337
return res
|
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases.
|
class Solution:
def superPow(self, a, b):
"""
:type a: int
:type b: List[int]
:rtype: int
"""
b.reverse()
p = b.pop()
res = pow(a,p) % 1337
while b:
p = b.pop()
res = pow(res,10) % 1337
res = res * pow(a,p) % 1337
return res
|
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases.
|
class Solution:
def superPow(self, a, b):
"""
:type a: int
:type b: List[int]
:rtype: int
"""
temp1 = a
temp = 1
for i in range(len(b)-1,-1,-1):
if i<len(b)-1:
temp1 = pow(temp1,10) % 1337
temp2 = pow(temp1,b[i]) % 1337
temp = temp * temp2 % 1337
return temp
|
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases.
|
class Solution:
def superPow(self, a, b):
"""
:type a: int
:type b: List[int]
:rtype: int
"""
from functools import reduce
return 0 if a % 1337 == 0 else pow(a, reduce(lambda x, y: (x * 10 + y) % 1140, b) + 1140, 1337)
|
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases.
|
class Solution:
def superPow(self, a, b):
"""
:type a: int
:type b: List[int]
:rtype: int
"""
def eular(mod):
res, a = mod, mod
i = 2
while i * i <= a:
if a % i == 0:
res = res // i * (i - 1)
while a % i == 0:
a //= i
i += 1
if a > 1:
res = res // a * (a - 1)
return res
def qpow(x, n, mod):
res = 1
while n > 0:
if n&1 != 0:
res = res * x % mod
x = x * x % mod
n >>= 1
return res
phi = eular(1337)
n = 0
for num in b:
n = (n*10 % phi + num) % phi
return qpow(a, n, 1337)
|
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:Special thanks to @Stomach_ache for adding this problem and creating all test cases.
|
class Solution:
def superPow(self, a, b):
"""
:type a: int
:type b: List[int]
:rtype: int
"""
a, b = a % 1337, int("".join([str(i) for i in b]))
base, temp, extra = {1: a}, 1, {a: 1}
for temp in range(2, 1338):
rem = base[temp - 1] * a % 1337
if rem in extra:
start = extra[rem]
length = temp - start
break
else:
base[temp] = rem
extra[rem] = temp
if b in base:
return base[b]
return base[start + (b - start) % length]
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.