Skip to content

Commit c98c566

Browse files
committed
Day - 81 work
1 parent 293656e commit c98c566

File tree

4 files changed

+121
-3
lines changed

4 files changed

+121
-3
lines changed

README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 117 |
10-
| Current Streak | 80 days |
11-
| Longest Streak | 80 ( August 17, 2015 - November 04, 2015 ) |
9+
| Total Problems | 119 |
10+
| Current Streak | 81 days |
11+
| Longest Streak | 81 ( August 17, 2015 - November 05, 2015 ) |
1212

1313
</center>
1414

@@ -194,5 +194,10 @@ Include contains single header implementation of data structures and some algori
194194
|Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array.| [find_min_rotated.cpp](leet_code_problems/find_min_rotated.cpp)|
195195
|Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.| [threeSumClosest.cpp](leet_code_problems/threeSumClosest.cpp)|
196196
|Given n non-negative integers a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub>, where each represents a point at coordinate (i, a<sub>i</sub>). n vertical lines are drawn such that the two endpoints of line i is at (i, a<sub>i</sub>) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container.| [maxArea.cpp](leet_code_problems/maxArea.cpp)|
197+
| Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. Example in solution comments | [sumRootToLeafNumbers.cpp](leet_code_problems/sumRootToLeafNumbers.cpp)|
198+
| Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.| [maxProfitStock.cpp](leet_code_problems/maxProfitStock.cpp)|
199+
200+
201+
197202

198203

leet_code_problems/maxProfitStock.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Say you have an array for which the ith element is the price of a given stock on day i.
3+
* If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),
4+
* Design an algorithm to find the maximum profit.
5+
*/
6+
7+
#include <iostream>
8+
#include <vector>
9+
10+
int maxProfit(std::vector<int>& prices) {
11+
if (prices.size() == 0 ) {
12+
return 0;
13+
}
14+
std::vector<int> dp(prices.size());
15+
dp[0] = 0;
16+
int buyPrice = prices[0];
17+
int mostProfit = 0;
18+
for ( size_t i = 1; i < prices.size(); ++i ) {
19+
if ( prices[i] >= buyPrice ) {
20+
dp[i] = dp[i-1] + prices[i] - prices[i-1];
21+
} else {
22+
dp[i] = 0;
23+
buyPrice = prices[i];
24+
}
25+
if (mostProfit < dp[i]) {
26+
mostProfit = dp[i];
27+
}
28+
}
29+
return mostProfit;
30+
}
31+
32+
void printStocks( std::vector<int> & stocks ) {
33+
std::cout << "Stock prices:";
34+
for ( auto stock : stocks ) {
35+
std::cout << stock << " ";
36+
}
37+
std::cout << std::endl;
38+
}
39+
int main() {
40+
std::vector<int> prices{ 45, 33, 66, 78, 90, 47 };
41+
printStocks(prices);
42+
std::cout << "Max profit from above price range:" << maxProfit(prices) << std::endl;
43+
return 0;
44+
}

leet_code_problems/run

-8.19 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
*
3+
* Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
4+
*
5+
* An example is the root-to-leaf path 1->2->3 which represents the number 123.
6+
*
7+
* Find the total sum of all root-to-leaf numbers.
8+
*
9+
* For example,
10+
*
11+
* 1
12+
* / \
13+
* 2 3
14+
* The root-to-leaf path 1->2 represents the number 12.
15+
* The root-to-leaf path 1->3 represents the number 13.
16+
*
17+
* Return the sum = 12 + 13 = 25.
18+
*/
19+
20+
#include <iostream>
21+
#include <vector>
22+
23+
struct TreeNode {
24+
int val;
25+
TreeNode * left;
26+
TreeNode * right;
27+
TreeNode( int data ) : val{ data }, left{ nullptr }, right{ nullptr } { }
28+
};
29+
30+
void rootToLeafSum( TreeNode* root, int & total, int & curr ) {
31+
if( root == nullptr ) {
32+
return;
33+
}
34+
curr = (10*curr) + root->val;
35+
if (root && root->left == nullptr && root->right == nullptr ) {
36+
total += curr;
37+
}
38+
int oldCurr = curr;
39+
rootToLeafSum(root->left, total, curr);
40+
rootToLeafSum(root->right, total, oldCurr);
41+
}
42+
43+
44+
int sumNumbers(TreeNode* root) {
45+
int total = 0;
46+
int curr = 0;
47+
rootToLeafSum(root, total, curr);
48+
return total;
49+
}
50+
51+
52+
void printTree( TreeNode * root ) {
53+
if ( root ) {
54+
printTree( root->left );
55+
std::cout << root->val << " ";
56+
printTree( root->right );
57+
}
58+
}
59+
60+
61+
int main() {
62+
TreeNode * root = new TreeNode( 1 );
63+
root->left = new TreeNode( 2 );
64+
root->right = new TreeNode( 3 );
65+
std::cout << "Tree in order:";
66+
printTree(root);
67+
std::cout << "Output : " << sumNumbers(root) << std::endl;
68+
return 0;
69+
}

0 commit comments

Comments
 (0)