Created
August 3, 2014 11:29
-
-
Save jasonlvhit/0e3cf8f08869f293e4f0 to your computer and use it in GitHub Desktop.
define vector init auto
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
#include <iostream> | |
#include <vector> | |
using namespace std; | |
#define max(a, b) a > b ? a : b | |
class Solution { | |
public: | |
template<typename T> | |
static inline T min(const T &a, const T &b){ | |
return a > b ? b : a; | |
} | |
/* | |
template<typename T> | |
static inline T max(const T &a, const T &b){ | |
return a > b ? a : b; | |
} | |
*/ | |
int minimumTotal(vector<vector<int> > &triangle) { | |
for (int i = triangle.size() - 2; i >= 0; i--){ | |
for (size_t j = 0; j <= i; j++) | |
triangle[i][j] += min(triangle[i + 1][j], triangle[i + 1][j + 1]); | |
} | |
return triangle[0][0]; | |
} | |
int maxSubArray(int A[], int n) { | |
int re = INT_MIN, pre = 0; | |
for (size_t i = 0; i < n; i++){ | |
pre = max(A[i], A[i] + pre); | |
re = max(pre, re); | |
} | |
return re; | |
} | |
}; | |
int main(){ | |
//int A[11] = { 1, 1, 1, 2, 2, 2, 4, 5, 5, 5 }; | |
vector<vector<int> > triangle = { { 1 }, { 1, 1 }, { 1, 1, 1 }, { 1, 1, 1, 1 } }; | |
for (auto i : triangle){ | |
for (auto j : i) | |
printf("%d\t", j); | |
printf("\n"); | |
} | |
printf("%d\n", max(1, 33)); | |
int A[9] = { -2, 1, -3, 4, -1, 2, 1, -5, 4 }; | |
Solution s; | |
printf("%d", s.maxSubArray(A, 9)); | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment