Skip to content

Commit 6b682f8

Browse files
authored
Create Find the Score of All Prefixes of an Array.py
1 parent 99157b2 commit 6b682f8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
We define the conversion array conver of an array arr as follows:
3+
4+
conver[i] = arr[i] + max(arr[0..i]) where max(arr[0..i]) is the maximum value of arr[j] over 0 <= j <= i.
5+
We also define the score of an array arr as the sum of the values of the conversion array of arr.
6+
7+
Given a 0-indexed integer array nums of length n, return an array ans of length n where ans[i] is the score of the prefix nums[0..i].
8+
'''
9+
10+
11+
12+
class Solution:
13+
def findPrefixScore(self, nums: List[int]) -> List[int]:
14+
15+
max_pref = [nums[0]]
16+
n = len(nums)
17+
for i in range(1, n):
18+
19+
max_pref.append(max(nums[i], max_pref[-1]))
20+
21+
conver = []
22+
23+
for i in range(n):
24+
conver.append(nums[i] + max_pref[i])
25+
26+
ans = [conver[0]]
27+
for i in range(1, n):
28+
ans.append(conver[i] + ans[-1])
29+
30+
return ans

0 commit comments

Comments
 (0)