-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.cpp
More file actions
88 lines (74 loc) · 1.86 KB
/
Triangle.cpp
File metadata and controls
88 lines (74 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Source : https://oj.leetcode.com/problems/triangle/
// Author : zheng yi xiong
// Date : 2014-12-16
/**********************************************************************************
*
* Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
* For example, given the following triangle
* [
* [2],
* [3,4],
* [6,5,7],
* [4,1,8,3]
* ]
* The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
* Note:
* Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
*
**********************************************************************************/
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
int row = triangle.size();
if (0 == row)
{
return 0;
}
vector<int> miniMum(row);
for (int i = 0; i < row; ++i)
{
miniMum[i] = triangle[row - 1][i];
}
for (int i = row - 2; 0 <= i; --i)
{
for (int j = 0; j <= i; ++j)
{
miniMum[j] = min(triangle[i][j] + miniMum[j], triangle[i][j] + miniMum[j + 1]);
}
}
return miniMum[0];
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int n = 20;
int maxNumber = 20;
if ( argc > 2) {
n = _wtoi(argv[1]);
maxNumber = _wtoi(argv[2]);
}
vector<vector<int>> triangle(n, vector<int>(n));
srand(time(0));
cout<< "triangle: [ \n";
for(int i=0; i< n - 1; ++i) {
cout<< " [ ";
for (int j = 0; j < i; ++j)
{
triangle[i][j] = rand() % maxNumber;
cout<<triangle[i][j]<< ", ";
}
triangle[i][i] = rand() % maxNumber;
cout<<triangle[i][i]<< " ]\n";
}
cout<<" ]\n";
Solution so;
int minimum = so.minimumTotal(triangle);
cout<<"minimum ="<<minimum<<endl;
system("pause");
return 0;
}