-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargestAltitude
More file actions
32 lines (24 loc) · 916 Bytes
/
largestAltitude
File metadata and controls
32 lines (24 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#prefix sum exercise.
# 1. create a new array 2. first element needs to be copied
# 3. do the cumulative sum
# made some comments
class Solution:
def longestAltitude(self, gain):
n = len(gain)
# [0] is an array with a zero element
# need to create a new array for prefix sum
# but in order to get a zero need to do (n+1) below
prefix_sum = [0] * (n+1)
prefix_sum[0] = gain[0]
print(prefix_sum)
#trick: if zero is not in the input arr
#then how do you take into acct?????
#-4,-3,-2,-1,4,3,2
for i in range(1, n):
prefix_sum[i] = prefix_sum[i-1] + gain[i]
print(prefix_sum[i])
print(prefix_sum[i-n])
print(gain[i])
return max(prefix_sum)
myVar = Solution()
myVar.longestAltitude([-4,-3,-2,-1,4,3,2])