Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c95eadc

Browse files
Add files via upload
1 parent 9c58516 commit c95eadc

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# You are given an array prices where prices[i] is the price of a given stock on the ith day.
2+
# You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
3+
# Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
4+
5+
# Example 1:
6+
# Input: prices = [7,1,5,3,6,4]
7+
# Output: 5
8+
# Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
9+
# Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
10+
11+
# Example 2:
12+
# Input: prices = [7,6,4,3,1]
13+
# Output: 0
14+
# Explanation: In this case, no transactions are done and the max profit = 0.
15+
16+
# Constraints:
17+
# 1 <= prices.length <= 105
18+
# 0 <= prices[i] <= 104
19+
20+
# -------------------------------------------------------------------------------------------------------------------------------------------------------------------
21+
22+
# SLIDING WINDOW:
23+
# Two pointers move in the same direction
24+
# one variable will be selling price(r) and one will be buying(l)
25+
# only move selling price iteratively
26+
# moving buying price only when minimum is found
27+
# Start at 0 and 1 index
28+
# maintain a profit variable to calculate max profit, initiating at 0
29+
# compare l and r -> if l < r, calculate profit and update the profit variable to get max profit, move r one place further
30+
# if l > r, profits are negative, so move l. Now, l can move 1 step, but there's no point in doing that
31+
# currently, r is at the minimum point and we want l to be at minimum
32+
# so we move l to r's place
33+
# increment r in any case
34+
# continue loop until r is out of bounds
35+
36+
37+
class Solution:
38+
def maxProfit(self, prices: List[int]) -> int:
39+
40+
l, r = 0, 1
41+
profit = 0
42+
43+
while r < len(prices):
44+
if prices[l] < prices[r]:
45+
profit = max(profit, prices[r] - prices[l])
46+
else:
47+
l = r
48+
r += 1
49+
50+
return profit
51+
52+
Time Complexity: O(n)
53+
Space Complexity: O(1)
54+
55+
# Companies:
56+
# Amazon- 34
57+
# Google- 21
58+
# Meta- 17
59+
# Microsoft- 11
60+
# Morgan Stanley- 6
61+
# Tiktok- 3
62+
# Salesforce- 3
63+
# Tech Mahindra- 2
64+
# Adobe- 2
65+
# Zoho- 8
66+
# Apple- 5
67+
# Uber- 5
68+
# Nvidia- 4
69+
# Accenture- 4
70+
# Infosys- 3
71+
# Samsung- 3
72+
# Paypal- 3
73+
# TCS- 2
74+
# Oracle- 2
75+
# Yahoo- 26
76+
# Goldman Sachs- 25
77+
# Yandex- 22
78+
# IBM- 17
79+
# JP Morgan- 15
80+
# Bolt- 12
81+
# Walmart Labs- 11
82+
# Zoox- 9
83+
# Media.net- 8
84+
# Citadel- 6

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /