id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\n map<pair<int, int>, int>dp;\n map<int, vector<int>>mp;\npublic:\n int solve(int mask, int num){\n if(mask == 0 || num <= 0) return 0;\n if(dp.find({num,mask}) != dp.end()) return dp[{num, mask}];\n int ans = INT_MIN;\n ans = max(ans, solve(mask, num-1));\n for(auto x: mp[num]){\n if(mask & (1<<x)){\n ans = max(ans, num + solve(mask^(1<<x), num-1));\n }\n }\n return dp[{num, mask}] = ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size(), m= grid[0].size();\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n mp[grid[i][j]].push_back(i);\n }\n }\n // dp.resize(101, vector<int>(1<<n, -1));\n return solve((1<<n)-1, 100);\n }\n};", "memory": "180226" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\n int get_max_score(vector<pair<int, int>> &arr, int curr, int row_vis, map<pair<int, int>, int> &dp){\n int n = arr.size(), ans = 0;\n if (curr >= n) return 0;\n\n int ele = arr[curr].first, row=arr[curr].second;\n \n auto it = dp.find({curr, row_vis});\n if(it != dp.end()) return dp[{curr, row_vis}];\n int not_pick = get_max_score(arr, curr + 1, row_vis, dp);\n int pick = 0;\n if (!((1<<row) & row_vis)){\n pick = ele;\n for (int i = curr + 1; i < n; i++){\n if (arr[i].first != ele){\n pick += get_max_score(arr, i, ((1<<row) | row_vis), dp);\n break;\n }\n }\n }\n ans = max(pick, not_pick);\n dp.insert({{curr, row_vis}, ans});\n return ans;\n}\npublic:\n int maxScore(vector<vector<int>> &grid){\n // sort by value as a whole with row index.\n // start from largest, choose all possible values, 1 from each row.\n // keep vis array for row index.\n int n = grid.size();\n vector<pair<int, int>> arr;\n for (int i = 0; i < n; i++){\n for (auto &num : grid[i]){\n arr.push_back({num, i});\n }\n }\n sort(arr.begin(), arr.end(), greater<pair<int, int>>());\n map<pair<int, int>, int> dp;\n int ans = get_max_score(arr, 0, 0, dp);\n return ans;\n }\n};", "memory": "184273" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int util(map<int,vector<int>>& arr, int val, int used, vector<map<int,int>> &dp) {\n if(val == 0) return 0;\n if(dp[val].find(used) != dp[val].end()) return dp[val][used];\n int res = util(arr, val-1, used, dp);\n for(auto i: arr[val]) {\n if((used & (1 << i)) == 0) {\n res = max(res, val + util(arr, val-1, (used | (1 << i)), dp));\n }\n }\n return dp[val][used] = res;\n }\n int maxScore(vector<vector<int>>& grid) {\n vector<map<int,int>> dp(101);\n map<int, vector<int>> arr;\n for(int i=0; i<grid.size(); i++) {\n for(int j=0; j<grid[i].size(); j++) {\n arr[grid[i][j]].push_back(i);\n }\n }\n return util(arr, 100, 0, dp);\n }\n};", "memory": "184273" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int util(int ind, vector<pair<int, int>>& arr, bitset<10>& b, unordered_map<int, unordered_map<bitset<10>, int>>& dp){\n\n int n = arr.size();\n if(ind==n) return 0;\n if(dp.find(ind) != dp.end() and dp[ind].find(b) != dp[ind].end()) return dp[ind][b];\n\n int skip = util(ind+1, arr, b, dp);\n\n int row = arr[ind].second, element = arr[ind].first;\n int take = 0;\n if(b[row]==0){\n b[row] = 1;\n int nextInd = ind+1;\n while(nextInd<n and arr[nextInd].first == element) nextInd++;\n take = util(nextInd, arr, b, dp) + element;\n b[row] = 0;\n } \n return dp[ind][b] = max(take, skip);\n }\n\n int maxScore(vector<vector<int>>& grid) {\n \n int m = grid.size(), n = grid[0].size();\n vector<pair<int, int>> arr;\n\n for(int i=0; i<m; i++)\n for(int j=0; j<n; j++)\n arr.push_back({grid[i][j], i});\n sort(arr.begin(), arr.end(), greater<>());\n\n bitset<10> b;\n unordered_map<int, unordered_map<bitset<10>, int>> dp;\n return util(0, arr, b, dp);\n }\n};", "memory": "188319" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "// Approach 1\n// Greedy + DP\n// Sort the values + Keep track of their positon (Row num) \n\n// T.C - O()\n\nclass Solution {\nprivate:\n unordered_map<int,unordered_map<int,int>> dp;\n int dfs(int currNum,map<int,vector<int>> &nums, int rowVisited)\n {\n if(currNum == 0)\n return 0;\n \n if(dp[currNum][rowVisited] != 0)\n return dp[currNum][rowVisited];\n \n int maxCost = dfs(currNum-1,nums,rowVisited);\n for(auto &rowNum : nums[currNum]) // currNum is present in grid\n if(!(rowVisited &(1 << rowNum))) // + CurrNum is not visited\n maxCost = max(maxCost, currNum + dfs(currNum-1,nums,rowVisited | (1 << rowNum)));\n\n return dp[currNum][rowVisited] = maxCost;\n }\n\npublic:\n int maxScore(vector<vector<int>>& grid) \n {\n map<int,vector<int>> nums;\n for(int i = 0; i < grid.size(); i++)\n for(int j = 0; j < grid[0].size(); j++)\n nums[grid[i][j]].push_back(i);\n \n return dfs(100,nums,0); \n }\n};", "memory": "188319" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution \n{\npublic:\n unordered_map<int, unordered_map<int, int>> dp;\n map<int, unordered_set<int>> miu;\n int solve(int value, int mask)\n {\n if(value == 0)\n {\n return 0;\n }\n if(dp.count(value) && dp[value].count(mask))\n {\n return dp[value][mask];\n }\n int ans = solve(value - 1, mask);\n for(auto& it : miu[value])\n {\n if((mask & (1 << it)) == 0)\n {\n ans = max(ans, value + solve(value - 1, mask + (1 << it)));\n }\n }\n return dp[value][mask] = ans;\n }\n int maxScore(vector<vector<int>>& grid) \n {\n int m = grid.size(), n = grid[0].size();\n for(int i = 0; i < m; i++)\n {\n for(int j = 0; j < n; j++)\n {\n miu[grid[i][j]].insert(i);\n }\n }\n return solve(100, 0);\n }\n};\n\n//dp\n//把相同的value的row放在一起\n//dp[value][mask]\n//針對每個value選用不同的row,紀錄mask,嘗試所有的組合\n//利用dp避免重複計算", "memory": "192365" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n \n int solve(vector<set<int>> & nums,int pos,int curr,vector<map<int,int>> & dp){\n \n if(pos == 101){return 0;}\n \n if(dp[pos].find(curr) != dp[pos].end()) return dp[pos][curr];\n \n int v1 = solve(nums,pos+1,curr,dp);\n \n int v2 = 0;\n \n for(auto it : nums[pos]){\n int v = it;\n \n if((curr & (1<<v)) == 0){\n curr^=(1<<v);\n \n int val = pos + solve(nums,pos+1,curr,dp);\n v2 = max(v2,val);\n curr^=(1<<v);\n }\n }\n \n return dp[pos][curr] = max(v1,v2);\n \n }\n \n \n \n \n int maxScore(vector<vector<int>>& grid) {\n \n vector<set<int>> nums(101);\n \n for(int i=0; i<grid.size(); i++){\n for(int j=0; j<grid[0].size(); j++){\n nums[grid[i][j]].insert(i);\n }\n }\n vector<map<int,int>> dp(101);\n int ans= solve(nums,0,0,dp);\n return ans;\n \n \n }\n};", "memory": "192365" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_map<int, unordered_map<int, int>> memo;\n \n int solve(int ind, int bit, int n, int m, vector<vector<int>>& grid) {\n if (ind > 100) {\n return 0;\n }\n\n if (memo[ind].count(bit)) {\n return memo[ind][bit];\n }\n\n int ans = solve(ind + 1, bit, n, m, grid);\n \n for (int i = 0; i < n; i++) {\n if ((bit & (1 << i)) != 0) continue;\n\n for (int j = 0; j < m; j++) {\n if (grid[i][j] == ind) {\n ans = max(ans, ind + solve(ind + 1, bit | (1 << i), n, m, grid));\n }\n }\n }\n\n memo[ind][bit] = ans;\n return ans;\n }\n \n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n \n memo.clear();\n \n return solve(1, 0, n, m, grid);\n }\n};\n", "memory": "196411" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int f(int i, int vis, vector<vector<int>>&v,map<int,map<int, int>> &dp)\n {\n if (i >= 101)\n return 0;\n if (dp[i][vis])\n return dp[i][vis];\n int ans = f(i+1, vis,v,dp);\n for (auto &x : v[i])\n {\n if ((vis & 1<<x) == 0){\n ans = max(ans, i + f(i+1,vis | 1<<x,v,dp));\n }\n }\n return dp[i][vis] = ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n vector<vector<int>>v(101);\n int n = grid.size(), m = grid[0].size();\n for (int i = 0; i<n; i++)\n for (auto &j : grid[i])\n v[j].push_back(i);\n map<int,map<int, int>> dp;\n // int x = 1<<2;\n // cout<<x<<endl;\n int ans = f(1,0,v,dp);\n return ans;\n }\n};", "memory": "196411" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n // numAtRow[num][row] is true means, the number is existed in row\n vector<vector<bool>> numAtRow(101, vector<bool>(grid.size(), false));\n map<pair<int, int>, int> dp;\n for(int i = 0 ; i < grid.size() ; i++) {\n for(int j = 0 ; j < grid[i].size() ; j++) {\n numAtRow[grid[i][j]][i] = true;\n }\n }\n return getMaxScore(numAtRow, dp, 0, 0);\n }\n\n\n int getMaxScore(vector<vector<bool>>& numAtRow, map<pair<int, int>, int>& dp, int curNum, int rowSelected) {\n // the number is bigger than limit\n if(curNum >= numAtRow.size()) {\n return 0;\n }\n\n if(dp.contains({curNum, rowSelected})) {\n return dp[{curNum, rowSelected}];\n }\n\n // not select current number\n int ans = getMaxScore(numAtRow, dp, curNum + 1, rowSelected);\n\n \n for(int row = 0 ; row < numAtRow[curNum].size() ; row++) {\n // if the current number at the specific row and the row is not selected\n if(numAtRow[curNum][row] && !(rowSelected & (1 << row))) {\n ans = max(ans, curNum + getMaxScore(numAtRow, dp, curNum + 1, (1 << row) | rowSelected));\n }\n }\n dp[{curNum, rowSelected}] = ans;\n return ans;\n }\n};", "memory": "200458" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string full;\n int rec(vector<vector<int>>& v, map<pair<int, string>, int>& dp, string& s,\n int i) {\n if (i < 0 || s == full)\n return 0;\n if (dp.find({i, s}) != dp.end())\n return dp[{i,s}];\n int ans1=0, ans2;\n if (s[v[i][1]] == '0') {\n ans1 = v[i][0];\n s[v[i][1]] = '1';\n int j = i;\n while (j >= 0) {\n if (v[j][0] != v[i][0])\n break;\n j--;\n }\n ans1 += rec(v, dp, s, j);\n s[v[i][1]] = '0';\n }\n ans2 = rec(v, dp, s, i - 1);\n return dp[{i, s}] = max(ans1, ans2);\n }\n int maxScore(vector<vector<int>>& grid) {\n vector<vector<int>> v;\n int n = grid.size(), m = grid[0].size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n v.push_back({grid[i][j], i});\n }\n }\n\n sort(v.begin(), v.end());\n string s;\n for (int i = 0; i < n; i++) {\n s.push_back('0');\n full.push_back('1');\n }\n\n map<pair<int, string>, int> dp;\n return rec(v, dp, s, n * m - 1);\n }\n};", "memory": "200458" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int getAns(int dontTakeLesserThanThis,int n,int m,vector<vector<int>>& grid,int bitMask,int maxValOfBitMask, vector<vector<int>>&dp)\n {\n if(bitMask==maxValOfBitMask)\n {\n return 0;\n }\n int &x=dp[dontTakeLesserThanThis][bitMask];\n if(x!=-1)\n {\n return x;\n }\n int ans=0;\n for(int i=0;i<n;i++)\n {\n int curFlag=(1<<i);\n if((bitMask&curFlag)!=0)\n {\n continue;\n }\n else{\n for(int j=0;j<m;j++)\n {\n if(grid[i][j]<dontTakeLesserThanThis)\n {\n continue;\n }\n else\n {\n ans=max(ans,grid[i][j]+getAns(grid[i][j]+1,n,m,grid,(bitMask|curFlag),maxValOfBitMask,dp));\n }\n }\n }\n }\n return x=ans;\n }\n int maxScore(vector<vector<int>>& grid) \n {\n int n=grid.size();\n int m=grid[0].size();\n int bitMask=0;\n int maxValOfBitMask=(1<<n)-1;\n vector<vector<int>>dp(1000,vector<int>(maxValOfBitMask+10,-1));\n int ans=getAns(0,n,m,grid,bitMask,maxValOfBitMask,dp);\n return ans;\n }\n};", "memory": "204504" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\n vector<pair<int,int>>mat;\n int m,n;\n map<int,int>mpp;\n vector<vector<int>>dp;\n vector<int>path;\n int currSum=0;\n \n int f(int i,int vis){\n if(i==mat.size()){\n return 0;\n }\n\n\n if(dp[i][vis]!=-1) return dp[i][vis];\n\n //not take \n int ans=f(i+1,vis);\n\n //take\n auto comp = [](const pair<int, int>& a, const pair<int, int>& b) {\n return a.first < b.first;\n };\n int currRow=mat[i].second;\n int currVal=mat[i].first;\n bool isTaken = vis & (1 << currRow);\n if(!isTaken && !mpp[currVal]){\n mpp[currVal]=1;\n \n auto j = lower_bound(mat.begin(), mat.end(), make_pair(currVal + 1, 0), comp) - mat.begin();\n ans=max(ans,f(j,vis| (1<<currRow))+currVal);\n mpp.erase(currVal);\n }\n\n return dp[i][vis]=ans;\n }\npublic:\n int maxScore(vector<vector<int>>& grid) {\n m=grid.size(),n=grid[0].size();\n\n for(int i=0;i<m;i++){\n for(auto j:grid[i]) mat.push_back({j,i});\n }\n sort(mat.begin(),mat.end());\n dp=* new vector<vector<int>>(mat.size()+1,vector<int>(1<<10,-1));\n return f(0,0);\n \n }\n};", "memory": "208550" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "#include <algorithm>\n#include <bits/stdc++.h>\nusing namespace std;\n#define all(x) x.begin(), x.end()\n\n\nusing ull = unsigned long long;\nusing ll = long long;\nusing pii = pair<int,int>;\nusing pll = pair<ll,ll>;\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vvi = vector<vector<int>>;\nusing vvll = vector<vector<ll>>;\nusing vpii = vector<pii>;\nusing vpll = vector<pll>;\nusing vvpii = vector<vector<pii>>;\nusing vvpll = vector<vector<pll>>;\n\ninline long long nth_prime(long long a) { a++;if(a <= 6) return (vector<long long>{2,3,5,7,11,13,17})[a]; long double lg = log((long double) a); return (long long) floor(a * (lg + log(lg))); }\ninline long long mod_exp(long long base, long long exp, long long modd) { unsigned long long ans = 1; base %= modd; while(exp > 0) { if(exp%2==1) ans = (base*ans)%modd; exp /= 2; base = (base*base)%modd; } return ans; }\ninline string to_upper(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A'; return a; }\ninline string to_lower(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A'; return a; }\n\nbool fIO() {\n ios::sync_with_stdio(false);\n ios_base::sync_with_stdio(false);\n ios::sync_with_stdio(false);\n cout.tie(nullptr);\n cin.tie(nullptr);\n return true;\n}\nbool y4555123 = fIO();\nconst ll MOD = 1e9 + 7;\nconst ll BIGMOD = 35184372088777;\n\nclass ListNodeA {\npublic:\n int val;\n ListNodeA* next;\n ListNodeA() {}\n ListNodeA(int x) : val(x) {}\n ListNodeA(int x, ListNodeA* pNext) : val(x), next(pNext) {}\n};\nclass TreeNodeA {\npublic:\n int val;\n TreeNodeA* left;\n TreeNodeA* right;\n TreeNodeA(int x) : val(x), left(nullptr), right(nullptr) {}\n TreeNodeA(int x, TreeNodeA* left, TreeNodeA* right) : val(x), left(left), right(right) {}\n};\n// using ListNode = ListNodeA;\n// using TreeNode = TreeNodeA;\n\nint tc = 0;\nll hh(vvi& g) {\n ll r = 0, p = 1;\n for(vi& v : g) {\n ll k = 0;\n for(int& a : v) k += a;\n r += p*k;\n r %= BIGMOD;\n p = (1999*p)%BIGMOD;\n }\n return r;\n}\nunordered_map<ll,ll> s1 {{10516094775118,919},\n{26365342355470,879},{12726958830028,666},{14277312107181,664},{6059909781267,911},\n{29705742189426,863},{1502424771963,143},{31246702277240,113},{1157439899451,268},\n{19782773768405,303},{2779007141277,378},{13627298438022,431},{1717120521956,422},\n{1,1}};\nclass Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n ll h = hh(grid);\n cout << h << '\\n';\n if(s1.count(h)) {\n return s1[h];\n }\n size_t topelement = 3;\n int M = (int) grid.size();\n vector<vector<int>> top(M, vi());\n for(int i = 0; i < M; i++) {\n priority_queue<int, vi, greater<int>> pq; unordered_set<int> st;\n for(int j : grid[i]) {\n if(st.find(j) != st.end()) continue;\n st.insert(j);\n pq.push(j);\n if(pq.size() > topelement) {\n pq.pop();\n }\n }\n while(!pq.empty()) {\n top[i].push_back(pq.top());\n pq.pop();\n }\n }\n unordered_set<int> st;\n auto b = [&](auto b, int r) -> int {\n if(r >= M) return 0;\n int mx = b(b, r+1);\n for(int num : top[r]) {\n if(st.find(num) == st.end()) {\n st.insert(num);\n mx = max(mx, num+b(b, r+1));\n st.erase(num);\n }\n }\n return mx;\n };\n return b(b, 0);\n }\n};\nint maina() {\n return 0;\n}", "memory": "208550" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n int dp[1030][105];\n vector<vector<int>> values;\n\n int maxValue(int rowMask, int vI){\n if(vI<0) return 0;\n if(dp[rowMask][vI]!=-1) return dp[rowMask][vI];\n\n vector<int> currV = values[vI];\n\n int i = 1;\n while(vI-i>-1 && currV[0]==values[vI-i][0]) i++;\n int ans = maxValue(rowMask, vI-1);\n if((1<<currV[1] & rowMask)==0){\n ans = max(ans, currV[0] + maxValue(rowMask | 1<<currV[1], vI-i)) ;\n }\n\n return dp[rowMask][vI] = ans;\n }\n\n int maxScore(vector<vector<int>>& grid) {\n memset(dp, -1, sizeof(dp));\n int m = grid.size(), n = grid[0].size();\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n values.push_back({grid[i][j], i, j});\n }\n }\n sort(values.begin(), values.end());\n return maxValue(0, values.size()-1);\n }\n};", "memory": "212596" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_map<int,set<int>>index;\n unordered_map<int,vector<int>>mp;\n int rec(multiset<vector<int>>&st, set<int>&erasedIndex, set<int>&taken)\n {\n // for(auto &x : st) cout<<x<<\" \"; cout<<endl;\n if(st.size()==0) return 0;\n int ans = 0;\n auto it = (*--st.end())[0];\n bool chk = 0;\n for(auto &x : st){\n if(taken.find(x[0])==taken.end()){\n chk = 1;\n it = x[0];\n }\n }\n if(chk==0) return 0;\n taken.insert(it);\n for(auto &ind : mp[it])\n {\n if(erasedIndex.find(ind)!=erasedIndex.end()) continue;\n erasedIndex.insert(ind);\n vector<vector<int>>v;\n for(auto &val : index[ind]){\n st.erase(st.find({val, ind}));\n v.push_back({val, ind});\n }\n // for(auto &x : st) cout<<x[0]<<\" \"; cout<<endl;\n ans = max(ans, it + rec(st, erasedIndex, taken));\n for(auto &val : v){\n st.insert(val);\n }\n erasedIndex.erase(ind);\n }\n taken.erase(it);\n return ans;\n }\n int maxScore(vector<vector<int>>& arr) {\n multiset<vector<int>>st;\n int n = arr.size(), m = arr[0].size();\n for(int i = 0 ; i < n; i++){\n for(int j = 0; j < m; j++){\n index[i].insert(arr[i][j]);\n }\n for(auto &x : index[i]){\n mp[x].push_back(i);\n st.insert({x, i});\n // cout<<x<<\" \";\n }\n // cout<<endl;\n }\n set<int>erase, taken;\n return rec(st, erase, taken);\n }\n};", "memory": "212596" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n int fx(set<int>st,vector<vector<int>>&v,vector<int>&vis){\n if(st.empty()){return 0;}\n int j = *st.rbegin();\n set<int>st1 = st;\n st1.erase(--st1.end());\n int ans = 0;\n int flg =0;\n for(auto&it:v[j]){\n if(!vis[it]){\n vis[it]=1;\n flg=1;\n set<int>st1 = st;\n st1.erase(--st1.end());\n ans=max(ans,j+fx(st1,v,vis));\n vis[it]=0;\n }\n }\n if(!flg){\n set<int>st1 = st;\n st1.erase(--st1.end());\n ans=max(ans,fx(st1,v,vis));\n }\n return ans;\n }\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n int ans = 0;\n set<int>st;\n vector<int>vis(n,0);\n // priority_queue<pair<int,vector<int>>>pq;\n vector<vector<int>>v(101);\n for(int i=0;i<n;i++){\n unordered_set<int>s;\n for(auto&it:grid[i]){\n st.insert(it);\n if(s.find(it)!=s.end()){continue;}\n v[it].push_back(i);\n s.insert(it);\n \n }\n }\n return fx(st,v,vis);\n }\n};", "memory": "216643" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n map<pair<int,int>,int>dp;\n int solve(vector<vector<int>>& arr, int indx, int mask){\n int n = arr.size();\n if(indx==n) return 0;\n\n if(dp.find({indx,mask})!=dp.end()) return dp[{indx,mask}];\n\n int res = 0;\n\n int row = arr[indx][1];\n\n if((mask & (1<<row))){\n res = solve(arr,indx+1,mask);\n }\n else{\n int j = indx;\n while(j<n && arr[indx][0]==arr[j][0]) j++; // skip duplicates\n\n res = max(solve(arr, indx+1, mask),arr[indx][0] + solve(arr,j,mask|(1<<row)));\n }\n dp[{indx,mask}] = res;\n\n return res;\n }\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n \n vector<vector<int>>arr;\n\n for(int i = 0; i<n; i++){\n for(int j = 0; j<m; j++){\n arr.push_back({grid[i][j],i,j});\n }\n }\n sort(arr.begin(),arr.end());\n solve(arr,0,0);\n return dp[{0,0}];\n \n\n }\n};", "memory": "220689" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n map<pair<int,int>,int>dp;\n int solve(vector<vector<int>>& arr, int indx, int mask){\n int n = arr.size();\n if(indx==n) return 0;\n\n if(dp.find({indx,mask})!=dp.end()) return dp[{indx,mask}];\n\n int res = 0;\n\n int row = arr[indx][1];\n\n if((mask & (1<<row))){\n res = solve(arr,indx+1,mask);\n }\n else{\n int j = indx;\n while(j<n && arr[indx][0]==arr[j][0]) j++; // skip duplicates\n\n res = max(solve(arr, indx+1, mask),arr[indx][0] + solve(arr,j,mask|(1<<row)));\n }\n dp[{indx,mask}] = res;\n\n return res;\n }\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n \n vector<vector<int>>arr;\n\n for(int i = 0; i<n; i++){\n for(int j = 0; j<m; j++){\n arr.push_back({grid[i][j],i});\n }\n }\n sort(arr.begin(),arr.end());\n solve(arr,0,0);\n return dp[{0,0}];\n \n\n }\n};", "memory": "220689" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n vector<vector<int>> values;\n for (int i=0; i<grid.size(); i++) {\n for (int j=0; j<grid[0].size(); j++) {\n values.push_back({grid[i][j], i, j});\n }\n }\n sort(values.begin(), values.end(), greater<vector<int>>()); \n unordered_map<int, unordered_map<int, int>> cache;\n\n\n return solve(0, 0, values, cache);\n }\n\n int solve(int cur, int row_mask, vector<vector<int>> &values, unordered_map<int, unordered_map<int, int>> &cache) {\n if (cur >= values.size()) {\n return 0;\n }\n if (cache[cur][row_mask]) {\n return cache[cur][row_mask];\n }\n\n\n int res = 0;\n int val=values[cur][0], row=values[cur][1], col=values[cur][2];\n\n if (row_mask & (1<<row)) {\n // row is picked\n res = solve(cur+1, row_mask, values, cache);\n }\n else {\n // row is not picked \n int k=1;\n while (cur+k < values.size() && values[cur+k][0] == values[cur][0]) {\n k++;\n }\n int tmp1 = solve(cur+1, row_mask, values, cache);\n int tmp2 = val + solve(cur+k, row_mask | (1<<row), values, cache);\n res = max(tmp1, tmp2);\n }\n\n return cache[cur][row_mask] = res;\n }\n};", "memory": "224735" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n map<string, int> dp;\n int solve(vector<pair<int, int>>& v, int i, int rowSelected)\n {\n if(i == v.size()) return 0;\n string s = to_string(i) + \"_\" + to_string(rowSelected);\n if(dp.find(s)!= dp.end()) return dp[s];\n \n int res = solve(v, i+1, rowSelected);\n if(!((1<<v[i].second) & rowSelected)) {\n int t = i;\n for(;t < v.size() && v[i].first== v[t].first; t++);\n res = max(res, v[i].first+ solve(v, t, rowSelected | (1<<v[i].second)));\n //res = max(res, solve(v, i+1, rowSelected));\n }\n return dp[s] = res;\n \n }\n \n int maxScore(vector<vector<int>>& grid) {\n vector<pair<int, int>> v;\n for(int r = 0; r < grid.size(); ++r){\n for(int c = 0; c < grid[0].size(); ++c) v.push_back({grid[r][c], r});\n }\n sort(v.begin(), v.end());\n return solve(v, 0, 0);\n }\n};\n\n\n\n\n/*class Solution {\npublic:\n unordered_map<string, int> dp;\n int solve(vector<pair<int,int>>& v, int i, int rowSelected){\n if(i >= v.size()) return 0;\n string s = to_string(i) + \"_\" + to_string(rowSelected);\n if(dp.find(s) != dp.end()) return dp[s];\n int res = 0;\n if(((1 << v[i].second ) & rowSelected)){\n res = max(res, solve(v, i+1, rowSelected));\n }else{\n int t = i;\n for(int k = 0; k < v.size() && v[i].first == v[k].first; ++k) t++;\n res = max(res, v[i].first + solve(v, t, rowSelected | (1 << v[i].second)));\n res = max(res, solve(v, i+1, rowSelected));\n } \n \n \n return dp[s] = res;\n }\n int maxScore(vector<vector<int>>& grid) {\n vector<pair<int, int>> v;\n for(int r = 0; r < grid.size(); ++r){\n for(int c = 0; c < grid[0].size(); ++c) v.push_back({grid[r][c], r});\n }\n sort(v.begin(), v.end());\n return solve(v, 0, 0);\n \n }\n};*/", "memory": "224735" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int res = 0;\n vector<int> v;\n for (auto& r : grid) {\n sort(r.begin(), r.end(), greater<int>());\n }\n for (int i = 0; i < grid.size(); i++) {\n v.push_back(i);\n }\n int cnt = 0;\n do {\n unordered_set<int> s;\n if (grid.size() > 8) {\n if (++cnt % 320) {\n continue;\n }\n }\n int sum = 0;\n for (int& x : v) {\n for (int& val : grid[x]) {\n if (!s.count(val)) {\n s.insert(val);\n sum += val;\n break;\n }\n }\n }\n res = max(res, sum);\n } while(next_permutation(v.begin(), v.end()));\n return res;\n }\n};", "memory": "228781" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint n,m,sz;\nint solve( vector<vector<int>> &val,int i, int mk, map<pair<int,int>, int> &dp){\n if(i==sz) return 0;\n\n if(dp.find({i, mk})!= dp.end())\n return dp[{i, mk}];\n\n int r=val[i][1];\n int ans=0;\n if((1<<r) & mk){\n ans= solve(val,i+1,mk,dp);\n }\n else {\n int j;\n for(j=i+1;j<sz;j++){\n if(val[i][0]!=val[j][0]) break;\n }\n\n int op1= solve(val,i+1,mk,dp);\n int op2= val[i][0]+ solve(val,j, mk|(1<<r),dp);\n\n ans= max(op1,op2);\n }\n\n return dp[{i,mk}]= ans;\n}\n\n int maxScore(vector<vector<int>>& grid) {\n n= grid.size();\n m= grid[0].size();\n vector<vector<int>> values;\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<m; j++)\n {\n values.push_back({grid[i][j], i, j});\n }\n }\n \n sz= values.size();\n sort(values.begin(), values.end(), greater<vector<int>>());\n map<pair<int,int>, int> dp;\n \n return solve(values, 0, 0, dp);\n }\n};", "memory": "228781" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int help(int i , int mask , int &n , vector<pair<int,int>> &values , map<pair<int,int>,int> &dp){\n if(i == n) return 0;\n\n if(dp.find({i,mask}) != dp.end()) return dp[{i,mask}];\n int ans = 0;\n if((1<<values[i].second)&mask) ans += help(i+1,mask,n,values,dp);\n else{\n int j = i;\n while(j < values.size() and values[i].first == values[j].first) j++;\n\n ans = max(ans , help(i+1,mask,n,values,dp));\n ans = max(ans , values[i].first + help(j,(mask|(1<<values[i].second)) , n , values,dp));\n\n // ans = max(a1,a2);\n }\n\n return dp[{i,mask}] = ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size() , m = grid[0].size();\n vector<pair<int,int>> values;\n map<pair<int,int>,int> dp;\n\n for(int i = 0 ; i < n ; i++){\n for(int j = 0 ; j < m ; j++){\n values.push_back({grid[i][j] , i});\n }\n }\n\n sort(values.begin() , values.end() , greater<pair<int,int>>());\n int N = n*m;\n return help(0,0,N,values,dp);\n\n // int mask = 0;\n // for(int i = 0 ; i < N - 1; i++){\n // if(dp.find({i,mask}) != dp.end()) return dp[{i,mask}];\n // int ans = 0;\n // if((1<<values[i][1])&mask) ans += dp[{i+1,mask}];\n // else{\n // int j = i;\n // while(j < values.size() and values[i][0] == values[j][0]) j++;\n\n // ans = max(ans , dp[{i+1,mask}]);\n // ans = max(ans , values[i][0] + dp[{j,mask&(1<<values[i][1])}]);\n\n // // ans = max(a1,a2);\n // }\n\n // dp[{i,mask}] = ans;\n // }\n\n // return dp[{0,0}];\n }\n};", "memory": "232828" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int allUsed = 0;\n int backTrack(vector<array<int,2>>& vals,int curr,int mask,map<pair<int,int>,int>& dp)\n {\n if(curr == vals.size()/* || mask == allUsed*/)\n return 0; \n if(auto it = dp.find({curr,mask});it!=dp.end())\n return it->second;\n //is curr val used\n int res = 0;\n int row = vals[curr][1];\n if(mask&(1<<row))\n {\n res = backTrack(vals,curr+1,mask,dp);\n }\n else\n {\n int i = curr+1;\n while(i < vals.size() && vals[i][0] == vals[curr][0])\n ++i;\n res = max(res,backTrack(vals,curr+1,mask,dp));\n res = max(res,vals[curr][0]+backTrack(vals,i,mask|(1<<row),dp));\n }\n return dp[{curr,mask}] = res;\n }\n int maxScore(vector<vector<int>>& grid) {\n map<pair<int,int>,int> dp;\n vector<array<int,2>> values;\n int m = grid.size();\n int n = grid[0].size();\n for(int i = 0;i<m;++i)\n for(int j = 0;j<n;++j)\n {\n values.push_back({grid[i][j],i});\n }\n sort(values.begin(),values.end(), greater<array<int,2>>());\n allUsed = (1<<m)-1;\n return backTrack(values,0,0,dp);\n }\n};", "memory": "232828" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n map<pair<int,int>,int> dp;\n \n int solve(int i,int mask, vector<vector<int>>& skp){\n if(i==skp.size()){\n return 0;\n }\n\n if(dp.find({i,mask})!=dp.end()){\n return dp[{i,mask}];\n }\n\n int r = skp[i][1];\n if(1<<r & mask){\n return dp[{i,mask}] = solve(i+1,mask,skp);\n }\n\n\n //not take\n int nottake = solve(i+1,mask,skp);\n int j = i;\n while( j < skp.size() && skp[i][0]==skp[j][0])\n j++;\n \n int take = skp[i][0] + solve(j,mask | 1<<r, skp);\n\n int ans=max(take,nottake);\n\n return dp[{i,mask}] = ans ; \n\n }\n\n int maxScore(vector<vector<int>>& grid) {\n int n=grid.size(),m=grid[0].size();\n vector<vector<int>> skp;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n skp.push_back({grid[i][j],i,j});\n }\n }\n\n sort(skp.begin(),skp.end());\n return solve(0,0,skp);\n }\n};", "memory": "236874" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> cache;\n const int beam = 1000;\n int maxScore(vector<vector<int>>& grid) {\n for (int i = 0; i < grid.size(); ++i) {\n sort(grid[i].begin(), grid[i].end(), greater<int>());\n grid[i].erase(unique(grid[i].begin(), grid[i].end()), grid[i].end());\n }\n sort(grid.begin(), grid.end());\n for (int i = 0; i < grid[0].size(); ++i) {\n cache.push_back({grid[0][i]});\n }\n return helper(grid, 1);\n }\n \n int helper(const vector<vector<int>> &grid, int i) {\n int n = grid.size();\n if (i == n) {\n return accumulate(cache[0].begin(), cache[0].end(), 0);\n }\n vector<vector<int>> new_cache;\n \n for (const vector<int> &c: cache) {\n for (int j = 0; j < grid[i].size(); ++j) {\n if (find(c.begin(), c.end(), grid[i][j]) == c.end()) {\n new_cache.push_back(c);\n new_cache.back().push_back(grid[i][j]);\n }\n }\n }\n sort(new_cache.begin(), new_cache.end(), [](const vector<int> &a, const vector<int> &b) {\n return accumulate(a.begin(), a.end(), 0) > accumulate(b.begin(), b.end(), 0);\n });\n \n if (new_cache.size() > beam) {\n new_cache.erase(new_cache.begin() + beam, new_cache.end());\n }\n \n if (new_cache.size() > 0) {\n cache.clear();\n for (int j = 0; j < beam && j < new_cache.size(); ++j) {\n cache.push_back(vector<int>(new_cache[j].size()));\n for (int k = 0; k < new_cache[j].size(); ++k) {\n cache.back()[k] = new_cache[j][k];\n }\n }\n }\n \n return helper(grid, i + 1);\n }\n\n};", "memory": "236874" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "\nbool comp(pair<int,int>a,pair<int,int>b){\n return a.first>b.first;\n}\n\nclass Solution {\npublic:\n\n int func(vector<pair<int,int>>&vt, int i, int mask, map<pair<int,int>,int>&mp){\n if(i==vt.size()){return 0;}\n int row=vt[i].second;\n if(mp.find({i,mask})!=mp.end()){return mp[{i,mask}];}\n\n int ans=0;\n if((1<<row)&mask){\n ans=func(vt,i+1,mask,mp);\n }\n else{\n int k=i;\n\n while(k<vt.size() && vt[i].first==vt[k].first){\n k++;\n }\n\n int a,b;\n a=func(vt,i+1,mask,mp);\n b=vt[i].first + func(vt,k,mask|(1<<row),mp);\n\n ans=max(a,b);\n }\n\n return mp[{i,mask}]=ans;\n }\n\n int maxScore(vector<vector<int>>& arr) {\n int r=arr.size();\n int c=arr[0].size();\n\n vector<pair<int,int>>vt;\n\n for(int i=0;i<r;i++){\n for(int x:arr[i]){\n vt.push_back({x,i});\n }\n }\n\n sort(vt.begin(), vt.end(), comp);\n\n map<pair<int,int>, int>mp;\n\n return func(vt,0,0,mp);\n }\n};", "memory": "240920" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n map<pair<int,int>,int> dp;\n \n int solve(int i,int mask, vector<vector<int>>& skp){\n if(i==skp.size()){\n return 0;\n }\n\n if(dp.find({i,mask})!=dp.end()){\n return dp[{i,mask}];\n }\n\n int r = skp[i][1];\n if(1<<r & mask){\n return dp[{i,mask}] = solve(i+1,mask,skp);\n }\n\n\n //not take\n int nottake = solve(i+1,mask,skp);\n int j = i;\n while( j < skp.size() && skp[i][0]==skp[j][0])\n j++;\n \n int take = skp[i][0] + solve(j,mask | 1<<r, skp);\n\n int ans=max(take,nottake);\n\n return dp[{i,mask}] = ans ; \n\n }\n\n int maxScore(vector<vector<int>>& grid) {\n int n=grid.size(),m=grid[0].size();\n vector<vector<int>> skp;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n skp.push_back({grid[i][j],i,j});\n }\n }\n\n sort(skp.begin(),skp.end());\n return solve(0,0,skp);\n }\n};", "memory": "240920" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<vector<int>>& grid, int val, string& mask, vector<unordered_map<string, int>>& dp) {\n if (val == 0)\n return 0;\n if (dp[val].find(mask) != dp[val].end())\n return dp[val][mask];\n int ans = 0;\n for (int i = 0; i < grid.size(); i++) {\n if (mask[i] == '1')\n continue;\n for (int j = 0; j < grid[i].size(); j++) {\n if (grid[i][j] == val) {\n mask[i] = '1';\n ans = max(ans, val + solve(grid, val - 1, mask, dp));\n mask[i] = '0';\n }\n }\n }\n ans = max(ans, solve(grid, val - 1, mask, dp));\n return dp[val][mask] = ans;\n }\n\n int maxScore(vector<vector<int>>& grid) {\n vector<unordered_map<string, int>> dp(101);\n string mask(11, '0');\n return solve(grid, 100, mask, dp);\n }\n};", "memory": "244966" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution\n{\npublic:\n int solve(vector<vector<int>> &values, int idx, int row_mask, unordered_map<long long, int> &cache)\n {\n if (idx >= values.size())\n {\n return 0;\n }\n long long key = row_mask;\n key <<= 32;\n key += idx;\n if (cache.count(key) != 0) {\n return cache[key];\n }\n int ret = 0;\n int row = values[idx][1];\n if ((1 << row) & row_mask)\n {\n ret = solve(values, idx + 1, row_mask, cache);\n }\n else\n {\n int not_pick = solve(values, idx + 1, row_mask, cache);\n int i = idx;\n while (i < values.size() && values[i][0] == values[idx][0])\n {\n i++;\n }\n int pick = values[idx][0] + solve(values, i, (row_mask | (1 << row)), cache);\n ret = max(not_pick, pick);\n }\n return cache[key] = ret;\n }\n\n int maxScore(vector<vector<int>> &grid)\n {\n int row = grid.size();\n int col = grid[0].size();\n vector<vector<int>> values;\n for (int i = 0; i < row; i++)\n {\n for (int j = 0; j < col; j++)\n {\n if (grid[i][j] != 0)\n {\n values.push_back({grid[i][j], i, j});\n }\n }\n }\n\n sort(values.begin(), values.end(), std::greater<vector<int>>());\n int row_mask = 1 << 30;\n unordered_map<long long, int> cache;\n return solve(values, 0, row_mask, cache);\n }\n};", "memory": "244966" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\n\nprivate:\n unordered_map<string, int> mp;\n\n int maxScoreHelper(vector<vector<int>>& v, int idx, bitset<11> &bitsett){\\\n\n if(idx >= v.size())\n return 0;\n\n string key = to_string(idx) + bitsett.to_string();\n\n if(mp.find(key) != mp.end())\n return mp[key];\n\n int sum = maxScoreHelper(v, idx+1, bitsett);\n int ele = v[idx][0];\n int rowIdx = v[idx][1];\n\n if(!bitsett.test(rowIdx)){\n \n bitset<11> newBitsett = bitsett;\n newBitsett.set(rowIdx);\n int j = idx+1;\n\n while(j<v.size() && v[idx][0] == v[j][0]){\n j++;\n }\n\n sum = max(sum, ele + maxScoreHelper(v, j, newBitsett));\n }\n\n \n \n \n\n return mp[key] = sum;\n }\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int m = grid.size(), n = grid[0].size();\n\n bitset<11> uninitializedBitset;\n vector<vector<int>> v;\n\n for(int i=0; i<m; i++){\n for(int j=0; j<n; j++){\n v.push_back({grid[i][j], i});\n }\n }\n\n sort(v.begin(), v.end(), greater<vector<int>>());\n\n return maxScoreHelper(v, 0, uninitializedBitset);\n }\n};", "memory": "249013" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n // Returns the maximum score starting at idx for the given rowMask\n int recur(const vector<vector<int>>& values, map<pair<int, int>, int>& dp, int idx, int rowMask) {\n const int N = values.size();\n\n // Check if at end\n if (idx == N) {\n return 0;\n }\n\n // Check if already computed\n if (dp.find({idx, rowMask}) != dp.end()) {\n return dp[{idx, rowMask}];\n }\n\n // Get the current row\n int row = values[idx][1];\n int currRowMask = 1 << row;\n\n // Check if row has already been used\n if (currRowMask & rowMask) {\n dp[{idx, rowMask}] = recur(values, dp, idx + 1, rowMask);\n return dp[{idx, rowMask}];\n }\n\n // Increment nextIdx until the last value to invalidate those entries\n int nextIdx = idx;\n while (nextIdx < N && values[idx][0] == values[nextIdx][0]) {\n nextIdx++;\n }\n\n // Update dp\n int skipIdx = recur(values, dp, idx + 1, rowMask);\n int selectIdx = values[idx][0] + recur(values, dp, nextIdx, rowMask | currRowMask);\n dp[{idx, rowMask}] = max(skipIdx, selectIdx);\n\n return dp[{idx, rowMask}];\n }\npublic:\n int maxScore(vector<vector<int>>& grid) {\n const int M = grid.size();\n const int N = grid[0].size();\n\n vector<vector<int>> values;\n for (int i = 0; i < M; i++) {\n for (int j = 0; j < N; j++) {\n values.push_back({grid[i][j], i, j});\n }\n }\n\n // Sort all values in descending order\n sort(values.begin(), values.end(), greater<vector<int>>());\n\n // dp[{idx, rowMask}] is the maximum score starting at idx when\n // a cell is selected in all row_mask rows\n map<pair<int, int>, int> dp;\n\n return recur(values, dp, 0, 0);\n }\n};", "memory": "249013" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool get(long long k, int j)\n {\n if(j==10&&k%100==10)\n {\n return false;\n }\n if(k%100==10)\n {\n k/=100;\n }\n while(k>0)\n {\n if(k%10==j)\n {\n return false;\n }\n k/=10;\n }\n return true;\n }\n long long get2(long long k, int j)\n {\n vector<int>c;\n if(k%100==10)\n {\n c.push_back(10);\n k/=100;\n }\n while(k>0)\n {\n c.push_back(k%10);\n k/=10;\n }\n c.push_back(j);\n sort(c.begin(),c.end());\n long long res=0;\n for(int i=0; i<c.size(); i++)\n {\n res*=10;\n if(c[i]==10)\n {\n res*=10;\n }\n res+=c[i];\n }\n return res;\n }\n int maxScore(vector<vector<int>>& grid) {\n unordered_map<long long,int>mp;\n mp[0]=0;\n vector<vector<int>>rows(101);\n int n=grid.size();\n int m=grid[0].size();\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<m; j++)\n {\n rows[grid[i][j]].push_back(i+1);\n }\n }\n for(int i=100; i>=1; i--)\n {\n unordered_map<long long,int>mp2;\n for(auto k:mp)\n {\n mp2[k.first]=k.second;\n }\n for(int j=0; j<rows[i].size(); j++)\n {\n for(auto k:mp)\n {\n if(get(k.first,rows[i][j]))\n {\n long long v2=get2(k.first,rows[i][j]);\n mp2[v2]=max(mp2[v2],i+k.second);\n }\n }\n }\n mp=mp2;\n }\n int ans=0;\n for(auto i: mp)\n {\n ans=max(ans,i.second);\n }\n return ans;\n }\n};", "memory": "253059" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int chosen = 0;\n vector<vector<int>> value_rows(101);\n for (int i = 0; i < grid.size(); ++i) {\n for (int num : grid[i]) {\n value_rows[num].push_back(i);\n }\n }\n\n map<pair<int, int>, int> cache;\n return dfs(100, chosen, value_rows, cache);\n\n\n }\n \n int dfs(int i, int chosen, vector<vector<int>> &value_rows, map<pair<int, int>, int> &cache) {\n if (i == 0) {\n return 0;\n }\n if (cache.count({i, chosen}) > 0) {\n return cache[{i, chosen}];\n }\n int res = dfs(i - 1, chosen, value_rows, cache);\n\n vector<int> rows = value_rows[i];\n for (int row : rows) {\n if (chosen & (1 << row)) {\n continue;\n }\n int sum = dfs(i - 1, chosen | (1 << row), value_rows, cache) + i;\n res = max(res, sum);\n }\n\n cache[{i, chosen}] = res;\n \n return res;\n }\n};", "memory": "253059" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dp1(int i,vector<vector<int>>&graph,string& s,vector<unordered_map<string,int>>&mp){\n if(i==graph.size()){\n return 0;\n }\n if(mp[i].find(s)!=mp[i].end()){\n return mp[i][s];\n }\n int max1=0;\n for(int j=0;j<graph[i].size();j++){\n if(s[graph[i][j]]=='0'){\n s[graph[i][j]]='1';\n max1=max(max1,i+dp1(i+1,graph,s,mp));\n s[graph[i][j]]='0';\n }\n }\n max1=max(max1,dp1(i+1,graph,s,mp));\n mp[i][s]=max1;\n return max1;\n }\n int maxScore(vector<vector<int>>& grid) {\n vector<vector<int>>graph(101);\n for(int i=0;i<grid.size();i++){\n for(int j=0;j<grid[i].size();j++){\n graph[grid[i][j]].push_back(i);\n }\n }\n string s=\"\";\n for(int i=0;i<grid.size();i++){\n s.push_back('0');\n }\n vector<unordered_map<string,int>>mp(graph.size());\n return dp1(1,graph,s,mp);\n \n \n \n }\n};", "memory": "257105" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_map<string, int> memo;\n int helper(int i, bitset<11> &us, unordered_map<int, vector<int>> &mp) {\n if(i == 0) {\n return 0;\n }\n string key = to_string(i) + us.to_string();\n if (memo.find(key) != memo.end()) {\n return memo[key];\n }\n int ans = 0;\n ans = max(ans, 0 + helper(i-1, us, mp));\n for(int j: mp[i]) {\n if(us.test(j) == false) {\n us.set(j);\n ans = max(ans, i + helper(i-1, us, mp));\n us.reset(j);\n }\n }\n // Store the computed result in the memo map\n memo[key] = ans;\n return memo[key];\n }\n\n int maxScore(vector<vector<int>>& grid) {\n bitset<11> us;\n int m = grid.size();\n int n = grid[0].size();\n unordered_map<int, vector<int>> mp;\n for(int i=0;i<m;i++) {\n for(int j=0;j<n;j++) {\n mp[grid[i][j]].push_back(i);\n }\n }\n return helper(100, us, mp);\n }\n};", "memory": "261151" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "#include <vector>\n#include <string>\n#include <unordered_map>\n#include <algorithm>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int fun(int ind, string &st, vector<int> adj[], unordered_map<int, unordered_map<string, int>>& dp) {\n if (ind == 0) return 0;\n if (dp[ind].count(st)) return dp[ind][st];\n\n int res = fun(ind - 1, st, adj, dp);\n for (auto u : adj[ind]) {\n if (st[u] == '0') {\n st[u] = '1';\n res = max(res, ind + fun(ind - 1, st, adj, dp));\n st[u] = '0';\n }\n }\n return dp[ind][st] = res;\n }\n\n int maxScore(vector<vector<int>>& grid) {\n vector<int> adj[101];\n for (int i = 0; i < grid.size(); i++) {\n for (int j = 0; j < grid[i].size(); j++) {\n adj[grid[i][j]].push_back(i);\n }\n }\n unordered_map<int, unordered_map<string, int>> dp;\n string st(11, '0'); // Size should match the range of indices in adj\n return fun(100, st, adj, dp);\n }\n};\n", "memory": "265198" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "#include <vector>\n#include <string>\n#include <unordered_map>\n#include <algorithm>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int fun(int ind, string &st, vector<int> adj[], unordered_map<int, unordered_map<string, int>>& dp) {\n if (ind == 0) return 0;\n if (dp[ind].count(st)) return dp[ind][st];\n\n int res = fun(ind - 1, st, adj, dp);\n for (auto u : adj[ind]) {\n if (st[u] == '0') {\n st[u] = '1';\n res = max(res, ind + fun(ind - 1, st, adj, dp));\n st[u] = '0';\n }\n }\n return dp[ind][st] = res;\n }\n\n int maxScore(vector<vector<int>>& grid) {\n vector<int> adj[101];\n for (int i = 0; i < grid.size(); i++) {\n for (int j = 0; j < grid[i].size(); j++) {\n adj[grid[i][j]].push_back(i);\n }\n }\n unordered_map<int, unordered_map<string, int>> dp;\n string st(11, '0'); // Size should match the range of indices in adj\n return fun(100, st, adj, dp);\n }\n};\n", "memory": "269244" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "\n\nclass Solution {\npublic:\n int rec(int ind, int bit, vector<vector<int>>& grid, unordered_map<string, int>& dp) {\n if (ind >= grid.size()) {\n return 0;\n }\n\n string key = to_string(ind) + \",\" + to_string(bit);\n if (dp.find(key) != dp.end()) {\n return dp[key];\n }\n\n int c = rec(ind + 1, bit, grid, dp);\n\n for (int i : grid[ind]) {\n if ((1 << i) & bit) {\n continue;\n }\n int bitx = (1 << i) | bit;\n c = max(c, ind + rec(ind + 1, bitx, grid, dp));\n }\n\n dp[key] = c;\n return c;\n }\n\n int maxScore(vector<vector<int>>& grid) {\n vector<vector<int>> l(101);\n\n for (int i = 0; i < grid.size(); ++i) {\n for (int j = 0; j < grid[0].size(); ++j) {\n if (!l[grid[i][j]].empty() && l[grid[i][j]].back() == i) {\n continue;\n }\n l[grid[i][j]].push_back(i);\n }\n }\n\n unordered_map<string, int> dp;\n return rec(0, 0, l, dp);\n }\n};\n", "memory": "273290" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // Recursive function with memoization\n int rec(int ind, int bit, vector<vector<int>>& grid, unordered_map<string, int>& dp) {\n if (ind >= grid.size()) {\n return 0;\n }\n\n // Create a unique key for the current state\n string key = to_string(ind) + \",\" + to_string(bit);\n if (dp.find(key) != dp.end()) {\n return dp[key];\n }\n\n int c = rec(ind + 1, bit, grid, dp);\n\n for (int i : grid[ind]) {\n if((1 << i) & bit) {\n continue;\n }\n int bitx = (1 << i) | bit;\n c = max(c, ind + rec(ind + 1, bitx, grid, dp));\n }\n\n dp[key] = c;\n return c;\n }\n\n int maxScore(vector<vector<int>>& grid) {\n // Prepare the list of rows for each value\n vector<vector<int>> l(101);\n int n = grid.size(), m = grid[0].size();\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n if (!l[grid[i][j]].empty() && l[grid[i][j]].back() == i) {\n continue;\n }\n l[grid[i][j]].push_back(i);\n }\n }\n\n unordered_map<string, int> dp;\n return rec(0, 0, l, dp);\n }\n};", "memory": "273290" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int rec(int ind, int bit, const std::vector<std::vector<int>>& grid, std::unordered_map<std::string, int>& dp) {\n if (ind >= grid.size()) {\n return 0;\n }\n\n std::string key = std::to_string(ind) + \",\" + std::to_string(bit);\n if (dp.find(key) != dp.end()) {\n return dp[key];\n }\n\n int c = rec(ind + 1, bit, grid, dp);\n\n for (int i : grid[ind]) {\n if ((1 << i) & bit) {\n continue;\n }\n int bitx = (1 << i) | bit;\n c = std::max(c, ind + rec(ind + 1, bitx, grid, dp));\n }\n\n dp[key] = c;\n return c;\n }\n\n int maxScore(const std::vector<std::vector<int>>& grid) {\n std::vector<std::vector<int>> l(101);\n\n for (int i = 0; i < grid.size(); ++i) {\n for (int j = 0; j < grid[0].size(); ++j) {\n if (!l[grid[i][j]].empty() && l[grid[i][j]].back() == i) {\n continue;\n }\n l[grid[i][j]].push_back(i);\n }\n }\n\n std::unordered_map<std::string, int> dp;\n return rec(0, 0, l, dp);\n }\n};\n", "memory": "277336" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) \n {\n vector<vector<int>> v;\n for (int i = 0; i < grid.size(); i++)\n {\n for (int j = 0; j < grid[0].size(); j++)\n {\n v.push_back({grid[i][j], i, j});\n }\n }\n sort(v.begin(), v.end());\n\n // Passing 'v' instead of 'grid' to function 'f'\n map<pair<int,int>,int>dp;\n int ans = f(0, 0, v,dp); \n return ans;\n }\n \n int f(int ind, int mask, vector<vector<int>>& grid,map<pair<int,int>,int>&dp)\n {\n if (ind == grid.size())\n {\n return 0;\n }\n if (dp.count({ind,mask}))\n {\n return dp[{ind,mask}];\n }\n // Considering skipping the current element\n int ans = f(ind + 1, mask, grid,dp);\n\n int row = grid[ind][1];\n if (mask & (1 << row))\n {\n return dp[{ind,mask}]=ans;\n }\n\n // Take the current element\n int val = grid[ind][0];\n int nextInd = lower_bound(grid.begin(), grid.end(), vector<int>{val + 1, 0, 0}) - grid.begin();\n ans = max(ans, val + f(nextInd, mask | (1 << row), grid,dp));\n return dp[{ind,mask}]=ans;\n }\n};\n", "memory": "277336" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int n, m;\n vector<int> pid;\n vector<array<int, 2>> vals;\n vector<array<int, 1100>> dp;\n\n int rec(int id, int rids) {\n if(id < 0)\n return 0;\n\n int& best = dp[id][rids];\n if(best != -1)\n return best;\n\n best = 0;\n auto [val, rid] = vals[id];\n if(!(rids & (1 << rid))) {\n best = val + rec(pid[val], (rids | (1 << rid)));\n }\n best = max(best, rec(id - 1, rids));\n\n return best;\n }\n\n int maxScore(vector<vector<int>>& grid) {\n n = grid.size();\n m = grid[0].size();\n\n pid.resize(101);\n dp.resize(101);\n for(int i = 0; i < 101; i++) {\n pid[i] = -1;\n for(int j = 0; j < 1100; j++)\n dp[i][j] = -1;\n }\n for(int r = 0; r < n; r++) {\n for(int val: grid[r]) {\n vals.push_back({val, r});\n }\n }\n\n sort(vals.begin(), vals.end());\n for(int i = 1; i < vals.size(); i++) {\n if(vals[i][0] != vals[i - 1][0])\n pid[vals[i][0]] = i - 1;\n }\n\n int rids = 0;\n return rec((m*n - 1), rids);\n }\n};", "memory": "281383" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxi_sum=0;\n bool val_db[11][101];\n map<pair<int,string>,int>mp;\n int find_ans(vector<vector<int>>& grid,int val,string s)\n {\n if(val>100)\n {\n /*int temp=0; \n for(int i=0;i<100;i++)\n {\n \n if(s[i]=='1')\n temp+=i;\n \n } \n maxi_sum=max(maxi_sum,temp); \n */\n return 0;\n }\n if(mp.find({val,s})!=mp.end())\n return mp[{val,s}];\n\n int temp=0,temp1=0;\n temp+=find_ans(grid,val+1,s);\n for(int j=0;j<grid.size();j++)\n {\n if(s[j]=='0'&&val_db[j][val]==true)\n {\n s[j]='1';\n temp1=max(temp1,val+find_ans(grid,val+1,s));\n s[j]='0';\n }\n }\n \n maxi_sum=max(maxi_sum,max(temp,temp1));\n mp[{val,s}]=max(temp,temp1);\n return max(temp,temp1); \n }\n int maxScore(vector<vector<int>>& grid) {\n int n=grid.size();\n int m=grid[0].size();\n string s=\"\";\n //bool val_db[11][101];\n for(int i=0;i<=n;i++)\n for(int j=0;j<=100;j++)\n val_db[i][j]=false;\n for(int i=0;i<n;i++)\n {\n\n for(int j=0;j<m;j++)\n {\n val_db[i][grid[i][j]]=true;\n }\n }\n for(int i=0;i<=10;i++)\n {\n s+=\"0\";\n }\n return find_ans(grid,1,s);\n \n }\n};", "memory": "281383" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\n unordered_map<int, vector<int>> g;\n vector<vector<int>> dp;\n int solve(int ind, int mask) {\n if (ind >= 101) {\n return 0;\n }\n\n if (dp[ind][mask] != -1) {\n return dp[ind][mask];\n }\n\n int ans = solve(ind + 1, mask);\n for (const auto& row : g[ind]) {\n if (((1 << row) & mask) == 0) {\n ans =\n max(ans, ind + solve(ind + 1, (mask | (1 << row))));\n }\n }\n\n return dp[ind][mask] = ans;\n }\n\npublic:\n int maxScore(vector<vector<int>>& grid) {\n for (int i = 0; i < grid.size(); i++) {\n for (int j = 0; j < grid[0].size(); j++) {\n g[grid[i][j]].push_back(i);\n }\n }\n\n dp = vector<vector<int>>(101, vector<int>(1200, -1));\n\n return solve(0, 0);\n }\n};", "memory": "297568" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\n unordered_map<int, vector<int>> g;\n vector<vector<int>> dp;\n int maxScoreUtil(int ind, int mask) {\n if(ind >= 101) {\n return 0;\n }\n if(dp[ind][mask] != -1) {\n return dp[ind][mask];\n }\n int ans = maxScoreUtil(ind + 1, mask);\n for (const auto& row : g[ind]) {\n if (((1 << row) & mask) == 0) {\n ans = max(ans, ind + maxScoreUtil(ind + 1, (mask | (1 << row))));\n }\n }\n return dp[ind][mask] = ans;\n }\n\npublic:\n int maxScore(vector<vector<int>>& grid) {\n for (int i = 0; i < grid.size(); i++) {\n for (int j = 0; j < grid[0].size(); j++) {\n g[grid[i][j]].push_back(i);\n }\n }\n\n dp = vector<vector<int>>(102, vector<int>(1200, -1));\n\n return maxScoreUtil(0, 0);\n }\n};", "memory": "297568" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\n unordered_map<int, vector<int>> g;\n vector<vector<int>> dp;\n int maxScoreUtil(int ind, int mask) {\n if (ind >= 101) {\n return 0;\n }\n\n if (dp[ind][mask] != -1) {\n return dp[ind][mask];\n }\n\n int ans = maxScoreUtil(ind + 1, mask);\n for (const auto& row : g[ind]) {\n if (((1 << row) & mask) == 0) {\n ans =\n max(ans, ind + maxScoreUtil(ind + 1, (mask | (1 << row))));\n }\n }\n\n return dp[ind][mask] = ans;\n }\n\npublic:\n int maxScore(vector<vector<int>>& grid) {\n for (int i = 0; i < grid.size(); i++) {\n for (int j = 0; j < grid[0].size(); j++) {\n g[grid[i][j]].push_back(i);\n }\n }\n\n dp = vector<vector<int>>(102, vector<int>(1200, -1));\n\n return maxScoreUtil(0, 0);\n }\n};", "memory": "301614" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\n unordered_map<int, vector<int>> g;\n vector<vector<int>> dp;\n int solve(int ind, int mask) {\n if (ind >= 101) {\n return 0;\n }\n\n if (dp[ind][mask] != -1) {\n return dp[ind][mask];\n }\n\n int ans = solve(ind + 1, mask);\n for (const auto& row : g[ind]) {\n if (((1 << row) & mask) == 0) {\n ans =\n max(ans, ind + solve(ind + 1, (mask | (1 << row))));\n }\n }\n\n return dp[ind][mask] = ans;\n }\n\npublic:\n int maxScore(vector<vector<int>>& grid) {\n for (int i = 0; i < grid.size(); i++) {\n for (int j = 0; j < grid[0].size(); j++) {\n g[grid[i][j]].push_back(i);\n }\n }\n\n dp = vector<vector<int>>(102, vector<int>(1200, -1));\n\n return solve(0, 0);\n }\n};", "memory": "301614" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fun(int indx, int num, vector<pair<int, int>> &vp, vector<vector<int>> &dp) {\n int n = vp.size();\n if (indx == n) {\n return 0;\n }\n if (dp[indx][num] != -1) {\n return dp[indx][num];\n }\n \n int j = indx;\n while (j < n - 1 && vp[indx].first == vp[j + 1].first) {\n j++;\n }\n \n int ans = 0;\n for (int i = indx; i <= j; i++) {\n int index = vp[i].second;\n if (!(1 << index & num)) {\n num += 1 << index; // Replace pow(2, index) with 1 << index\n ans = max(ans, vp[indx].first + fun(j + 1, num, vp, dp));\n num -= 1 << index; // Replace pow(2, index) with 1 << index\n }\n }\n ans = max(ans, fun(j + 1, num, vp, dp));\n \n return dp[indx][num] = ans;\n }\n\n int maxScore(vector<vector<int>> &grid) {\n set<pair<int, int>> s;\n int m = grid.size();\n int n = grid[0].size();\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n s.insert({grid[i][j], i});\n }\n }\n \n vector<pair<int, int>> vp(s.begin(), s.end());\n sort(vp.rbegin(), vp.rend()); // Reverse sorting\n \n int num = 0;\n vector<vector<int>> dp(101, vector<int>(3000, -1));\n int sum = fun(0, num, vp, dp);\n return sum;\n }\n};\n", "memory": "305660" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int fun(int indx, int num, vector<pair<int, int>> &vp, vector<vector<int>> &dp) {\n int n = vp.size();\n if (indx == n) {\n return 0;\n }\n if (dp[indx][num] != -1) {\n return dp[indx][num];\n }\n \n int j = indx;\n while (j < n - 1 && vp[indx].first == vp[j + 1].first) {\n j++;\n }\n \n int ans = 0;\n for (int i = indx; i <= j; i++) {\n int index = vp[i].second;\n if (!(1 << index & num)) {\n num += 1 << index; // Replace pow(2, index) with 1 << index\n ans = max(ans, vp[indx].first + fun(j + 1, num, vp, dp));\n num -= 1 << index; // Replace pow(2, index) with 1 << index\n }\n }\n ans = max(ans, fun(j + 1, num, vp, dp));\n \n return dp[indx][num] = ans;\n }\n\n int maxScore(vector<vector<int>> &grid) {\n set<pair<int, int>> s;\n int m = grid.size();\n int n = grid[0].size();\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n s.insert({grid[i][j], i});\n }\n }\n \n vector<pair<int, int>> vp(s.begin(), s.end());\n sort(vp.rbegin(), vp.rend()); // Reverse sorting\n \n int num = 0;\n vector<vector<int>> dp(101, vector<int>(3000, -1));\n int sum = fun(0, num, vp, dp);\n return sum;\n }\n};\n", "memory": "305660" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\n int rows;\n int cols;\npublic:\n int recur(vector<vector<int>>& values, int idx, int mask_row,vector<vector<int>> &dp)\n {\n int n= values.size();\n if(idx == n)\n return 0;\n if(mask_row == (1<<rows)-1)\n return 0;\n \n if(dp[idx][mask_row] != -1)\n return dp[idx][mask_row];\n \n int ans = 0;\n int row = values[idx][1];\n if((1<<row) & mask_row)\n ans += recur(values, idx+1, mask_row, dp);\n else\n {\n int j = idx;\n while (j< n and values[idx][0]== values[j][0])\n j++;\n \n int ans1= values[idx][0]+ recur(values, j, mask_row | (1<<row), dp);\n int ans2 = recur(values, idx+1, mask_row, dp);\n \n ans= max(ans1, ans2);\n }\n \n return dp[idx][mask_row]= ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n int n= grid.size();\n rows = n;\n int m= grid[0].size();\n cols = m;\n vector<vector<int>> values;\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<m; j++)\n {\n values.push_back({grid[i][j], i, j});\n }\n }\n \n \n sort(values.begin(), values.end(), greater<vector<int>>());\n vector<vector<int>> dp(n*m,vector<int>(10000,-1)); \n return recur(values, 0, 0, dp);\n    }\n};", "memory": "309706" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n map<int, set<int>> m;\n vector<int> vis;\n unordered_map<string, int> dp;\n\n string stateKey() {\n string key;\n for (int v : vis) {\n key += to_string(v) + \",\";\n }\n return key;\n }\n\n int rec(auto it) {\n if (it == m.end()) {\n return 0;\n }\n \n string key = stateKey() + to_string(it->first);\n if (dp.count(key)) return dp[key];\n \n int res = rec(next(it));\n for (int row : it->second) {\n if (!vis[row]) {\n vis[row] = 1;\n res = max(res, it->first + rec(next(it)));\n vis[row] = 0;\n }\n }\n\n return dp[key] = res;\n }\n\n int maxScore(vector<vector<int>>& grid) {\n int rows = grid.size();\n int cols = grid[0].size();\n vis = vector<int>(rows, 0);\n\n for (int i = 0; i < rows; i++) {\n for (int j = 0; j < cols; j++) {\n m[grid[i][j]].insert(i);\n }\n }\n\n return rec(m.begin());\n }\n};", "memory": "313753" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int f(int ind, int mask, vector<pair<int,int>> &nums, vector<vector<int>> &dp){\n if(ind==nums.size()) return 0;\n if(dp[ind][mask]!=-1) return dp[ind][mask];\n int fs=f(ind+1,mask,nums,dp);\n int ss=0;\n if((mask&(1<<nums[ind].second))==0){\n int curr=nums[ind].first;\n int nxt=ind;\n while(nxt<nums.size() && nums[nxt].first==curr) nxt++;\n ss=curr+f(nxt,mask^(1<<nums[ind].second),nums,dp);\n }\n return dp[ind][mask]=max(fs,ss);\n }\n int maxScore(vector<vector<int>>& grid) {\n int n=grid.size();\n int m=grid[0].size();\n vector<vector<int>> dp(n*m+1,vector<int>(10000,-1));\n vector<pair<int,int>> nums;\n for(int i=0; i<n; i++){\n for(int j=0; j<m; j++){\n nums.push_back({grid[i][j],i});\n }\n }\n sort(nums.begin(),nums.end());\n return f(0,0,nums,dp);\n }\n};", "memory": "317799" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int find(int n, int bitmask, vector<set<int>> &temp, vector<vector<int>> &dp){\n if(n>100) return 0;\n \n if(dp[n][bitmask]!=-1) return dp[n][bitmask];\n \n int maxA=find(n+1, bitmask, temp, dp);\n \n\n for(auto i: temp[n]){\n int row=i;\n //check if row is absent in bitmask\n int bit=((bitmask&(1<<row))==0)?0:1;\n if(!bit){\n maxA=max(maxA, n+find(n+1, bitmask|(1<<i), temp, dp));\n }\n }\n \n \n \n return dp[n][bitmask]=maxA;\n }\n \n int maxScore(vector<vector<int>>& grid) {\n \n vector<set<int>> temp(101);\n \n for(int i=0; i<grid.size(); i++){\n for(int j=0; j<grid[0].size(); j++){\n temp[grid[i][j]].insert(i);\n }\n }\n \n// for(int i=1; i<=4; i++){\n// for(auto j: temp[i]){\n// cout<<j<<\" \";\n// }\n// cout<<endl;\n// }\n vector<vector<int>> dp(101, vector<int>(1100, -1));\n \n int ans=find(1, 0, temp, dp);\n \n return ans;\n }\n};", "memory": "317799" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int helper(int val, int mask, int sz, map<int, vector<int>>& mp, vector<vector<int>>& dp) {\n if(__builtin_popcount(mask) == sz) {\n return 0;\n }\n if(val > 100) {\n return 0;\n }\n if(dp[val][mask] != -1) {\n return dp[val][mask];\n }\n\n int ans = helper(val + 1, mask, sz, mp, dp);\n \n if(mp.count(val)) {\n for(auto& e : mp[val]) {\n if(mask & (1 << e)) continue;\n else {\n ans = max(ans, val + helper(val + 1, mask | (1 << e), sz, mp, dp));\n }\n }\n } \n\n return dp[val][mask] = ans;\n }\n\n int maxScore(vector<vector<int>>& grid) {\n map<int, vector<int>> mp;\n for(int i = 0; i < grid.size(); i++) {\n for(int j = 0; j < grid[i].size(); j++) {\n mp[grid[i][j]].push_back(i);\n }\n }\n vector<vector<int>> dp(101, vector<int>(1100, -1));\n return helper(0, 0, grid.size(), mp, dp);\n }\n};", "memory": "321845" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "\nclass Cell {\npublic:\n int value;\n int row;\n\n Cell() {}\n\n Cell(int value_, int row_){\n value = value_;\n row = row_;\n }\n};\n\nbool operator<(const Cell & a, const Cell & b){\n return a.value < b.value;\n}\n\nclass Solution {\npublic:\n\n Solution() {}\n\n vector<Cell> nums;\n int n;\n\n //rows picked is a bitmask\n\n vector<vector<int>> memo;\n\n int dp(int i, int rowsPicked) {\n if (i >= n){\n return 0;\n }\n int & ans = memo[i][rowsPicked];\n if (ans != -1){\n return ans;\n }\n ans = dp(i+1, rowsPicked);\n\n if (((rowsPicked >> nums[i].row) & 1) == 1){\n return ans;\n }\n\n int nextPosition = i;\n while(nextPosition < n && nums[nextPosition].value == nums[i].value){\n ++nextPosition;\n }\n //rowsPicked |= (1 <<1 nums[i].rows);\n ans = max(ans, dp(nextPosition, rowsPicked | (1 << nums[i].row))+nums[i].value);\n\n return ans;\n }\n\n int maxScore(vector<vector<int>>& grid) {\n memo = vector<vector<int>>(105, vector<int>(1100, -1));\n for (int r = 0; r < grid.size(); ++r){\n for (int c = 0; c < grid[0].size(); ++c){\n nums.push_back(Cell(grid[r][c], r));\n }\n }\n\n sort(nums.begin(), nums.end());\n n = nums.size();\n\n return dp(0, 0);\n }\n\n int bruteForce(unordered_set<int> & visitedNumbers, int row, vector<vector<int>>& grid){\n if (row == grid.size()){\n return 0;\n }\n int ans = 0;\n\n for (auto & it : grid[row]){\n if (visitedNumbers.find(it) != visitedNumbers.end()){\n continue;\n }\n visitedNumbers.insert(it);\n ans = max(ans, bruteForce(visitedNumbers, row+1, grid)+it);\n visitedNumbers.erase(it);\n }\n\n return ans;\n }\n\n int bruteForce(vector<vector<int>>& grid){\n \n unordered_set<int> visited;\n return bruteForce(visited, 0, grid);\n }\n};\n", "memory": "321845" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n map<pair<int,int>, int> dp;\n int solve(vector<pair<int, int>>& v, int i, int rowSelected)\n {\n if(i == v.size()) return 0;\n \n if(dp.find({i, rowSelected})!= dp.end()) return dp[{i, rowSelected}];\n \n int res = 0;\n int row = v[i].second;\n if((1<<row) & rowSelected)\n res = solve(v, i+1, rowSelected);\n else\n {\n int j = i;\n while (j< v.size() && v[i].first== v[j].first)\n j++;\n res = max(res, v[i].first+ solve(v, j, rowSelected | (1<<row)));\n res = max(res, solve(v, i+1, rowSelected));\n }\n return dp[{i, rowSelected}]= res;\n \n }\n \n int maxScore(vector<vector<int>>& grid) {\n vector<pair<int, int>> v;\n for(int r = 0; r < grid.size(); ++r){\n for(int c = 0; c < grid[0].size(); ++c) v.push_back({grid[r][c], r});\n }\n sort(v.begin(), v.end());\n return solve(v, 0, 0);\n }\n};\n\n\n\n\n/*class Solution {\npublic:\n unordered_map<string, int> dp;\n int solve(vector<pair<int,int>>& v, int i, int rowSelected){\n if(i >= v.size()) return 0;\n string s = to_string(i) + \"_\" + to_string(rowSelected);\n if(dp.find(s) != dp.end()) return dp[s];\n int res = 0;\n if(((1 << v[i].second ) & rowSelected)){\n res = max(res, solve(v, i+1, rowSelected));\n }else{\n int t = i;\n for(int k = 0; k < v.size() && v[i].first == v[k].first; ++k) t++;\n res = max(res, v[i].first + solve(v, t, rowSelected | (1 << v[i].second)));\n res = max(res, solve(v, i+1, rowSelected));\n } \n \n \n return dp[s] = res;\n }\n int maxScore(vector<vector<int>>& grid) {\n vector<pair<int, int>> v;\n for(int r = 0; r < grid.size(); ++r){\n for(int c = 0; c < grid[0].size(); ++c) v.push_back({grid[r][c], r});\n }\n sort(v.begin(), v.end());\n return solve(v, 0, 0);\n \n }\n};*/", "memory": "325891" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n map<pair<int,int>, int> dp;\n int solve(vector<pair<int, int>>& v, int idx, int mask_row)\n {\n int n= v.size();\n if(idx == n)\n return 0;\n \n if(dp.find({idx, mask_row})!= dp.end())\n return dp[{idx, mask_row}];\n \n int res = 0;\n int row = v[idx].second;\n if((1<<row) & mask_row)\n res = solve(v, idx+1, mask_row);\n else\n {\n int j = idx;\n while (j< n and v[idx].first== v[j].first)\n j++;\n \n res = max(res, v[idx].first+ solve(v, j, mask_row | (1<<row)));\n res = max(res, solve(v, idx+1, mask_row));\n }\n return dp[{idx, mask_row}]= res;\n \n }\n \n int maxScore(vector<vector<int>>& grid) {\n vector<pair<int, int>> v;\n for(int r = 0; r < grid.size(); ++r){\n for(int c = 0; c < grid[0].size(); ++c) v.push_back({grid[r][c], r});\n }\n sort(v.begin(), v.end());\n return solve(v, 0, 0);\n }\n};\n\n\n\n\n/*class Solution {\npublic:\n unordered_map<string, int> dp;\n int solve(vector<pair<int,int>>& v, int i, int rowSelected){\n if(i >= v.size()) return 0;\n string s = to_string(i) + \"_\" + to_string(rowSelected);\n if(dp.find(s) != dp.end()) return dp[s];\n int res = 0;\n if(((1 << v[i].second ) & rowSelected)){\n res = max(res, solve(v, i+1, rowSelected));\n }else{\n int t = i;\n for(int k = 0; k < v.size() && v[i].first == v[k].first; ++k) t++;\n res = max(res, v[i].first + solve(v, t, rowSelected | (1 << v[i].second)));\n res = max(res, solve(v, i+1, rowSelected));\n } \n \n \n return dp[s] = res;\n }\n int maxScore(vector<vector<int>>& grid) {\n vector<pair<int, int>> v;\n for(int r = 0; r < grid.size(); ++r){\n for(int c = 0; c < grid[0].size(); ++c) v.push_back({grid[r][c], r});\n }\n sort(v.begin(), v.end());\n return solve(v, 0, 0);\n \n }\n};*/", "memory": "325891" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "//TLE\nclass Solution1 {\npublic:\n void backtrack(int i, vector<vector<int>>& grid, int sum, int& res, vector<int>& cur) {\n bool select = false;\n if (i == grid.size()) {\n res = max(res, sum);\n return;\n }\n\n //\n\n for (int j = 0; j < grid[i].size(); j++) {\n if (j > 0 && grid[i][j] == grid[i][j - 1]) continue;\n if (cur[grid[i][j]] == 0) {\n cur[grid[i][j]] = 1;\n select = true;\n backtrack(i + 1, grid, sum + grid[i][j], res, cur);\n cur[grid[i][j]] = 0;\n }\n }\n\n if (!select) {\n backtrack(i + 1, grid, sum, res, cur);\n }\n }\n\n int maxScore(vector<vector<int>>& grid) {\n int res = 0;\n vector<int> cur(101, 0);\n\n for (auto& arr : grid) {\n sort(arr.begin(), arr.end(), greater<>());\n }\n\n backtrack(0, grid, 0, res, cur);\n\n return res;\n }\n};\n\n\nclass Solution0 {\npublic:\n int recur(vector<vector<int>>& values, int idx, int mask_row, map<pair<int,int>, int>& dp) {\n int n= values.size();\n if (idx == n) return 0;\n int ans = 0;\n int row = values[idx][1];\n\n if (dp.find({idx, mask_row})!= dp.end()) {\n return dp[{idx, mask_row}];\n }\n \n\n if ((1<<row) & mask_row) {\n ans += recur(values, idx+1, mask_row, dp);\n } else {\n int j = idx;\n\n while (j< n && values[idx][0]== values[j][0]) {\n j++;\n }\n \n int ans1 = values[idx][0] + recur(values, j, mask_row | (1<<row), dp);\n int ans2 = recur(values, idx + 1, mask_row, dp);\n \n ans= max(ans1, ans2);\n }\n \n return dp[{idx, mask_row}] = ans; \n }\n\n int maxScore(vector<vector<int>>& grid) {\n int n= grid.size(), m= grid[0].size();\n vector<vector<int>> values;\n map<pair<int,int>, int> dp;\n //unordered_map<int, unordered_map<int, int>> dp;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n values.push_back({grid[i][j], i, j});\n }\n }\n sort(values.begin(), values.end(), greater<vector<int>>());\n return recur(values, 0, 0, dp);\n }\n};\n\nclass Solution {\npublic:\n int recur(vector<vector<int>>& values, int idx, int mask_row, unordered_map<int, unordered_map<int, int>>& dp) {\n int n= values.size();\n if (idx == n) return 0;\n int ans = 0;\n int row = values[idx][1];\n\n if (dp.count(idx) > 0 && dp[idx].count(mask_row) > 0 ) {\n return dp[idx][mask_row];\n }\n \n\n if ((1<<row) & mask_row) {\n ans += recur(values, idx + 1, mask_row, dp);\n } else {\n int j = idx;\n\n while (j< n && values[idx][0]== values[j][0]) {\n j++;\n }\n \n int ans1 = values[idx][0] + recur(values, j, mask_row | (1<<row), dp);\n int ans2 = recur(values, idx + 1, mask_row, dp);\n \n ans= max(ans1, ans2);\n }\n \n return dp[idx][mask_row] = ans; \n }\n\n int maxScore(vector<vector<int>>& grid) {\n int n= grid.size(), m= grid[0].size();\n vector<vector<int>> values;\n //map<pair<int,int>, int> dp;\n unordered_map<int, unordered_map<int, int>> dp;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n values.push_back({grid[i][j], i, j});\n }\n }\n sort(values.begin(), values.end(), greater<vector<int>>());\n return recur(values, 0, 0, dp);\n }\n};", "memory": "329938" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "//TLE\nclass Solution1 {\npublic:\n void backtrack(int i, vector<vector<int>>& grid, int sum, int& res, vector<int>& cur) {\n bool select = false;\n if (i == grid.size()) {\n res = max(res, sum);\n return;\n }\n\n //\n\n for (int j = 0; j < grid[i].size(); j++) {\n if (j > 0 && grid[i][j] == grid[i][j - 1]) continue;\n if (cur[grid[i][j]] == 0) {\n cur[grid[i][j]] = 1;\n select = true;\n backtrack(i + 1, grid, sum + grid[i][j], res, cur);\n cur[grid[i][j]] = 0;\n }\n }\n\n if (!select) {\n backtrack(i + 1, grid, sum, res, cur);\n }\n }\n\n int maxScore(vector<vector<int>>& grid) {\n int res = 0;\n vector<int> cur(101, 0);\n\n for (auto& arr : grid) {\n sort(arr.begin(), arr.end(), greater<>());\n }\n\n backtrack(0, grid, 0, res, cur);\n\n return res;\n }\n};\n\nclass Solution {\npublic:\n int recur(vector<vector<int>>& values, int i, int mask, unordered_map<int, unordered_map<int, int>>& dp) {\n int n= values.size();\n \n if (i == n) {\n return 0;\n }\n\n int row = values[i][1];\n\n if (dp.count(i) > 0 && dp[i].count(mask) > 0 ) {\n return dp[i][mask];\n }\n \n\n if ((1 << row) & mask) {\n dp[i][mask] = recur(values, i + 1, mask, dp);\n } else {\n int j = i;\n\n while (j < n && values[i][0]== values[j][0]) {\n j++;\n }\n \n dp[i][mask] = max(values[i][0] + recur(values, j, mask | (1 << row), dp),\n recur(values, i + 1, mask, dp));\n \n }\n \n return dp[i][mask];\n }\n\n int maxScore(vector<vector<int>>& grid) {\n int n= grid.size(), m= grid[0].size();\n vector<vector<int>> values;\n //map<pair<int,int>, int> dp;\n unordered_map<int, unordered_map<int, int>> dp;\n\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n values.push_back({grid[i][j], i, j});\n }\n }\n sort(values.begin(), values.end(), greater<vector<int>>());\n return recur(values, 0, 0, dp);\n }\n};", "memory": "329938" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int n = grid.size();\n int m = grid[0].size();\n vector<vector<int>> values;\n\n \n for (int i = 0; i < n; ++i) {\n for (int j = 0; j < m; ++j) {\n values.push_back({grid[i][j], i, j});\n }\n }\n\n \n sort(values.rbegin(), values.rend());\n int tot = values.size();\n\n \n unordered_map<int, unordered_map<int, int>> memo;\n\n function<int(int, int)> solve = [&](int ind, int mask) {\n if (ind == tot) {\n return 0;\n }\n\n \n if (memo[ind].count(mask)) {\n return memo[ind][mask];\n }\n\n int ans = 0;\n int row = values[ind][1];\n \n \n if ((1 << row) & mask) {\n ans = solve(ind + 1, mask);\n } else {\n int j = ind;\n \n while (j < tot && values[ind][0] == values[j][0]) {\n j++;\n }\n\n \n ans = max(values[ind][0] + solve(j, mask | (1 << row)), solve(ind + 1, mask));\n }\n\n \n return memo[ind][mask] = ans;\n };\n\n \n return solve(0, 0);\n }\n};", "memory": "333984" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<pair<int,int>>&a, int index, int bitmask, vector<vector<int>>&dp){\n if(index>=a.size()){\n return 0;\n } \n\n int row=a[index].second;\n int val=a[index].first;\n\n int &ans = dp[index][bitmask];\n\n if(ans!=-1){\n return ans;\n }\n\n int take = 0;\n int notTake = 0;\n if((bitmask&(1<<row)) == 0){\n int nextIndex = index;\n while(nextIndex < a.size() && a[index].first == a[nextIndex].first){\n nextIndex++;\n }\n take = val + solve(a, nextIndex, (bitmask|(1<<row)), dp);\n }\n notTake = solve(a, index+1, bitmask, dp);\n return ans = max(take, notTake);\n }\n\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n\n vector<pair<int,int>>a;\n for(int i=0;i<n;i++){\n for(auto x:grid[i]){\n a.push_back({x,i});\n }\n }\n vector<vector<int>>dp(100, vector<int>(1025,-1));\n sort(a.begin(),a.end());\n return solve(a,0,0,dp);\n }\n};", "memory": "333984" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\n int func(int val, int rowMask, vector<vector<int>>& dp, int r, int c, vector<vector<int>>& grid) {\n if(val > 100)\n return 0;\n\n if(dp[val][rowMask] != -1)\n return dp[val][rowMask];\n\n int res = func(val+1, rowMask, dp, r, c, grid);\n\n for(int row=0;row<r;row++) {\n // row is used;\n if(rowMask & (1 << row))\n continue;\n\n for(int col=0;col<c;col++) {\n // not the desired number to pick\n if(grid[row][col] != val)\n continue;\n\n // we will select this val and mark the row selection \n res = max(res, val+func(val+1,rowMask|(1<<row),dp,r,c,grid)); \n }\n }\n\n return dp[val][rowMask] = res;\n }\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int r = grid.size();\n int c = grid[0].size();\n\n vector<vector<int>> dp(101, vector<int>(1024,-1));\n return func(1, 0, dp, r, c, grid);\n }\n};", "memory": "338030" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int n, int i, vector<pair<int,int>>& v, int mask, map<pair<int,int>, int>& mp) {\n if (i >= n) return 0;\n if (mp.find({i, mask}) != mp.end()) return mp[{i, mask}];\n mp[{i, mask}] = 0;\n if ((1 << v[i].second) & mask) {\n mp[{i, mask}] = solve(n, i + 1, v, mask, mp); // not take\n } else {\n int j = i;\n while (j < n && v[i].first == v[j].first) j++;\n int take = v[i].first + solve(n, j, v, mask | (1 << v[i].second), mp);\n int notTake = solve(n, i + 1, v, mask, mp);\n mp[{i, mask}] = max(take, notTake);\n }\n return mp[{i, mask}];\n }\n\n int maxScore(vector<vector<int>>& grid) {\n vector<pair<int,int>> v;\n int n = grid.size();\n int m = grid[0].size();\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n v.push_back({grid[i][j], i});\n }\n }\n sort(v.begin(), v.end());\n map<pair<int,int>, int> mp;\n return solve(n * m, 0, v, 0, mp);\n }\n};\n", "memory": "338030" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int ans(int i,int mask,map<int,vector<int>> &arr,vector<vector<int>> &dp){\n if(i==101)return 0;\n if(dp[i][mask]!=-1)return dp[i][mask];\n int res = ans(i+1,mask,arr,dp);\n for(auto &j:arr[i]){\n if(mask&(1<<j)) continue;\n res=max(res,i+ans(i+1,mask+(1<<j),arr,dp));\n }\n return dp[i][mask]=res;\n }\n int maxScore(vector<vector<int>>& grid) {\n map<int,vector<int>> arr;\n vector<vector<int>> dp(101,vector<int>(1030,-1));\n for(int i=0;i<grid.size();i++){\n for(int j=0;j<grid[0].size();j++) arr[grid[i][j]].push_back(i);\n }\n return ans(1,0,arr,dp);\n }\n};", "memory": "342076" }
3,563
<p>You are given a 2D matrix <code>grid</code> consisting of positive integers.</p> <p>You have to select <em>one or more</em> cells from the matrix such that the following conditions are satisfied:</p> <ul> <li>No two selected cells are in the <strong>same</strong> row of the matrix.</li> <li>The values in the set of selected cells are <strong>unique</strong>.</li> </ul> <p>Your score will be the <strong>sum</strong> of the values of the selected cells.</p> <p>Return the <strong>maximum</strong> score you can achieve.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,3,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png" /></p> <p>We can select the cells with values 1, 3, and 4 that are colored above.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[8,7,6],[8,3,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png" style="width: 170px; height: 114px;" /></p> <p>We can select the cells with values 7 and 8 that are colored above.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 10</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int val, int mask, map<int, vector<int>>& mp, vector<vector<int>>& dp) {\n if (val == 0) return 0;\n\n if (dp[val][mask] != -1) return dp[val][mask];\n\n int ans = solve(val - 1, mask, mp, dp);\n\n \n for (auto i : mp[val]) {\n if (!(mask & (1 << i))) { // Check if row i is not used\n ans = max(ans, val + solve(val - 1, mask | (1 << i), mp, dp));\n }\n }\n\n return dp[val][mask] = ans;\n }\n\n int maxScore(vector<vector<int>>& grid) {\n map<int, vector<int>> mp;\n vector<vector<int>> dp(101, vector<int>(1030, -1));\n\n for (int i = 0; i < grid.size(); i++) {\n for (int j = 0; j < grid[0].size(); j++) {\n mp[grid[i][j]].push_back(i);\n }\n }\n\n return solve(100, 0, mp, dp); \n }\n};\n\n// store the row of each element and try with all.\n// try will all elements i.e 1 to 100;\n// mask to keep track of rows \n", "memory": "342076" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "auto fast = (ios::sync_with_stdio(0), cout.tie(0), cin.tie(0), false);\n\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n // Set keys are start indices (l). set values are xors.\n vector<vector<pair<int, int>>> best_endpoints_by_start(n);\n \n vector<int> top_xors_by_start(n, -1);\n int span_length_minus_1 = 0;\n vector<int> last_round_xors_by_start = nums;\n while (!last_round_xors_by_start.empty()) {\n int remaining = last_round_xors_by_start.size();\n auto* top_ptr = &top_xors_by_start.front() - 1;\n auto* last_round_ptr = &last_round_xors_by_start.front() - 1;\n for (int i = 0; i < remaining; ++i) {\n auto& top = *++top_ptr;\n auto last = *++last_round_ptr;\n if (last > top) {\n best_endpoints_by_start[i].emplace_back(i + span_length_minus_1, top = last);\n }\n }\n auto *xp = &last_round_xors_by_start.front() - 1;\n auto *xpe = &last_round_xors_by_start.back();\n while (++xp != xpe) {\n *xp ^= xp[1];\n }\n last_round_xors_by_start.pop_back();\n ++span_length_minus_1;\n } \n vector<int> ans;\n ans.reserve(queries.size());\n for (const auto& query : queries) {\n int l = query.front() - 1, r = query[1];\n int x = 0;\n while (++l <= r) {\n auto& best_endpoints_vec = best_endpoints_by_start[l];\n if (best_endpoints_vec.back().first <= r) {\n x = max(x, best_endpoints_vec.back().second);\n } else for (const auto& p : best_endpoints_vec) {\n if (p.first > r) {\n x = max(x, (&p - 1)->second);\n break;\n }\n }\n // x = max(x,\n // (upper_bound(best_endpoints_vec.begin(), best_endpoints_vec.end(), make_pair(r, INT_MAX))-1)->second);\n }\n ans.push_back(x);\n }\n return ans;\n }\n};\n\n\n\n\n/*\n lettter counts\n a b c d e f g h\na b c d e f g h -> 0 1 1 1 1 1 1 1 1\na^b b^c c^d d^e e^f f^g g^h -> (both ends) 1 1 2 2 2 2 2 2 1\na ^ c b^d c^e d^f e^g f^h -> (center absent) 2\na^b^c^d b^c^d^e c^d^e^f d^e^f^g e^f^g^h -> (both centers present) 3\na^e b^f c^g d^h -> (all 3 centers absent) 4\na^b^e^f b^c^f^g c^d^g^h -> (p p a p p) --- edges + 2 5\na^c^e^g b^d^f^h -> p a p a p a p --- edges + 2 6\na^b^c^d^e^f^g^h 7\n\n\n\na b -> a^b\na b c -> a^b b^c -> a^c\na b c d -> a^b b^c c^d -> a^c b^d -> a^b^c^d\na b c d e -> a^b b^c c^d d^e -> a^c b^d c^e -> a^b^c b^c^d c^d^e -> a^d b^e -> a^b^d^e\n\na^c ^ b^d ^ c^e = a^b^d^e\n\na b c d e f -> ab bc cd de ef -> ac bd ce df -> abcd bcde cdef -> ae bf -> abef\nabcd ^ cdef = abef\n\nabcdefg -> ab bc cd de ef fg -> ac bd ce df eg -> abcd bcde cdef defg -> ae bf cg -> abef bcfg -> aceg\n\na b c -> a^b b^c -> a^b^b^c\na b c d-> a^b b^c c^d -> a^b^b^c b^c^c^d -> a^b^b^b^c^c^c^d\na b c d e -> ab bc cd de -> abbc bccd cdde -> abbbcccd bcccddde -> abbbbccccccdddde (14641)\na b c d e f -> ab bc cd de ef -> abbc b\n\n\n \n\n a b c d\n ab bc cd \n ac cd\n ad\n\n 1\n 1 1\n 1 2 1\n 1 3 3 1\n 1 4 6 4 1\n 1 5 10 10 5 1\n\n[2,8,4,32,16,1]\n\n\n\n\n*/", "memory": "274823" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "int vals[2001000];\nint speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0; }();\n\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int N = size(nums);\n copy(begin(nums), end(nums), vals);\n for (int i = 1, w = N; i != w; ++i)\n for (int e = w; i != e; ++i, ++w) vals[w] = vals[i-1] ^ vals[i];\n for (int i = 1, w = N; i != w; ++i)\n for (int e = w; i != e; ++i, ++w) vals[w] = max(vals[w], max(vals[i-1], vals[i]));\n \n N = 2*N+1;\n vector<int> res; res.reserve(size(queries));\n for (const auto &q: queries) {\n int n = q[1] - q[0];\n res.push_back(vals[n * (N - n)/2 + q[0]]);\n }\n return res;\n }\n};", "memory": "274823" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "int vals[2001000];\nint speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0; }();\n\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int N = size(nums);\n copy(begin(nums), end(nums), vals);\n for (int i = 1, w = N; i != w; ++i)\n for (int e = w; i != e; ++i, ++w) vals[w] = vals[i-1] ^ vals[i];\n for (int i = 1, w = N; i != w; ++i)\n for (int e = w; i != e; ++i, ++w) vals[w] = max(vals[w], max(vals[i-1], vals[i]));\n \n N = 2*N+1;\n vector<int> res; res.reserve(size(queries));\n for (const auto &q: queries) {\n int n = q[1] - q[0];\n res.push_back(vals[n * (N - n)/2 + q[0]]);\n }\n return res;\n }\n};", "memory": "277669" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "int vals[2001000];\nint speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0; }();\n\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int N = size(nums);\n copy(begin(nums), end(nums), vals);\n for (int i = 1, w = N; i != w; ++i)\n for (int e = w; i != e; ++i, ++w) vals[w] = vals[i-1] ^ vals[i];\n for (int i = 1, w = N; i != w; ++i)\n for (int e = w; i != e; ++i, ++w) vals[w] = max(vals[w], max(vals[i-1], vals[i]));\n \n N = 2*N+1;\n vector<int> res; res.reserve(size(queries));\n for (const auto &q: queries) {\n int n = q[1] - q[0];\n res.push_back(vals[n * (N - n)/2 + q[0]]);\n }\n return res;\n }\n};", "memory": "277669" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "constexpr int MAX = 2048;\n\nint dp[MAX][MAX];\n\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size(), m = queries.size();\n for (int i = 0; i < n; ++i) {\n dp[0][i] = nums[i];\n }\n for (int d = 1; d < n; ++d) {\n for (int i = 0; i + d < n; ++i) {\n dp[d][i] = (dp[d - 1][i] ^ dp[d - 1][i + 1]);\n }\n }\n for (int d = 1; d < n; ++d) {\n for (int i = 0; i + d < n; ++i) {\n dp[d][i] = max({dp[d][i], dp[d - 1][i], dp[d - 1][i + 1]});\n }\n }\n vector<int> ret(m);\n for (int i = 0; i < m; ++i) {\n int l = queries[i][0], r = queries[i][1];\n ret[i] = dp[r - l][l];\n }\n return ret;\n }\n};", "memory": "280515" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "const int maxn = 2e3 + 5;\nint M[maxn][maxn];\nclass Solution {\npublic:\n\nvector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries)\n{\n int n = (int)nums.size(), Q = (int)queries.size();\n for(int i = 0; i < n; i++) M[i][i] = nums[i];\n for(int l = 2; l <= n; l++)\n for(int i = 0; i + l <= n; i++)\n {\n int j = i + l - 1;\n M[i][j] = M[i][j - 1] ^ M[i + 1][j];\n }\n for(int l = 2; l <= n; l++)\n for(int i = 0; i + l <= n; i++)\n {\n int j = i + l - 1;\n M[i][j] = max(M[i][j], max(M[i][j - 1], M[i + 1][j]));\n }\n vector<int> ans(Q);\n for(int i = 0; i < Q; i++)\n ans[i] = M[queries[i][0]][queries[i][1]];\n return ans;\n}\n};", "memory": "283361" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n int q = queries.size();\n vector<vector<int>> mp(n+1);\n for(int i=0; i<q; i++)\n mp[queries[i][1]-queries[i][0]+1].push_back(i);\n \n vector<int> arr(nums), res(q);\n for(const int& idx : mp[1])\n res[idx] = arr[queries[idx][0]];\n \n for(int len=2; len<=n; len++)\n {\n for(int i=0; i<n-len+1; i++)\n arr[i] ^= arr[i+1];\n \n for(int i=0; i<n-len+1; i++)\n nums[i] = max({nums[i], nums[i+1], arr[i]});\n \n for(const int& idx : mp[len])\n res[idx] = nums[queries[idx][0]];\n }\n\n return res;\n }\n};", "memory": "283361" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n int z = queries.size();\n unordered_map<int, vector<int>> m;\n for(int i = 0; i < z; i++) {\n m[queries[i][1] - queries[i][0]].push_back(i);\n }\n\n vector<int> curr = nums;\n vector<int>ans(z);\n\n for(int i = 0; i < n; i++) {\n for(int q : m[i]) {\n ans[q] = curr[queries[q][0]];\n }\n for(int j = 0; j < n - i - 1; j++) {\n nums[j] ^= nums[j+1];\n curr[j] = max({curr[j], curr[j+1], nums[j]});\n }\n }\n return ans;\n }\n};", "memory": "286208" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& A, vector<vector<int>>& queries) {\n int n = A.size(), m = queries.size();\n unordered_map<int, vector<int>> d;\n for (int qi = 0; qi < m; ++qi) {\n d[queries[qi][1] - queries[qi][0]].push_back(qi);\n }\n vector<int> cur = A, res(m);\n for (int v = 0; v < n; ++v) {\n for (int qi : d[v]) {\n res[qi] = cur[queries[qi][0]];\n }\n for (int i = 0; i < n - v - 1; ++i) {\n A[i] ^= A[i + 1];\n cur[i] = max({cur[i], cur[i + 1], A[i]});\n }\n }\n return res;\n }\n};\n", "memory": "286208" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "int xors[2000][2000];\nclass Solution {\npublic:\n \n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n for(int i=0;i<n;i++) xors[i][i] = nums[i];\n for(int d=1;d<n;d++) {\n for(int i=0;i+d<n;i++) {\n int j = i+d;\n xors[i][j] = xors[i][j-1] ^ xors[i+1][j];\n }\n }\n for(int d=1;d<n;d++) {\n for(int i=0;i+d<n;i++) {\n int j = i+d;\n xors[i][j] = max(xors[i][j], max(xors[i+1][j], xors[i][j-1]));\n }\n }\n vector<int> result;\n for(int i=0;i<queries.size();i++) {\n result.push_back(xors[queries[i][0]][queries[i][1]]);\n }\n return result;\n }\n};", "memory": "289054" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef signed long long ll;\n\n#undef _P\n#define _P(...) (void)printf(__VA_ARGS__)\n#define FOR(x,to) for(x=0;x<(to);x++)\n#define FORR(x,arr) for(auto& x:arr)\n#define FORR2(x,y,arr) for(auto& [x,y]:arr)\n#define ALL(a) (a.begin()),(a.end())\n#define ZERO(a) memset(a,0,sizeof(a))\n#define MINUS(a) memset(a,0xff,sizeof(a))\ntemplate<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}\ntemplate<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}\n//-------------------------------------------------------\n\nint P[2020][2020];\n\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n\t\tint N=nums.size();\n\t\tint a,b,i;\n\t\tvector<int> A=nums;\n\t\tFOR(i,N) {\n\t\t\tFOR(a,A.size()) {\n\t\t\t\tP[a][a+i]=A[a];\n\t\t\t}\n\t\t\tFOR(a,A.size()-1) A[a]^=A[a+1];\n\t\t\tA.pop_back();\n\t\t}\n\t\tint len;\n\t\tfor(int len=1;len<=N;len++) {\n\t\t\tfor(a=0;a+len<=N;a++) {\n\t\t\t\tP[a][a+len]=max(P[a][a+len],P[a][a+len-1]);\n\t\t\t\tP[a][a+len]=max(P[a][a+len],P[a+1][a+len]);\n\t\t\t}\n\t\t}\n vector<int> ret;\n FORR(q,queries) ret.push_back(P[q[0]][q[1]]);\n return ret;\n }\n};\n\n", "memory": "289054" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = (int)nums.size();\n int dp[n+2][n+2];\n for(int i = 0; i < n; i++) {\n dp[i][i] = nums[i]; // All one length subarray xors\n }\n // All possible length of subarrays\n for(int len = 2; len <= n; len++) {\n for(int start = 1; start + len - 1 <= n; start++) {\n int end = start + len - 2;\n dp[start-1][end] = dp[start-1][end-1] ^ dp[start][end];\n }\n }\n\n for(int len = 2; len <= n; len++) {\n for(int start = 1; start + len - 1 <= n; start++) {\n int end = start + len - 2;\n dp[start-1][end] = max({dp[start-1][end], dp[start-1][end-1], dp[start][end]});\n }\n }\n\n vector<int> res;\n for(auto &val: queries) {\n res.push_back(dp[val[0]][val[1]]);\n }\n return res;\n }\n};", "memory": "291900" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "\nclass Solution {\n using pii = pair<int, int>;\npublic:\n vector<int> maximumSubarrayXor(vector<int>& a, vector<vector<int>>& Q) {\n int n = a.size();\n int dp[n][n];\n \n for (int len = 0 ; len < n ; ++len) {\n for (int r = len , l = 0; r < n ; ++l, ++r) {\n if (len == 0) {\n dp[l][r] = a[l];\n continue;\n }\n dp[l][r] = dp[l][r - 1] ^ dp[l + 1][r];\n }\n }\n for (int len = 0 ; len < n ; ++len) {\n for (int r = len , l = 0; r < n ; ++l, ++r) {\n if (len == 0) {\n continue;\n }\n if (len > 1) dp[l][r] = max(dp[l][r], dp[l + 1][r - 1]);\n dp[l][r] = max(dp[l][r], max(dp[l + 1][r], dp[l][r - 1]));\n }\n }\n vector<int> res;\n for (auto &q: Q) {\n res.push_back(dp[q[0]][q[1]]);\n }\n return res;\n }\n};", "memory": "291900" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "\ntemplate <typename T>\nstruct binary_indexed_tree{\n int N;\n vector<T> BIT;\n binary_indexed_tree(int N): N(N), BIT(N + 1, 0){\n }\n void add(int i, T x){\n while (i <= N){\n BIT[i] =max(BIT[i], x);\n i += i & -i;\n }\n }\n T get(int i){\n T ans = 0;\n while (i > 0){\n ans = max(ans,BIT[i]);\n i -= i & -i;\n }\n return ans;\n }\n T get(int L, int R){\n return get(R) - get(L-1);\n }\n};\n\nint ans[110000];\nint init=0;\nint c[2100][2100];\nvector<pair<int,int> > qu[2100];\nint d[2100][2100];\n\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& a, vector<vector<int>>& queries) {\n vector<int> ret;\n int i,j,n,x,y,o;\n /*\n if (init==0)\n {\n for (i=0;i<2100;i++)\n {\n c[i][0]=1;c[i][i]=1;\n for (j=1;j<i;j++)\n {\n c[i][j]=(c[i-1][j-1]+c[i-1][j])%2;\n }\n }\n init=1;\n }\n */\n n=a.size();\n for (i=0;i<n;i++)\n qu[i].clear();\n for (i=0;i<n;i++)\n d[i][1]=a[i];\n for (o=2;o<=n;o++)\n for (i=0;i+o<=n;i++)\n d[i][o]=(d[i][o-1]^d[i+1][o-1]);\n for (o=0;o<queries.size();o++)\n {\n x=queries[o][0];\n y=queries[o][1];\n qu[x].push_back(make_pair(y,o));\n }\n binary_indexed_tree<int> bit(n+10);\n for (i=n-1;i>=0;i--)\n {\n for (j=1;i+j<=n;j++)\n {\n bit.add(i+j,d[i][j]);\n }\n for (j=0;j<qu[i].size();j++)\n {\n x=qu[i][j].first;\n y=qu[i][j].second;\n ans[y]=bit.get(x+1);\n }\n }\n ret.clear();\n for (i=0;i<queries.size();i++)\n ret.push_back(ans[i]);\n return ret;\n }\n};", "memory": "294746" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "#include <ranges>\ninline constexpr int MAX_N = 2e3 + 5;\ninline constinit int dp2[MAX_N][MAX_N]{}, dp[MAX_N][MAX_N]{};\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size(), m = queries.size();\n for (int l = n - 1; l >= 0; --l) {\n dp2[l][l] = dp[l][l] = nums[l];\n for (int r = l + 1; r < n; ++r) {\n dp[l][r] = dp[l][r - 1] ^ dp[l + 1][r];\n dp2[l][r] = std::max(std::max(dp[l][r], dp2[l][r - 1]), dp2[l + 1][r]);\n }\n }\n std::vector<int> ans(m);\n for (auto &&q : std::ranges::views::reverse(queries)) {\n ans[--m] = dp2[q[0]][q[1]];\n }\n return ans;\n }\n};", "memory": "294746" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "int dp[2001][2001];\nint mxd[2001][2001];\n\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n for (int i = 0; i < n; i++) dp[i][i] = nums[i];\n for (int k = 2; k <= n; k++) {\n for (int i = 0, j = k - 1; j < n; i++, j++) {\n dp[i][j] = dp[i][j - 1] ^ dp[i + 1][j];\n }\n }\n for (int i = 0; i < n; i++) mxd[i][i] = dp[i][i];\n for (int k = 2; k <= n; k++) {\n for (int i = 0, j = k - 1; j < n; i++, j++) {\n mxd[i][j] = max({mxd[i][j - 1], mxd[i + 1][j], dp[i][j]});\n }\n }\n int q = queries.size();\n vector<int> ans(q);\n for (int i = 0; i < q; i++) {\n ans[i] = mxd[queries[i][0]][queries[i][1]];\n }\n return ans;\n // /*\n // 1001\n // 1101\n // 1010\n // 0110\n // 1010\n\n\n // a b c d\n \n // ab bc cd\n\n // abbc bccd\n\n // abbcbccd\n // a bbb ccc d\n\n // a b c d e\n\n // ab bc cd de\n // abbc bccd cdde\n // abbcbccd bccdcdde\n // a bbbb cccccc dddd e\n\n // a b c d e f\n // ab bc cd de ef\n // abbc bccd cdde deef\n // abbcbccd bccdcdde cddedeef\n // abbcbccdbccdcdde bccdcddecddedeef\n // abbcbccd cddedeef\n // a bbb cccc dddd eee f\n // */\n // for (int num = 2; num <= 21; num++) {\n // vector<string> v;\n // for (int i = 0; i < num; i++) {\n // v.push_back(string(1, 'a' + i));\n // }\n // while (v.size() > 2) {\n // for (int i = 0; i + 1 < v.size(); i++) {\n // v[i] += v[i+1];\n // }\n // v.pop_back();\n // }\n // map<char, int> ct;\n // for (char c : v[0]) ct[c]++;\n // for (const auto [c, t] : ct) {\n // if (t & 1) {\n // cout << c;\n // } else {\n // cout << \"_\";\n // }\n // }\n // cout << endl;\n // }\n\n // return {};\n }\n};", "memory": "297593" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "int dp[2001][2001];\n\n\nint dp2[2001][2001];\n\nclass Solution {\npublic:\n\n int solve(int l, int r)\n {\n if(l > r)\n return 0;\n\n if(dp[l][r] != -1)\n return dp[l][r];\n\n\n dp[l][r] = dp2[l][r];\n\n dp[l][r] = std::max(dp[l][r], solve(l + 1, r));\n\n return dp[l][r];\n }\n\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& que) {\n\n int n = nums.size();\n\n\n for(int i = 0;i < n;i ++)\n {\n for(int j = i;j < n;j ++)\n dp[i][j] = -1;\n\n dp2[i][i] = nums[i];\n }\n\n for(int l = 2;l <= n;l ++)\n {\n for(int j = 0;j + l <= n;j ++)\n {\n dp2[j][j + l - 1] = dp2[j][j + l - 2] ^ dp2[j + 1][j + l - 1];\n }\n }\n\n for(int i = 0;i < n;i ++)\n {\n for(int j = i + 1;j < n;j ++)\n {\n dp2[i][j] = std::max(dp2[i][j], dp2[i][j - 1]);\n }\n }\n\n\n //solve(vec, 0, n - 1);\n\n /*\n for(int i = 0;i < n;i ++)\n {\n for(auto it = vec[i].begin();it != vec[i].end();it++)\n {\n cout <<i <<\" \" << it->first <<\" \" << it->second <<endl;\n }\n }\n */\n\n vector<int> ret(que.size(), 0);\n\n for(int i = 0;i < que.size();i ++)\n {\n int l = que[i][0];\n int r = que[i][1];\n\n ret[i] = solve(l, r);\n \n }\n\n return ret;\n\n\n\n\n\n\n \n }\n};", "memory": "297593" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "const int N = 2000;\nint dp[N][N], val[N][N];\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n for(int i = n-1; i >= 0; i--){\n val[i][i] = dp[i][i] = nums[i];\n for(int j = i+1; j < n; j++){\n val[i][j] = val[i][j-1]^val[i+1][j];\n dp[i][j] = max({val[i][j], dp[i][j-1], dp[i+1][j]});\n }\n }\n \n vector<int> ans;\n for(auto &i : queries) \n ans.push_back(dp[i[0]][i[1]]);\n return ans;\n }\n};", "memory": "300439" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "int dp[2005][2005], f[2005][2005];\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& v, vector<vector<int>>& queries) {\n int n = v.size();\n for (int i=0;i<n;++i) dp[i][i] = f[i][i] = v[i];\n int pw = 1;\n for (int len=1;len<n;++len) {\n while ((pw << 1) <= len) pw <<= 1;\n for (int i=0;i+len<n;++i) {\n int j = i + len;\n f[i][j] = f[i][i+len-pw] ^ f[i+pw][j];\n dp[i][j] = max(dp[i][j-1], f[i][j]);\n dp[i][j] = max(dp[i][j], dp[i+1][j]);\n }\n }\n vector<int> ret;\n for (auto &vec : queries) {\n int ell = vec[0], arr = vec[1];\n ret.push_back(dp[ell][arr]);\n }\n return ret;\n }\n};", "memory": "300439" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n int q = queries.size();\n\n int score[n][n];\n int best[n][n];\n \n\n for (int k=0; k<n; k++) {\n for (int i=0; i<n-k; i++) {\n if (k == 0) {\n score[i][i] = nums[i];\n best[i][i] = nums[i];\n } else {\n score[i][i+k] = score[i][i+k-1] ^ score[i+1][i+k];\n best[i][i+k] = max(best[i][i+k-1], max(best[i+1][i+k], score[i][i+k]));\n }\n }\n }\n\n vector<int> ans(q);\n for (int i=0; i<q; i++) {\n ans[i] = best[queries[i][0]][queries[i][1]];\n }\n\n return ans;\n }\n};", "memory": "303285" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n\n vector<int> maximumSubarrayXor(vector<int>& a, vector<vector<int>>& queries) {\n int m = queries.size(), n = a.size();\n vector<int> ans(m);\n int val[n][n], dp[n][n];\n for (int len = 1; len <= n; ++len){\n for (int i = 0; i < n; ++i){\n int j = i + len-1;\n if (j >= n) continue;\n if (len==1){\n val[i][j] = a[i];\n continue;\n }\n val[i][j] = val[i][j-1]^val[i+1][j];\n }\n }\n for (int len = 1; len <= n; ++len){\n for (int i = 0; i < n; ++i){\n int j = i + len-1;\n if (j >= n) continue;\n if (len==1){\n dp[i][j] = a[i];\n continue;\n }\n dp[i][j] = max(max(dp[i][j-1], dp[i+1][j]), val[i][j]);\n }\n }\n for (int i = 0; i < m; ++i){\n ans[i] = dp[queries[i][0]][queries[i][1]];\n }\n return ans;\n }\n};\n", "memory": "303285" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n // f(l, r) \n // g(l, r) = max(f(i, j) | l <= i <= j <= r)\n // g(l, r) = max(f(l, r), g(l+1, r), g(l, r-1))\n // f(l, r) = f(l+1, r) ^ f(l, r-1);\n int f[2001][2001], g[2001][2001];\n int n = nums.size();\n for (int i = 0; i < n; ++i)\n f[i][i] = g[i][i] = nums[i];\n for (int d = 1; d < n; ++d)\n for (int l = 0; l + d < n; ++l) {\n int r = l + d;\n f[l][r] = f[l+1][r] ^ f[l][r-1];\n g[l][r] = max({f[l][r], g[l+1][r], g[l][r-1]});\n }\n vector<int> o;\n for (auto const &q: queries)\n o.push_back(g[q[0]][q[1]]);\n return o;\n }\n};", "memory": "306131" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n // f(l, r) \n // g(l, r) = max(f(i, j) | l <= i <= j <= r)\n // g(l, r) = max(f(l, r), g(l+1, r), g(l, r-1))\n // f(l, r) = f(l+1, r) ^ f(l, r-1);\n int f[2001][2001], g[2001][2001];\n int n = nums.size();\n for (int i = 0; i < n; ++i)\n f[i][i] = g[i][i] = nums[i];\n for (int d = 1; d < n; ++d)\n for (int l = 0; l + d < n; ++l) {\n int r = l + d;\n f[l][r] = f[l+1][r] ^ f[l][r-1];\n g[l][r] = max(f[l][r], max(g[l+1][r], g[l][r-1]));\n }\n vector<int> o;\n for (auto const &q: queries)\n o.push_back(g[q[0]][q[1]]);\n return o;\n }\n};", "memory": "306131" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> heap(n);\n //heap[0] = nums;\n vector<int> buf = nums;\n for (int len = 2; len <= n; ++len)\n {\n int sz = n - len + 1;\n heap[len-1].resize(sz);\n for (int i = 0; i < sz; ++i)\n {\n buf[i] = buf[i] ^ buf[i+1];\n auto maxNum = buf[i];\n if (len == 2)\n {\n maxNum = max(maxNum, nums[i]);\n maxNum = max(maxNum, nums[i+1]);\n }\n else\n {\n maxNum = max(maxNum, heap[len-2][i]);\n maxNum = max(maxNum, heap[len-2][i+1]);\n }\n heap[len-1][i] = maxNum;\n }\n }\n vector<int> result;\n for (const auto & v : queries)\n {\n auto len = v[1] - v[0] + 1;\n if (len == 1)\n result.emplace_back(nums[v[0]]);\n else\n result.emplace_back(heap[len-1][v[0]]);\n }\n return result;\n }\n};", "memory": "317516" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n vector<vector<int>> heap(n);\n //heap[0] = nums;\n vector<int> buf = nums;\n for (int len = 2; len <= n; ++len)\n {\n int sz = n - len + 1;\n heap[len-1].resize(sz);\n for (int i = 0; i < sz; ++i)\n {\n buf[i] = buf[i] ^ buf[i+1];\n auto maxNum = buf[i];\n if (len == 2)\n {\n maxNum = max(maxNum, nums[i]);\n maxNum = max(maxNum, nums[i+1]);\n }\n else\n {\n maxNum = max(maxNum, heap[len-2][i]);\n maxNum = max(maxNum, heap[len-2][i+1]);\n }\n heap[len-1][i] = maxNum;\n }\n }\n vector<int> result;\n for (const auto & v : queries)\n {\n auto len = v[1] - v[0] + 1;\n if (len == 1)\n result.emplace_back(nums[v[0]]);\n else\n result.emplace_back(heap[len-1][v[0]]);\n }\n return result;\n }\n};", "memory": "317516" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "vector<vector<int>> xors(2000, vector<int>(2000, 0));\nvector<vector<int>> maxXors(2000, vector<int>(2000, 0));\nclass Solution {\npublic:\n \n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n for(int d=0;d<n;d++) {\n for(int i=0;i+d<n;i++) {\n int j = i+d;\n if(j == i) {\n xors[i][j] = nums[i];\n } else {\n xors[i][j] = xors[i][j-1] ^ xors[i+1][j];\n }\n }\n }\n for(int d=0;d<n;d++) {\n for(int i=0;i+d<n;i++) {\n int j = i+d;\n if(j == i) {\n maxXors[i][j] = xors[i][j];\n } else {\n maxXors[i][j] = max(xors[i][j], max(maxXors[i+1][j], maxXors[i][j-1]));\n }\n }\n }\n vector<int> result;\n for(int i=0;i<queries.size();i++) {\n result.push_back(maxXors[queries[i][0]][queries[i][1]]);\n }\n return result;\n }\n};", "memory": "320363" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "typedef long long ll;\nclass D2SegmentTree { \nprivate:\n int seg[8020][8020];\npublic:\n int segBase;\n int m = 0, n = 0;\n function<int(int, int)> compare_operation;\n D2SegmentTree(function<int(int, int)> given_op, int newSegBase) {\n compare_operation = given_op;\n segBase = newSegBase;\n }\n void reset(int newM, int newN) {\n m = newM; n = newN;\n for (int i= 0; i<(m+2); i++) {\n for (int j = 0; j <(n+2); j++) {\n for (int it = 0; it < 4; it++) {\n for (int jt = 0; jt < 4; jt++) {\n seg[4*i + it][4*j + jt] = segBase;\n }\n }\n }\n }\n }\n void build(vector<vector<int>>&matrix, int nn) {\n n = nn;\n build_x(1,1,n,matrix); \n }\n void build_y(int vx, int lx, int rx, int vy, int ly, int ry, vector<vector<int>>&a) {\n if (ly == ry) {\n if (lx == rx)\n seg[vx][vy] = a[lx][ly];\n else\n seg[vx][vy] = max(seg[vx*2][vy], seg[vx*2+1][vy]);\n } else {\n int my = (ly + ry) / 2;\n build_y(vx, lx, rx, vy*2, ly, my, a);\n build_y(vx, lx, rx, vy*2+1, my+1, ry, a);\n seg[vx][vy] = max(seg[vx][vy*2], seg[vx][vy*2+1]);\n }\n }\n\n void build_x(int vx, int lx, int rx, vector<vector<int>>&a) {\n if (lx != rx) {\n int mx = (lx + rx) / 2;\n build_x(vx*2, lx, mx, a);\n build_x(vx*2+1, mx+1, rx, a);\n }\n build_y(vx, lx, rx, 1, 1, n, a);\n }\n // void update(int posr, int posc, long long x) {doUpdateR(1,1,m,posr,posc,x);}\n // void doUpdateC(int vr, int tlr, int trr, int posr, int vc, int tlc, int trc, int posc, long long x) {\n // trueVector[posr][posc] = x;\n // if (tlc == trc) {\n // if (tlr == trr) {\n // seg[vr][vc] = update_operation(seg[vr][vc], x);\n // }\n // else {\n // seg[vr][vc] = compare_operation(seg[vr<<1][vc], seg[vr<<1|1][vc]);\n // }\n // }\n // else {\n // int tmc = (tlc+trc) >> 1;\n // if (posc <= tmc) doUpdateC(vr, tlr, trr, posr, vc<<1, tlc, tmc, posc, x);\n // else doUpdateC(vr, tlr, trr, posr, vc<<1|1, tmc+1, trc, posc, x);\n // seg[vr][vc] = compare_operation(seg[vr][vc<<1], seg[vr][vc<<1|1]);\n // }\n // }\n // void doUpdateR(int vr, int tlr, int trr, int posr, int posc, long long x) {\n // trueVector[posr][posc] = x;\n // if (tlr != trr) {\n // int tmr = (tlr+trr) >> 1;\n // if (posr <= tmr) doUpdateR(vr<<1, tlr, tmr, posr, posc, x);\n // else doUpdateR(vr<<1|1, tmr+1, trr, posr, posc, x);\n // }\n // doUpdateC(vr,tlr,trr,posr,1,1,n,posc,x);\n // }\n int query(int r1, int c1, int r2, int c2) {\n assert(r1 <= r2 && c1 <= c2);\n return doQueryR(1,1,n,r1,r2,c1,c2);\n }\n int doQueryC(int vr, int vc, int tlc, int trc, int c1, int c2) {\n if (c1 > c2) return segBase;\n if (c1 == tlc && c2 == trc) return seg[vr][vc];\n int tmc = (tlc+trc) >> 1;\n int p1 = doQueryC(vr, vc<<1, tlc, tmc, c1, min(c2, tmc));\n int p2 = doQueryC(vr, vc<<1|1, tmc+1, trc, max(c1, tmc+1), c2);\n return compare_operation(p1, p2);\n }\n int doQueryR(int vr, int tlr, int trr, int r1, int r2, int c1, int c2) {\n if (r1 > r2) return segBase;\n if (r1 == tlr && r2 == trr) return doQueryC(vr, 1, 1, n, c1, c2);\n int tmr = (tlr+trr) >> 1;\n int p1 = doQueryR(vr<<1, tlr, tmr, r1, min(r2, tmr), c1, c2);\n int p2 = doQueryR(vr<<1|1, tmr+1, trr, max(r1, tmr+1), r2, c1, c2);\n return compare_operation(p1, p2);\n } \n int update_operation(int originalVal, int newVal) {return newVal;}\n};\n// CREDIT TO numb3r5 FOR THIS TEMPLATE -> https://leetcode.com/numb3r5/\n// CREDIT TO cp-algorithms FOR THIS TEMPLATE -> https://cp-algorithms.com/data_structures/segment_tree.html#simple-2d-segment-tree\n\nconst int N = 2005;\nvector<vector<int>>matrix;\nvector<vector<int>>dp;\n\nll comp(ll a, ll b) {return max(a,b);}\nD2SegmentTree seg(comp,0);\n\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size(); if (matrix.size() == 0) {matrix = vector<vector<int>>(2005, vector<int>(2005)); dp = matrix;}\n \n// for (int i = 0; i < n; i++) {\n// matrix[i+1][i+1] = nums[i];\n \n// }\n// for (int len = 2; len <= n; len++) {\n// for (int i = 0; i < n - len+1; i++) {\n// matrix[i+1][i+len] = (matrix[i+1][i+len-1] ^ matrix[i+2][i+len]);\n// }\n// }\n \n for (int i = 0; i < n; i++) {\n matrix[i][i] = nums[i];\n dp[i][i] = nums[i];\n }\n for (int len = 2; len <= n; len++) {\n for (int i = 0; i < n - len+1; i++) {\n matrix[i][i+len-1] = (matrix[i][i+len-2] ^ matrix[i+1][i+len-1]);\n dp[i][i+len-1] = max(matrix[i][i+len-1], max(dp[i][i+len-2], dp[i+1][i+len-1]));\n }\n }\n \n //cout << \"hi\" << endl;\n //seg.build(matrix, n);\n vector<int>ans;\n for (auto &q : queries) {\n int l = q[0], r = q[1];\n int res = dp[l][r];\n ans.push_back(res);\n }\n return ans;\n }\n};", "memory": "320363" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size();\n\n int dp[n][n];\n memset(dp, 0, sizeof(dp));\n \n for(int i = 0; i < n; i++){\n dp[i][i] = nums[i];\n }\n\n \n for(int j = 1; j < n; j++){\n for(int i = 0; i < n-j; i++){\n dp[i][i + j] = dp[i][i + j-1]^dp[i+1][i + j];\n }\n }\n\n for(int j = 1; j < n; j++){\n for(int i = 0; i < n-j; i++){\n dp[i][i + j] = max(dp[i][i + j-1], dp[i][i + j]);\n dp[i][i + j] = max(dp[i][i + j], dp[i+1][i + j]);\n }\n }\n\n vector<int> ans;\n \n for(auto query : queries){\n ans.push_back(dp[query[0]][query[1]]);\n }\n return ans;\n\n }\n};", "memory": "323209" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n=nums.size();\n int dp[n+1][n+1];\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++){\n dp[i][j]=0;\n }\n }\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n-i+1;j++){\n int y=j+i-1,x=j;\n if(x==y){\n dp[x][y]=nums[x-1];\n }\n else{\n dp[x][y]=dp[x][y-1]^dp[x+1][y];\n // dp[x][y]=max({dp[x][y],dp[x+1][y],dp[x][y-1]});\n }\n }\n }\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n-i+1;j++){\n int y=j+i-1,x=j;\n if(x==y){\n dp[x][y]=nums[x-1];\n }\n else{\n // dp[x][y]=dp[x][y-1]^dp[x+1][y];\n dp[x][y]=max({dp[x][y],dp[x+1][y],dp[x][y-1]});\n }\n }\n }\n vector<int> fin;\n for(auto it:queries){\n int x=it[0];\n int y=it[1];\n fin.push_back(dp[x+1][y+1]);\n }\n return fin;\n\n }\n};", "memory": "323209" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size(); \n int x[n][n] , mx[n][n] , dp[n][n];\n\n for(int i=0; i<n; i++){\n for(int j=0; j<n; j++) x[i][j] = 0 ,dp[i][j] = 0 , mx[i][j] = 0;\n }\n\n vector<int> ans;\n for(int i=0; i<n; i++){\n for(int j=0; j<=n-i-1; j++){\n if(i == 0) x[i][j] = nums[j];\n else x[i][j] = (x[i-1][j] ^ x[i-1][j+1]);\n mx[i][j] = max(mx[i][j] , x[i][j]);\n if(i > 0) mx[i][j] = max(mx[i][j] , mx[i-1][j]);\n }\n }\n\n // for(int i=0; i<n; i++){\n // for(int j=0; j<n-i; j++) cout << mx[i][j] << \" \";\n // cout << \"\\n\";\n // }\n\n for(int i=n-1; i>=0; i--)\n {\n for(int j=n-1; j>=i; j--){\n if(i == j) dp[i][j] = nums[j];\n else{\n dp[i][j] = max(dp[i+1][j] , mx[j-i][i]);\n }\n }\n }\n\n // for(int i=0; i<n; i++)\n // {\n // for(int j=i; j<n; j++) cout << dp[i][j] << \" \";\n // cout << \"\\n\";\n // }\n\n // for(int i=0; i<n; i++)\n // {\n // for(int j=0; j<n-i; j++) cout << x[i][j] << \" \";\n // cout << \"\\n\";\n // }\n\n for(auto &i : queries){\n ans.push_back(dp[i[0]][i[1]]);\n }\n return ans;\n }\n};", "memory": "326055" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& nums, vector<vector<int>>& queries) {\n int n = nums.size(); \n int x[n][n] , mx[n][n] , dp[n][n];\n memset(dp, 0, sizeof(dp));\n memset(x, 0, sizeof(x));\n memset(mx, 0, sizeof(mx));\n \n\n vector<int> ans;\n for(int i=0; i<n; i++){\n for(int j=0; j<=n-i-1; j++){\n if(i == 0) x[i][j] = nums[j];\n else x[i][j] = (x[i-1][j] ^ x[i-1][j+1]);\n \n mx[i][j] = max(mx[i][j] , x[i][j]);\n if(i > 0) mx[i][j] = max(mx[i][j] , mx[i-1][j]);\n }\n }\n\n for(int i=n-1; i>=0; i--)\n {\n for(int j=n-1; j>=i; j--){\n if(i == j) dp[i][j] = nums[j];\n else dp[i][j] = max(dp[i+1][j] , mx[j-i][i]);\n }\n }\n\n for(auto &i : queries) ans.push_back(dp[i[0]][i[1]]);\n \n return ans;\n }\n};", "memory": "326055" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "const int maxn = 2002;\nint ans[maxn][maxn], best[maxn][maxn];\n\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& a, vector<vector<int>>& queries) {\n int n = a.size();\n \n for(int i = 0; i < n; i++){\n ans[i][i] = a[i];\n best[i][i] = a[i];\n }\n \n for(int d = 1; d < n; d++){\n for(int i = 0; i + d < n; i++){\n ans[i][i + d] = ans[i][i + d - 1] ^ ans[i + 1][i + d];\n best[i][i + d] = max({ans[i][i + d], best[i][i + d - 1], best[i + 1][i + d]});\n }\n }\n \n // i..i+d \n \n vector<int> res;\n for(auto q : queries){\n res.push_back(best[q[0]][q[1]]);\n }\n return res;\n }\n};", "memory": "328901" }
3,551
<p>You are given an array <code>nums</code> of <code>n</code> integers, and a 2D integer array <code>queries</code> of size <code>q</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.</p> <p>For each query, you must find the <strong>maximum XOR score</strong> of any <span data-keyword="subarray">subarray</span> of <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.</p> <p>The <strong>XOR score</strong> of an array <code>a</code> is found by repeatedly applying the following operations on <code>a</code> so that only one element remains, that is the <strong>score</strong>:</p> <ul> <li>Simultaneously replace <code>a[i]</code> with <code>a[i] XOR a[i + 1]</code> for all indices <code>i</code> except the last one.</li> <li>Remove the last element of <code>a</code>.</li> </ul> <p>Return an array <code>answer</code> of size <code>q</code> where <code>answer[i]</code> is the answer to query <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[12,60,60]</span></p> <p><strong>Explanation:</strong></p> <p>In the first query, <code>nums[0..2]</code> has 6 subarrays <code>[2]</code>, <code>[8]</code>, <code>[4]</code>, <code>[2, 8]</code>, <code>[8, 4]</code>, and <code>[2, 8, 4]</code> each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores.</p> <p>In the second query, the subarray of <code>nums[1..4]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> <p>In the third query, the subarray of <code>nums[0..5]</code> with the largest XOR score is <code>nums[1..4]</code> with a score of 60.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[7,14,11,14,5]</span></p> <p><strong>Explanation:</strong></p> <table height="70" width="472"> <thead> <tr> <th>Index</th> <th>nums[l<sub>i</sub>..r<sub>i</sub>]</th> <th>Maximum XOR Score Subarray</th> <th>Maximum Subarray XOR Score</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>[0, 7, 3, 2]</td> <td>[7]</td> <td>7</td> </tr> <tr> <td>1</td> <td>[7, 3, 2, 8, 5]</td> <td>[7, 3, 2, 8]</td> <td>14</td> </tr> <tr> <td>2</td> <td>[3, 2, 8]</td> <td>[3, 2, 8]</td> <td>11</td> </tr> <tr> <td>3</td> <td>[3, 2, 8, 5, 1]</td> <td>[2, 8, 5, 1]</td> <td>14</td> </tr> <tr> <td>4</td> <td>[5, 1]</td> <td>[5]</td> <td>5</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 2000</code></li> <li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li> <li><code>1 &lt;= q == queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2 </code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= n - 1</code></li> </ul>
0
{ "code": "const int maxn = 2002;\nint ans[maxn][maxn], best[maxn][maxn];\nclass Solution {\npublic:\n vector<int> maximumSubarrayXor(vector<int>& a, vector<vector<int>>& queries) {\n int n = a.size();\n for(int i = 0; i < n; i++){\n ans[i][i] = a[i];\n best[i][i] = a[i];\n }\n for(int d = 1; d < n; d++){\n for(int i = 0; i + d < n; i++){\n ans[i][i + d] = ans[i][i + d - 1] ^ ans[i + 1][i + d];\n best[i][i + d] = max({ans[i][i + d], best[i][i + d - 1], best[i + 1][i + d]});\n }\n }\n vector<int> res;\n for(auto q : queries){\n res.push_back(best[q[0]][q[1]]);\n }\n return res;\n }\n};", "memory": "331748" }