Reddit
Question
·
2026 Q1
·
India
POTD Day 1: Stupid Data Structure used to solve the problem
14 upvotes
9 replies
Question Details
I am currently pursuing MBA from a prestigious Indian institute, but like I mentioned in my previous post, I love DSA/CP as I see it as simply a way to exercise my logical reasoning and mathematical s
Full Details
I am currently pursuing MBA from a prestigious Indian institute, but like I mentioned in my previous post, I love DSA/CP as I see it as simply a way to exercise my logical reasoning and mathematical skills, and I had a recent passion revival for it. So I will solve the LC POTD everyday. Will ofc try LC hard whenever I find time and also try to give as many LC weeklies as possible, but I want to see how many regular POTD streak I can maintain, which, at the minimum, I wanna maintain to get the LC T-shirt free. Anyway, I solved today's POTD with a stupid Data Structure, but here is the problem, as well as my solution for anyone to see: [https://leetcode.com/problems/maximum-non-negative-product-in-a-matrix/?envType=daily-question&envId=2026-03-23](https://leetcode.com/problems/maximum-non-negative-product-in-a-matrix/?envType=daily-question&envId=2026-03-23) https://preview.redd.it/5hng1jglotqg1.png?width=592&format=png&auto=webp&s=c8f3a39f55bc3a289f9a83850b014994ca560a51 class Solution { public: long long maxi = INT_MIN; int N = 1e9+7; int maxProductPath(vector<vector<int>>& grid) { queue<pair<pair<int,int>,long long>>gq; int m = grid.size(); int n = grid[0].size(); gq.push({{0,0},grid[0][0]}); vector<vector<pair<long long,long long>>>visited(m,vector<pair<long long,long long>>(n,{INT_MIN,INT_MAX})); while (!gq.empty()){ int x = gq.front().first.first; int y = gq.front().first.second; long long val = gq.front().second; gq.pop(); if (x==m-1 && y==n-1) maxi=max(maxi,val); if (val > 0){ if (visited[x][y].first >= val) continue; visited[x][y].first = val; } else{ if (visited[x][y].second <= val) continue; visited[x][y].second = val; } if (x+1<m) gq.push({{x+1,y},(val * 1LL* grid[x+1][y])}); if (y+1<n) gq.push({{x,y+1},(val * 1LL* grid[x][y+1])}); } // cout<<maxi<<endl; return maxi%N >= 0 ? maxi%N : -1; } };
Free preview — Unlock all questions →
Topics
Stack Queue