Pergunta de entrevista da empresa Google

Given a matrix of unsorted ints, find the path with maximum sum.

Respostas da entrevista

Sigiloso

16 de out. de 2018

Make the above one better

Sigiloso

19 de out. de 2018

I wonder if they were looking for something like below- void GetMaxPath(const vector>&V, int r, int c, unordered_setS, int sum, vector>vv, vector>&VR,int&max) { if (r = V.size() || c = V[0].size()) return; if (S.find(c + r*V[0].size()) == S.end()) { sum += V[r][c]; S.insert(c + r*V[0].size()); vv.push_back(pair(r, c)); if (sum > max) { max = sum; VR = vv; } vector>dir = { {0,-1},{-1,0},{0,1},{1,0} }; for (auto e : dir) { int x = r + e[0], y = c+e[1]; GetMaxPath(V, x, y, S, sum, vv, VR, max); } } } vector> Maxpath(vector>V) { vector>VR; int max = numeric_limits::min(); for (int i = 0; i S; int sum = 0; vector>Vv; GetMaxPath(V, i, j, S, sum, Vv,VR,max); } return VR; }