Skip to content

Commit 148d039

Browse files
committed
Add comments to explain the solutions
1 parent f053ee5 commit 148d039

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

src/bestTimeToBuyAndSellStock/bestTimeToBuyAndSellStock.II.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
class Solution {
1717
public:
18+
//
19+
// find all of ranges: which start a valley with the nearest peak after
20+
// add their delta together
21+
//
1822
int maxProfit(vector<int> &prices) {
1923
int max=0, begin=0, end=0;
2024
bool up=false, down=false;
@@ -32,7 +36,7 @@ class Solution {
3236
max += (prices[end] - prices[begin]);
3337
}
3438
}
35-
39+
// edge case
3640
if (begin < prices.size() && up==true){
3741
end = prices.size() - 1;
3842
max += (prices[end] - prices[begin]);

src/bestTimeToBuyAndSellStock/bestTimeToBuyAndSellStock.III.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616

1717
class Solution {
1818
public:
19+
// Dynamic Programming
20+
//
21+
// Considering prices[n], and we have a position "i", we could have
22+
// 1) the maxProfit1 for prices[0..i]
23+
// 2) the maxProfit2 for proices[i..n]
24+
//
25+
// So,
26+
// for 1) we can go through the prices[n] forwardly.
27+
// forward[i] = max( forward[i-1], price[i] - lowestPrice[0..i] )
28+
// for 2) we can go through the prices[n] backwoardly.
29+
// backward[i] = max( backward[i+1], highestPrice[i..n] - price[i])
30+
//
1931
int maxProfit(vector<int> &prices) {
2032
if (prices.size()<=1) return 0;
2133

src/bestTimeToBuyAndSellStock/bestTimeToBuyAndSellStock.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@
1313

1414
class Solution {
1515
public:
16+
//
17+
// This solution is O(1) space dynamic programming
18+
//
19+
// We can make sure the max profit at least be ZERO. So,
20+
// 1) we have two pointers (begin & end )
21+
// 2) if prices[end] - prices[begin] > 0, then move the "end" pointer to next
22+
// 3) if prices[end] - prices[begin] <= 0, then move the "begin" pointer to current posstion.
23+
// 4) tracking the max profit
24+
//
25+
// Notes:
26+
// Some people think find the highest-price & lowest-price, this is wrong.
27+
// Because the highest-price must be after lowest-price
28+
//
1629
int maxProfit(vector<int> &prices) {
1730

1831
int max=0, begin=0, end=0, delta=0;

src/binaryTreeMaximumPathSum/binaryTreeMaximumPathSum.cpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,24 @@ struct TreeNode {
3232
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
3333
};
3434

35-
35+
//The solution is quite simple can be explained by itself
3636
int maxPathSum(TreeNode *root, int& maxSum ) {
3737

3838
if (NULL == root) return 0;
3939

40-
int val = root->val;
41-
int left = root->left ? maxPathSum(root->left, maxSum) : 0;
42-
int right = root->right ? maxPathSum(root->right, maxSum) : 0;
43-
44-
int maxBranch = left + val > right + val ?
45-
max(left + val, val) : max(right + val, val);
40+
//get the maxPathSum for both left and right branch
41+
int left = maxPathSum(root->left, maxSum);
42+
int right = maxPathSum(root->right, maxSum);
4643

44+
// The max sum could be one of the following situations:
45+
// 1) root + left
46+
// 2) root + right
47+
// 3) root
48+
// 4) root + left + right
49+
//
50+
// And the whole function need to return the the max of 1) 2) 3)
51+
int val = root->val;
52+
int maxBranch = left > right ? max(left + val, val) : max(right + val, val);
4753
int m = max(left + right + val, maxBranch);
4854

4955
maxSum = max(maxSum, m);

0 commit comments

Comments
 (0)