Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
yaozc
ProjectLeet
Commits
d9548ae3
Commit
d9548ae3
authored
Aug 05, 2019
by
yaozc
Browse files
keep going
parent
beb751fa
Changes
1
Hide whitespace changes
Inline
Side-by-side
amazon.cpp
View file @
d9548ae3
...
...
@@ -1660,14 +1660,25 @@ public:
Best
Time
to
Buy
and
Sell
Stock
Input
:
[
7
,
1
,
5
,
3
,
6
,
4
]
Output
:
5
Explanation
:
Buy
on
day
2
(
price
=
1
)
and
sell
on
day
5
(
price
=
6
),
profit
=
6
-
1
=
5.
Not
7
-
1
=
6
,
as
selling
price
needs
to
be
larger
than
buying
price
.
class
Solution
{
public:
int
maxProfit
(
vector
<
int
>&
prices
)
{
int
min
=
INT_MAX
;
int
profit
=
0
;
for
(
int
i
=
0
;
i
<
prices
.
size
();
i
++
){
if
(
prices
[
i
]
<
min
){
min
=
prices
[
i
];
}
else
{
profit
=
std
::
max
(
profit
,
prices
[
i
]
-
min
);
}
}
return
profit
;
}
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment