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 9ee0278

Browse files
committed
Add solution 123.
1 parent 8a4689a commit 9ee0278

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
3+
/**
4+
* @param Integer[] $prices
5+
* @return Integer
6+
*/
7+
function maxProfit($prices) {
8+
$hold1 = -2147483647; $hold2 = -2147483647;
9+
$release1 = 0; $release2 = 0;
10+
foreach($prices as $i){ // Assume we only have 0 money at first
11+
$release2 = max($release2, $hold2+$i); // The maximum if we've just sold 2nd stock so far.
12+
$hold2 = max($hold2, $release1-$i); // The maximum if we've just buy 2nd stock so far.
13+
$release1 = max($release1, $hold1+$i); // The maximum if we've just sold 1nd stock so far.
14+
$hold1 = max($hold1, -$i); // The maximum if we've just buy 1st stock so far.
15+
}
16+
return $release2; ///Since release1 is initiated as 0, so release2 will always higher than release1.
17+
}
18+
}

0 commit comments

Comments
(0)

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