Skip to content

Commit a0d8a31

Browse files
committed
added solutions in leetcode/python/arrays
1 parent 7ef4ae8 commit a0d8a31

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def maxProfit(self, prices):
3+
"""
4+
:type prices: List[int]
5+
:rtype: int
6+
"""
7+
#preprocessing
8+
minprice = 10000000000
9+
maxprofit = 0
10+
for i in range(len(prices)):
11+
if prices[i] < minprice:
12+
minprice = prices[i]
13+
elif (prices[i] - minprice > maxprofit):
14+
maxprofit = prices[i] - minprice
15+
return maxprofit
16+
17+
18+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
@staticmethod
3+
def area(i,j,h1,h2):
4+
return (j-i)*min(h1,h2)
5+
def maxArea(self, height):
6+
"""
7+
:type height: List[int]
8+
:rtype: int
9+
"""
10+
n= len(height)
11+
i=0
12+
j=n-1
13+
res= 0
14+
while (i<j):
15+
res= max(res,Solution.area(i,j,height[i],height[j]))
16+
#pointer calculation and movement
17+
18+
if (height[i]<height[j]):
19+
i+=1
20+
else:
21+
j-=1
22+
return res
23+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def productExceptSelf(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: List[int]
6+
"""
7+
n= len(nums)
8+
prodSoFar= 1 #running product
9+
output = [nums[n-1]]*n
10+
11+
#preprocessing the array
12+
for i in range(n-2,-1,-1):
13+
output[i]= output[i+1]*nums[i]
14+
15+
16+
for i in range(n-1):
17+
output[i]= prodSoFar*output[i+1]
18+
prodSoFar*= nums[i]
19+
20+
output[n-1]= prodSoFar
21+
22+
return output
23+
24+
25+
26+
'''
27+
28+
Time complexity of the above algorith: O(n), n is the length of the input array
29+
30+
Space complexity: O(1), constant space
31+
'''

0 commit comments

Comments
 (0)