|
| 1 | +#include <algorithm> |
| 2 | +#include <vector> |
| 3 | +#include <queue> |
| 4 | +#include <set> |
| 5 | +#include <limits> |
| 6 | +#include <map> |
| 7 | +#include <unordered_set> |
| 8 | +#include <unordered_map> |
| 9 | +#include <iterator> |
| 10 | +#include <sstream> |
| 11 | +#include <iostream> // includes cin to read from stdin and cout to write to stdout |
| 12 | +using namespace std; // since cin and cout are both in namespace std, this saves some text |
| 13 | + |
| 14 | +class Solution { |
| 15 | +public: |
| 16 | + long long maxPoints(vector<vector<int>>& points) { |
| 17 | + int height = points.size(); |
| 18 | + int width = points[0].size(); |
| 19 | + |
| 20 | + vector<long long> prevRows(width, 0); |
| 21 | + |
| 22 | + for (int y = 0; y < height; ++y) { |
| 23 | + vector<long long> currRows(width, 0); |
| 24 | + |
| 25 | + long long best; |
| 26 | + |
| 27 | + best = prevRows[0]; |
| 28 | + vector<long long> leftToRight(width, 0); |
| 29 | + // leftToRight[0] = prevRows[0]; |
| 30 | + for (int x = 0; x < width; ++x) { |
| 31 | + best = max(best, prevRows[x]); |
| 32 | + leftToRight[x] = best; |
| 33 | + best--; |
| 34 | + } |
| 35 | + |
| 36 | + best = prevRows[width - 1]; |
| 37 | + vector<long long> rightToLeft(width, 0); |
| 38 | + // rightToLeft[width - 1] = rightToLeft[width - 1]; |
| 39 | + for (int x = width - 1; x >= 0; --x) { |
| 40 | + best = max(best, prevRows[x]); |
| 41 | + rightToLeft[x] = best; |
| 42 | + best--; |
| 43 | + } |
| 44 | + |
| 45 | + for (int x = 0; x < width; ++x) { |
| 46 | + currRows[x] = points[y][x] + max(leftToRight[x], rightToLeft[x]); |
| 47 | + } |
| 48 | + prevRows = currRows; |
| 49 | + } |
| 50 | + |
| 51 | + return *max_element(begin(prevRows), end(prevRows)); |
| 52 | + } |
| 53 | +}; |
0 commit comments