# tc=O(n) # sc=O(n) class Solution: def finalPrices(self, prices: List[int]) -> List[int]: stack=[] ans=[price for price in prices] #print(ans) n=len(prices) for i in range(n): while(stack and stack[-1][0]>=prices[i]): originalprice,index=stack.pop() ans[index]=(originalprice-prices[i]) #print('originalPrice=',originalprice,'new price=',ans[index],'discount=',prices[i]) stack.append((prices[i],i)) #print(stack) #print(ans) return ans
# tc=O(n2) # sc=O(n) class Solution: def finalPrices(self, prices: List[int]) -> List[int]: ans=[] n=len(prices) for i in range(n): discount=0 for j in range(i+1,n): if(prices[j]
# tc=O(n)
# sc=O(n)
class Solution:
def finalPrices(self, prices: List[int]) -> List[int]:
stack=[]
ans=[price for price in prices]
#print(ans)
n=len(prices)
for i in range(n):
while(stack and stack[-1][0]>=prices[i]):
originalprice,index=stack.pop()
ans[index]=(originalprice-prices[i])
#print('originalPrice=',originalprice,'new price=',ans[index],'discount=',prices[i])
stack.append((prices[i],i))
#print(stack)
#print(ans)
return ans
Hindi Explanation: th-cam.com/video/KYBwEo6ENH0/w-d-xo.html
# tc=O(n2)
# sc=O(n)
class Solution:
def finalPrices(self, prices: List[int]) -> List[int]:
ans=[]
n=len(prices)
for i in range(n):
discount=0
for j in range(i+1,n):
if(prices[j]