/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
Distributed under the BSD license.
(See accompanying file LICENSE.txt at
https://g...content-available-to-author-only...b.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
*******************************************************************/
#include <algorithm>
#include <iostream>
int getMaxValue_solution1(const int* values, int rows, int cols)
{
if(values == nullptr || rows <= 0 || cols <= 0)
return 0;
int** maxValues = new int*[rows];
for(int i = 0; i < rows; ++i)
maxValues[i] = new int[cols];
for(int i = 0; i < rows; ++i)
{
for(int j = 0; j < cols; ++j)
{
int left = 0;
int up = 0;
if(i > 0)
up = maxValues[i - 1][j];
if(j > 0)
left = maxValues[i][j - 1];
maxValues[i][j] = std::max(left, up) + values[i * cols + j];
}
}
int maxValue = maxValues[rows - 1][cols - 1];
for(int i = 0; i < rows; ++i)
delete[] maxValues[i];
delete[] maxValues;
return maxValue;
}
int getMaxValue_solution2(const int* values, int rows, int cols)
{
if(values == nullptr || rows <= 0 || cols <= 0)
return 0;
int* maxValues = new int[cols];
for(int i = 0; i < rows; ++i)
{
for(int j = 0; j < cols; ++j)
{
int left = 0;
int up = 0;
if(i > 0)
up = maxValues[j];
if(j > 0)
left = maxValues[j - 1];
maxValues[j] = std::max(left, up) + values[i * cols + j];
}
}
int maxValue = maxValues[cols - 1];
delete[] maxValues;
return maxValue;
}
// ==============test code================
void test(const char* testName, const int* values, int rows, int cols, int expected)
{
if(getMaxValue_solution1(values, rows, cols) == expected)
std::cout << testName << ": solution1 passed." << std::endl;
else
std::cout << testName << ": solution1 FAILED." << std::endl;
if(getMaxValue_solution2(values, rows, cols) == expected)
std::cout << testName << ": solution2 passed." << std::endl;
else
std::cout << testName << ": solution2 FAILED." << std::endl;
}
void test1()
{
int values[][4] = {
{ 1, 10, 3, 8 },
{ 12, 2, 9, 6 },
{ 5, 7, 4, 50 },
{ 3, 7, 16, 5 }
};
int expected = 85;
test("test2", (const int*) values, 4, 4, expected);
}
int main(int argc, char* argv[])
{
test1();
return 0;
}
LyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKQ29weXJpZ2h0KGMpIDIwMTYsIEhhcnJ5IEhlCkFsbCByaWdodHMgcmVzZXJ2ZWQuCgpEaXN0cmlidXRlZCB1bmRlciB0aGUgQlNEIGxpY2Vuc2UuCihTZWUgYWNjb21wYW55aW5nIGZpbGUgTElDRU5TRS50eHQgYXQKaHR0cHM6Ly9nLi4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5iLmNvbS96aGVkYWhodC9Db2RpbmdJbnRlcnZpZXdDaGluZXNlMi9ibG9iL21hc3Rlci9MSUNFTlNFLnR4dCkKKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKi8KCiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxpb3N0cmVhbT4KCmludCBnZXRNYXhWYWx1ZV9zb2x1dGlvbjEoY29uc3QgaW50KiB2YWx1ZXMsIGludCByb3dzLCBpbnQgY29scykKewogICAgaWYodmFsdWVzID09IG51bGxwdHIgfHwgcm93cyA8PSAwIHx8IGNvbHMgPD0gMCkKICAgICAgICByZXR1cm4gMDsKCiAgICBpbnQqKiBtYXhWYWx1ZXMgPSBuZXcgaW50Kltyb3dzXTsKICAgIGZvcihpbnQgaSA9IDA7IGkgPCByb3dzOyArK2kpCiAgICAgICAgbWF4VmFsdWVzW2ldID0gbmV3IGludFtjb2xzXTsKCiAgICBmb3IoaW50IGkgPSAwOyBpIDwgcm93czsgKytpKQogICAgewogICAgICAgIGZvcihpbnQgaiA9IDA7IGogPCBjb2xzOyArK2opCiAgICAgICAgewogICAgICAgICAgICBpbnQgbGVmdCA9IDA7CiAgICAgICAgICAgIGludCB1cCA9IDA7CgogICAgICAgICAgICBpZihpID4gMCkKICAgICAgICAgICAgICAgIHVwID0gbWF4VmFsdWVzW2kgLSAxXVtqXTsKCiAgICAgICAgICAgIGlmKGogPiAwKQogICAgICAgICAgICAgICAgbGVmdCA9IG1heFZhbHVlc1tpXVtqIC0gMV07CgogICAgICAgICAgICBtYXhWYWx1ZXNbaV1bal0gPSBzdGQ6Om1heChsZWZ0LCB1cCkgKyB2YWx1ZXNbaSAqIGNvbHMgKyBqXTsKICAgICAgICB9CiAgICB9CgogICAgaW50IG1heFZhbHVlID0gbWF4VmFsdWVzW3Jvd3MgLSAxXVtjb2xzIC0gMV07CgogICAgZm9yKGludCBpID0gMDsgaSA8IHJvd3M7ICsraSkKICAgICAgICBkZWxldGVbXSBtYXhWYWx1ZXNbaV07CiAgICBkZWxldGVbXSBtYXhWYWx1ZXM7CgogICAgcmV0dXJuIG1heFZhbHVlOwp9CgppbnQgZ2V0TWF4VmFsdWVfc29sdXRpb24yKGNvbnN0IGludCogdmFsdWVzLCBpbnQgcm93cywgaW50IGNvbHMpCnsKICAgIGlmKHZhbHVlcyA9PSBudWxscHRyIHx8IHJvd3MgPD0gMCB8fCBjb2xzIDw9IDApCiAgICAgICAgcmV0dXJuIDA7CgogICAgaW50KiBtYXhWYWx1ZXMgPSBuZXcgaW50W2NvbHNdOwogICAgZm9yKGludCBpID0gMDsgaSA8IHJvd3M7ICsraSkKICAgIHsKICAgICAgICBmb3IoaW50IGogPSAwOyBqIDwgY29sczsgKytqKQogICAgICAgIHsKICAgICAgICAgICAgaW50IGxlZnQgPSAwOwogICAgICAgICAgICBpbnQgdXAgPSAwOwoKICAgICAgICAgICAgaWYoaSA+IDApCiAgICAgICAgICAgICAgICB1cCA9IG1heFZhbHVlc1tqXTsKCiAgICAgICAgICAgIGlmKGogPiAwKQogICAgICAgICAgICAgICAgbGVmdCA9IG1heFZhbHVlc1tqIC0gMV07CgogICAgICAgICAgICBtYXhWYWx1ZXNbal0gPSBzdGQ6Om1heChsZWZ0LCB1cCkgKyB2YWx1ZXNbaSAqIGNvbHMgKyBqXTsKICAgICAgICB9CiAgICB9CgogICAgaW50IG1heFZ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