-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
applewjg
committed
Oct 6, 2014
1 parent
93e49c4
commit d9192c0
Showing
3 changed files
with
14 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/* | ||
Author: Annie Kim, [email protected] | ||
Author: Annie Kim, [email protected] : King, [email protected] | ||
Date: Apr 28, 2013 | ||
Update: Oct 07, 2014 | ||
Problem: Best Time to Buy and Sell Stock | ||
Difficulty: Easy | ||
Source: http://leetcode.com/onlinejudge#question_121 | ||
|
@@ -15,13 +16,13 @@ | |
class Solution { | ||
public: | ||
int maxProfit(vector<int> &prices) { | ||
int imin = 0; | ||
int res = 0; | ||
for (int i = 1; i < prices.size(); ++i) | ||
int size = prices.size(); | ||
if (prices.empty()) return 0; | ||
int minVal = prices[0], res = 0; | ||
for (int i = 1; i < size; ++i) | ||
{ | ||
if (prices[i] < prices[imin]) | ||
imin = i; | ||
res = max(res, prices[i] - prices[imin]); | ||
minVal = min(minVal, prices[i]); | ||
res = max(res, prices[i] - minVal); | ||
} | ||
return res; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/* | ||
Author: Annie Kim, [email protected] | ||
Author: Annie Kim, [email protected] : King, [email protected] | ||
Date: Apr 28, 2013 | ||
Update: Jul 30, 2013 | ||
Update: Oct 07, 2014 | ||
Problem: Binary Tree Maximum Path Sum | ||
Difficulty: Easy | ||
Source: http://leetcode.com/onlinejudge#question_124 | ||
|
@@ -41,8 +41,7 @@ class Solution { | |
int l = maxPathSumRe(node->left, res); | ||
int r = maxPathSumRe(node->right, res); | ||
int sum = max(node->val, max(l, r) + node->val); | ||
res = max(res, sum); | ||
res = max(res, l + r + node->val); | ||
res = max(res, max(0, l) + max(0, r) + node->val); | ||
return sum; | ||
} | ||
}; |