inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
sA = sorted(enumerate(A), key= lambda x:x[1], reverse=True)
# print(sA)
L = len(A)
maxScore = -L #invalid value, just for initialization
theDis = -1 #invalid value, just for initialization, minimal dis should be 1
for i in range(L-1):
if sA[i][1] + sA[i+1][1] <= maxScore + 1:
# print('too small to continue', i)
break #two small to continue
for j in range(i+1, L):
curScore = sA[i][1] + sA[j][1]
if curScore <= maxScore + 1:
# print('internal break', i, j)
break
curDis = abs(sA[i][0]-sA[j][0])
curScore -= curDis
if curScore > maxScore:
maxScore = curScore
theDis = curDis
return maxScore
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
res = imax = 0
for i, a in enumerate(A):
res = max(res, imax + A[i] - i)
imax = max(imax, A[i] + i)
return res
# int n = a.size();
# int maxOverallGain = INT_MIN;
# int maxEndRight = a[n-1] - (n-1);
# for(int i=n-2; i>=0; i--)
# {
# maxEndRight = max(maxEndRight, a[i+1] - (i+1));
# maxOverallGain = max(maxOverallGain, a[i] + i + maxEndRight);
# }
# return maxOverallGain;
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
maxI = A[0]
ans = A[0] + A[1] - 1
for i in range(1, len(A)):
if ans < maxI + A[i] - i:
ans = maxI + A[i] - i
if maxI < A[i] + i:
maxI = A[i] + i
return ans
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
i_sum = []
j_sum = []
prev = -float('inf')
max_sum = -float('inf')
for i in reversed(list(range(len(A)))):
if i > 0:
if A[i]-i > prev:
j_sum.append(A[i]-i)
prev = A[i] - i
else:
j_sum.append(prev)
i_sum.append(A[i]+i)
for i in reversed(list(range(1,len(i_sum)))):
max_sum = max(max_sum, i_sum[i]+j_sum[i-1])
return max_sum
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
res = maxx = 0
for i in range(len(A)):
res = max(res, maxx+A[i])
maxx = max(maxx, A[i]) - 1
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
# score = A[i] + A[j] + i - j = A[i] + i + A[j] - j
# max(A[i]+i + A[j] - j) = max(A[i]+i) + max(A[j]-j), i<j
max_i = 0
score_max_i_j = []
for j in range(len(A)):
score_max_i_j.append(max_i + A[j] - j)
max_i = max(max_i, A[j]+j)
return max(score_max_i_j)
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
a, b, n = 0, 1, len(A)
def opt(i,j):
return A[i] + A[j] + i - j
ans = [A[0], opt(0,1)] + [0 for _ in range(n-2)]
for i in range(2, n):
opt_a, opt_b = opt(a, i), opt(b, i)
if opt_a > opt_b:
ans[i] = opt_a
b = i
else:
ans[i] = opt_b
a = b
b = i
return max(ans)
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
res = 0
n = len(A)
m = A[0]
j = 0
for i in range(1, n):
res = max(res, (A[i]+A[j]+j-i))
if A[i]+i-j > m:
m = A[i]
j = i
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
# consider B[i] = A[i] + i, C[i] = A[i] -j
# Find the B[i] + C[j]
B = [A[i] + i for i in range(len(A))]
C = [A[i] - i for i in range(len(A))]
p = 0
q = 1
max_score = 0
# for each q = 1, 2,..., len(A) - 1
# update the max of B[0], ...., B[j-1]: B[p]
# calculate B[q] - B[p]
while q < len(B):
if B[q-1] > B[p]:
p = q - 1
max_score = max(max_score, B[p] + C[q])
q += 1
return max_score
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
# A = [8 , 1 ,5 ,2 6,]
# output = 11
# Explanation:
# i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
# print(A)
# max_score = 0
# for i in range(len(A)):
# first = A[i] # this is my number
# for j in range(0,len(A)):
# second = A[j]
# max_score = max(max_score ,A[i] + A[j] + i - j )
# print(A[i] + A[j])
# return max_score
i = A[0]
max_ans = 0
for j in range(1,len(A)):
x = A[j]
print(x)
max_ans = max(max_ans, i + x - j)
i = max(i , x + j)
return max_ans
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
# consider B[i] = A[i] + i, C[i] = A[i] -j
# Find the B[i] + C[j]
B = [A[i] + i for i in range(len(A))]
C = [A[i] - i for i in range(len(A))]
p = 0
q = 1
max_score = 0
while q < len(B):
if B[q-1] > B[p]:
p = q - 1
max_score = max(max_score, B[p] + C[q])
q += 1
return max_score
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
preMax = A[0]
ans = 0
for i, a in enumerate(A[1:], 1):
ans = max(ans, a - i + preMax)
preMax = max(preMax, a + i)
# print(preMax,ans)
return ans
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
res = imax = 0
for i, a in enumerate(A):
res = max(res, imax + A[i] - i)
imax = max(imax, A[i] + i)
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
best_of_prev = A[0] - 1
maximum = 0
for spot in A[1:]:
if spot + best_of_prev > maximum:
maximum = spot + best_of_prev
if spot > best_of_prev:
best_of_prev = spot
best_of_prev -= 1
return maximum
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
res = 0
maximum = A[0]
for i in range(1, len(A)):
cur = A[i]-i
res = max(res, cur+maximum)
maximum = max(maximum, A[i]+i)
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
ans = 0
best_pre = 0
for i in range(1,len(A)):
ans = max(ans, A[best_pre]+best_pre + A[i]- i)
if A[i]+i > A[best_pre] + best_pre:
best_pre = i
return ans
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
# A = [8 , 1 ,5 ,2 6,]
# output = 11
# Explanation:
# i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
i = A[0]
max_ans = 0
for j in range(1,len(A)):
x = A[j]
print(x)
max_ans = max(max_ans, i + x - j)
i = max(i , x + j)
return max_ans
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
best = 0
i = 0
for j in range(1, len(A)):
s1 = A[i] + A[j] + i - j
s2 = A[j] + A[j-1] + j - 1 - j
if s2 > s1:
i = j - 1
best = max(best, s1, s2)
return best
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
lmax=A[0]
gmax=float('-inf')
for i in range(1,len(A)):
cur=lmax+A[i]-i
lmax=max(lmax,A[i]+i)
gmax=max(gmax,cur)
return gmax
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int: # O(n log n)?
# dynamic programming / memoization solution? => wrong!!!
## [1, 2, 3, 4] => [[1, 2], [2, 3], [3, 4]] sum: a + b - 1
## [[1, 3], [2, 4]] sum: [a, b-1] + [b] - 1 # get midpoint and look-up left and right parts
## [[1, 4]] sum: [1:2] + [3:4] -1
### example: A = [8,1,5,2,6]
## memo[dist=1]: [8+1-1=8, 1+5-1=5, 5+2-1=6, 2+6-1=7]
## memp[dist=2]: [8+5-1=13]
# memo = {(i, i): A[i] for i in range(A)} # (i, j) = sum(i:j)
# max_score = 0 # max(memo.values())
# for dist in range(1, len(A)):
# for i in range(len(A) - dist):
# # get sum
# mid = (i + dist) // 2 # for pairs of dist 1 => 0, 1; mid == 0
# left_sum = memo[(i, mid)]
# right_sum = memo[(mid+1, i+dist)]
# memo[(i, i + dist)] = left_sum + right_sum - 1
# max_score = max(memo[(i, i + dist)], max_score) # update max score
# return max_score
## idea 2: lagging and leading pointers => calc their score. Move leading on, if score goes down, move lagging next
lag, lead = 0, 1
prev_max_score = A[0]
prev_max_idx = 0
max_score = 0
for i in range(1, len(A)):
score = prev_max_score + A[i] - (i - prev_max_idx)
max_score = max(score, max_score)
if A[i] >= score - A[i]:
prev_max_idx = i
prev_max_score = A[i]
return max_score
# ex: [100, 1, 1, 1, 1....[1]*_99_, 51, 51, 1, 1, [1]*_99_, 100]
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
best_i = 0
res = 0
for i, v in enumerate(A):
res = max(res, v-i+best_i)
best_i = max(best_i, v+i)
return res
return res
# best_i = 0
# res = 0
# for i, v in enumerate(A):
# res = max(res, v-i+best_i)
# best_i = max(best_i, v+i)
# return res
# best_i = 0
# res = 0
# for i, v in enumerate(A):
# res = max(res, v- i + best_i)
# best_i = max(best_i, v+i)
# return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
d=0
m=A[0]
for i in range(1,len(A)):
d=max(d,A[i-1]+(i-1))
m=max(A[i]-i+d,m)
return m
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
best_of_prev = A[0] - 1
maximum = 0
for spot in A[1:]:
if spot + best_of_prev > maximum:
maximum = spot + best_of_prev
if spot > best_of_prev:
best_of_prev = spot
best_of_prev -= 1
return maximum
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
n = len(A)
pre = A[0] + 0
res = 0
for i in range(1, n):
# res = max(res, pre + A[i] - i)
# pre = max(pre, A[i] + i)
res = res if res > pre + A[i] - i else pre + A[i] - i
pre = pre if pre > A[i] + i else A[i] + i
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
n = len(A)
res = max_i = -n
for j in range(1,n):
max_i = max(max_i, A[j-1] + j-1)
res = max(res, max_i + A[j] - j)
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
idx = [0]
for i in range(1, len(A)):
if A[i]+i-idx[-1]> A[idx[-1]]:
idx.append(i)
res = float('-inf')
i = 1
j = 0
#print(idx)
while i < len(A):
if j+1 < len(idx) and idx[j+1] < i:
j += 1
res = max(res, A[i]+A[idx[j]]+idx[j]-i)
i += 1
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
arr = [A[0] + A[j] - j for j in range(1, len(A))]
print(arr)
maxdiff = 0
ans = max(arr)
for j in range(1, len(arr)):
maxdiff = max(maxdiff, A[j] - A[0] + j)
ans = max(ans, arr[j] + maxdiff)
return ans
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
max_score = 0
# prev = ans = -float('inf')
# for i in range(1, len(A)):
# prev = max(prev, A[i-1]+i-1)
# ans = max(ans, prev+A[i]-i)
# return ans
prev = ans = -float('inf')
# A[i] + A[j] + i - j
for i in range(1, len(A)):
prev = max(prev, A[i-1] + i - 1)
ans = max(ans, prev + A[i]-i)
return ans
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
i_sum = [0]*len(A)
j_sum = [0]*len(A)
for i in range(len(A)):
i_sum[i] = A[i] + i
i_sum[-1] = 0
for j in range(len(A)):
j_sum[j] = A[j] - j
print(j_sum)
prefix_sum = [-float('inf')]*len(A)
for j in range(len(A)-2,-1,-1):
prefix_sum[j] = max(j_sum[j+1], prefix_sum[j+1])
max_sum = -float('inf')
for i in range(len(A)):
max_sum = max(max_sum,prefix_sum[i] + i_sum[i])
print(prefix_sum)
return max_sum
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
n = len(A)
# dp = [0] * n
# dp[0] = A[0]
dp = 0
ans = 0
for j in range(0,n):
# ans = A[j]-j + max_i{0, j-1}(A[i]+i)
# = A[j]-j] + dp[j-1]
# dp[j] = max(dp[j-1], A[j]+j)
# ans = max(ans, A[j]-j + dp[j-1])
# dp[j] = max(dp[j-1], A[j]+j)
ans = max(ans, A[j]-j + dp)
dp = max(dp, A[j]+j)
# print(dp)
return ans
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
# maxScore = 0
# for i in range(0,len(A)):
# for j in range(i+1,len(A)):
# val = A[i] + A[j] + i - j
# if val > maxScore:
# maxScore = val
# return maxScore
best = 0
i = 0
for j in range(1, len(A)):
s1 = A[i] + A[j] + i - j
s2 = A[j] + A[j-1] - 1
if s2 > s1:
i = j - 1
best = max(best, s1, s2)
return best
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
cur, ans = 0, 0
for a in A:
ans = max(ans, cur + a)
cur = max(cur, a) -1
return ans
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
#we want to find the two biggest numbers closest to eachother.
dp_i = [0 for i in range(len(A))]
dp_j = [0 for i in range(len(A))]
dp_i[0] = A[0]
dp_j[-1] = A[-1] - (len(A) - 1)
for i in range(1, len(A)):
dp_i[i] = max(A[i] + i, dp_i[i - 1])
for j in range(len(A) - 2, -1 , -1):
dp_j[j] = max(A[j] - j, dp_j[j + 1])
max_sum = 0
for i in range(len(A) - 1):
max_sum = max(max_sum, dp_i[i] + dp_j[i + 1])
return max_sum
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
total = 0
n = len(A)
if n <= 2:
return sum(A) - 1
a_1 = [a+i for i, a in enumerate(A)]
a_2 = [a-j for j, a in enumerate(A)]
dp2 = [a for a in a_2]
for i in range(n-2, -1, -1):
dp2[i] = max(dp2[i+1], a_2[i+1])
dp1 = [a for a in a_1]
dp1[0] += dp2[0]
for i in range(1, n-1):
dp1[i] = max(a_1[i]+dp2[i], dp1[i-1])
return dp1[-2]
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
point = (A[0], 0)
best_val = A[0]
best_ind = 0
max_score = 0
for ind in range(1, len(A)):
max_score = max(max_score, best_val+A[ind]+best_ind-ind)
if A[ind]+ind-best_ind >= best_val:
best_val = A[ind]
best_ind = ind
return max_score
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
d = A[1]+A[0]-1
m = A[0]
for j in range(1, len(A)):
if m+ A[j] - j > d:
d = m+A[j]-j
if A[j]+j >= m:
m = A[j]+j
return d
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
if len(A) <= 1:
return 0
max_A_plus_i = max(A[0], A[1]+1)
max_score = A[0] + A[1] - 1
for i in range(2, len(A)):
max_score = max(max_score, max_A_plus_i + A[i] - i)
max_A_plus_i = max(max_A_plus_i, A[i] + i)
return max_score
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
res = 0
keep = (A[0], 0)
for j in range(1, len(A)):
res = max(res, keep[0] + A[j] + keep[1] - j)
if A[j] >= keep[0] or keep[0] - A[j] < j - keep[1]:
keep = (A[j], j)
# print(j, keep, res)
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
cur=res=0
for i,a in enumerate(A):
print(res, cur)
res = max(res, cur + a - i)
cur = max(cur, a + i)
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
st = []
ret = 0
for i, s in enumerate(A):
while len(st) > 0 and st[-1][0] < s:
s_j, j = st.pop()
ret = max(ret, s+s_j-abs(i-j))
if len(st) > 0:
ret = max(ret, s+st[-1][0]-abs(st[-1][1]-i))
st.append((s, i))
return ret
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
#
# @lc app=leetcode id=1014 lang=python3
#
# [1014] Best Sightseeing Pair
#
# https://leetcode.com/problems/best-sightseeing-pair/description/
#
# algorithms
# Medium (50.18%)
# Likes: 266
# Dislikes: 15
# Total Accepted: 10.6K
# Total Submissions: 21K
# Testcase Example: '[8,1,5,2,6]'
#
# Given an array A of positive integers, A[i] represents the value of the i-th
# sightseeing spot, and two sightseeing spots i and j have distance j - i
# between them.
#
# The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) :
# the sum of the values of the sightseeing spots, minus the distance between
# them.
#
# Return the maximum score of a pair of sightseeing spots.
#
#
#
# Example 1:
#
#
# Input: [8,1,5,2,6]
# Output: 11
# Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
#
#
#
#
# Note:
#
#
# 2 <= A.length <= 50000
# 1 <= A[i] <= 1000
#
#
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
# Count the current best score in all previous sightseeing spot.
# Note that, as we go further, the score of previous spot decrement.
cur = res = 0
for a in A:
res = max(res, cur + a)
cur = max(cur, a) - 1
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
maxa = [x+i for i, x in enumerate(A)]
for i in range(1, len(maxa)): maxa[i] = max(maxa[i], maxa[i-1])
res = float('-inf')
for j in range(1, len(A)):
res = max(res, A[j] - j + maxa[j-1])
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
m = -float('inf')
res = -float('inf')
for i in range(len(A)-1,-1,-1):
res = max(res,A[i]+i+m)
m = max(m,A[i]-i)
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
N = len(A)
max_ = 0
prev = A[-1]-N+1
for i in range(N-2,-1,-1):
max_ = max(max_, prev + A[i]+i)
prev = max(prev, A[i]-i)
return max_
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
res = float('-inf')
maxleft = float('-inf')
for i, a in enumerate(A):
res = max(res, maxleft + a - i)
maxleft = max(maxleft, a + i)
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
# generate a vector
#A_left = [a + i for i, a in enumerate(A)]
#A_right = [a - i for i, a in enumerate(A)]
_max_left = A[0]
_max = 0
for j in range(1, len(A)):
_max = max(_max_left + A[j] - j, _max)
_max_left = max(_max_left, A[j] + j)
return _max
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, a: List[int]) -> int:
d=[]
for i in range(len(a)-1,-1,-1):
if i!=0:
d.append([a[i]-i,i])
d.sort()
s=0
for i in range(0,len(a)-1):
while d[-1][1]<=i:
d.pop()
s=max(s,a[i]+i+d[-1][0])
return s
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, a: List[int]) -> int:
d=[]
for i in range(len(a)-1,-1,-1):
if i!=0:
d.append([a[i]-i,i])
d.sort()
s=0
for i in range(0,len(a)):
while d!=[] and d[-1][1]<=i:
d.pop()
if d==[]:
break
s=max(s,a[i]+i+d[-1][0])
return s
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
import heapq
import math
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
h = []
ans = -math.inf
for i,a in enumerate(A):
if h:
ans = max(ans, a-i-h[0])
heapq.heappush(h, -i-a)
return ans
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
prev=ans=float('-inf')
for i in range(1,len(A)):
prev=max(prev,A[i-1]+i-1)
ans=max(ans,prev+A[i]-i)
return ans
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
# generate a vector
A_left = [a + i for i, a in enumerate(A)]
A_right = [a - i for i, a in enumerate(A)]
_max_left = A_left[0]
_max = 0
for j in range(1, len(A_right)):
_max = max(_max_left + A_right[j], _max)
_max_left = max(_max_left, A_left[j])
return _max
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
left = 0
res = 0
max_pre = 0
curr_res = 0
for right in range(1, len(A)):
max_pre = max(max_pre, A[right-1] + (right-1))
curr_res = max_pre + A[right] - right
res = max(res, curr_res)
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
res, leftmax, cnt = 0, 0, 0
for num in A:
res = max(res, num + leftmax - cnt)
if num > leftmax - cnt:
leftmax = num
cnt = 0
cnt += 1
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
ans = 0
best = A[0]-1
for i in range(1, len(A)):
ans = max(ans, A[i]+best)
best = max(best-1, A[i]-1)
return ans
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
best = 0
i = 0
for j in range(1, len(A)):
s1 = A[i] + A[j] + i - j
s2 = A[j] + A[j-1] + j - 1 - j
if s2 > s1:
i = j - 1
best = max(best, s1, s2)
return best
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
# maxScore = 0
# for i in range(0,len(A)):
# for j in range(i+1,len(A)):
# val = A[i] + A[j] + i - j
# if val > maxScore:
# maxScore = val
# return maxScore
K = A[0]
best = float('-inf')
for j in range(1,len(A)):
x = A[j]
best = max(best, K + x - j )
K = max(K , x + j )
return best
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
max_score = 0
scores = [0] *len(A)
for i in range (1,len(A)):
if i == 1:
scores[i] = A[0] - (len(A) - (i - 1))
else:
scores[i] = max(A[i - 1] - (len(A) - (i - 1)),scores[i-1])
for j in range (1,len(A)):
adj_score = (len(A) - j) + scores[j] + A[j]
if adj_score > max_score:
max_score = adj_score
return max_score
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
m = p = 0
for i, n in enumerate(A):
m = max(m, p + n - i)
p = max(p, n + i)
return m
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
left = [i+a for i,a in enumerate(A)]
right = [b-j for j, b in enumerate(A)]
for i in range(1, len(left)):
left[i] = max(left[i], left[i-1])
for i in range(len(right)-1)[::-1]:
right[i] = max(right[i], right[i+1])
return max(l+r for l, r in zip(left[:-1], right[1:]))
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
import heapq
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
return solution(A)
def solution(A):
right_heap = [(-(a - i), a, i) for i, a in enumerate(A)]
heapq.heapify(right_heap)
current_max = -1
for i in range(len(A) - 1):
right_max_score = -1
while len(right_heap) > 0 and right_heap[0][2] <= i:
heapq.heappop(right_heap)
el = right_heap[0]
right_max_score = A[i] + el[1] - abs(el[2] - i)
current_max = max(right_max_score, current_max)
return current_max
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
cur = res = 0
for a in A:
res = max(res, cur + a)
cur = max(cur, a) - 1
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
return self.with_sort(A)
def with_sort(self, A):
n = len(A)
max_list = [(i[0] + i[1], i[0]) for i in sorted(enumerate(A),
key=lambda x:-(x[1] + x[0] * 1.000000001))]
idx = 0
max_s = 0
for j in range(n-1, -1, -1):
s = A[j] - j
while idx < n and max_list[idx][1] >= j:
idx += 1
if idx < n:
s += max_list[idx][0]
max_s = max(max_s, s)
return max_s
def count_sort(self, A):
C = [[] for _ in range(51001)]
for i, a in enumerate(A):
C[a + i].append((a+i, i))
res = []
k = 0
for c in C:
if len(c) > 0:
res.extend(c)
return list(reversed(res))
def brute_force(self, A):
max_score = 0
n = len(A)
for i in range(n):
for j in range(i+1, n):
s = A[i] + A[j] + i - j
if s > max_score:
max_score = s
return max_score
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
if len(A) < 2:
return -1
best_so_far = A[0] + 0
res = 0
for j in range(1, len(A)):
res = max(res, best_so_far + A[j] - j)
if best_so_far < A[j] + j:
best_so_far = A[j] + j
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
res = imax = 0
for i, a in enumerate(A):
res = max(res, imax + a - i)
imax = max(imax, a + i)
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
if len(A) <= 1:
return -float('inf')
candidate = max(A[0], A[1]+1)
res = A[0]+A[1]-1
for i in range(2, len(A)):
res = max(res, candidate+A[i]-i)
candidate = max(candidate, A[i]+i)
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
import sys
val = A[0] + 0
ans = -sys.maxsize
A.pop(0)
for i,num in enumerate(A):
ans = max(ans,val+A[i] - i - 1)
val = max(val,A[i]+i+1)
return ans
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
if not A:
return 0
temp=A[0]
ans=-999
for i in range(1, len(A)):
ans=max(ans, temp+A[i]-i)
temp=max(temp, A[i]+i)
return ans
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
largest = 0
i = 0
while(i<len(A)-1):
largest = max(largest,A[i]+A[i+1]-1)
if A[i]>A[i+1]:
A[i],A[i+1]=A[i+1]+1,A[i]-1
i += 1
return largest
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
i = 0
res = 0
for j in range(1, len(A)):
ti = A[i] + i
res = max(A[j] - j + ti, res)
if A[j] + j > ti:
i = j
return res
|
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
Return the maximum score of a pair of sightseeing spots.
Example 1:
Input: [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
Note:
2 <= A.length <= 50000
1 <= A[i] <= 1000
|
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
# we have to find A[i] + i + A[j] - j
# we will fix max(A[i] + i) and find for diff values of A[j] - j
if len(A) == 0 : return 0
maxI = A[0] + 0
res = 0
for j in range(1,len(A)):
res = max(res,maxI+A[j]-j)
maxI = max(maxI,A[j]+j)
return res
# for i in range(len(A)-1):
# for j in range(i+1,len(A)):
# score = max(score,A[i] + A[j] + i - j)
# return score
|
A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits and all we know is that all integers in the array were in the range [1, k] and there are no leading zeros in the array.
Given the string s and the integer k. There can be multiple ways to restore the array.
Return the number of possible array that can be printed as a string s using the mentioned program.
The number of ways could be very large so return it modulo 10^9 + 7
Example 1:
Input: s = "1000", k = 10000
Output: 1
Explanation: The only possible array is [1000]
Example 2:
Input: s = "1000", k = 10
Output: 0
Explanation: There cannot be an array that was printed this way and has all integer >= 1 and <= 10.
Example 3:
Input: s = "1317", k = 2000
Output: 8
Explanation: Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]
Example 4:
Input: s = "2020", k = 30
Output: 1
Explanation: The only possible array is [20,20]. [2020] is invalid because 2020 > 30. [2,020] is ivalid because 020 contains leading zeros.
Example 5:
Input: s = "1234567890", k = 90
Output: 34
Constraints:
1 <= s.length <= 10^5.
s consists of only digits and doesn't contain leading zeros.
1 <= k <= 10^9.
|
class Solution:
def numberOfArrays(self, s: str, k: int) -> int:
dp = [-1] * len(s)
return self.dfs(s, k, 0, dp)
def dfs(self, s: str, k: int, start: int, dp: List[int]) -> int:
if start == len(s):
return 1
if s[start] == '0':
return 0
if dp[start] != -1:
return dp[start]
res, num = 0, 0
for i in range(start, len(s)):
num = num * 10 + (ord(s[i]) - ord('0'))
if num > k:
break
res += self.dfs(s, k, i + 1, dp)
res %= 10**9 + 7
dp[start] = res
return res
|
A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits and all we know is that all integers in the array were in the range [1, k] and there are no leading zeros in the array.
Given the string s and the integer k. There can be multiple ways to restore the array.
Return the number of possible array that can be printed as a string s using the mentioned program.
The number of ways could be very large so return it modulo 10^9 + 7
Example 1:
Input: s = "1000", k = 10000
Output: 1
Explanation: The only possible array is [1000]
Example 2:
Input: s = "1000", k = 10
Output: 0
Explanation: There cannot be an array that was printed this way and has all integer >= 1 and <= 10.
Example 3:
Input: s = "1317", k = 2000
Output: 8
Explanation: Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]
Example 4:
Input: s = "2020", k = 30
Output: 1
Explanation: The only possible array is [20,20]. [2020] is invalid because 2020 > 30. [2,020] is ivalid because 020 contains leading zeros.
Example 5:
Input: s = "1234567890", k = 90
Output: 34
Constraints:
1 <= s.length <= 10^5.
s consists of only digits and doesn't contain leading zeros.
1 <= k <= 10^9.
|
class Solution:
def numberOfArrays(self, s: str, k: int) -> int:
n = len(s)
b = ord('0')
MOD = 10 ** 9 + 7
dp = collections.deque([0] * 10)
dp[0] = 1
for i in range(n - 1, -1, -1):
if s[i] == '0':
dp.pop()
dp.appendleft(0)
continue
num = 0
cnt = 0
for j in range(i, n):
num = num * 10 + ord(s[j]) - b
if num <= k:
cnt += dp[j - i] % MOD
else:
break
dp.appendleft(cnt % MOD)
dp.pop()
return dp[0]
|
A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits and all we know is that all integers in the array were in the range [1, k] and there are no leading zeros in the array.
Given the string s and the integer k. There can be multiple ways to restore the array.
Return the number of possible array that can be printed as a string s using the mentioned program.
The number of ways could be very large so return it modulo 10^9 + 7
Example 1:
Input: s = "1000", k = 10000
Output: 1
Explanation: The only possible array is [1000]
Example 2:
Input: s = "1000", k = 10
Output: 0
Explanation: There cannot be an array that was printed this way and has all integer >= 1 and <= 10.
Example 3:
Input: s = "1317", k = 2000
Output: 8
Explanation: Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]
Example 4:
Input: s = "2020", k = 30
Output: 1
Explanation: The only possible array is [20,20]. [2020] is invalid because 2020 > 30. [2,020] is ivalid because 020 contains leading zeros.
Example 5:
Input: s = "1234567890", k = 90
Output: 34
Constraints:
1 <= s.length <= 10^5.
s consists of only digits and doesn't contain leading zeros.
1 <= k <= 10^9.
|
class Solution:
def numberOfArrays(self, s: str, k: int) -> int:
MOD = 10 ** 9 + 7
n = len(s)
@lru_cache(None)
def dp(i):
if i == n:
return 1
if s[i] == '0':
return 0
num = 0
ans = 0
for j in range(i, n):
num = num * 10 + ord(s[j]) - ord('0')
if num > k:
break
ans += dp(j+1)
return ans % MOD
return dp(0)
|
A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits and all we know is that all integers in the array were in the range [1, k] and there are no leading zeros in the array.
Given the string s and the integer k. There can be multiple ways to restore the array.
Return the number of possible array that can be printed as a string s using the mentioned program.
The number of ways could be very large so return it modulo 10^9 + 7
Example 1:
Input: s = "1000", k = 10000
Output: 1
Explanation: The only possible array is [1000]
Example 2:
Input: s = "1000", k = 10
Output: 0
Explanation: There cannot be an array that was printed this way and has all integer >= 1 and <= 10.
Example 3:
Input: s = "1317", k = 2000
Output: 8
Explanation: Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]
Example 4:
Input: s = "2020", k = 30
Output: 1
Explanation: The only possible array is [20,20]. [2020] is invalid because 2020 > 30. [2,020] is ivalid because 020 contains leading zeros.
Example 5:
Input: s = "1234567890", k = 90
Output: 34
Constraints:
1 <= s.length <= 10^5.
s consists of only digits and doesn't contain leading zeros.
1 <= k <= 10^9.
|
class Solution:
def numberOfArrays(self, S: str, K: int) -> int:
import sys
sys.setrecursionlimit(10**9+7)
@lru_cache(None)
def ways(idx):
if idx == len(S): return 1
if S[idx] == '0': return 0
ans = 0
num = 0
for i in range(idx, len(S)):
num = num*10 + (ord(S[i]) - ord('0'))
if num > K: break
ans = (ans + ways(i+1)) % 1000000007
return ans
return ways(0)
|
A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits and all we know is that all integers in the array were in the range [1, k] and there are no leading zeros in the array.
Given the string s and the integer k. There can be multiple ways to restore the array.
Return the number of possible array that can be printed as a string s using the mentioned program.
The number of ways could be very large so return it modulo 10^9 + 7
Example 1:
Input: s = "1000", k = 10000
Output: 1
Explanation: The only possible array is [1000]
Example 2:
Input: s = "1000", k = 10
Output: 0
Explanation: There cannot be an array that was printed this way and has all integer >= 1 and <= 10.
Example 3:
Input: s = "1317", k = 2000
Output: 8
Explanation: Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]
Example 4:
Input: s = "2020", k = 30
Output: 1
Explanation: The only possible array is [20,20]. [2020] is invalid because 2020 > 30. [2,020] is ivalid because 020 contains leading zeros.
Example 5:
Input: s = "1234567890", k = 90
Output: 34
Constraints:
1 <= s.length <= 10^5.
s consists of only digits and doesn't contain leading zeros.
1 <= k <= 10^9.
|
class Solution:
def numberOfArrays(self, s: str, k: int) -> int:
n = len(s)
b = ord('0')
MOD = 10 ** 9 + 7
@lru_cache(None)
def dp(i):
if i == n:
return 1
if s[i] == '0':
return 0
cnt = 0
num = ord(s[i]) - b
while num <= k:
cnt += dp(i + 1) % MOD
i += 1
if i < n:
num = num * 10 + ord(s[i]) - b
else:
break
return cnt % MOD
return dp(0)
|
A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits and all we know is that all integers in the array were in the range [1, k] and there are no leading zeros in the array.
Given the string s and the integer k. There can be multiple ways to restore the array.
Return the number of possible array that can be printed as a string s using the mentioned program.
The number of ways could be very large so return it modulo 10^9 + 7
Example 1:
Input: s = "1000", k = 10000
Output: 1
Explanation: The only possible array is [1000]
Example 2:
Input: s = "1000", k = 10
Output: 0
Explanation: There cannot be an array that was printed this way and has all integer >= 1 and <= 10.
Example 3:
Input: s = "1317", k = 2000
Output: 8
Explanation: Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]
Example 4:
Input: s = "2020", k = 30
Output: 1
Explanation: The only possible array is [20,20]. [2020] is invalid because 2020 > 30. [2,020] is ivalid because 020 contains leading zeros.
Example 5:
Input: s = "1234567890", k = 90
Output: 34
Constraints:
1 <= s.length <= 10^5.
s consists of only digits and doesn't contain leading zeros.
1 <= k <= 10^9.
|
class Solution:
def numberOfArrays(self, s: str, k: int) -> int:
import sys
sys.setrecursionlimit(10**9+7)
@lru_cache(None)
def ways(idx):
if idx == len(s): return 1
if s[idx] == '0': return 0
ans = 0
num = 0
for i in range(idx, len(s)):
num = num*10 + (ord(s[i]) - ord('0'))
if num > k: break
ans = (ans + ways(i+1)) % 1000000007
return ans
return ways(0)
|
A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits and all we know is that all integers in the array were in the range [1, k] and there are no leading zeros in the array.
Given the string s and the integer k. There can be multiple ways to restore the array.
Return the number of possible array that can be printed as a string s using the mentioned program.
The number of ways could be very large so return it modulo 10^9 + 7
Example 1:
Input: s = "1000", k = 10000
Output: 1
Explanation: The only possible array is [1000]
Example 2:
Input: s = "1000", k = 10
Output: 0
Explanation: There cannot be an array that was printed this way and has all integer >= 1 and <= 10.
Example 3:
Input: s = "1317", k = 2000
Output: 8
Explanation: Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]
Example 4:
Input: s = "2020", k = 30
Output: 1
Explanation: The only possible array is [20,20]. [2020] is invalid because 2020 > 30. [2,020] is ivalid because 020 contains leading zeros.
Example 5:
Input: s = "1234567890", k = 90
Output: 34
Constraints:
1 <= s.length <= 10^5.
s consists of only digits and doesn't contain leading zeros.
1 <= k <= 10^9.
|
class Solution:
def numberOfArrays(self, s: str, k: int) -> int:
dp = [-1] * len(s)
return self.dfs(s, k, 0, dp)
def dfs(self, s: str, k: int, start: int, dp: List[int]) -> int:
if start == len(s):
return 1
if s[start] == '0':
return 0
if dp[start] != -1:
return dp[start]
res, num = 0, 0
for i in range(start, len(s)):
num = num * 10 + (ord(s[i]) - ord('0'))
if num > k:
break
res += self.dfs(s, k, i + 1, dp)
res %= 10**9 + 7
dp[start] = res
return res
|
A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits and all we know is that all integers in the array were in the range [1, k] and there are no leading zeros in the array.
Given the string s and the integer k. There can be multiple ways to restore the array.
Return the number of possible array that can be printed as a string s using the mentioned program.
The number of ways could be very large so return it modulo 10^9 + 7
Example 1:
Input: s = "1000", k = 10000
Output: 1
Explanation: The only possible array is [1000]
Example 2:
Input: s = "1000", k = 10
Output: 0
Explanation: There cannot be an array that was printed this way and has all integer >= 1 and <= 10.
Example 3:
Input: s = "1317", k = 2000
Output: 8
Explanation: Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]
Example 4:
Input: s = "2020", k = 30
Output: 1
Explanation: The only possible array is [20,20]. [2020] is invalid because 2020 > 30. [2,020] is ivalid because 020 contains leading zeros.
Example 5:
Input: s = "1234567890", k = 90
Output: 34
Constraints:
1 <= s.length <= 10^5.
s consists of only digits and doesn't contain leading zeros.
1 <= k <= 10^9.
|
class Solution:
def numberOfArrays(self, s: str, k: int) -> int:
cache = {}
n = len(s)
mod = 10 ** 9 + 7
def process(i):
if i == n:
return 1
else:
if s[i] == '0':
return 0
else:
if not i in cache:
ans = 0
x = 0
j = 0
while i+j < n:
x = x * 10 + (ord(s[i+j]) - 48)
if x <= k:
ans += process(i+j+1)
j += 1
else:
break
cache[i] = ans % mod
return cache[i]
return process(0)
|
A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits and all we know is that all integers in the array were in the range [1, k] and there are no leading zeros in the array.
Given the string s and the integer k. There can be multiple ways to restore the array.
Return the number of possible array that can be printed as a string s using the mentioned program.
The number of ways could be very large so return it modulo 10^9 + 7
Example 1:
Input: s = "1000", k = 10000
Output: 1
Explanation: The only possible array is [1000]
Example 2:
Input: s = "1000", k = 10
Output: 0
Explanation: There cannot be an array that was printed this way and has all integer >= 1 and <= 10.
Example 3:
Input: s = "1317", k = 2000
Output: 8
Explanation: Possible arrays are [1317],[131,7],[13,17],[1,317],[13,1,7],[1,31,7],[1,3,17],[1,3,1,7]
Example 4:
Input: s = "2020", k = 30
Output: 1
Explanation: The only possible array is [20,20]. [2020] is invalid because 2020 > 30. [2,020] is ivalid because 020 contains leading zeros.
Example 5:
Input: s = "1234567890", k = 90
Output: 34
Constraints:
1 <= s.length <= 10^5.
s consists of only digits and doesn't contain leading zeros.
1 <= k <= 10^9.
|
MOD = int(1e9 + 7)
class Solution:
def numberOfArrays(self, s: str, k: int) -> int:
dp = [-1 for _ in s]
n = len(s)
def recurse(i: int) -> int:
if i == n:
return 1
if dp[i] != -1:
return dp[i]
result = 0
val = 0
for j in range(i, n):
val = 10 * val + (ord(s[j]) - ord('0'))
if val > k:
break
elif j == n - 1 or s[j + 1] != '0':
result = (result + recurse(j + 1)) % MOD
dp[i] = result
return result
return recurse(0)
|
You are given a string expression representing a Lisp-like expression to return the integer value of.
The syntax for these expressions is given as follows.
An expression is either an integer, a let-expression, an add-expression, a mult-expression, or an assigned variable. Expressions always evaluate to a single integer.
(An integer could be positive or negative.)
A let-expression takes the form (let v1 e1 v2 e2 ... vn en expr), where let is always the string "let", then there are 1 or more pairs of alternating variables and expressions, meaning that the first variable v1 is assigned the value of the expression e1, the second variable v2 is assigned the value of the expression e2, and so on sequentially; and then the value of this let-expression is the value of the expression expr.
An add-expression takes the form (add e1 e2) where add is always the string "add", there are always two expressions e1, e2, and this expression evaluates to the addition of the evaluation of e1 and the evaluation of e2.
A mult-expression takes the form (mult e1 e2) where mult is always the string "mult", there are always two expressions e1, e2, and this expression evaluates to the multiplication of the evaluation of e1 and the evaluation of e2.
For the purposes of this question, we will use a smaller subset of variable names. A variable starts with a lowercase letter, then zero or more lowercase letters or digits. Additionally for your convenience, the names "add", "let", or "mult" are protected and will never be used as variable names.
Finally, there is the concept of scope. When an expression of a variable name is evaluated, within the context of that evaluation, the innermost scope (in terms of parentheses) is checked first for the value of that variable, and then outer scopes are checked sequentially. It is guaranteed that every expression is legal. Please see the examples for more details on scope.
Evaluation Examples:
Input: (add 1 2)
Output: 3
Input: (mult 3 (add 2 3))
Output: 15
Input: (let x 2 (mult x 5))
Output: 10
Input: (let x 2 (mult x (let x 3 y 4 (add x y))))
Output: 14
Explanation: In the expression (add x y), when checking for the value of the variable x,
we check from the innermost scope to the outermost in the context of the variable we are trying to evaluate.
Since x = 3 is found first, the value of x is 3.
Input: (let x 3 x 2 x)
Output: 2
Explanation: Assignment in let statements is processed sequentially.
Input: (let x 1 y 2 x (add x y) (add x y))
Output: 5
Explanation: The first (add x y) evaluates as 3, and is assigned to x.
The second (add x y) evaluates as 3+2 = 5.
Input: (let x 2 (add (let x 3 (let x 4 x)) x))
Output: 6
Explanation: Even though (let x 4 x) has a deeper scope, it is outside the context
of the final x in the add-expression. That final x will equal 2.
Input: (let a1 3 b2 (add a1 1) b2)
Output 4
Explanation: Variable names can contain digits after the first character.
Note:
The given string expression is well formatted: There are no leading or trailing spaces, there is only a single space separating different components of the string, and no space between adjacent parentheses. The expression is guaranteed to be legal and evaluate to an integer.
The length of expression is at most 2000. (It is also non-empty, as that would not be a legal expression.)
The answer and all intermediate calculations of that answer are guaranteed to fit in a 32-bit integer.
|
class Solution(object):
def parse(self,expression,d,i):
count = 0
start = i
if expression[i] == "(":
count += 1
i += 1
while count != 0:
if expression[i] == "(":
count += 1
elif expression[i] == ")":
count -= 1
i += 1
val = self.evaluate(expression[start:i],d)
else:
while i < len(expression) and expression[i] != " " and expression[i] != ")":
i += 1
val = expression[start:i]
if self.isnumber(val):
val = int(val)
return i,val
def get_left_right(self,expression,d):
i = 0
count = 0
i,left = self.parse(expression,d,0)
if i == len(expression) or expression[i] == ")":
return left,None,i
i += 1
i,right = self.parse(expression,d,i)
return left,right,i
def isnumber(self,s):
for c in s:
if ord("0") <= ord(c) <= ord("9") or c == "+" or c == "-":
continue
else:
return False
return True
def evaluate(self, expression,d = {}):
"""
:type expression: str
:rtype: int
"""
if self.isnumber(expression):
return int(expression)
newd = {}
for key in d:
newd[key] = d[key]
expression = expression[1:len(expression)-1]
oper = ""
if expression[0:3] == "add" or expression[:3] == "let":
oper = expression[0:3]
expression = expression[4:]
else:
oper = "mult"
expression = expression[5:]
if oper == "mult" or oper == "add":
left,right,_ = self.get_left_right(expression,newd)
if isinstance(left,str):
left = newd[left]
if isinstance(right,str):
right = newd[right]
if oper == "mult":
return left*right
else:
return left + right
i = 0
while True:
left,right,i = self.get_left_right(expression,newd)
expression = expression[i+1:]
if right == None:
if isinstance(left,str):
return newd[left]
return left
if isinstance(right,str):
right = newd[right]
newd[left] = right
# s = Solution()
# print(s.evaluate("(let x 2 (mult x (let x 3 y 4 (add x y))))"))
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
dp = [0] + [-1 for i in range(days[-1])]
for day in days:
dp[day] = 0
for i in range(1, len(dp)):
if dp[i] == -1:
dp[i] = dp[i-1]
else:
dp[i] = min(
dp[i-1] + costs[0],
dp[max(i-7, 0)] + costs[1],
dp[max(i-30, 0)] + costs[2],
)
return dp[-1]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
dp = [0] * (days[-1] + 1)
pos = 0
for i in range(1, days[-1] + 1):
if i == days[pos]:
d1 = i - 1
d2 = i - 7 if i - 7 > 0 else 0
d3 = i - 30 if i - 30 > 0 else 0
dp[i] = min(dp[d1] + costs[0], dp[d2] + costs[1], dp[d3] + costs[2])
pos += 1
else:
dp[i] = dp[i - 1]
return dp[-1]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
@lru_cache(None)
def recur(day):
if day > 365:
return 0
elif day in days:
ret = math.inf
for c, d in zip(costs, [1,7,30]):
ret = min(ret, c + recur(day + d))
return ret
else:
return recur(day + 1)
return recur(0)
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
n = len(days)
m = len(costs)
ranges = [1, 7, 30]
dp = [float('inf')] * (n+1)
dp[0] = 0
#print(f\"n={n} m={m} dp={dp}\")
for j in range(1, n+1):
for i in range(1, m+1):
for k in range(j, n+1):
if days[k-1] - days[j-1] >= ranges[i-1]:
break
#print(f\"k={k} i={i} j={j}\")
dp[k] = min(dp[k], costs[i-1] + dp[j-1])
return dp[n]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, arr: List[int], costs: List[int]) -> int:
dp = [float('inf')]*(len(arr)+1)
dp[0] = 0
for i in range(len(arr)):
j = i
prev = dp[i]
while j < len(arr) and arr[j] < arr[i] + 30:
if arr[j] == arr[i]:
dp[j+1] = min(dp[j+1],prev+costs[0],prev+costs[1],prev+costs[2])
elif arr[j] - arr[i] < 7:
dp[j+1] = min(dp[j+1],prev+costs[1],prev+costs[2])
elif arr[j] - arr[i] < 30:
dp[j+1] = min(dp[j+1],prev+costs[2])
j += 1
print(dp)
return dp[-1]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
min_costs = [-1 for i in range(days[-1])]
if 1 in days:
min_costs[0] = min(costs)
else:
min_costs[0] = 0
return self.helper(days, costs, min_costs, days[-1] - 1)
def helper(self, days: List[int], costs: List[int], min_costs: List[int], i) -> int:
if i == 0:
return min_costs[0]
if i < 0:
return 0
if i + 1 not in days:
min_costs[i] = self.helper(days, costs, min_costs, i-1)
if min_costs[i] != -1:
return min_costs[i]
c_1 = self.helper(days, costs, min_costs, i-1)
c_7 = self.helper(days, costs, min_costs, i-7)
c_30 = self.helper(days, costs, min_costs, i-30)
min_costs[i] = min(c_1 + costs[0], c_7 + costs[1], c_30 + costs[2])
return min_costs[i]
# Brute force: branch for each ticket type: O(3^n)
# min_cost(i) = minimum cost of traveling i days. (1-indexed).
# min_cost(i) = min(
# (min_cost(i-1) + costs[0]),
# (min_cost(i-1) + costs[1]),
# (min_cost(i-1) + costs[2]))
'''
min_cost(1) = min(costs[0], costs[1], costs[2])
min_cost(i) = min cost of traveling up to the days[i]th day.
min_cost(0) = min(costs[0], costs[1], costs[2])
min_cost(i < 0) = min(costs[0], costs[1], costs[2])
min_cost(i) = (min_cost(i-1) + costs[0], min_cost(i-7) + costs[1], min_cost(i-30) + costs[2])
min_cost(i) = minimum cost of traveling i days, (1-indexed).
min_cost(1) = min(costs[0], costs[1], costs[2])
min_cost(i < 1) = min(costs[0], costs[1], costs[2]) ???
min_cost(i) = (min_cost(i-1) + costs[0], min_cost(i-7) + costs[1], min_cost(i-30) + costs[2])
**** This doesn't account for the case where a longer pass may be cheaper.
'''
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
dp = [0] * (days[-1] + 1)
for day in range(0, days[-1] + 1):
if day not in days:
dp[day] = dp[max(0, day - 1)]
else:
dp[day] = min(dp[max(0, day - 1)] + costs[0], dp[max(0, day - 7)] + costs[1], dp[max(0, day - 30)] + costs[2])
return dp[-1]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
dp = [float('inf')] * (len(days) + 1)
dp[0] = 0
for i in range(1, len(days) + 1):
j = 0
while i + j < len(dp):
if days[i + j - 1] <= days[i - 1]:
dp[i + j] = min(dp[i + j], dp[i - 1] + costs[0])
if days[i + j - 1] <= days[i - 1] + 6:
dp[i + j] = min(dp[i + j], dp[i - 1] + costs[1])
if days[i + j - 1] <= days[i - 1] + 29:
dp[i + j] = min(dp[i + j], dp[i - 1] + costs[2])
else:
break
j += 1
return dp[-1]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
def prev(x,day,dp):
ans=0
for i in day:
if i<=x:
# print(dp[i],i,x)
ans = dp[i]
else:
break
return ans
class Solution:
def mincostTickets(self, day: List[int], cos: List[int]) -> int:
dp=[10000000]*(370)
tmp=[0]*32
tmp[1]=cos[0]
tmp[7]=cos[1]
tmp[30]=cos[2]
day.sort()
n = len(day)
for i in range(n):
d = day[i]
for j in [1,7,30]:
ab = prev(max(0,d-j),day,dp) + tmp[j]
#print(ab)
dp[d] = min(dp[d] , ab)
#print(dp[200:])
return dp[day[-1]]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
dp = [0] * (days[-1]+1)
dp[0] = 0
for j in range(len(days)):
i = days[j]
for k in range(1,i+1):
if k == i:
if k-7 >= 0 and k-30 >= 0:
dp[k] = min(dp[k-1]+costs[0], dp[k-7]+costs[1], dp[k-30]+costs[2])
elif k-7 >= 0:
dp[k] = min(dp[k-1]+costs[0], dp[k-7]+costs[1], costs[2])
else:
dp[k] = min(dp[k-1]+costs[0], costs[1], costs[2])
else:
if k != 1:
if dp[k] == 0:
dp[k] = dp[k-1]
return dp[-1]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
'''
days = [1,4,6,7,8,20]
dp = 11 9 8 6 4 2
2 7 2
'''
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
# if costs = [7,2,15] change to [2,2,15]
for i in range(1,-1,-1):
if costs[i] > costs[i+1]: costs[i] = costs[i+1]
N = len(days)
dp = [0 for _ in range(N+1)]
dp[-2] = costs[0]
dp[-1] = 0 # trick to avoid list index out of bounds
for i in range(N-2,-1,-1):
dp[i] = costs[0] + dp[i+1]
for j in range(i+1,N):
if days[j] - days[i] < 7:
dp[i] = min(dp[i], costs[1] + dp[j+1])
elif days[j] - days[i] < 30:
dp[i] = min(dp[i], costs[2] + dp[j+1])
return dp[0]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
dp = [float('inf') for _ in range(days[-1] + 1)]
dp[0] = 0
set_days = set(days)
for i in range(1, len(dp)):
if i not in set_days:
dp[i] = dp[i - 1]
else:
dp[i] = min(dp[max(0, i - 1)] + costs[0],
dp[max(0, i - 7)] + costs[1],
dp[max(0, i - 30)] + costs[2])
return dp[-1]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
k = len(costs)
n = len(days)
dp = [[0] * (k+1) for _ in range(n)]
for i in range(n):
dp[i][0] = float('inf')
for d in range(i):
if days[i] - days[d] < 30:
dp[i][0] = min(dp[i][0], dp[d][3])
if days[i] - days[d] < 7:
dp[i][0] = min(dp[i][0], dp[d][2])
for j in range(1, k+1):
if i == 0:
dp[i][j] = costs[j-1]
else:
dp[i][j] = costs[j-1] + min(dp[i-1])
return min(dp[-1])
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
n = len(days)
if not days:
return 0
total = [float('inf') for _ in range(n)]
total[0] = min(costs)
for i in range(1,n):
curday = days[i]
total[i] = total[i-1]+min(costs)
for j in range(i-1, -1, -1):
diff = curday-days[j]
if diff < 7:
if j>0:
total[i] = min(total[i], total[j-1]+costs[1])
else:
total[i] = min(total[i], costs[1])
if diff < 30:
if j>0:
total[i] = min(total[i], total[j-1]+costs[2])
else:
total[i] = min(total[i], costs[2])
else:
break
return total[-1]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
from collections import defaultdict
class Solution:
def mincostTickets(self, days: List[int], cost: List[int]) -> int:
dp = defaultdict(int)
days = set(days)
for i in range(365, 0, -1):
if i in days:
dp[i] = min(dp[i+1] + cost[0], dp[i+7] + cost[1], dp[i+30] + cost[2])
else:
dp[i] = dp[i+1]
return dp[1]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
dp = [float('inf') for d in range(days[-1]+1)]
dp[0] = 0
pass_num_days = [1, 7, 30]
days_set = set(days)
for day in range(days[-1]+1):
if day not in days_set:
dp[day] = dp[max(0, day-1)]
else:
for num_days, cost in zip(pass_num_days, costs):
dp[day] = min(dp[day], dp[max(0, day-num_days)] + cost)
print(dp)
return dp[-1]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
given = {}
for day in days:
given[day] = True
dp = [0 for _ in range(days[-1]+1)]
dp[1] = costs[0]
for day in range(days[-1]+1):
if day not in given:
dp[day] = dp[day-1]
else:
# purchase one day pass
one_day_pass = dp[max(0, day-1)] + costs[0]
# purchase one week pass
one_week_pass = dp[max(0, day-7)] + costs[1]
# purchase one month pass
one_month_pass = dp[max(0,day-30)] + costs[2]
dp[day] = min(one_day_pass, one_week_pass, one_month_pass)
print(dp)
return dp[-1]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
cost = [0 for j in range(366)]
for i in range(len(cost)):
if(i in days):
cost[i] = min((cost[i-1] if i-1>=0 else 0) + costs[0],
(cost[i-7] if i-7>=0 else 0) + costs[1],
(cost[i-30] if i-30>=0 else 0) + costs[2])
else:
cost[i] = cost[max(i-1,0)]
return cost[365]
# cost = [0 for j in range(days)]
# for i, d in enumerate(days):
# if(d <= 7):
# cost[i] = cost[i-1] + c[0] # take day pass
# if(d <= 30): # choose between day pass and month pass
# cost[i] = min(cost[i-1]+c[0], cost)
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
if not days:
return 0
lastday = days[-1]
dp = [0] * (lastday + 1)
for i in range(1, lastday + 1):
if i not in days:
dp[i] = dp[i - 1]
continue
prev_1 = dp[i - 1] if i >= 1 else 0
prev_7 = dp[i - 7] if i >= 7 else 0
prev_30 = dp[i - 30] if i >= 30 else 0
dp[i] = min(prev_1 + costs[0], prev_7 + costs[1], prev_30 + costs[2])
return dp[-1]
|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000
|
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
if not days:
return 0
dp = [0] * len(days)
# given a day, find the dp index should look for
# include start, not include end
def search(start: int, end: int, day:int)-> int:
if start == end - 1:
if day >= days[start]:
return start
else:
return -1
m = int((end - start)/2) + start
if day >= days[m]:
return search(m, end, day)
else:
return search(start, m, day)
m = {day:i for i, day in enumerate(days)}
for i,day in enumerate(days):
prev_1 = day - 1
prev_7 = day - 7
prev_30 = day - 30
c_1 = costs[0]
c_7 = costs[1]
c_30 = costs[2]
if prev_1 in m:
c_1 += dp[m[prev_1]]
elif prev_1 >= 0 and prev_1 >= days[0]:
c_1 += dp[search(0, i, prev_1)]
if prev_7 in m:
c_7 += dp[m[prev_7]]
elif prev_7 >= 0 and prev_7 >= days[0]:
c_7 += dp[search(0, i, prev_7)]
if prev_30 in m:
c_30 += dp[m[prev_30]]
elif prev_30 >= 0 and prev_30 >= days[0]:
c_30 += dp[search(0, i, prev_30)]
dp[i] = min(c_1, c_7, c_30)
return dp[-1]
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.