Skip to content

Commit 889b3a1

Browse files
committed
Day - 83 work
1 parent 7884426 commit 889b3a1

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

Diff for: README.md

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

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 120 |
10-
| Current Streak | 82 days |
11-
| Longest Streak | 82 ( August 17, 2015 - November 06, 2015 ) |
9+
| Total Problems | 121 |
10+
| Current Streak | 83 days |
11+
| Longest Streak | 83 ( August 17, 2015 - November 07, 2015 ) |
1212

1313
</center>
1414

@@ -196,7 +196,7 @@ Include contains single header implementation of data structures and some algori
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)|
197197
| 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)|
198198
| 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-
199+
| Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time.| [minPath.cpp](leet_code_problems/minPath.cpp)|
200200

201201

202202

Diff for: leet_code_problems/minPath.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
3+
*
4+
* Note: You can only move either down or right at any point in time.
5+
*
6+
* Can we do it in O(1) space complexity?
7+
*/
8+
9+
#include <iostream>
10+
#include <vector>
11+
#include <utility>
12+
13+
int minPathSum(std::vector<std::vector<int>>& grid) {
14+
size_t m = grid.size();
15+
size_t n = grid[0].size();
16+
for ( size_t i = 1; i < m; ++i ) {
17+
grid[i][0] += grid[i-1][0];
18+
}
19+
for ( size_t i = 1; i < n; ++i ) {
20+
grid[0][i] += grid[0][i-1];
21+
}
22+
23+
for ( size_t i = 1; i < m; ++i ) {
24+
for ( size_t j = 1; j < n; ++j ) {
25+
grid[i][j] += std::min( grid[i-1][j], grid[i][j-1]);
26+
}
27+
}
28+
return grid[m-1][n-1];
29+
}
30+
31+
int main() {
32+
std::vector<std::vector<int>> grid {
33+
{ 1, 0, 5 },
34+
{ 2, 3, 1},
35+
{ 4, 1, 9}
36+
};
37+
for ( size_t i = 0; i < grid.size(); ++i ) {
38+
for ( size_t j = 0; j < grid[0].size(); ++j ) {
39+
std::cout << grid[i][j] << " ";
40+
}
41+
std::cout << std::endl;
42+
}
43+
std::cout << "Min cost for above grid from moving top left to bottom right is "
44+
<< minPathSum(grid) << std::endl;
45+
return 0;
46+
}

Diff for: leet_code_problems/run

17.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)