id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int> result (queries.size(), -1);\n map<int, int> dis;\n int largest = -1;\n for (int i = 0; i < queries.size(); i++) {\n int curDis = abs(queries[i][0]) + abs(queries[i][1]);\n if (i <= k-1) {\n largest = max(largest, curDis); \n dis[curDis]++;\n } else if (curDis < largest) {\n dis[curDis]++;\n } else {\n result[i] = result[i-1];\n continue;\n }\n if (i >= k-1) {\n auto it = dis.find(largest);\n if (it->second <= 0) {\n --it;\n } \n it->second--;\n //cout << it->first << it->second << endl;\n largest = it->first;\n result[i] = it->first;\n }\n\n }\n return result;\n }\n};",
"memory": "302810"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n multiset<int> mp, mp_copy;\n int n = queries.size();\n vector<int> ans(n, -1);\n for(int i=0; i<queries.size(); i++){\n int num = abs(queries[i][0])+abs(queries[i][1]);\n \n if(mp.size()>=k){\n if(*mp.rbegin()>num){\n cout << *mp.rbegin() << \" \" << i << endl;\n // mp.erase(*mp.rbegin());\n mp.erase(--mp.end());\n // cout << \"inserting \" << num << \" at index \" << i << endl;\n mp.insert(num);\n }\n }\n else{\n mp.insert(num);\n }\n // cout << mp.size() <<\"at index\" << i << endl;\n\n if(mp.size()==k)ans[i] = *mp.rbegin();\n \n }\n\n return ans;\n\n\n }\n};",
"memory": "303644"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int val = -1;\n const int n = queries.size();\n vector<int> ans(n);\n multiset<int, greater<int>> s;\n for (int i = 0; i < n; i++) {\n int distance = abs(queries[i][0]) + abs(queries[i][1]);\n if (s.size() < k) {\n s.insert(distance);\n if (s.size() == k) {\n val = *s.begin();\n ans[i] = val;\n } else {\n ans[i] = -1;\n }\n continue;\n }\n\n if (distance < val) {\n s.erase(s.begin());\n s.insert(distance);\n val = *s.begin();\n }\n ans[i] = val;\n \n }\n return ans;\n }\n};",
"memory": "303644"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dist(const vector<int>& q) {\n return abs(q[0])+abs(q[1]);\n }\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n = queries.size();\n vector<int> res(n, -1);\n if (n < k) {\n return res;\n }\n map<int,int> s;\n for (int i=0; i<k; ++i) {\n int d = dist(queries[i]);\n s[d] ++;\n res[i] = i==k-1 ? (s.rbegin()->first) : -1;\n }\n int val = s.rbegin()->first;\n int rank = s.rbegin()->second;\n for (int i=k; i<n; ++i) {\n int d = dist(queries[i]);\n s[d] ++;\n if (d < val) {\n if (rank == 1) {\n auto p = s.find(val); p --;\n val = p->first;\n rank = p->second;\n } else {\n rank --;\n }\n }\n res[i] = val;\n }\n return res;\n }\n};",
"memory": "304478"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n map<int, int> mp; int mp_size = 0; \n vector<int> ans (queries.size()); \n \n for (int i=0; i<queries.size(); i++) {\n // add latest co-ordinate to queue \n mp[abs(queries[i][0]) + abs(queries[i][1])]++; \n mp_size++; \n \n // cout << (mp.rbegin()->first) << endl; \n \n if (mp_size < k) {\n ans[i] = -1; \n } else if (mp_size == k) {\n ans[i] = mp.rbegin()->first; \n } else {\n mp_size--; \n if ((mp.rbegin())->second == 1) {\n mp.erase(mp.rbegin()->first); \n } else {\n (mp.rbegin())->second--;\n }\n ans[i] = mp.rbegin()->first; \n }\n }\n \n return ans; \n }\n};",
"memory": "304478"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n = queries.size();\n vector<int> ans(n, -1);\n if (n < k) return ans;\n multiset<int> st;\n \n for (int i = 0; i < k; ++i)\n st.insert(abs(queries[i][0]) + abs(queries[i][1]));\n auto it = prev(st.end());\n ans[k - 1] = *it;\n \n for (int i = k; i < n; ++i) {\n int c = abs(queries[i][0]) + abs(queries[i][1]);\n st.insert(c);\n if (c < *it) {\n it--;\n }\n ans[i] = *it;\n }\n \n return ans;\n }\n};",
"memory": "305311"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n = queries.size();\n \n vector<int> ans(n, -1);\n multiset<int> st;\n \n \n if(k > n) \n return ans;\n \n for(int i = 0;i < k ;i++) {\n ans[i] = -1;\n st.insert(abs(queries[i][0]) + abs(queries[i][1]));\n }\n ans[k - 1] = *(--st.end());\n int cnt = k;\n for(int i = k;i < n; ++i) {\n st.insert(abs(queries[i]\n [0]) + abs(queries[i][1]));\n cnt++;\n if(cnt > k) {\n st.erase((--st.end()));\n cnt--;\n }\n \n ans[i] = *(--st.end());\n }\n \n return ans;\n }\n};",
"memory": "305311"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "const int speedup = [] { ios::sync_with_stdio(0); cin.tie(0); return 0; }();\nclass Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n multiset<int>ms;\n vector<int>ans(queries.size(),-1);\n for(int i=0;i<queries.size();i++){\n int d=abs(queries[i][0])+abs(queries[i][1]);\n ms.insert(d);\n if(ms.size()>k)ms.erase(--ms.end());\n if(i>=k-1){\n ans[i]=*ms.rbegin();\n }\n }\n \n return ans;\n }\n \n};",
"memory": "306145"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<vector<int>> maxHeap;\n vector<int> answer;\n\n for(int i=0;i<queries.size();i++) {\n int distance = abs(queries[i][0]) + abs(queries[i][1]);\n if(maxHeap.size() == k){\n if(distance <= maxHeap.top()[0]) {\n maxHeap.push({distance,i});\n maxHeap.pop();\n }\n } \n else if(maxHeap.size() < k) {\n maxHeap.push({distance,i});\n }\n\n if(maxHeap.size() == k) answer.push_back(maxHeap.top()[0]);\n else answer.push_back(-1);\n }\n return answer; \n }\n};",
"memory": "306145"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class cmp{\npublic:\n bool operator()(const vector<int> &a, const vector<int> &b)\n {\n return abs(a[0]) + abs(a[1]) < abs(b[0]) + abs(b[1]);\n }\n};\nclass Solution\n{\npublic:\n vector<int> resultsArray(vector<vector<int>> &queries, int k)\n {\n vector<int> ret;\n priority_queue<vector<int>, vector<vector<int>>, cmp> pq;\n for (auto &query : queries) {\n pq.push(query);\n while (pq.size() > k) {\n pq.pop();\n }\n if (pq.size() < k) {\n ret.push_back(-1);\n } else {\n const auto &top = pq.top();\n ret.push_back(abs(top[0]) + abs(top[1]));\n }\n }\n return ret;\n }\n};",
"memory": "306979"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n if (k > queries.size()) {\n return vector<int>(queries.size(), -1);\n }\n\n vector<int> res(k-1, -1);\n res.reserve(queries.size());\n multiset<int> dists{};\n\n for (int i = 0; i < k-1; ++i) {\n dists.insert(abs(queries[i][0]) + abs(queries[i][1]));\n }\n for (int i = k-1; i < queries.size(); ++i) {\n dists.insert(abs(queries[i][0]) + abs(queries[i][1]));\n if (dists.size() > k) dists.erase(prev(dists.end(), 1));\n res.push_back(*prev(dists.end(), 1));\n }\n return res;\n }\n};",
"memory": "306979"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n // priority_queue<int ,vector<int>,greater<int>>pq;\n priority_queue<int> pq;\n\nvector<int> res;\n vector<vector<int>> q = queries;\n for(int i=0;i<q.size();i++){\n pq.push(abs(q[i][0])+abs(q[i][1]));\n\n if(pq.size() < k){\n res.push_back(-1);\n continue;\n }\n else{\n if(pq.size()>k){\n pq.pop();\n }\n res.push_back(pq.top());\n continue;\n }\n int m=k-1;\n priority_queue<int,vector<int>,greater<int>>pq1;\n while(m--){\n pq1.push(pq.top());\n pq.pop();\n }\n res.push_back(pq.top());\n while(!pq1.empty()){\n pq.push(pq1.top());\n pq1.pop();\n }\n }\n return res;\n }\n};",
"memory": "307813"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void func(vector<int>& ans, vector<vector<int>> q, priority_queue<int>& pq, int k){\n for(int i=0; i<q.size(); i++){\n if(pq.size()<k)\n pq.push(abs(q[i][0])+abs(q[i][1]));\n else{\n if(abs(q[i][0])+abs(q[i][1])<pq.top()){\n pq.pop();\n pq.push(abs(q[i][0])+abs(q[i][1]));\n }\n }\n if(pq.size()<k)\n ans.push_back(-1);\n else\n ans.push_back(pq.top());\n }\n }\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int> res;\n priority_queue<int> pq;\n //int dist = INT_MAX;\n func(res, queries, pq, k);\n return res;\n }\n};",
"memory": "307813"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n ios_base::sync_with_stdio(false); cin.tie(0);\n #define q queries\n vector<int> ans;\n multiset<int> ms;\n for (int i=0;i<q.size();++i){\n // ms.insert(abs(q[i][0])+abs(q[i][1]));\n // if (ms.size()<k) ans.push_back(-1);\n // else{\n // auto it=ms.begin(); advance(it, k-1);\n // ans.push_back(*it);\n // }\n if (ms.size()<k){\n ms.insert(abs(q[i][0])+abs(q[i][1]));\n if (ms.size()<k) ans.push_back(-1);\n else ans.push_back(*ms.rbegin());\n }\n else{\n auto it=ms.rbegin(); \n if (abs(q[i][0])+abs(q[i][1])<*it){\n ms.erase(next(it).base());\n ms.insert(abs(q[i][0])+abs(q[i][1]));\n }\n ans.push_back(*ms.rbegin());\n }\n }\n return ans;\n }\n};",
"memory": "308646"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n ios_base::sync_with_stdio(false); cin.tie(0);\n #define q queries\n vector<int> ans;\n multiset<int> ms;\n for (int i=0;i<q.size();++i){\n // ms.insert(abs(q[i][0])+abs(q[i][1]));\n // if (ms.size()<k) ans.push_back(-1);\n // else{\n // auto it=ms.begin(); advance(it, k-1);\n // ans.push_back(*it);\n // }\n if (ms.size()<k){\n ms.insert(abs(q[i][0])+abs(q[i][1]));\n if (ms.size()<k) ans.push_back(-1);\n else ans.push_back(*ms.rbegin());\n }\n else{\n auto it=ms.rbegin(); \n if (abs(q[i][0])+abs(q[i][1])<*it){\n ms.erase(next(it).base());\n ms.insert(abs(q[i][0])+abs(q[i][1]));\n }\n ans.push_back(*ms.rbegin());\n }\n }\n return ans;\n }\n};",
"memory": "308646"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int msz = 0;\n map<int, int> cnt;\n vector<int> res;\n for (const auto& q : queries) {\n const auto dist = abs(q[0]) + abs(q[1]);\n cnt[dist] += 1;\n msz += 1;\n if (msz > k) {\n auto it = prev(end(cnt));\n if (it->second == 1) cnt.erase(it);\n else it->second -= 1;\n }\n if (msz < k) res.push_back(-1);\n else res.push_back(prev(end(cnt))->first);\n }\n return res;\n }\n};",
"memory": "309480"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n = (int) queries.size();\n\n vector<int> res;\n int kth = -1;\n\n map<int, int> mp;\n\n int sz = 0;\n\n for (auto& query : queries) {\n int x = query[0];\n int y = query[1];\n int cur = abs(x) + abs(y);\n\n if (kth != cur)\n ++mp[cur];\n\n ++sz;\n\n if (kth == -1) {\n if (sz == k) \n kth = (*mp.rbegin()).first;\n } else if (kth != -1 && cur < kth) {\n --mp[kth];\n if (mp[kth] == 0) {\n auto itr = mp.lower_bound(kth);\n --itr;\n kth = (*itr).first;\n }\n }\n res.emplace_back(kth);\n }\n\n return res;\n }\n};",
"memory": "309480"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int> res;\n multiset<int> s;\n for(auto &i : queries){\n int dis = abs(i[0]) + abs(i[1]);\n s.insert(dis);\n if(s.size() < k)\n res.push_back(-1);\n else{\n if(s.size() > k)\n s.erase(prev(s.end()));\n res.push_back(*s.rbegin());\n }\n }\n return res;\n }\n};",
"memory": "310314"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n = queries.size();\n multiset<int> st;\n vector<int> ans;\n for(int i=0; i<n; i++){\n st.insert(abs(queries[i][0])+abs(queries[i][1]));\n if(st.size()>k) st.erase(prev(st.end()));\n if(st.size()<k) ans.push_back(-1);\n else if(st.size() == k) ans.push_back(*st.rbegin());\n }\n return ans;\n }\n};",
"memory": "310314"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& q, int k) {\n vector<int>ans,v;\n if(q.size()<k)\n {\n for(int i=0;i<q.size();i++)\n {\n ans.push_back(-1);\n }\n return ans;\n }\n multiset<int>s;\n int maxi=1e9;\n for(int i=0;i<k;i++)\n {\n v.push_back(abs(q[i][0])+abs(q[i][1]));\n s.insert(abs(q[i][0])+abs(q[i][1]));\n if(v.size()==k)break;\n ans.push_back(-1);\n }\n sort(v.begin(),v.end());\n ans.push_back(v[k-1]);\n for(int i=k;i<q.size();i++)\n {\n if(abs(q[i][0])+abs(q[i][1])<*s.rbegin())\n {\n s.erase(s.lower_bound(*s.rbegin()));\n s.insert(abs(q[i][0])+abs(q[i][1]));\n }\n ans.push_back(*s.rbegin());\n }\n return ans;\n }\n};",
"memory": "311148"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& q, int k) {\n int n=q.size();\n vector<int> v(n);\n for(int i=0;i<n;i++){\n int d = abs(q[i][0])+ abs(q[i][1]);\n v[i]=d;\n }\n\n vector<int> ans;\n multiset<int> st;\n for(int i=0;i<n;i++){\n st.insert(v[i]);\n auto it =st.end();\n it--;\n if(st.size()<k) ans.push_back(-1);\n else if(st.size()==k) ans.push_back(*it);\n else{\n st.erase(it);\n it=st.end();\n it--;\n ans.push_back(*it);\n }\n }\n return ans;\n }\n};",
"memory": "311981"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n std::vector<int> results;\n std::set<int> distances;\n std::priority_queue<int> minHeap;\n\n for (const auto& query : queries) {\n int x = query[0];\n int y = query[1];\n int distance = std::abs(x) + std::abs(y);\n \n // Insert the new distance into the set\n distances.insert(distance);\n \n // Update the min-heap\n minHeap.push(distance);\n if (minHeap.size() > k) {\n minHeap.pop();\n }\n\n // Determine the k-th nearest distance\n if (minHeap.size() < k) {\n results.push_back(-1);\n } else {\n results.push_back(minHeap.top());\n }\n }\n\n return results;\n }\n};",
"memory": "312815"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int>res;\n set<int>dist;\n priority_queue<int>minHeap;\n for(const auto& query:queries){\n int x=query[0];\n int y=query[1];\n \n int distance=abs(x)+abs(y);\n dist.insert(distance);\n \n minHeap.push(distance);\n if(minHeap.size()>k){\n minHeap.pop();\n }\n if(minHeap.size()<k){\n res.push_back(-1);\n }\n else{\n res.push_back(minHeap.top());\n }\n }\n return res;\n }\n};",
"memory": "312815"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int> results;\n priority_queue<int> maxHeap; // This will store the k smallest distances (max-heap)\n set<int> distances; // To keep track of all unique distances\n \n for (const vector<int>& query : queries) {\n int x = query[0];\n int y = query[1];\n int distance = abs(x) + abs(y);\n \n // Insert the new distance into the set\n distances.insert(distance);\n \n // Add the new distance to the max-heap\n maxHeap.push(distance);\n \n // Ensure the heap size does not exceed k\n if (maxHeap.size() > k) {\n maxHeap.pop();\n }\n \n // Get the k-th smallest distance\n if (maxHeap.size() < k) {\n results.push_back(-1);\n } else {\n results.push_back(maxHeap.top());\n }\n }\n \n return results;\n }\n};\n ",
"memory": "313649"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int> results;\n priority_queue<int> maxHeap; // Max-heap to keep track of the k smallest distances\n set<int> distanceSet; // Set to ensure all distances are unique\n \n for (const auto& query : queries) {\n int x = query[0];\n int y = query[1];\n int distance = abs(x) + abs(y);\n \n // Insert the new distance into the set\n distanceSet.insert(distance);\n \n // If there are fewer than k elements, directly add to heap\n if (maxHeap.size() < k) {\n maxHeap.push(distance);\n } else if (distance < maxHeap.top()) {\n // If the heap already has k elements, only insert if the new distance is smaller than the largest in the heap\n maxHeap.pop();\n maxHeap.push(distance);\n }\n \n // Check if we have enough obstacles\n if (maxHeap.size() < k) {\n results.push_back(-1);\n } else {\n // The k-th smallest distance is at the top of the max-heap\n results.push_back(maxHeap.top());\n }\n }\n \n return results;\n }\n};",
"memory": "313649"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "#include <vector>\n#include <queue>\n#include <set>\n#include <cmath>\n\nusing namespace std;\n\nclass Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n // Min-heap to store the k nearest obstacles\n priority_queue<int> minHeap;\n // Set to track the coordinates of obstacles\n set<pair<int, int>> obstacles;\n // Result array to store the k-th nearest obstacle distances\n vector<int> results;\n \n for (const auto& query : queries) {\n int x = query[0];\n int y = query[1];\n int distance = abs(x) + abs(y);\n \n // Insert the obstacle coordinate into the set\n obstacles.insert({x, y});\n \n // If the heap has fewer than k elements, insert the distance\n if (minHeap.size() < k) {\n minHeap.push(distance);\n results.push_back(minHeap.size() < k ? -1 : minHeap.top());\n } else {\n // If the new distance is smaller than the largest distance in the heap, replace it\n if (distance < minHeap.top()) {\n minHeap.pop();\n minHeap.push(distance);\n }\n results.push_back(minHeap.top());\n }\n }\n \n return results;\n }\n};",
"memory": "314483"
} |
3,495 | <p>There is an infinite 2D plane.</p>
<p>You are given a positive integer <code>k</code>. You are also given a 2D array <code>queries</code>, which contains the following queries:</p>
<ul>
<li><code>queries[i] = [x, y]</code>: Build an obstacle at coordinate <code>(x, y)</code> in the plane. It is guaranteed that there is <strong>no</strong> obstacle at this coordinate when this query is made.</li>
</ul>
<p>After each query, you need to find the <strong>distance</strong> of the <code>k<sup>th</sup></code> <strong>nearest</strong> obstacle from the origin.</p>
<p>Return an integer array <code>results</code> where <code>results[i]</code> denotes the <code>k<sup>th</sup></code> nearest obstacle after query <code>i</code>, or <code>results[i] == -1</code> if there are less than <code>k</code> obstacles.</p>
<p><strong>Note</strong> that initially there are <strong>no</strong> obstacles anywhere.</p>
<p>The <strong>distance</strong> of an obstacle at coordinate <code>(x, y)</code> from the origin is given by <code>|x| + |y|</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,7,5,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Initially, there are 0 obstacles.</li>
<li>After <code>queries[0]</code>, there are less than 2 obstacles.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 3 and 7.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 3, 5, and 7.</li>
<li>After <code>queries[3]</code>, there are obstacles at distances 3, 3, 5, and 7.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,8,6]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>After <code>queries[0]</code>, there is an obstacle at distance 10.</li>
<li>After <code>queries[1]</code>, there are obstacles at distances 8 and 10.</li>
<li>After <code>queries[2]</code>, there are obstacles at distances 6, 8, and 10.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= queries.length <= 2 * 10<sup>5</sup></code></li>
<li>All <code>queries[i]</code> are unique.</li>
<li><code>-10<sup>9</sup> <= queries[i][0], queries[i][1] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int> results;\n priority_queue<int> maxHeap; \n set<pair<int, int>> obstacles; \n \n for (const auto& query : queries) {\n int x = query[0];\n int y = query[1];\n int distance = abs(x) + abs(y);\n \n obstacles.insert({x, y});\n \n maxHeap.push(distance);\n \n if (maxHeap.size() > k) {\n maxHeap.pop();\n }\n \n if (maxHeap.size() < k) {\n results.push_back(-1);\n } else {\n results.push_back(maxHeap.top());\n }\n }\n \n return results;\n }\n};\n",
"memory": "314483"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 0 | {
"code": "class Solution {\n int f[2][1024], a[101];\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size(), N = 1 << n;\n\n for (int i = 0; i < n; i++){\n for (int j = 0; j < m; j++){\n a[grid[i][j]] |= 1 << i;\n }\n }\n\n for (int i = 1; i <= 100; i++) {\n copy(f[!(i & 1)], f[!(i & 1)] + N, f[i & 1]);\n\n for (int j = 0; j < n; j++) {\n if (a[i] >> j & 1) {\n for (int k = 0; k < N; k++) {\n if (!(k >> j & 1)) {\n f[i & 1][k | 1 << j] = max(f[i & 1][k | 1 << j], f[!(i & 1)][k] + i);\n }\n }\n }\n }\n }\n\n return *max_element(f[0], f[0] + N);\n }\n};\n",
"memory": "22423"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 0 | {
"code": "inline constexpr int MAX_N = 11;\ninline constinit short rows[101]{}, lvl_[1 << MAX_N]{}, nlvl_[1 << MAX_N]{};\ninline constinit bool lvl_has[1 << MAX_N]{};\n\ntemplate <typename T>\nstruct Swapper {\n T* ptr;\n inline auto &operator[](int idx) { return (*ptr)[idx]; }\n inline auto &operator->() { return ptr; }\n};\n\nclass Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size();\n std::memset(&rows[0], 0, sizeof(rows));\n int high = 1, low = 100;\n for (int r = 0; r < n; ++r) {\n for (int x : grid[r]) {\n rows[x] |= (1 << r);\n high = std::max(x, high);\n low = std::min(x, low);\n }\n }\n\n Swapper lvl{&lvl_}, nlvl{&nlvl_};\n int ans = 0, lvl_i = 0, nlvl_i = 0, lcount = 1;\n lvl_has[lvl[lvl_i++] = (1 << n) - 1] = true;\n for (int x = high; x >= low - 1; --x) {\n if (lcount == 0 && lvl[0] == 0) {\n return ans;\n }\n for (int i = 0; i < lvl_i; ++i) {\n int bs = lvl[i];\n for (int b = bs & rows[x]; b; b &= ~(b & -b)) {\n int next = bs & ~(b & -b);\n if (!lvl_has[next]) {\n lvl_has[nlvl[nlvl_i++] = next] = true;\n lcount++;\n }\n }\n if (lvl_has[bs]) {\n --lcount;\n lvl_has[bs] = false;\n }\n }\n if (lcount == 0 && lvl[0] == 0) {\n return ans;\n }\n if (nlvl_i > 0) {\n ans += x;\n std::swap(lvl, nlvl);\n lvl_i = nlvl_i;\n nlvl_i = 0;\n }\n }\n return ans;\n }\n};",
"memory": "22423"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 1 | {
"code": "#define lli long long int \n#define ld long double\n#define vi vector<int>\n#define vlli vector<lli>\n#define vpii vector<pair<int, int>>\n#define pb push_back\n#define all(__x) __x.begin(),__x.end()\n\ntemplate<typename T> void debug(T _a) {cout << _a << \" \";}\ntemplate<typename T1, typename T2> void debug(pair<T1, T2> _p) {cout<<\"{\";debug(_p.first);cout<<\": \";debug(_p.second);cout<<\"}\\n\";}\ntemplate<typename T> void debug(vector<T> _aa) {for (auto h: _aa) debug(h); cout << endl;}\ntemplate<typename T> void debug(deque<T> _aa) {for (auto h: _aa) debug(h); cout << endl;}\ntemplate<typename T> void debug(multiset<T> _aa) {for (auto h: _aa) debug(h); cout << endl;}\ntemplate<typename T> void debug(set<T> _aa) {for (auto h: _aa) debug(h); cout << endl;}\ntemplate<typename T1, typename T2> void debug(map<T1, T2> _mm) {for (auto h: _mm) debug(h);}\n\nclass Solution {\npublic:\n int dp[101][1 << 10], n, m;\n\n int maxScore(vector<vector<int>>& g) {\n memset(dp, 0, sizeof dp);\n n = g.size(), m = g[0].size();\n vector<set<int>> idx(101);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n idx[g[i][j]].insert(i);\n }\n }\n int ans = 0;\n for (int i = 1; i <= 100; i++) {\n for (int j = 0; j < (1 << 10); j++) {\n dp[i][j] = max(dp[i][j], dp[i-1][j]);\n for (int k: idx[i]) {\n if ((j >> k) & 1) continue;\n dp[i][j | (1 << k)] = max(dp[i][j | (1 << k)], i+dp[i-1][j]);\n ans = max(ans, dp[i][j | (1 << k)]);\n }\n }\n }\n return ans;\n }\n};",
"memory": "26469"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 1 | {
"code": "#define lli long long int \n#define ld long double\n#define vi vector<int>\n#define vlli vector<lli>\n#define vpii vector<pair<int, int>>\n#define pb push_back\n#define all(__x) __x.begin(),__x.end()\n\ntemplate<typename T> void debug(T _a) {cout << _a << \" \";}\ntemplate<typename T1, typename T2> void debug(pair<T1, T2> _p) {cout<<\"{\";debug(_p.first);cout<<\": \";debug(_p.second);cout<<\"}\\n\";}\ntemplate<typename T> void debug(vector<T> _aa) {for (auto h: _aa) debug(h); cout << endl;}\ntemplate<typename T> void debug(deque<T> _aa) {for (auto h: _aa) debug(h); cout << endl;}\ntemplate<typename T> void debug(multiset<T> _aa) {for (auto h: _aa) debug(h); cout << endl;}\ntemplate<typename T> void debug(set<T> _aa) {for (auto h: _aa) debug(h); cout << endl;}\ntemplate<typename T1, typename T2> void debug(map<T1, T2> _mm) {for (auto h: _mm) debug(h);}\n\nclass Solution {\npublic:\n int dp[101][1 << 10], n, m;\n\n int maxScore(vector<vector<int>>& g) {\n memset(dp, 0, sizeof dp);\n n = g.size(), m = g[0].size();\n vector<set<int>> idx(101);\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n idx[g[i][j]].insert(i);\n }\n }\n int ans = 0;\n for (int i = 1; i <= 100; i++) {\n for (int j = 0; j < (1 << 10); j++) {\n dp[i][j] = max(dp[i][j], dp[i-1][j]);\n for (int k: idx[i]) {\n if ((j >> k) & 1) continue;\n dp[i][j | (1 << k)] = max(dp[i][j | (1 << k)], i+dp[i-1][j]);\n ans = max(ans, dp[i][j | (1 << k)]);\n }\n }\n }\n return ans;\n }\n};",
"memory": "26469"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int helper(vector<pair<int, set<int>>>& temp, vector<vector<int>>& dp, int iter, int mask){\n if (iter == temp.size()){\n return 0;\n }\n\n if(dp[iter][mask] != -1){\n return dp[iter][mask];\n }\n int res = helper(temp, dp, iter + 1, mask);\n for (auto x: temp[iter].second){\n if ((mask & 1<<x) == 0){\n res = max(res, temp[iter].first + helper(temp, dp, iter+1, mask | (1<<x)));\n }\n }\n return dp[iter][mask] = res;\n }\n int maxScore(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n unordered_map<int,set<int>> temp;\n for (int i = 0; i < m; ++i){\n for (int j = 0; j< n; ++j){\n temp[grid[i][j]].insert(i);\n }\n }\n vector<pair<int, set<int>>> t2(temp.begin(),temp.end());\n sort(t2.begin(),t2.end(),[](pair<int, set<int>>&a, pair<int, set<int>>&b){return a.first > b.first;});\n vector<vector<int>> dp(t2.size() + 1, vector<int>((1 << m), -1));\n return helper(t2, dp, 0, 0);\n }\n};",
"memory": "30515"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int dp[101][1025];\n int solve(int v,int mask,map<int,vector<int>>&g)\n {\n if(v==0)\n return 0;\n if(dp[v][mask]!=-1)\n return dp[v][mask];\n \n int ans=solve(v-1,mask,g);\n\n for(auto x:g[v])\n {\n if((mask&(1<<x))==0)\n ans=max(ans,v+solve(v-1,(mask|(1<<x)),g));\n }\n dp[v][mask]=ans;\n return ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n map<int,vector<int>>mp;\n int n=grid.size(),m=grid[0].size();\n memset(dp,-1,sizeof(dp));\n\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n mp[grid[i][j]].push_back(i);\n }\n }\n return solve(100,0,mp);\n }\n};",
"memory": "30515"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n unordered_map<int,int>mp;\n int rows[15];\n int dp[105][1030];\n int rec(int num,int mask,map<int,vector<int>>&arr){\n\n // bases cases \n if(num==0) return 0;\n if(dp[num][mask]!=-1) return dp[num][mask];\n int ans=0;\n // all choices \n for(auto x:arr[num]){\n if(!(mask&(1<<x)))\n ans=max( ans, num + rec( num-1 ,mask|(1<<x),arr ) );\n \n }\n ans=max(ans,rec( num-1, mask,arr ));\n return dp[num][mask]=ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n memset(rows,0,sizeof(rows));\n int n=grid.size();\n int m=grid[0].size();\n\n map<int,vector<int>>arr;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n arr[grid[i][j]].push_back(i);\n }\n }\n memset(dp,-1,sizeof(dp)); \n return rec(100,0,arr);\n }\n};",
"memory": "34561"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int n, m;\n vector<vector<int>> mem;\n int maxScore(vector<vector<int>>& grid) {\n n = grid.size();\n m = grid[0].size();\n unordered_map<int, int> lut; // look-up table\n for (int i = 0; i < n; ++i) {\n for (auto& x : grid[i])\n lut[x] |= (1 << i);\n }\n vector<int> nums;\n for (auto& [x, _] : lut) {\n nums.push_back(x);\n }\n ranges::sort(nums);\n int u = 1 << n;\n mem = vector<vector<int>>(nums.size(), vector<int>(1 << n, -1));\n vector<vector<int>> f(nums.size() + 1, vector<int>(u));\n for(int i = 0; i < nums.size(); ++i) {\n int x = nums[i];\n for(int j = 0; j < u; ++j) {\n f[i + 1][j] = f[i][j];\n \n for (int b = lut[x], lb; b; b ^= lb) {\n lb = b & -b;\n if((j & lb) == 0)\n f[i + 1][j] = max(f[i + 1][j], f[i][j | lb] + x);\n }\n }\n \n\n }\n return f.back()[0];\n }\n};\n",
"memory": "34561"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\nconst int A = 102;\n\n void chmax(int& x, int y){\n x = max(x, y);\n }\n\n int maxScore(vector<vector<int>>& a) {\n int n = a.size(), m = a[0].size();\n vector<vector<int>> mxless(n, vector<int>(A));\n\n for (int i = 0; i < n; i++){\n for (int j = 0; j < m; j++){\n for (int x = 0; x < A; x++){\n if (a[i][j] < x) mxless[i][x] = max(mxless[i][x], a[i][j]);\n }\n }\n }\n\n vector<vector<int>> dp(1 << n, vector<int>(A));\n int ans = 0;\n\n for (int mask = 0; mask < (1 << n); mask++){\n for (int x = 0; x < A; x++){\n ans = max(ans, dp[mask][x]);\n for (int i = 0; i < n; i++){\n if (mask >> i & 1) continue;\n chmax(dp[mask ^ (1 << i)][mxless[i][x]], dp[mask][x] + mxless[i][x]);\n }\n }\n }\n \n return ans;\n }\n};",
"memory": "38608"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n#define ll long long\n#define all(v) v.begin(),v.end()\n#define F first\n#define S second\n#define pb push_back\n#define mp make_pair\n#define pi pair<ll,ll>\n#define pii pair<int,pi>\n#define rep(i,n) for(int i=0;i<n;i++)\n#define rrep(i,n) for(int i=n-1;i>=0;--i)\n#define cusrep(i,a,b,j) for(int i=a;i<b;i+=j)\n#define rng(i,a,b) for(int i=a;i<b;i++)\n#define rrng(i,a,b) for(int i=a;i>b;--i)\n#define ar array\n\nclass Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int r = grid.size();\n int c = grid[0].size();\n \n vector<set<int>> h(r);\n rep(i,r){\n rep(j,c){\n h[i].insert(grid[i][j]);\n }\n }\n vector<vector<int>> dp(1<<r, vector<int>(101, 0));\n rep(i,r){\n rng(j,1,101){\n dp[1<<i][j] = (h[i].contains(j) ? j : 0);\n }\n }\n rep(mask,1<<r){\n rep(i,r){\n if(mask&(1<<i)){\n rng(j,1,101){\n if(h[i].contains(j)){\n rng(k,1,j){\n dp[mask][j] = max(dp[mask][j], dp[mask^(1<<i)][k] + j);\n }\n }\n }\n }\n }\n }\n int ans = 0;\n rep(mask,1<<r){\n rng(j,1,101){\n ans=max(ans, dp[mask][j]);\n }\n }\n return ans;\n }\n};",
"memory": "38608"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int helper(int ind,int mask,vector<pair<int,int>>& v, vector<vector<int>> &dp){\n int n = v.size();\n if(ind==n) return 0;\n if(dp[ind][mask]!=-1) return dp[ind][mask];\n\n int pick = 0, notPick = 0;\n int row = v[ind].second;\n if(!(mask&(1<<row))){\n int i = ind;\n while(i<n && v[i].first==v[ind].first){\n i++;\n }\n pick = v[ind].first + helper(i,(mask|(1<<row)),v,dp);\n }\n notPick = helper(ind+1,mask,v,dp);\n return dp[ind][mask] = max(pick,notPick);\n }\n int maxScore(vector<vector<int>>& grid) {\n vector<pair<int,int>> v;\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 v.push_back({grid[i][j],i});\n }\n }\n sort(v.begin(),v.end());\n vector<vector<int>> dp(v.size()+1,vector<int>(1<<(m+1),-1));\n return helper(0,0,v,dp);\n }\n};",
"memory": "42654"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int helper(int ind,int mask,vector<pair<int,int>>& v, vector<vector<int>> &dp){\n int n = v.size();\n if(ind==n) return 0;\n if(dp[ind][mask]!=-1) return dp[ind][mask];\n\n int pick = 0, notPick = 0;\n int row = v[ind].second;\n if(!(mask&(1<<row))){\n int i = ind;\n while(i<n && v[i].first==v[ind].first){\n i++;\n }\n pick = v[ind].first + helper(i,(mask|(1<<row)),v,dp);\n }\n notPick = helper(ind+1,mask,v,dp);\n return dp[ind][mask] = max(pick,notPick);\n }\n\n int maxScore(vector<vector<int>>& grid) {\n vector<pair<int,int>> v;\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 v.push_back({grid[i][j],i});\n }\n }\n sort(v.begin(),v.end());\n vector<vector<int>> dp(v.size()+1,vector<int>(1<<(m+1),-1));\n return helper(0,0,v,dp);\n }\n};",
"memory": "42654"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n // int dp[101][1025];\n int findMax(int val, int mask, unordered_map<int, vector<int>>& arr, vector<vector<int>>& dp){\n if(val == 0) return 0;\n\n // cout<<val<<\" \"<<mask<<endl;\n if(dp[val][mask] != -1) return dp[val][mask];\n\n int maxi = findMax(val-1, mask, arr, dp);\n for(auto i: arr[val]){\n if(((1<<i) & mask) == 0){\n maxi = max(maxi, val + findMax(val-1, mask|(1<<i), arr, dp));\n }\n }\n\n return dp[val][mask] = maxi;\n }\n int maxScore(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vector<vector<int>> dp(101, vector<int> (1<<m, -1));\n // memset(dp, -1, sizeof(dp));\n unordered_map<int, vector<int>> arr;\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n arr[grid[i][j]].push_back(i);\n }\n }\n\n return findMax(100, 0, arr, dp);\n }\n};",
"memory": "46700"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>&g) {\n map<int,set<int>>m;\n int r(g.size()),c(g[0].size()),rishabh_maxi(0);\n for(int i=0;i<r;i++){\n for(int j=0;j<c;j++){\n m[g[i][j]].insert(i);\n rishabh_maxi=max(rishabh_maxi,g[i][j]);\n }\n }\n auto it=m.rbegin();\n int n=m.size(); \n vector<vector<int>>dp(rishabh_maxi+1,vector<int>(1<<(r+1),-1));\n function<int(map<int, set<int>>::reverse_iterator,int)>dfs=[&](map<int, set<int>>::reverse_iterator i,int mask){\n if(mask==((1<<r)-1) || i==m.rend()) return 0;\n int s(0);\n if(dp[i->first][mask] != -1) return dp[i->first][mask];\n for(auto e:i->second){\n if(!(mask&(1<<e))){ \n mask|=(1<<e);\n s=max(s,i->first+dfs(next(i),mask));\n mask^=(1<<e); \n }\n }\n s=max(s,dfs(next(i),mask));\n return dp[i->first][mask] = s;\n };\n return dfs(it,0);\n }\n};",
"memory": "46700"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int f(int rowMask,int prev,vector<vector<int>> &grid,vector<vector<int>> &dp){\n if(rowMask == ((1 << grid.size()) - 1)){\n return 0;\n }\n if(dp[rowMask][prev] != -1)return dp[rowMask][prev];\n int maxi = 0;\n for(int i = 0;i<grid.size();i++){\n for(int j = 0;j<grid[0].size();j++){\n if(((1 << i) & rowMask) == 0 && grid[i][j] > prev){\n maxi = max(grid[i][j] + f((rowMask | (1 << i)),grid[i][j],grid,dp),maxi);\n }\n }\n }\n return dp[rowMask][prev] = maxi;\n }\n int maxScore(vector<vector<int>>& grid) {\n vector<vector<int>> dp((1 << (grid.size() + 1)),vector<int> (101,-1));\n return f(0,0,grid,dp);\n }\n};",
"memory": "50746"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int m = grid.size(), n = grid[0].size();\n unordered_map<int, unordered_set<int>> val2row;\n for(int i = 0; i < m; i++)\n for(int j = 0; j < n; j++) {\n val2row[grid[i][j]].insert(i);\n }\n\n vector<vector<int>> dp(val2row.size() + 1, vector<int>(1024, 0));\n unordered_map<int, unordered_set<int>>::iterator it = val2row.begin();\n for(int i = val2row.size() - 1; i >= 0; i--, it++)\n for(int j = 0; j < (1 << m); j++) {\n dp[i][j] = max(dp[i][j], dp[i + 1][j]);\n for(int row : it->second) {\n if(j & (1 << row)) {\n dp[i][j] = max(dp[i][j], it->first + dp[i + 1][j & (~(1 << row))]);\n }\n }\n }\n\n return dp[0][(1 << m) - 1];\n\n\n \n \n \n \n \n\n }\n};\n\n/*\nmax value in the grid must be choosed\n\n1. select the values row by row\n(1) dp\nneed to store choosed numbers => too many states\n\n(2) binary search\nhard to find a way to check\n\n(3) greedy\nhard to get the priority of rows with same max value\n(a) row with lesser candidate(x)\nex. 8 7 \n 8 3 1\n(b) row with smaller next candidate(x)\nex. 8 6 5 4\n 8 7\n 8 7\n\n2. select the values in the decreasing order, store which rows are used\ndp[idx][used rows]\n*/",
"memory": "50746"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n void clean(vector<vector<int>>& arr,int m){\n int n = (int)arr.size();\n \n vector<vector<int>> ret;\n for(int i=0;i<n;i++){\n if(arr[i][(int)arr[i].size()-1] == m){\n arr[i].pop_back();\n }\n if(!arr[i].empty()){\n ret.push_back(arr[i]);\n }\n }\n \n arr = ret;\n }\n \n void search(vector<vector<int>> arr,int sum,int& maxSum){\n if(arr.empty()){\n maxSum = max(maxSum,sum);\n return;\n }\n \n int n = (int)arr.size();\n int m = 0;\n for(int i=0;i<n;i++){\n m = max(m,arr[i][(int)arr[i].size()-1]);\n }\n \n for(int i=0;i<n;i++){\n if(arr[i][(int)arr[i].size()-1] == m){\n vector<vector<int>> _arr = arr;\n for(int j=i;j<n-1;j++){\n _arr[j] = _arr[j+1];\n }\n _arr.pop_back();\n clean(_arr,m);\n search(_arr,sum+m,maxSum);\n }\n }\n }\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int n = (int)grid.size();\n int m = (int)grid[0].size();\n \n vector<vector<int>> arr;\n for(int i=0;i<n;i++){\n set<int> S;\n for(int j=0;j<m;j++){\n S.insert(grid[i][j]);\n }\n vector<int> _arr;\n for(set<int>::iterator it=S.begin();it!=S.end();it++){\n _arr.push_back(*it);\n }\n arr.push_back(_arr);\n }\n \n int ret = 0;\n search(arr,0,ret);\n return ret;\n }\n};",
"memory": "54793"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void dfs(int mask, vector<map<int,int>>& dp, vector<bool>&vis, vector<vector<int>>& grid)\n {\n //cout << \"entering with mask \" << mask << endl;\n vis[mask] = true;\n int m = grid.size();\n //cout << \"grid.size = \" << grid.size() << endl;\n for(int i = 0; i < m; i++)\n {\n if((1<<i) & mask)\n {\n int t = mask ^ (1<<i);\n //cout << \" predecessor \" << t << endl;\n if(!vis[t])\n {\n dfs(t, dp, vis, grid);\n }\n for(auto &u : grid[i])\n {\n for(auto &p : dp[t])\n {\n if(p.first >= u)\n {\n break;\n }\n dp[mask][u] = max(dp[mask][u], u+p.second);\n }\n }\n }\n }\n \n }\n int maxScore(vector<vector<int>>& grid) {\n int m = grid.size();\n //cout << \"rows: \" << m << endl;\n for(int i = 0; i < m; i++)\n {\n auto &g = grid[i];\n sort(g.begin(), g.end());\n g.erase(unique(g.begin(), g.end()), g.end());\n \n }\n int L = 1 << m;\n const int d = 105;\n vector<map<int,int>> dp(L);\n vector<bool> vis(L,0);\n vis[0] = true;\n dp[0][0] = 0;\n \n int ans = 0;\n \n for(int mask = 0; mask < L; mask++)\n {\n //cout << \"mask = \" << mask << endl;\n if(!vis[mask])\n {\n dfs(mask, dp, vis, grid);\n }\n for(auto &p : dp[mask])\n {\n //cout << \"dp(\" << mask << \", \" << p.first << \") = \" << p.second << endl;\n ans = max(ans, p.second);\n }\n }\n \n return ans;\n }\n};",
"memory": "54793"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp;\n int allOne;\n int find(vector<vector<int>> &num_pos, int idx, int mask){\n int n = num_pos.size();\n if (idx == n || mask == allOne)\n return 0;\n if (dp[idx][mask] != -1)\n return dp[idx][mask];\n\n int ans = -1;\n if ((1 << num_pos[idx][1]) & mask)\n ans = find(num_pos, idx + 1, mask);\n else {\n int j = idx + 1;\n while (j < n && num_pos[j][0] == num_pos[idx][0])\n j++;\n int pick = num_pos[idx][0] + find(num_pos, j, mask | (1 << num_pos[idx][1]));\n int not_pick = find(num_pos, idx + 1, mask);\n ans = max(pick, not_pick);\n }\n dp[idx][mask] = ans;\n return ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n int m = grid.size(), n = grid[0].size();\n dp.resize(m*n, vector<int>(1023, -1));\n allOne = 0;\n for (int i = 0; i < m; i++)\n allOne |= (1 << i);\n vector<vector<int>> num_pos;\n for (int i = 0; i < grid.size(); i++)\n for (int j = 0; j < grid[0].size(); j++)\n num_pos.push_back({grid[i][j], i, j});\n\n sort(num_pos.begin(), num_pos.end(), greater<>());\n return find(num_pos, 0, 0);\n }\n};",
"memory": "58839"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp;\n int allOne;\n int find(vector<vector<int>> &num_pos, int idx, int mask){\n int n = num_pos.size();\n if (idx == n || mask == allOne)\n return 0;\n if (dp[idx][mask] != -1)\n return dp[idx][mask];\n\n int ans = -1;\n if ((1 << num_pos[idx][1]) & mask)\n ans = find(num_pos, idx + 1, mask);\n else {\n int j = idx + 1;\n while (j < n && num_pos[j][0] == num_pos[idx][0])\n j++;\n int pick = num_pos[idx][0] + find(num_pos, j, mask | (1 << num_pos[idx][1]));\n int not_pick = find(num_pos, idx + 1, mask);\n ans = max(pick, not_pick);\n }\n dp[idx][mask] = ans;\n return ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n int m = grid.size(), n = grid[0].size();\n dp.resize(m*n, vector<int>(1023, -1));\n allOne = 0;\n for (int i = 0; i < m; i++)\n allOne |= (1 << i);\n vector<vector<int>> num_pos;\n for (int i = 0; i < grid.size(); i++)\n for (int j = 0; j < grid[0].size(); j++)\n num_pos.push_back({grid[i][j], i, j});\n\n sort(num_pos.begin(), num_pos.end(), greater<>());\n return find(num_pos, 0, 0);\n }\n};",
"memory": "58839"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\n vector<vector<int>> dp;\n int backtrack(vector<vector<int>>& arr, int i, int mask) {\n if (i >= arr.size()) return 0;\n if (dp[i][mask] != -1) return dp[i][mask];\n\n int acc = 0;\n if (!(mask & (1 << arr[i][1]))) { // take\n int nexti = i; // avoid picking same value again\n int newMask = mask | (1 << arr[i][1]); // avoid picking same row again\n while (nexti < arr.size() && arr[i][0] == arr[nexti][0]) nexti++;\n acc = max(acc, arr[i][0] + backtrack(arr, nexti, newMask));\n }\n // skip\n acc = max(acc, backtrack(arr, i+1, mask));\n return dp[i][mask] = acc;\n }\n\npublic:\n // Sorting it takes care of selecting unique values\n // Bitmask takes care of selecting unique rows\n int maxScore(vector<vector<int>>& grid) {\n vector<vector<int>> arr;\n for (int i = 0; i < grid.size(); i++) {\n for (int j = 0; j < grid[0].size(); j++) {\n arr.push_back({grid[i][j], i});\n }\n }\n sort(arr.begin(), arr.end());\n dp.resize(arr.size()+1, vector<int>((1<<10)+1, -1));\n\n return backtrack(arr, 0, 0);\n }\n};",
"memory": "62885"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<pair<int,int>> arr;\n vector<vector<int>> dp;\n int solve(int i , int mask){\n if(i == arr.size())\n return 0;\n if(~dp[i][mask])\n return dp[i][mask];\n int ans = solve(i + 1, mask);\n for(int j = i; j < arr.size(); j++){\n if(mask & (1 << arr[j].second))\n continue;\n pair<int,int> p = {arr[j].first + 1 , -1};\n int nxt = lower_bound(arr.begin() , arr.end() , p) - arr.begin();\n ans = max(ans, solve(nxt , mask | (1 << arr[j].second)) + arr[j].first);\n }\n return dp[i][mask] = ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n arr.clear();\n for(int i = 0; i < grid.size(); i++){\n for(int j = 0; j < grid[0].size(); j++){\n arr.push_back({grid[i][j] , i});\n }\n }\n \n sort(arr.begin() , arr.end());\n dp = vector<vector<int>> (arr.size() + 1 , vector<int> (1024,-1));\n return solve(0 , 0);\n }\n};",
"memory": "62885"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n for (auto& a : grid) {\n sort(a.begin(), a.end(), greater<int>());\n }\n\n vector<pair<int, unordered_set<int>>> dp(1 << grid.size());\n\n for (int i = 0; i < dp.size(); i++) {\n for (int j = 0; j < grid.size(); j++) {\n if ((i & (1 << j)) == 0) {\n int val = 0;\n for (int k = 0; k < grid[0].size(); k++) {\n if (dp[i].second.find(grid[j][k]) == dp[i].second.end()) {\n val = grid[j][k];\n break;\n }\n }\n\n unordered_set<int> ns(dp[i].second);\n if (val != 0) ns.insert(val);\n\n int nv = dp[i].first + val;\n int np = i | (1 << j);\n if (nv > dp[np].first) {\n dp[np].first = nv;\n dp[np].second = std::move(ns);\n }\n }\n }\n }\n\n return dp[dp.size() - 1].first;\n }\n};",
"memory": "66931"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int res = 0;\n \n int maxScore(vector<vector<int>>& grid) {\n vector<vector<int>>collect(grid.size());\n map<int, vector<int>>num_index;\n for(int i = 0; i < grid.size(); i++){\n unordered_set<int>us;\n for(int j = 0; j < grid[0].size(); j++){\n int temp = grid[i][j];\n if(us.count(temp) == 0){\n collect[i].push_back(temp);\n us.insert(temp);\n num_index[temp].push_back(i);\n }\n\n }\n }\n unordered_set<int> used;\n vector< pair<int, vector<int>> >num_index2;\n for(auto it = num_index.begin(); it != num_index.end(); it++){\n // for(auto s1 : s.second){\n // cout << \" \"<< s1;\n // }\n //cout << endl;\n //cout << s.first<<endl;\n num_index2.push_back({it->first, it->second});\n }\n //sort(num_index2.begin(), num_index2.end());\n reverse(num_index2.begin(), num_index2.end());\n // for(int i = 0; i < num_index2.size(); i++){\n // cout << \"in\" << num_index2[i].first<<endl;\n // }\n dfs(0, 0, num_index2, collect.size(), 0, used);\n return res;\n \n }\n\n void dfs(int index, int num_index, vector< pair<int, vector<int>> >&num_index2, int len, int sum, unordered_set<int>visit){\n if(index == len){\n \n return;\n }\n bool flag = 0;\n while(flag == 0 && num_index < num_index2.size()){\n //cout <<\"in \" << num_index << endl;\n //cout << num_index2[num_index].first<<endl;\n \n int temp = num_index2[num_index].first;\n //cout << temp << endl;\n auto vec = num_index2[num_index].second;\n //cout << vec.size()<<endl;\n for(int i = 0; i < vec.size(); i++){\n int index_temp = vec[i];\n if(visit.count(index_temp) == 0){\n flag = 1;\n visit.insert(index_temp);\n //cout << sum+temp << endl;\n res = max(res, sum + temp);\n dfs(index + 1, num_index + 1, num_index2, len, sum + temp, visit);\n visit.erase(index_temp);\n }\n }\n num_index++;\n\n \n }\n \n\n }\n};",
"memory": "66931"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int FUNC(vector<vector<int>>& A) {\n int n = A.size();\n vector<set<int>> V(101);\n int h = 0;\n for (int p = 0; p < A.size(); p++) {\n for (int q = 0; q < A[p].size(); q++) {\n V[A[p][q]].insert(p);\n h = max(h, A[p][q]);\n }\n }\n vector<vector<int>> dp(h + 1, vector<int>(pow(2, n), 0));\n for (int q = h; q >= 1; q--) {\n dp[q][0] = 0;\n }\n int ANS = h;\n while (!V[h].empty()) {\n int L = *V[h].begin();\n V[h].erase(V[h].begin());\n dp[h][pow(2, L)] = h;\n }\n for (int q = h - 1; q >= 1; q--) {\n for (int p = 1; p < pow(2, n); p++) {\n dp[q][p] = max(dp[q][p], dp[q + 1][p]);\n ANS = max(ANS, dp[q][p]);\n int d = p;\n vector<int> B;\n for (int b = 0; b < n; b++) {\n if (((d & 1) == 0) && V[q].find(b) != V[q].end()) {\n B.push_back(b);\n }\n d = d >> 1;\n }\n while (!B.empty()) {\n int R = B.back();\n int l = pow(2, R);\n B.pop_back();\n dp[q][p + l] = max(dp[q][p + l], dp[q + 1][p] + q);\n ANS = max(ANS, dp[q][p + l]);\n }\n }\n }\n return ANS;\n }\n int maxScore(vector<vector<int>>& grid) {\n return FUNC(grid);\n }\n};",
"memory": "70978"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "#include <vector>\n#include <unordered_map>\n#include <unordered_set>\n#include <algorithm>\n#include <string>\n\nclass Solution {\npublic:\n void sortRowsDescending(std::vector<std::vector<int>>& matrix) {\n for (auto& row : matrix) {\n std::sort(row.begin(), row.end(), std::greater<int>());\n }\n }\n\n std::string generateMemoKey(int currentRow, const std::unordered_set<int>& seenNumbers) {\n std::string key = std::to_string(currentRow) + \"-\";\n for (int num : seenNumbers) {\n key += std::to_string(num) + \",\";\n }\n return key;\n }\n\n int computeMaxScore(std::vector<std::vector<int>>& matrix, int rowIndex, std::unordered_set<int>& seenNumbers, int currentSum, int& globalMax, std::unordered_map<std::string, int>& memo) {\n if (rowIndex == matrix.size()) {\n return currentSum;\n }\n\n std::string memoKey = generateMemoKey(rowIndex, seenNumbers);\n if (memo.find(memoKey) != memo.end()) {\n return memo[memoKey];\n }\n\n int localMax = currentSum;\n\n for (int colIndex = 0; colIndex < matrix[rowIndex].size(); ++colIndex) {\n int cellValue = matrix[rowIndex][colIndex];\n if (seenNumbers.find(cellValue) == seenNumbers.end()) {\n int potentialScore = currentSum + cellValue;\n for (int nextRow = rowIndex + 1; nextRow < matrix.size(); ++nextRow) {\n potentialScore += matrix[nextRow][0];\n }\n if (potentialScore <= globalMax) break;\n\n seenNumbers.insert(cellValue);\n localMax = std::max(localMax, computeMaxScore(matrix, rowIndex + 1, seenNumbers, currentSum + cellValue, globalMax, memo));\n seenNumbers.erase(cellValue);\n }\n }\n\n localMax = std::max(localMax, computeMaxScore(matrix, rowIndex + 1, seenNumbers, currentSum, globalMax, memo));\n memo[memoKey] = localMax;\n globalMax = std::max(globalMax, localMax);\n\n return localMax;\n }\n\n int maxScore(std::vector<std::vector<int>>& matrix) {\n std::unordered_set<int> seenNumbers;\n std::unordered_map<std::string, int> memoization;\n int globalMax = 0;\n sortRowsDescending(matrix);\n return computeMaxScore(matrix, 0, seenNumbers, 0, globalMax, memoization);\n }\n};\n",
"memory": "70978"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int max_score(vector<vector<int>>& grid,int i,string &mp,vector<unordered_map<int, int> > &mp2, unordered_map<int, unordered_map<string, int>> &dp){\n int n=grid.size();\n if(i==n){\n return 0;\n }\n if(dp[i][mp]) return dp[i][mp];\n\n\n int ans=0;\n bool not_pick_up=true;\n for(auto x: grid[i]){\n int pick=0;\n for(int j=0;j<n;j++){\n if(i!=j){\n pick+=mp2[j][x];\n }\n }\n\n if(mp[x] == '0'){\n mp[x]='1';\n not_pick_up=false;\n ans=max(ans,max_score(grid,i+1,mp,mp2,dp)+x);\n mp[x]='0';\n if(!pick){\n break;\n }\n }\n }\n\n if(not_pick_up){\n ans=max(ans,max_score(grid,i+1,mp,mp2,dp));\n }\n return dp[i][mp]=ans;\n }\n\n\n\n int maxScore(vector<vector<int>>& grid) {\n int n=grid.size();\n int m=grid[0].size();\n string mp(101, '0');\n vector<unordered_map<int, int> > mp2(grid.size());\n for(int i=0;i<n;i++){\n sort(grid[i].rbegin(),grid[i].rend());\n for(int x: grid[i]){\n mp2[i][x]++;\n }\n }\n unordered_map<int, unordered_map<string, int>> dp;\n return max_score(grid,0,mp,mp2,dp);\n }\n};",
"memory": "75024"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int max_score(vector<vector<int>>& grid,int i,string &mp,vector<unordered_map<int, int> > &mp2, unordered_map<int, unordered_map<string, int>> &dp){\n int n=grid.size();\n if(i==n){\n return 0;\n }\n if(dp[i][mp]) return dp[i][mp];\n\n\n int ans=0;\n bool not_pick_up=true;\n for(auto x: grid[i]){\n int pick=0;\n for(int j=0;j<n;j++){\n if(i!=j){\n pick+=mp2[j][x];\n }\n }\n\n if(mp[x] == '0'){\n mp[x]='1';\n not_pick_up=false;\n ans=max(ans,max_score(grid,i+1,mp,mp2,dp)+x);\n mp[x]='0';\n if(!pick){\n break;\n }\n }\n }\n\n if(not_pick_up){\n ans=max(ans,max_score(grid,i+1,mp,mp2,dp));\n }\n return dp[i][mp]=ans;\n }\n\n\n\n int maxScore(vector<vector<int>>& grid) {\n int n=grid.size();\n int m=grid[0].size();\n string mp(101, '0');\n vector<unordered_map<int, int> > mp2(grid.size());\n for(int i=0;i<n;i++){\n sort(grid[i].rbegin(),grid[i].rend());\n for(int x: grid[i]){\n mp2[i][x]++;\n }\n }\n unordered_map<int, unordered_map<string, int>> dp;\n return max_score(grid,0,mp,mp2,dp);\n }\n};",
"memory": "75024"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n map<pair<string,int>,int> mp;\n int f(vector<vector<int>> &grid,int val,int n,string &mask){\n if(val==0)return 0;\n if(mp.find({mask,val})!=mp.end())return mp[{mask,val}];\n int ans=0;\n bool flag=false;\n for(int i=0;i<n;i++){\n if(mask[i]=='1')continue;\n for(auto it:grid[i]){\n if(it==val){\n mask[i]='1';\n ans=max(ans,it+f(grid,val-1,n,mask));\n flag=true;\n mask[i]='0';\n }\n }\n }\n if(!flag)ans=max(ans,f(grid,val-1,n,mask));\n return mp[{mask,val}]=ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n string mask;\n mask.clear();\n mask.resize(101,'0');\n int n=grid.size();\n mp.clear();\n return f(grid,100,n,mask);\n }\n};",
"memory": "79070"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n vector<set<int>> val_rows(101);\n for(int i = 0; i < n; i++){\n for(int j = 0; j < m; j++){\n auto x = grid[i][j];\n val_rows[x].insert(i);\n }\n }\n map<int, int> dp;\n dp[0] = 0;\n for(int i = 100; i >= 1; i--){\n if(val_rows[i].empty()) continue;\n map<int, int> ndp = dp;\n for(auto [mask, v] : dp){\n for(auto r : val_rows[i]){\n if(mask & (1 << r)) continue;\n ndp[mask | (1 << r)] = max(ndp[mask | (1 << r)], dp[mask] + i);\n }\n }\n dp = ndp;\n }\n auto res = 0;\n for(auto [mask, val] : dp){\n res = max(res, val);\n }\n return res;\n }\n};",
"memory": "79070"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void help(int id,vector<vector<int>>& a,unordered_map<int,int> &mp,unordered_set<int> &vis,int *ans)\n {\n int n=a.size();\n int m=a[0].size();\n if(id==n)\n {\n int sum=0;\n for(auto val:vis)\n {\n sum+=val;\n }\n *ans=max(*ans,sum);\n return;\n }\n help(id+1,a,mp,vis,ans);\n for(int j=m-1;j>=0;j--)\n {\n if(vis.find(a[id][j])!=vis.end())\n continue;\n vis.insert(a[id][j]);\n help(id+1,a,mp,vis,ans);\n vis.erase(a[id][j]);\n if(mp[a[id][j]]<=id)\n {\n break;\n }\n }\n }\n int maxScore(vector<vector<int>>& a) {\n unordered_map<int,int> mp;\n int i=0;\n for(auto& r:a)\n {\n for(auto v:r) mp[v]=i;\n i++;\n sort(r.begin(),r.end());\n }\n unordered_set<int> vis;\n int ans=0;\n help(0,a,mp,vis,&ans);\n return ans;\n }\n};",
"memory": "83116"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "vector<vector<vector<int>>>dp(102,vector<vector<int>>(102,vector<int>(1025,-1)));\nclass Solution {\npublic:\n \n int help(int i,vector<pair<int,int>>&v,int last,int mask){\n if(i==v.size())return 0;\n int ans=0;\n if(dp[i][last][mask]!=-1)return dp[i][last][mask];\n if(last!=v[i].first && ((mask>>v[i].second)&1)==0)ans=max(ans,v[i].first+help(i+1,v,v[i].first,(mask|(1<<v[i].second))));\n ans=max(ans,help(i+1,v,last,mask));\n dp[i][last][mask]=ans;\n return ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n int n=grid.size();int m=grid[0].size();\n vector<pair<int,int>>v;\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 for(int i=0;i<=n*m;i++){\n for(int j=0;j<=100;j++){\n for(int k=0;k<=1024;k++)dp[i][j][k]=-1;\n }\n }\n sort(v.begin(),v.end());\n int mask=0;\n\n return help(0,v,0,0);\n }\n};",
"memory": "83116"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\n\n vector<vector<int>> dp;\n int solve(int ind , vector<vector<int>> &values,int mask){\n if(ind==values.size())return 0;\n\n if(dp[mask][ind] != -1)return dp[mask][ind];\n\n int ans = solve(ind+1,values,mask);\n\n if(!(mask & (1<<values[ind][1]))){\n int j =ind;\n while(j<values.size() && values[ind][0] == values[j][0])j++;\n int temp = values[ind][0] + solve(j,values, mask | (1<<values[ind][1]));\n ans = max(ans , temp);\n }\n return dp[mask][ind] = ans;\n\n }\npublic:\n int maxScore(vector<vector<int>>& grid) {\n\n // cout<<(1<<10);\n \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});\n\n dp.resize(1025,vector<int>(values.size(),-1));\n \n sort(values.begin(),values.end(),greater<vector<int>>());\n return solve(0,values,0); \n }\n};",
"memory": "87163"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n for (auto& row : grid) {\n set<int> unique_elements(row.begin(), row.end());\n row.assign(unique_elements.rbegin(), unique_elements.rend());\n }\n\n priority_queue<pair<int, vector<vector<int>>>, vector<pair<int, vector<vector<int>>>>, greater<>> heap;\n heap.push({0, grid});\n int ans = 0;\n\n while (!heap.empty()) {\n auto [curr, rems] = heap.top();\n heap.pop();\n\n if (rems.empty()) {\n ans = max(ans, -curr);\n continue;\n }\n\n vector<vector<int>> rem(rems);\n vector<int> pos;\n int maxa = 0;\n for (int j = 0; j < rem.size(); ++j) {\n if (rem[j].empty()) continue;\n if (rem[j][0] > maxa) {\n pos = {j};\n maxa = rem[j][0];\n } else if (rem[j][0] == maxa) {\n pos.push_back(j);\n }\n }\n\n for (int j = 0; j < rem.size(); ++j) {\n rem[j].erase(remove(rem[j].begin(), rem[j].end(), maxa), rem[j].end());\n }\n\n for (int i : pos) {\n vector<vector<int>> t(rem);\n t.erase(t.begin() + i);\n heap.push({curr - maxa, t});\n }\n\n ans = max(ans, -curr);\n }\n\n return ans;\n }\n};",
"memory": "87163"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "\nclass Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size();\n unordered_map<int, vector<int>> vals;\n\n // Organize grid values into the vals map\n for (int i = 0; i < n; ++i) {\n for (int h : grid[i]) {\n vals[h].push_back(i);\n }\n }\n\n // Convert the vals map into a list of pairs\n vector<pair<int, vector<int>>> items(vals.begin(), vals.end());\n\n // Memoization table\n unordered_map<int, unordered_map<int, int>> memo;\n\n // Recursive function with memoization and bitmask\n function<int(int, int)> maxval = [&](int i, int mask) -> int {\n if (i >= items.size()) {\n return 0;\n }\n if (memo[i].count(mask)) {\n return memo[i][mask];\n }\n\n // Option 1: Skip the current value\n int res = maxval(i + 1, mask);\n\n // Option 2: Try to include the current value\n for (int row : items[i].second) {\n if (!(mask & (1 << row))) { // Check if row is not used\n int new_mask = mask | (1 << row); // Mark row as used\n res = max(res, items[i].first + maxval(i + 1, new_mask));\n }\n }\n\n // Store the result in the memoization table\n memo[i][mask] = res;\n return res;\n };\n\n // Start the recursion with the first item and an empty mask\n return maxval(0, 0);\n }\n};\n",
"memory": "91209"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "// Focus on uniqueness in values first, then uniqueness in rows \nclass Solution {\npublic:\n int help(int i,int n,vector<int>&val,map<int,vector<int>>&mpp, map<pair<int,int>,int>&dp,int mask){\n if(i==n) return 0;\n if(dp.find({i,mask})!=dp.end()) return dp[{i,mask}];\n\n int ans=0;\n for(int r:mpp[val[i]]){\n if((mask&(1<<r))==0){\n ans=max(ans,val[i]+help(i+1,n,val,mpp,dp,mask|(1<<r)));\n }\n }\n ans=max(ans,help(i+1,n,val,mpp,dp,mask));\n\n return dp[{i,mask}]=ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n set<int>st;\n for(auto &it:grid){\n for(int a:it){\n st.insert(a);\n }\n }\n vector<int>Uval(st.begin(),st.end());\n sort(Uval.begin(),Uval.end(),greater<int>());\n map<int,vector<int>>mpp;\n int n=grid.size();\n for(int i=0;i<n;i++){\n for(int a:grid[i]){\n mpp[a].push_back(i);\n }\n }\n int m=Uval.size();\n map<pair<int,int>,int>dp;\n // int mask=0;\n // in general int has 32 bit but here only 10 bit we take in use because each bit represent as a row\n // initial all row 0 if 1 then it is used .. 3,2,1,0 row no. as bit no. \n return help(0,m,Uval,mpp,dp,0);\n }\n};",
"memory": "91209"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n // int f(int row, vector<vector<int>>& grid, int m, int n, unordered_map<int,bool>& isPresent) {\n // if(row == m) {\n // return 0;\n // }\n // int ans = 0;\n // for(int i=row; i<m; i++) {\n // for(int j=0; j<n; j++) {\n // if(!isPresent[grid[i][j]]) {\n // isPresent[grid[i][j]] = 1;\n // ans = max(ans, grid[i][j] + f(i+1,grid,m,n,isPresent));\n // isPresent[grid[i][j]] = 0;\n // }\n // }\n // }\n // return ans;\n // }\n int f(int ind, int mask, vector<vector<int>>& numWtRows,vector<vector<int>>& dp, int sz) {\n if(ind >= sz) {\n return 0;\n }\n if(dp[ind][mask] != -1) {\n return dp[ind][mask];\n }\n int row = numWtRows[ind][1];\n int takeInd;\n for(takeInd=ind+1; takeInd < sz; takeInd++) {\n if(numWtRows[ind][0] != numWtRows[takeInd][0]) {\n break;\n }\n }\n int take = (!(mask & (1<<row))) ? numWtRows[ind][0] + f(takeInd, mask | (1<<row), numWtRows, dp, sz) : 0;\n int notTake = f(ind+1, mask, numWtRows, dp, sz);\n\n return dp[ind][mask] = max(take, notTake);\n }\n\n\n int maxScore(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vector<vector<int>> numWtRows;\n for(int i=0; i<m; i++) {\n for(int j=0; j<n; j++) {\n numWtRows.push_back({grid[i][j],i});\n }\n }\n int sz = numWtRows.size();\n sort(numWtRows.begin(), numWtRows.end());\n vector<vector<int>> dp(sz, vector<int>((1<<11),-1));\n f(0,0,numWtRows,dp,sz);\n return dp[0][0];\n }\n};",
"memory": "95255"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "\n\nclass Solution {\npublic:\n\n int rec(int ind,int mask, vector<vector<int>>&values,vector<vector<int>>&dp,int n){\n\n\n if(ind ==n)return 0;\n if(dp[ind][mask]!=-1){\n return dp[ind][mask];\n }\n int ans =0;\n int r = values[ind][1];\n if(((1<<r )& mask)){\n ans += rec(ind+1,mask,values,dp,n);\n }\n else{\n int j=ind;\n while(j<n && (values[ind][0]==values[j][0])){\n j++;\n }\n int ans1 = values[ind][0] + rec(j,(mask|(1<<r)),values,dp,n);\n int ans2 = 0 +rec(ind+1,mask,values,dp,n);\n ans =max(ans1,ans2);\n }\n\n return dp[ind][mask] =ans;\n }\n\n\n int maxScore(vector<vector<int>>& grid) {\n int n =grid.size();\n int m=grid[0].size();\n // dp with bitmask on rows\n \n vector<vector<int>>values;\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 \n sort(values.begin(),values.end(),greater<vector<int>>());\n int N=values.size();\n vector<vector<int>>dp(N,vector<int>(1<<11,-1));\n return rec(0,0,values,dp,N);\n }\n};",
"memory": "95255"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n unordered_map<int, vector<int>> rowMap;\n vector<vector<int>>dp;\n int dfs(int max_num, int chosen_row)\n {\n if(max_num == 0)\n return 0;\n if(dp[max_num][chosen_row] != -1)\n return dp[max_num][chosen_row];\n \n int& res = dp[max_num][chosen_row];\n res = dfs(max_num - 1, chosen_row);\n // 遍歷所有有max_num的row\n for(auto x : rowMap[max_num])\n {\n //以2進位代表選過的row,如果第x位為0代表row x還沒被選過\n if((chosen_row & (1 << x)) == 0)\n {\n res = max(res, dfs(max_num - 1, chosen_row | (1 << x))+ max_num);\n }\n }\n return res;\n }\n \n int maxScore(vector<vector<int>>& grid) {\n int mx = grid[0][0];\n \n for (int i = 0; i < grid.size(); ++i) {\n for (int j = 0; j < grid[i].size(); ++j) {\n int num = grid[i][j];\n\n // Update max if the current number is greater\n mx = max(mx, num);\n\n // Add the row index to the map for the current number\n // only if it's not already present\n if (rowMap[num].size() == 0 || rowMap[num].back() != i) {\n rowMap[num].push_back(i);\n }\n }\n }\n dp = vector<vector<int>>(mx + 1, vector<int>(1 << 10, -1));\n return dfs(mx, 0);\n }\n};",
"memory": "99301"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n map<pair<int, int>, int> dp;\n int helper(vector<vector<int>> & vv, int index, int mask) {\n if (index < 0) {\n return 0;\n }\n if (dp.find(make_pair(index, mask)) != dp.end()) {\n return dp[make_pair(index, mask)];\n }\n int len = vv.size();\n int value = vv[index][0];\n int maxNext = 0;\n maxNext = max(maxNext, helper(vv, index - 1, mask));\n for (int i = 1; i < vv[index].size(); i++) {\n int row = vv[index][i];\n if ((mask & (1 << row)) != 0) {\n continue;\n }\n mask |= (1 << row);\n maxNext = max(maxNext, value + helper(vv, index - 1, mask));\n mask -= (1 << row);\n }\n dp[make_pair(index, mask)] = maxNext;\n return maxNext;\n }\n int maxScore(vector<vector<int>>& grid) {\n // build a map from value to rows containing this value, descending\n // choose the one whose next smaller value is larger\n vector<set<int> > vec;\n map<int, unordered_set<int> > value2Rows; // from value to rows\n int i = 0;\n int r = grid.size();\n for (; i < r; i++) {\n auto row = grid[i];\n set<int> s;\n for (int n : row) {\n s.insert(n);\n value2Rows[n].insert(i);\n }\n vec.push_back(s);\n }\n vector<vector<int> > vv;\n for (auto it : value2Rows) {\n vv.push_back(vector<int>{});\n vv.back().push_back(it.first);\n for (int i : it.second) {\n vv.back().push_back(i);\n }\n }\n int size = vv.size();\n return helper(vv, size - 1, 0); \n }\n};",
"memory": "99301"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n set<int> chosen;\n unordered_map<int, vector<int>> numRowMap;\n for (int i = 0; i < grid.size(); ++i)\n {\n for (int j = 0; j < grid[i].size(); ++j)\n numRowMap[grid[i][j]].emplace_back(i);\n }\n vector<pair<int, vector<int>>> numRowVec;\n for (auto& p : numRowMap)\n {\n p.second.erase(unique(p.second.begin(), p.second.end()), p.second.end());\n numRowVec.emplace_back(p);\n }\n sort(numRowVec.begin(), numRowVec.end(), greater<pair<int, vector<int>>>());\n unordered_map<long, int> memo;\n return maxScoreRecursive(numRowVec, 0, chosen, grid.size(), memo);\n }\n\n int maxScoreRecursive(const vector<pair<int, vector<int>>>& numRowVec, int idx, set<int>& chosen, int num, unordered_map<long, int>& memo)\n {\n int result = 0;\n if (idx >= numRowVec.size())\n return 0;\n if (chosen.size() >= num)\n return 0;\n long key=0, multi = 10;\n for (const auto& v : chosen)\n key = key*multi + v;\n key = key*100 + idx;\n\n auto it = memo.find(key);\n if (it != memo.end())\n return it->second;\n int curr = numRowVec[idx].first;\n for (auto r : numRowVec[idx].second)\n {\n if (chosen.count(r) == 0)\n {\n chosen.emplace(r);\n auto rest = maxScoreRecursive(numRowVec, idx + 1, chosen, num, memo);\n chosen.erase(r);\n result = max(result, curr + rest);\n }\n }\n auto rest = maxScoreRecursive(numRowVec, idx + 1, chosen, num, memo);\n result = max(result, rest);\n memo[key] = result;\n return result;\n }\n\n int maxScoreRecursive2(const vector<vector<int>>& grid, int idx, set<int>& chosen, vector<map<pair<long, long>, int>>& memo) {\n int result = 0;\n if (idx >= grid.size())\n return 0;\n \n long key1 = 0, key2 = 0, multi = 1;\n int k = 0;\n for (const auto& v : chosen)\n {\n if (k == 5)\n multi = 1;\n if (k < 5)\n key1 += v * multi;\n else\n key2 += v * multi;\n multi *= 1000;\n ++k;\n }\n auto it = memo[idx].find({key1, key2});\n if (it != memo[idx].end())\n return it->second;\n for (int i = 0; i < grid[idx].size(); ++i)\n {\n if (chosen.count(grid[idx][i]) == 0)\n {\n int curr = grid[idx][i];\n chosen.emplace(grid[idx][i]);\n auto rest = maxScoreRecursive2(grid, idx + 1, chosen, memo);\n chosen.erase(grid[idx][i]);\n if (rest != -1)\n result = max(result, curr + rest);\n }\n }\n auto rest = maxScoreRecursive2(grid, idx + 1, chosen, memo);\n result = max(result, rest);\n memo[idx][{key1, key2}] = result;\n return result;\n }\n};",
"memory": "103348"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n set<int> chosen;\n unordered_map<int, vector<int>> numRowMap;\n for (int i = 0; i < grid.size(); ++i)\n {\n for (int j = 0; j < grid[i].size(); ++j)\n numRowMap[grid[i][j]].emplace_back(i);\n }\n vector<pair<int, vector<int>>> numRowVec;\n for (auto& p : numRowMap)\n {\n p.second.erase(unique(p.second.begin(), p.second.end()), p.second.end());\n numRowVec.emplace_back(p);\n }\n sort(numRowVec.begin(), numRowVec.end(), greater<pair<int, vector<int>>>());\n unordered_map<long, int> memo;\n return maxScoreRecursive(numRowVec, 0, chosen, grid.size(), memo);\n }\n\n int maxScoreRecursive(const vector<pair<int, vector<int>>>& numRowVec, int idx, set<int>& chosen, int num, unordered_map<long, int>& memo)\n {\n int result = 0;\n if (idx >= numRowVec.size())\n return 0;\n if (chosen.size() >= num)\n return 0;\n long key=0, multi = 10;\n for (const auto& v : chosen)\n key = key*multi + v;\n key = key*100 + idx;\n\n auto it = memo.find(key);\n if (it != memo.end())\n return it->second;\n int curr = numRowVec[idx].first;\n for (auto r : numRowVec[idx].second)\n {\n if (chosen.count(r) == 0)\n {\n chosen.emplace(r);\n auto rest = maxScoreRecursive(numRowVec, idx + 1, chosen, num, memo);\n chosen.erase(r);\n result = max(result, curr + rest);\n }\n }\n auto rest = maxScoreRecursive(numRowVec, idx + 1, chosen, num, memo);\n result = max(result, rest);\n memo[key] = result;\n return result;\n }\n\n int maxScoreRecursive2(const vector<vector<int>>& grid, int idx, set<int>& chosen, vector<map<pair<long, long>, int>>& memo) {\n int result = 0;\n if (idx >= grid.size())\n return 0;\n \n long key1 = 0, key2 = 0, multi = 1;\n int k = 0;\n for (const auto& v : chosen)\n {\n if (k == 5)\n multi = 1;\n if (k < 5)\n key1 += v * multi;\n else\n key2 += v * multi;\n multi *= 1000;\n ++k;\n }\n auto it = memo[idx].find({key1, key2});\n if (it != memo[idx].end())\n return it->second;\n for (int i = 0; i < grid[idx].size(); ++i)\n {\n if (chosen.count(grid[idx][i]) == 0)\n {\n int curr = grid[idx][i];\n chosen.emplace(grid[idx][i]);\n auto rest = maxScoreRecursive2(grid, idx + 1, chosen, memo);\n chosen.erase(grid[idx][i]);\n if (rest != -1)\n result = max(result, curr + rest);\n }\n }\n auto rest = maxScoreRecursive2(grid, idx + 1, chosen, memo);\n result = max(result, rest);\n memo[idx][{key1, key2}] = result;\n return result;\n }\n};",
"memory": "103348"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int solve(int n, int idx, bitset<10> &b, vector<pair<int,int>> &v, vector<unordered_map<bitset<10>, int>> &dp){\n while(idx<n && b[v[idx].second]){\n idx++;\n }\n if(idx == n) return 0;\n if(dp[idx].find(b)!=dp[idx].end()) return dp[idx][b];\n int ans = solve(n, idx+1, b, v,dp);\n int next = upper_bound(v.begin()+idx, v.end(), make_pair(v[idx].first,10)) - v.begin();\n b[v[idx].second]=1;\n ans = max(ans, solve(n, next, b, v,dp) + v[idx].first);\n b[v[idx].second]=0;\n return dp[idx][b] =ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size();\n vector<pair<int,int>> v;\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 bitset<10> b(0);\n vector<unordered_map<bitset<10>, int>> dp(n*m + 1);\n return solve(n*m, 0,b, v,dp);\n }\n};",
"memory": "107394"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\n #define long long ll\n int slove(int n, int i, vector<pair<int, vector<int>>>& v, int rowmask ,vector<vector<int>>&dp) {\n if (i == n)\n return 0;\n if(dp[i][rowmask]!=-1)\n return dp[i][rowmask];\n int take = 0, non_take = 0;\n non_take = slove(n, i + 1, v, rowmask,dp);\n vector<int> rows = v[i].second;\n int val = v[i].first;\n for (auto r : rows) {\n if (!((1 << r) & rowmask))\n take = max(take, val + slove(n, i + 1, v, rowmask | (1 << r),dp));\n }\n\n return dp[i][rowmask]= max(take, non_take);\n }\n\npublic:\n int maxScore(vector<vector<int>>& grid) {\n vector<pair<int, vector<int>>> v;\n map<int, vector<int>> mp;\n int r = grid.size();\n int c = grid[0].size();\n int maxel =0;\n for (int i = 0; i < r; i++) {\n for (int j = 0; j < c; j++) {\n mp[grid[i][j]].push_back(i);\n maxel = max(maxel,grid[i][j]);\n }\n }\n for (auto it : mp) {\n v.push_back({it.first, it.second});\n }\n int n = v.size();\n // for(auto it:v){\n // cout<<it.first<<\" ->\";\n // for(auto it1:it.second)\n // cout<<it1<<\" \";\n // cout<<endl;\n // }\n vector<vector<int>>dp(n,vector<int>(1<<r+1,-1));\n return slove(n, 0, v, 0,dp);\n }\n};",
"memory": "107394"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int solverec(int ele,int un,int &mx,unordered_map<int,vector<int>>& g,vector<vector<int>>&dp)\n {\n if(ele>mx)\n {\n return 0;\n }\n if(dp[ele][un]!=-1)\n {\n return dp[ele][un];\n }\n int ans=solverec(ele+1,un,mx,g,dp);\n for(auto &j:g[ele])\n {\n if(((1<<j) & un)==0)\n {\n ans=max(ans,ele+solverec(ele+1,(un | (1<<j)),mx,g,dp));\n }\n }\n return dp[ele][un]=ans;\n }\n int maxScore(vector<vector<int>>& grid) \n {\n int mx=0;\n unordered_map<int,vector<int>>g;\n for(int i=0;i<grid.size();i++)\n {\n for(int j=0;j<grid[i].size();j++)\n {\n mx=max(mx,grid[i][j]);\n g[grid[i][j]].push_back(i);\n }\n }\n vector<vector<int>>dp(mx+1,vector<int>(1300,-1));\n return solverec(0,0,mx,g,dp);\n }\n};",
"memory": "111440"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\n #define long long ll\n int slove(int &n, int i, vector<pair<int, vector<int>>>& v, int rowmask ,vector<vector<int>>&dp) {\n if (i == n)\n return 0;\n if(dp[i][rowmask]!=-1)\n return dp[i][rowmask];\n int take = 0, non_take = 0;\n non_take = slove(n, i + 1, v, rowmask,dp);\n vector<int> rows = v[i].second;\n int val = v[i].first;\n for (auto r : rows) {\n if (!((1 << r) & rowmask))\n take = max(take, val + slove(n, i + 1, v, rowmask | (1 << r),dp));\n }\n\n return dp[i][rowmask]= max(take, non_take);\n }\n\npublic:\n int maxScore(vector<vector<int>>& grid) {\n vector<pair<int, vector<int>>> v;\n unordered_map<int, vector<int>> mp;\n int r = grid.size();\n int c = grid[0].size();\n int maxel =0;\n for (int i = 0; i < r; i++) {\n for (int j = 0; j < c; j++) {\n mp[grid[i][j]].push_back(i);\n maxel = max(maxel,grid[i][j]);\n }\n }\n for (auto it : mp) {\n v.push_back({it.first, it.second});\n }\n int n = v.size();\n vector<vector<int>>dp(n,vector<int>(1<<r+1,-1));\n return slove(n, 0, v, 0,dp);\n }\n};",
"memory": "111440"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n int withOrWithouYou(int n, int ind, vector<pair<int, vector<int>>>& v,\n int rowmask, vector<vector<int>>& dp) {\n if (ind == n) {\n return 0;\n }\n\n if (dp[ind][rowmask] != -1) {\n return dp[ind][rowmask];\n }\n\n int with = 0, without = 0;\n without = withOrWithouYou(n, ind + 1, v, rowmask, dp);\n\n vector<int> rows = v[ind].second;\n int val = v[ind].first;\n\n for (auto r : rows) {\n if (!((1 << r) & rowmask)) {\n with = max(with, val + withOrWithouYou(n, ind + 1, v,\n rowmask | (1 << r), dp));\n }\n }\n\n dp[ind][rowmask] = max(with, without);\n return dp[ind][rowmask];\n }\n\npublic:\n int maxScore(vector<vector<int>>& grid) {\n vector<pair<int, vector<int>>> v;\n unordered_map<int, vector<int>> mp;\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 for (auto it : mp) {\n v.push_back({it.first, it.second});\n }\n int n = v.size();\n vector<vector<int>> dp(n, vector<int>(1 << grid.size() + 1, -1));\n return withOrWithouYou(n, 0, v, 0, dp);\n }\n};",
"memory": "115486"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\n #define long long ll\n int slove(int &n, int i, vector<pair<int, vector<int>>>& v, int rowmask ,vector<vector<int>>&dp) {\n if (i == n)\n return 0;\n if(dp[i][rowmask]!=-1)\n return dp[i][rowmask];\n int take = 0, non_take = 0;\n non_take = slove(n, i + 1, v, rowmask,dp);\n vector<int> rows = v[i].second;\n int val = v[i].first;\n for (auto r : rows) {\n if (!((1 << r) & rowmask))\n take = max(take, val + slove(n, i + 1, v, rowmask | (1 << r),dp));\n }\n\n return dp[i][rowmask]= max(take, non_take);\n }\n\npublic:\n int maxScore(vector<vector<int>>& grid) {\n vector<pair<int, vector<int>>> v;\n unordered_map<int, vector<int>> mp;\n int r = grid.size();\n int c = grid[0].size();\n int maxel =0;\n for (int i = 0; i < r; i++) {\n for (int j = 0; j < c; j++) {\n mp[grid[i][j]].push_back(i);\n maxel = max(maxel,grid[i][j]);\n }\n }\n for (auto it : mp) {\n v.push_back({it.first, it.second});\n }\n int n = v.size();\n vector<vector<int>>dp(n,vector<int>(1<<r+1,-1));\n return slove(n, 0, v, 0,dp);\n }\n};",
"memory": "115486"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dp[101][1025];\n int check(vector<vector<int>>&ans,int idx,int vis)\n {\n if(idx==ans.size())return 0;\n if(dp[idx][vis]!=-1)return dp[idx][vis];\n int a = ans[idx][0];\n int b = ans[idx][1];\n int sum = check(ans,idx+1,vis);\n if(((vis>>b) & 1) == 0)\n {\n vector<int>v = {a+1,0};\n int pos = lower_bound(ans.begin(),ans.end(),v)-ans.begin();\n sum = max(sum,a+check(ans,pos,vis|(1<<b)));\n }\n return dp[idx][vis] = sum;\n }\n int maxScore(vector<vector<int>>& grid) {\n vector<vector<int>>ans;\n \n for(int i=0;i<grid.size();i++)\n {\n for(int j=0;j<grid[0].size();j++)\n {\n ans.push_back({grid[i][j],i});\n }\n }\n sort(ans.begin(),ans.end());\n memset(dp,-1,sizeof(dp));\n return check(ans,0,0);\n }\n};",
"memory": "119533"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int solve(int num,int mask,vector<vector<int>>&pos,vector<vector<int>>&dp)\n {\n if(num==0)\n {\n return 0;\n }\n if(dp[num][mask]!=-1)\n {\n return dp[num][mask];\n }\n vector<int>curr=pos[num];\n int ans=solve(num-1,mask,pos,dp);\n for(int i=0; i<curr.size(); i++)\n {\n int index=curr[i];\n if(mask&(1<<index))\n {\n continue;\n }\n ans=max(ans,num+solve(num-1,mask|(1<<index),pos,dp));\n }\n return dp[num][mask]=ans;\n }\n int maxScore(vector<vector<int>>& grid) \n {\n int rows=grid.size();\n int cols=grid[0].size();\n vector<vector<int>>dp(101,vector<int>(1<<rows,-1));\n vector<vector<int>>pos(101,vector<int>());\n int maxi=INT_MIN;\n for(int i=0; i<rows; i++)\n {\n for(int j=0; j<cols; j++)\n {\n pos[grid[i][j]].push_back(i);\n maxi=max(maxi,grid[i][j]);\n }\n }\n return solve(100,0,pos,dp);\n }\n};",
"memory": "119533"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int solve(int num,int mask,vector<vector<int>>&pos,vector<vector<int>>&dp)\n {\n if(num==0)\n {\n return 0;\n }\n if(dp[num][mask]!=-1)\n {\n return dp[num][mask];\n }\n vector<int>curr=pos[num];\n int ans=solve(num-1,mask,pos,dp);\n for(int i=0; i<curr.size(); i++)\n {\n int index=curr[i];\n if(mask&(1<<index))\n {\n continue;\n }\n ans=max(ans,num+solve(num-1,mask|(1<<index),pos,dp));\n }\n return dp[num][mask]=ans;\n }\n int maxScore(vector<vector<int>>& grid) \n {\n int rows=grid.size();\n int cols=grid[0].size();\n vector<vector<int>>dp(101,vector<int>(1<<rows,-1));\n vector<vector<int>>pos(101,vector<int>());\n int maxi=INT_MIN;\n for(int i=0; i<rows; i++)\n {\n for(int j=0; j<cols; j++)\n {\n pos[grid[i][j]].push_back(i);\n maxi=max(maxi,grid[i][j]);\n }\n }\n return solve(maxi,0,pos,dp);\n }\n};",
"memory": "123579"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n vector<vector<int>> dp;\n vector<vector<int>> nums;\n\n int maxScore(vector<vector<int>>& grid) {\n\n int m=grid.size();int n=grid[0].size();\n\n dp.resize(101,vector<int>((1<<m),-1));\n nums.resize(101);\n\n for(int i=0;i<m;i++)\n {\n for(int j=0;j<n;j++)\n {\n nums[grid[i][j]].push_back(i);\n }\n }\n\n return solve(100,0);\n \n }\n\n int solve(int val,int hash)\n {\n\n if(val==0)\n {\n return 0;\n }\n\n if(dp[val][hash]!=-1)\n {\n return dp[val][hash];\n }\n\n int ans=0;vector<int> rows=nums[val];\n\n ans=max(ans,solve(val-1,hash));\n\n for(auto r:rows)\n {\n if(((1<<r) & hash)==0)\n {\n ans=max(ans,val+solve(val-1,((1<<r) | hash)));\n }\n }\n\n return dp[val][hash]=ans;\n\n }\n\n};",
"memory": "123579"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int traverse(vector<vector<int>>& values, int index, int row_mask, int n, vector<unordered_map<int, int>>& dp) {\n if (index >= values.size() || __builtin_popcount(row_mask) == n) {\n // reached end of list or all rows are taken.\n return 0;\n }\n\n if (dp[index].contains(row_mask)) {\n return dp[index][row_mask];\n }\n\n int best = 0;\n\n if ((row_mask & (1 << values[index][1])) == 0) {\n int next = index + 1;\n while (next < values.size() && values[index][0] == values[next][0]) {\n next++;\n }\n\n best = max(best, values[index][0] + traverse(values, next, row_mask | (1 << values[index][1]), n, dp));\n }\n best = max(best, traverse(values, index + 1, row_mask, n, dp));\n\n return dp[index][row_mask] = best;\n }\n\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 : grid[i]) {\n values.push_back({j, i});\n }\n }\n\n sort(values.begin(), values.end());\n\n vector<unordered_map<int, int>> dp(values.size());\n\n return traverse(values, 0, 0, grid.size(), dp);\n }\n};",
"memory": "127625"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n unordered_map<int, vector<int>> vals1;\n vector<pair<int, vector<int>>> vals;\n for (int i = 0; i < grid.size(); i++) {\n for (int j = 0; j < grid[0].size(); j++) {\n vals1[grid[i][j]].push_back(i);\n }\n }\n for (auto pair : vals1) {\n vals.push_back(pair);\n }\n vector<vector<int>> dp((1<<11), vector<int>(vals.size(), -1)); // mask and sum\n return helper(0,0,vals, dp);\n }\n int helper(int i, int mask, vector<pair<int, vector<int>>> & vals, vector<vector<int>> & dp) {\n if (i>= vals.size()) {\n return 0;\n }\n if (dp[mask][i]!=-1) {\n return dp[mask][i];\n }\n int curr = helper(i+1, mask, vals, dp);\n for (int r : vals[i].second) {\n if (!(mask & (1<<r))) { // row not used in mask\n curr = max(curr, helper(i+1, (mask | (1<<r)), vals, dp)+vals[i].first);\n }\n }\n dp[mask][i] = curr;\n return curr;\n }\n};",
"memory": "127625"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n unordered_map<int, unordered_map<string, int>> dp;\n int solve(vector<vector<int>>& nums, int index, string str) {\n if(index >= nums.size()) {\n return 0;\n }\n if(dp.find(index) != dp.end() && dp[index].find(str) != dp[index].end()) {\n return dp[index][str];\n }\n int nextIndex = index;\n while(nextIndex < nums.size() && nums[nextIndex][0] == nums[index][0]) {\n nextIndex++;\n }\n int answer = 0;\n int option1 = solve(nums, nextIndex, str);\n answer = max(answer, option1);\n for(int i = index; i < nextIndex; i++) {\n if(str[nums[i][1]] == '0') {\n str[nums[i][1]] = '1';\n int option2 = nums[i][0] + solve(nums, nextIndex, str);\n answer = max(answer, option2);\n str[nums[i][1]] = '0';\n }\n }\n return dp[index][str] = answer;\n }\n int maxScore(vector<vector<int>>& grid) {\n string str;\n for(int i = 0; i < 10; i++) {\n str += '0';\n }\n vector<vector<int>> nums;\n for(int i = 0; i < grid.size(); i++) {\n for(int j = 0; j < grid[i].size(); j++) {\n nums.push_back({grid[i][j], i});\n }\n }\n sort(nums.begin(), nums.end());\n return solve(nums, 0, str);\n }\n};",
"memory": "131671"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dp1(vector<vector<int>>& grid, vector<vector<int>>& memo, int i, int sum, int n, set<int>& s) {\n int m = grid[0].size();\n if (i >= n) {\n return 0;\n }\n if (memo[i][sum] != -1) {\n return memo[i][sum];\n }\n \n int ans = 0;\n for (int k = 0; k < m; k++) {\n if (s.find(grid[i][k]) != s.end()) {\n ans = max(ans, dp1(grid, memo, i + 1, sum, n, s));\n } else {\n s.insert(grid[i][k]);\n ans = max(ans, grid[i][k] + dp1(grid, memo, i + 1, sum + grid[i][k], n, s));\n s.erase(grid[i][k]);\n }\n }\n return memo[i][sum] = ans;\n }\n\n int dp2(vector<vector<int>>& grid, vector<vector<int>>& memo, int i, int sum, int n, set<int>& s) {\n int m = grid[0].size();\n if (i < 0) {\n return 0;\n }\n if (memo[i][sum] != -1) {\n return memo[i][sum];\n }\n \n int ans = 0;\n for (int k = 0; k < m; k++) {\n if (s.find(grid[i][k]) != s.end()) {\n ans = max(ans, dp2(grid, memo, i - 1, sum, n, s));\n } else {\n s.insert(grid[i][k]);\n ans = max(ans, grid[i][k] + dp2(grid, memo, i - 1, sum + grid[i][k], n, s));\n s.erase(grid[i][k]);\n }\n }\n return memo[i][sum] = ans;\n }\n\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n vector<vector<int>> memo1(n + 1, vector<int>(1001, -1)), memo2(n + 1, vector<int>(1001, -1));\n set<int> s1, s2;\n \n return max(dp1(grid, memo1, 0, 0, n, s1), dp2(grid, memo2, n - 1, 0, n, s2));\n }\n};",
"memory": "131671"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": " \nclass 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 int notTake=0;int take=0;\n if ((1 << v[i].second) & mask) {\n notTake = 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 take = v[i].first + solve(n, j, v, mask | (1 << v[i].second), mp);\n notTake = solve(n, i + 1, v, mask, mp);\n \n }\n return mp[{i, mask}]=max(take, notTake);;\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": "135718"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int max_score ( vector<vector<int>> & arr, int i, string &mp, vector<unordered_map<int, int> > &checker, unordered_map<int, unordered_map<string, int>> &dp){\n int n = arr.size();\n if(i == n)\n return 0;\n if(dp[i][mp])\n return dp[i][mp];\n int res = 0;\n bool flag = true;\n for(auto x : arr[i]){\n bool flag2 = 0;\n for (int j =0 ; j < arr.size(); j ++){\n if(i != j){\n flag2 += checker[j][x];\n }\n }\n if(flag2 == 0){\n mp[x] = '1';\n // cout << i << \" \" << x << endl;\n res = max(res, max_score(arr, i + 1, mp, checker, dp) + x);\n mp[x] = 'x';\n break;\n }\n else if(mp[x] == 'x'){\n mp[x] = '1';\n res = max(res, max_score(arr, i + 1, mp, checker, dp) + x);\n flag = false;\n mp[x] = 'x';\n }\n }\n \n if(flag)\n res = max(res, max_score(arr, i + 1, mp, checker, dp));\n return dp[i][mp] = res;;\n \n }\n int maxScore(vector<vector<int>>& arr) {\n string mp(101, 'x');\n vector<unordered_map<int, int> > checker(arr.size());\n \n for(int i = 0; i < arr.size(); i++){\n sort(arr[i].begin(), arr[i].end(), greater<>() );\n \n for(int x : arr[i])\n checker[i][x]++;\n }\n \n unordered_map<int, unordered_map<string, int>> dp;\n return max_score(arr, 0, mp, checker, dp);\n }\n};",
"memory": "135718"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\n vector<vector<int>> wr;//which row\n map<pair<int,int>,int> dp;\n int recursion (int num,int &st){\n if(num==0)\n return 0;\n if(dp.find({num,st})!=dp.end()) return dp[{num,st}];\n int ans = 0;\n for(auto &r:wr[num]){\n if(st&(1<<r)){\n st^=(1<<r);\n int p = num + recursion(num-1,st);//potential\n ans = max(ans,p);\n st^=(1<<r);\n }\n }\n return dp[{num,st}] = max(ans,recursion(num-1,st));\n }\npublic:\n int maxScore(vector<vector<int>>& grid) {\n wr.resize(101);\n int st = (1<<grid.size())-1;\n for(int i=0;i<grid.size();i++)\n for(int j=0;j<grid[i].size();j++)\n wr[grid[i][j]].push_back(i);\n \n return recursion (100,st);\n }\n};",
"memory": "151903"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\n vector<vector<int>> wr;//which row\n map<pair<int,int>,int> dp;\n int recursion (int num,int &st){\n if(num==0)\n return 0;\n if(dp.find({num,st})!=dp.end()) return dp[{num,st}];\n int ans = 0;\n for(auto &r:wr[num]){\n if(st&(1<<r)){\n st^=(1<<r);\n int p = num + recursion(num-1,st);//potential\n ans = max(ans,p);\n st^=(1<<r);\n }\n }\n return dp[{num,st}] = max(ans,recursion(num-1,st));\n }\npublic:\n int maxScore(vector<vector<int>>& grid) {\n wr.resize(101);\n int st = (1<<grid.size())-1;\n for(int i=0;i<grid.size();i++)\n for(int j=0;j<grid[i].size();j++)\n wr[grid[i][j]].push_back(i);\n \n return recursion (100,st);\n }\n};",
"memory": "151903"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\n int solve(int idx, int mask, vector<pair<int, int>>& v, vector<vector<int>>& dp) {\n if (idx == v.size()) {\n return 0;\n } else if (dp[mask][idx] != -1) {\n return dp[mask][idx];\n }\n int res;\n int num = v[idx].first;\n int row = v[idx].second;\n if (mask & (1 << row)) {\n res = solve(idx + 1, mask, v, dp);\n } else {\n int next = idx;\n while (next < v.size() && v[next].first == num) {\n next++;\n }\n int r1 = num + solve(next, mask | (1 << row), v, dp);\n int r2 = solve(idx + 1, mask, v, dp);\n res = max(r1, r2);\n }\n return dp[mask][idx] = res;\n }\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n vector<pair<int, int>> v;\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 sort(v.rbegin(), v.rend());\n vector<vector<int>> dp(2000, vector<int>(v.size() + 1, -1));\n return solve(0, 0, v, dp);\n }\n};",
"memory": "155949"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<map<int,int>> dp;\n int solve(vector<vector<int>> &arr, int n, int mask){\n if(n==0)\n return 0;\n if(dp[n].find(mask)!=dp[n].end()){\n return dp[n][mask];\n }\n int ans = 0;\n for(int index:arr[n]){\n if((mask & (1<<index)) == 0){\n ans = max(ans, n + solve(arr, n-1, mask | (1<<index)));\n }\n }\n ans = max(ans, solve(arr, n-1, mask));\n return dp[n][mask] =ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n dp.resize(101);\n vector<vector<int>> arr(101);\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 solve(arr, 100, 0); \n }\n};",
"memory": "155949"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n int lar = -1;\n map<pair<int,int >, int > dp;\n\n int calScore(int ind,vector< vector<int> > &g, int row){\n if(ind > lar)\n return 0;\n \n if(dp.find({ind,row}) != dp.end())\n return dp[{ind,row}];\n\n int score = calScore(ind+1,g,row);\n\n for(auto r : g[ind]){\n if(((row>>r) &1) == 0)\n {\n int row_ = (row | (1<<r));\n score = max(ind + calScore(ind+1,g,row_),score);\n }\n }\n return dp[{ind,row}] = score;\n }\n\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n lar = -1;\n\n map<int, set<int> >mp;\n for(int i = 0;i<n;i++)\n {\n for(int j = 0;j<m;j++){\n mp[grid[i][j]].insert(i);\n lar = max(lar,grid[i][j]);\n }\n }\n vector<vector<int>> g(lar+1);\n\n for(auto v: mp){\n for(auto el : v.second)\n {\n g[v.first].push_back(el);\n }\n }\n int row = 0;\n int maxscore = calScore(1,g,row);\n\n return maxscore;\n }\n};",
"memory": "159995"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int recur(int ind,int vis,vector<vector<int>>& v,vector<vector<int>>& dp)\n {\n if(ind==v.size())\n return 0;\n if(dp[ind][vis]!=-1)\n return dp[ind][vis];\n int ans=recur(ind+1,vis,v,dp);\n if((vis&(1<<v[ind][1])))\n return dp[ind][vis]=ans;\n int lb=lower_bound(v.begin()+ind,v.end(),vector<int>{v[ind][0]+1,0})-v.begin();\n ans=max(ans,v[ind][0]+recur(lb,(vis|(1<<v[ind][1])),v,dp));\n return dp[ind][vis]=ans;\n }\n int maxScore(vector<vector<int>>& grid) {\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});\n }\n }\n vector<vector<int>> dp(v.size(),vector<int>((1<<10),-1));\n sort(v.begin(),v.end());\n return recur(0,0,v,dp);\n }\n};",
"memory": "159995"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "struct Cell {\n int value;\n int row;\n int col;\n\n Cell(int value, int row, int col) : value(value), row(row), col(col) {};\n\n bool operator>(const Cell& rhs) const { \n return value > rhs.value;\n } \n};\n\nclass Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n\n vector<Cell> cells;\n\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n cells.emplace_back(grid[i][j], i, j);\n }\n }\n\n sort(cells.begin(), cells.end(), greater<Cell>());\n\n int bitmask = 0;\n return maxScore(cells, 0, bitmask);\n }\n\nprivate:\n unordered_map<int, int> memo;\n\n int maxScore(const vector<Cell>& cells, int idx, int bitmask) {\n if (idx >= cells.size()) {\n return 0;\n }\n\n int key = ((idx << 10) | bitmask);\n if (memo.contains(key)) {\n return memo[key];\n }\n\n // Not select\n int score1 = maxScore(cells, idx + 1, bitmask);\n\n // Select\n int score2 = 0;\n int row = cells[idx].row;\n int col = cells[idx].col;\n if ((bitmask & (1 << row)) == 0) {\n int nextIdx = idx + 1;\n\n while (nextIdx < cells.size() && cells[idx].value == cells[nextIdx].value) {\n nextIdx++;\n }\n\n score2 = cells[idx].value + maxScore(cells, nextIdx, (bitmask | (1 << row)));\n }\n\n return memo[key] = max(score1, score2);\n }\n};",
"memory": "164041"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "struct Cell {\n int value;\n int row;\n int col;\n\n Cell(int value, int row, int col) : value(value), row(row), col(col) {};\n\n bool operator>(const Cell& rhs) const { \n return value > rhs.value;\n } \n};\n\nclass Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n\n vector<Cell> cells;\n\n for (int i = 0; i < m; i++) {\n for (int j = 0; j < n; j++) {\n cells.emplace_back(grid[i][j], i, j);\n }\n }\n\n sort(cells.begin(), cells.end(), greater<Cell>());\n\n int bitmask = 0;\n return maxScore(cells, 0, bitmask);\n }\n\nprivate:\n unordered_map<int, int> memo;\n\n int maxScore(const vector<Cell>& cells, int idx, int bitmask) {\n if (idx >= cells.size()) {\n return 0;\n }\n\n int key = ((idx << 10) | bitmask);\n if (memo.contains(key)) {\n return memo[key];\n }\n\n // Not select\n int score1 = maxScore(cells, idx + 1, bitmask);\n\n // Select\n int score2 = 0;\n int row = cells[idx].row;\n int col = cells[idx].col;\n if ((bitmask & (1 << row)) == 0) {\n int nextIdx = idx + 1;\n\n while (nextIdx < cells.size() && cells[idx].value == cells[nextIdx].value) {\n nextIdx++;\n }\n\n score2 = cells[idx].value + maxScore(cells, nextIdx, (bitmask | (1 << row)));\n }\n\n return memo[key] = max(score1, score2);\n }\n};",
"memory": "164041"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size();\n array<vector<int>, 101> a;\n for (int i = 0; i < n; i++) {\n auto &v = grid[i];\n sort(v.begin(), v.end());\n for (int j = 0; j < m; j++) {\n if (j == 0 || v[j] != v[j - 1])\n a[grid[i][j]].push_back(i);\n }\n }\n unordered_map<int, int> ma;\n function<int(int, int)> f\n = [&](int p, int mask) {\n if (p == 0)\n return 0;\n int key = (p << 10) + mask;\n auto it = ma.find(key);\n if (it != ma.end())\n return it->second;\n \n int res = f(p - 1, mask);\n for (int x : a[p]) {\n if (mask & (1 << x))\n continue;\n res = max(res, p + f(p - 1, mask | (1 << x)));\n }\n return ma[key] = res;\n };\n return f(100, 0);\n }\n};",
"memory": "168088"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size(), m = grid[0].size();\n array<set<int>, 101> a;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < m; j++) {\n a[grid[i][j]].insert(i);\n }\n }\n unordered_map<int, int> ma;\n function<int(int, int)> f\n = [&](int p, int mask) {\n if (p == 0)\n return 0;\n int key = (p << 10) + mask;\n auto it = ma.find(key);\n if (it != ma.end())\n return it->second;\n \n int res = f(p - 1, mask);\n for (int x : a[p]) {\n if (mask & (1 << x))\n continue;\n res = max(res, p + f(p - 1, mask | (1 << x)));\n }\n return ma[key] = res;\n };\n return f(100, 0);\n }\n};",
"memory": "168088"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\n map<pair<int, int>, int> dp;\n\n int findMax(int idx, int rowMask, vector<vector<int>>& values) {\n int n = values.size();\n if (idx == n) return 0;\n\n if (dp.find({idx, rowMask}) != dp.end()) return dp[{idx, rowMask}];\n\n int way1 = findMax(idx + 1, rowMask, values);\n\n int way2 = 0;\n int currRow = values[idx][1];\n if ((rowMask & (1 << currRow)) == 0) {\n way2 = values[idx][0];\n int nextIdx = idx + 1;\n while (nextIdx < n && values[nextIdx][0] == way2) {\n nextIdx++;\n }\n way2 += findMax(nextIdx, rowMask | (1 << currRow), values);\n }\n\n return dp[{idx, rowMask}] = max(way1, way2);\n }\n\npublic:\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n\n vector<vector<int>> values;\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());\n\n return findMax(0, 0, values);\n }\n};\n",
"memory": "172134"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dfs(int idx , vector<pair<int,int>> &v , int mask,map<pair<int,int>,int>&dp)\n {\n if(!mask)\n {\n return 0;\n }\n if(idx<0)\n {\n return 0;\n }\n if(dp.find({idx,mask})!=dp.end())\n {\n return dp[{idx,mask}];\n }\n int row = v[idx].second;\n int ans = dfs(idx-1,v,mask,dp);\n if(mask&(1<<row)){\n int j = idx;\n while(j>=0 && v[idx].first==v[j].first)\n {\n j--;\n }\n ans = max(ans,v[idx].first+dfs(j,v,mask^(1<<row),dp));\n }\n return dp[{idx,mask}]=ans;\n }\n int maxScore(vector<vector<int>>& grid) {\n int n = grid.size(),m = grid[0].size();\n int mask = (1<<n)-1;\n vector<pair<int,int>> v;\n for(int i =0;i<n;i++)\n {\n for(int j =0;j<m;j++)\n {\n v.push_back({grid[i][j],i});\n }\n }\n sort(v.begin(),v.end());\n map<pair<int,int>,int> dp;\n return dfs(m*n-1,v,mask,dp);\n }\n};",
"memory": "172134"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\n\tmap<int, map<int, int>> dp;\n\tint f(map<int, vector<int>> &mp, int mask, int num, int n, int m, vector<vector<int>> &a) {\n\t\tif (num <= 0) return 0;\n\n\t\tif (dp.count(num) && dp[num].count(mask)) return dp[num][mask];\n\n\t\tint ans = f(mp, mask, num - 1, n, m, a);\n\t\tfor (auto &row : mp[num])\n\t\t\tif ((mask & (1LL << row)) == 0) ans = max(ans, num + f(mp, mask | (1LL << row), num - 1, n, m, a));\n\t\treturn dp[num][mask] = ans;\n\t}\npublic:\n\tint maxScore(vector<vector<int>>& a) {\n\t\tint n = a.size(), m = a[0].size();\n\t\tmap<int, vector<int>> mp;\n\t\tint maxi = 0;\n\t\tfor (int i = 0; i < n; ++i) {\n\t\t\tfor (auto &e : a[i]) mp[e].emplace_back(i), maxi = max(maxi, e);\n\t\t}\n\t\treturn f(mp, 0, maxi, n, m, a);\n\t}\n};\n\n\n/*\nॐ নমঃ শিৱায়\n */",
"memory": "176180"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int dp[1<<10][105];\n int f(int mask,int ptr,map<int,set<int>>&mp,vector<int>&v){\n \n \n if(ptr==v.size())return 0;\n if(dp[mask][ptr]!=-1)return dp[mask][ptr];\n int ans=0;\n bool flag=false;\n int val=v[ptr];\n \n \n auto it=mp.find(val);\n set<int>st;\n st=it->second;\n \n for(auto itt:st){\n if(mask&(1<<itt)){\n \n }else{\n \n ans=max(ans,val+f(mask|(1<<itt),ptr+1,mp,v));\n }\n }\n ans=max(ans,f(mask,ptr+1,mp,v));\n \n return dp[mask][ptr]= ans;\n \n \n \n \n \n \n }\n int maxScore(vector<vector<int>>& grid) {\n \n int n=grid.size();\n int m=grid[0].size();\n map<int,set<int>>mp;\n set<int>stt;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n mp[grid[i][j]].insert(i);\n stt.insert(grid[i][j]);\n }\n }\n vector<int>v;\n for(auto it:stt)v.push_back(it);\n reverse(v.begin(),v.end());\n memset(dp,-1,sizeof(dp));\n return f(0,0,mp,v);\n \n }\n};",
"memory": "176180"
} |
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> </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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= grid.length, grid[i].length <= 10</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n class Compare {\n public:\n bool operator () (const vector<int> & a, const vector<int> & b) const {\n int l = a.size();\n for (int i = 0; i < l; i++)\n if (a[i] != b[i])\n return a[i] < b[i];\n return false;\n }\n };\n\n void dfs(int i, int m, int n, const vector<vector<int>>& grid, unordered_set<int>& selected, const vector<int>& suffixSum, int sum, int &ans, int & count) {\n count++;\n if (count >= 10000000)\n return;\n if (i == m) {\n if (sum > ans)\n ans = sum;\n return;\n }\n if (sum + suffixSum[i] <= ans)\n return;\n for (int j = 0; j < min(m - i + 4, n); j++)\n if (selected.find(grid[i][j]) == selected.end()) {\n selected.insert(grid[i][j]);\n dfs(i + 1, m, n, grid, selected, suffixSum, sum + grid[i][j], ans, count);\n selected.erase(grid[i][j]);\n }\n if (m - i < 4)\n dfs(i + 1, m, n, grid, selected, suffixSum, sum, ans, count);\n }\n int maxScore(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n for (int i = 0; i < m; i++)\n sort(grid[i].begin(), grid[i].end(), greater<int>());\n Compare cmp;\n sort(grid.begin(), grid.end(), cmp);\n unordered_set<int> selected;\n vector<int> suffixSum(m);\n suffixSum[m - 1] = grid[m - 1][0];\n for (int i = m - 2; i >= 0; i--)\n suffixSum[i] = suffixSum[i + 1] + grid[i][0];\n int ans = 0;\n int count = 0;\n dfs(0, m, n, grid, selected, suffixSum, 0, ans, count);\n return ans;\n }\n};",
"memory": "180226"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.