id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string coordinate1, string coordinate2) {\n int first = coordinate1[0]-96;\n int second = coordinate2[0]-96;\n int first_row = int(coordinate1[1]);\n int second_row = int(coordinate2[1]);\n printf(\"%d\",first);\n if(first%2 && second%2 || first%2==0 && second%2==0){\n if(first_row%2 && second_row%2 || first_row%2==0 && second_row%2==0) return true;\n else return false;\n }\n else{\n if(first_row%2 && second_row%2 || first_row%2==0 && second_row%2==0) return false;\n else return true;\n }\n return false;\n }\n};", "memory": "8000" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string coordinate1, string coordinate2) {\n int first = coordinate1[0]-96;\n int second = coordinate2[0]-96;\n int first_row = int(coordinate1[1]);\n int second_row = int(coordinate2[1]);\n printf(\"%d\",first);\n if(first%2 && second%2 || first%2==0 && second%2==0){\n if(first_row%2 && second_row%2 || first_row%2==0 && second_row%2==0) return true;\n else return false;\n }\n else{\n if(first_row%2 && second_row%2 || first_row%2==0 && second_row%2==0) return false;\n else return true;\n }\n return false;\n }\n};", "memory": "8100" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string coordinate1, string coordinate2) {\n auto getSum = [](std::string coordinate) {\n int column = coordinate[0] - 'a' + 1;\n int row = coordinate[1] - '0';\n return column + row;\n };\n \n return getSum(coordinate1) % 2 == getSum(coordinate2) % 2;\n\n }\n};\n\n", "memory": "8100" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string coordinate1, string coordinate2) {\n string f1 = \"\", f2 = \"\";\n if(((coordinate1[0]-'a')+1)%2==0 && (coordinate1[1]-'0')%2==0 || ((coordinate1[0]-'a')+1)%2==1 && (coordinate1[1]-'0')%2==1){\n f1 = \"black\";\n }\n else {\n f1 = \"white\";\n }\n if(((coordinate2[0]-'a')+1)%2==0 && (coordinate2[1]-'0')%2==0 || ((coordinate2[0]-'a')+1)%2==1 && (coordinate2[1]-'0')%2==1){\n f2 = \"black\";\n }\n else {\n f2 = \"white\";\n }\n if(f1 == f2) return true;\n return false;\n }\n};", "memory": "8200" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n char colorofsquare(string s){\n int col=s[0]-'0';\n int row=8-(s[1]-'0');\n\n char ans='w';\n\n if(col%2==0){\n if(row%2==0){\n ans='w';\n return ans;\n }\n else{\n return 'b';\n }\n }\n else{\n if(row%2==0){\n ans='b';\n return ans;\n }\n else{\n ans='w';\n return ans;\n }\n }\n return ans;\n }\n\n bool checkTwoChessboards(string c1, string c2) {\n c1[0]=(c1[0]-'a')+'0';\n c2[0]=(c2[0]-'a')+'0';\n\n return colorofsquare(c1)==colorofsquare(c2);\n }\n};", "memory": "8200" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n char colorofsquare(string s){\n int col=s[0]-'0';\n int row=8-(s[1]-'0');\n\n char ans='w';\n\n if(col%2==0){\n if(row%2==0){\n ans='w';\n return ans;\n }\n else{\n return 'b';\n }\n }\n else{\n if(row%2==0){\n ans='b';\n return ans;\n }\n else{\n ans='w';\n return ans;\n }\n }\n return ans;\n }\n\n bool checkTwoChessboards(string c1, string c2) {\n c1[0]=(c1[0]-'a')+'0';\n c2[0]=(c2[0]-'a')+'0';\n\n return colorofsquare(c1)==colorofsquare(c2);\n }\n};", "memory": "8300" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n string find_color(string x)\n {\n string ans = \"\";\n if((x[0]-'a')%2==0)\n {\n if(x[1]%2==0)\n ans = \"white\";\n else\n ans = \"black\";\n }\n else\n {\n if(x[1]%2==0)\n ans = \"black\";\n else\n ans = \"white\"; \n }\n return ans;\n }\n \n \n bool checkTwoChessboards(string coordinate1, string coordinate2) {\n string s1 = find_color(coordinate1);\n string s2 = find_color(coordinate2);\n return s1==s2;\n }\n};", "memory": "8400" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string coordinate1, string coordinate2) {\n vector<vector<int>> chess(8, vector<int>(8));\n for(int i=0;i<8;i++)\n {\n for(int j=0;j<8;j++)\n {\n if(i%2==0)\n {\n if(j%2==0)\n {\n chess[i][j]= 1; // black\n }\n else\n {\n chess[i][j]= 0; // white\n }\n }\n else\n {\n if(j%2==0)\n {\n chess[i][j]= 0;\n }\n else\n {\n chess[i][j]= 1;\n }\n }\n }\n }\n\n int val1= (coordinate1[1]-'0') - 1;\n int val2= coordinate1[0]- 'a';\n int tal1= (coordinate2[1] - '0') - 1;\n int tal2= coordinate2[0]- 'a';\n int ans1;\n int ans2;\n for(int i=0;i<8;i++)\n {\n for(int j=0;j<8;j++)\n {\n if(val1==i && val2==j)\n {\n ans1= chess[i][j];\n }\n if(tal1==i && tal2==j)\n {\n ans2= chess[i][j];\n }\n }\n }\n if(ans1== ans2)\n {\n return true;\n }\n return false;\n }\n};", "memory": "8500" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string coordinate1, string coordinate2) \n {\n set<char>black;\n set<char>white;\n int c=0;\n for(char ch='a';ch<='h';ch++)\n {\n if(c%2==0)\n {\n black.insert(ch);\n }\n else\n {\n white.insert(ch);\n }\n c++;\n }\n char ch1=coordinate1[0];\n char ch2=coordinate2[0];\n int ch3=coordinate1[1]-'0';\n int ch4=coordinate2[1]-'0';\n int z=abs(ch3-ch4);\n if(black.find(ch1)!=black.end() && black.find(ch2)!=black.end())\n {\n if(z%2==0)\n {\n return true;\n }\n else\n {\n return false;\n }\n }\n else if(white.find(ch1)!=white.end() && white.find(ch2)!=white.end())\n {\n if(z%2==0)\n {\n return true;\n }\n else\n {\n return false;\n }\n }\n else\n {\n if(z%2==0)\n {\n return false;\n }\n else\n {\n return true;\n }\n }\n }\n};", "memory": "8600" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string c1, string c2) {\n unordered_set<char> aceg = {'a', 'c', 'e', 'g'};\n int c1color = 0;\n int c2color = 0;\n if(aceg.find(c1[0]) != aceg.end()){\n if(c1[1]%2 != 0){\n c1color = 0;\n }\n else {\n c1color = 1;\n }\n } else {\n if(c1[1]%2 != 0){\n c1color = 1;\n }\n else {\n c1color = 0;\n }\n }\n if(aceg.find(c2[0]) != aceg.end()){\n if(c2[1]%2 != 0){\n c2color = 0;\n }\n else {\n c2color = 1;\n }\n } else {\n if(c2[1]%2 == 0){\n c2color = 0;\n }\n else {\n c2color = 1;\n }\n }\n return c1color == c2color;\n }\n};", "memory": "8600" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n std::vector<int> result;\n const int n = queries.size();\n result.reserve(n);\n int h[k];\n for (int i = 0; i<k && i<n; ++i) {\n int v = abs(queries[i][0]) + abs(queries[i][1]);\n h[i] = v;\n }\n std::make_heap(h, h+k);\n int step = 0;\n for (;step<k-1 && step<n; ++step) {\n result.push_back(-1);\n }\n if (step < n) {\n result.push_back(h[0]);\n ++ step;\n }\n for (;step<n; ++step) {\n int v = abs(queries[step][0]) + abs(queries[step][1]);\n if (v < h[0]) {\n std::pop_heap(h, h+k);\n h[k-1] = v;\n std::push_heap(h, h+k);\n }\n result.push_back(h[0]);\n }\n return result;\n }\n};", "memory": "248616" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "priority_queue<int> que;\n\nclass Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n = queries.size();\n while (!que.empty()) {\n que.pop();\n }\n vector<int> ret(n);\n for (int i = 0; i < n; ++i) {\n int x = queries[i][0], y = queries[i][1], d = abs(x) + abs(y);\n if (que.size() < k) {\n que.push(d);\n } else if (que.top() > d) {\n que.pop();\n que.push(d);\n }\n ret[i] = que.size() < k ? -1 : que.top();\n }\n return ret;\n }\n};", "memory": "248616" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "#include <iostream>\n#include <vector>\n#include <fstream>\n\ntemplate <typename T, typename Comparator>\nclass Pyramid {\nprivate:\n std::vector<T> array;\n Comparator comparator;\n int size;\n\n int left_child(int index);\n\n int right_child(int index);\n\n int parent(int index);\n\n bool has_two_children(int index);\n\n bool has_the_only_child(int index);\n\n bool has_children(int index);\n\n void sift_down(int start_index);\n\n void sift_up(int index);\n\npublic:\n\n Pyramid();\n\n Pyramid(const std::vector<T>& v);\n\n void reserve(int new_capacity);\n\n void build_pyramid();\n\n const std::vector<T>& get_vector() const;\n\n void print() const;\n\n int get_size() const;\n\n void extract_root();\n\n const T& get_root() const;\n\n void push(const T& new_value);\n};\n\n\ntemplate <typename T, typename Comparator>\nPyramid<T, Comparator>::Pyramid() : size(0) {}\n\n\ntemplate <typename T, typename Comparator>\nint Pyramid<T, Comparator>::left_child(int index) {\n return 2 * index + 1;\n}\n\n\ntemplate <typename T, typename Comparator>\nint Pyramid<T, Comparator>::right_child(int index) {\n return 2 * index + 2;\n}\n\n\ntemplate <typename T, typename Comparator>\nint Pyramid<T, Comparator>::parent(int index) {\n return (index - 1) / 2;\n}\n\n\ntemplate <typename T, typename Comparator>\nbool Pyramid<T, Comparator>::has_two_children(int index) {\n return right_child(index) < size;\n}\n\n\ntemplate <typename T, typename Comparator>\nbool Pyramid<T, Comparator>::has_the_only_child(int index) {\n return left_child(index) == size - 1;\n}\n\n\ntemplate <typename T, typename Comparator>\nbool Pyramid<T, Comparator>::has_children(int index) {\n return left_child(index) < size;\n}\n\n\ntemplate <typename T, typename Comparator>\nvoid Pyramid<T, Comparator>::sift_down(int start_index) {\n int index = start_index;\n bool swapped = true;\n while (has_children(index) && swapped) {\n if (has_two_children(index)) {\n int max_child_index = (comparator.more(array[left_child(index)], array[right_child(index)])) ? left_child(index) : right_child(index);\n if (comparator.less(array[index], array[max_child_index])) {\n std::swap(array[index], array[max_child_index]);\n index = max_child_index;\n } else {\n swapped = false;\n }\n }\n else if (has_the_only_child(index)) {\n if (comparator.less(array[index], array[left_child(index)])) {\n std::swap(array[index], array[left_child(index)]);\n index = left_child(index);\n } else {\n swapped = false;\n }\n }\n }\n}\n\n\ntemplate <typename T, typename Comparator>\nvoid Pyramid<T, Comparator>::sift_up(int index) {\n while (comparator.less(array[parent(index)], array[index])) {\n std::swap(array[parent(index)], array[index]);\n index = parent(index);\n }\n}\n\n\ntemplate <typename T, typename Comparator>\nPyramid<T, Comparator>::Pyramid(const std::vector<T>& v) : array(v), size(v.size()), comparator() {\n build_pyramid();\n}\n\n\ntemplate <typename T, typename Comparator>\nvoid Pyramid<T, Comparator>::build_pyramid() {\n for (int i = 1; i < size; ++i) {\n sift_up(i);\n }\n}\n\n\ntemplate <typename T, typename Comparator>\nconst std::vector<T>& Pyramid<T, Comparator>::get_vector() const {\n return array;\n}\n\n\ntemplate <typename T, typename Comparator>\nvoid Pyramid<T, Comparator>::print() const {\n std::cout << \"pyramid: \";\n for (int i = 0; i < size; ++i) {\n std::cout << array[i] << ' ';\n }\n std::cout << '\\n';\n}\n\n\ntemplate <typename T, typename Comparator>\nvoid Pyramid<T, Comparator>::extract_root() {\n std::swap(array[0], array[size - 1]);\n array.pop_back();\n --size;\n sift_down(0);\n}\n\n\ntemplate <typename T, typename Comparator>\nconst T& Pyramid<T, Comparator>::get_root() const {\n return array[0];\n}\n\n\ntemplate <typename T, typename Comparator>\nint Pyramid<T, Comparator>::get_size() const {\n return size;\n}\n\n\ntemplate<typename T, typename Comparator>\nvoid Pyramid<T, Comparator>::push(const T& new_value) {\n array.push_back(new_value);\n sift_up(size);\n ++size;\n}\n\n\ntemplate<typename T, typename Comparator>\nvoid Pyramid<T, Comparator>::reserve(int new_capacity) {\n array.reserve(new_capacity);\n}\n\n\nclass Comparator_int {\n public:\n bool less(int first, int second) const {\n return first < second;\n }\n\n bool more(int first, int second) const {\n return first > second;\n }\n };\n\n\n\nclass Solution {\npublic:\n std::vector<int> resultsArray(std::vector<std::vector<int>>& queries, int k) {\n\n std::vector<int> result;\n result.reserve(queries.size());\n\n Pyramid<int, Comparator_int> pyramid;\n pyramid.reserve(k);\n\n for (int i = 0; i < queries.size(); ++i) {\n\n int value = abs(queries[i][0]) + abs(queries[i][1]);\n\n if (i + 1 <= k) {\n pyramid.push(value);\n int new_result = (i + 1 == k) ? pyramid.get_root() : -1;\n result.push_back(new_result);\n } else {\n if (value < pyramid.get_root()) {\n pyramid.extract_root();\n pyramid.push(value);\n }\n result.push_back(pyramid.get_root());\n }\n }\n\n return result;\n }\n};", "memory": "249450" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "#include <iostream>\n#include <vector>\n#include <fstream>\n\ntemplate <typename T, typename Comparator>\nclass Pyramid {\nprivate:\n std::vector<T> array;\n Comparator comparator;\n int size;\n\n int left_child(int index);\n\n int right_child(int index);\n\n int parent(int index);\n\n bool has_two_children(int index);\n\n bool has_the_only_child(int index);\n\n bool has_children(int index);\n\n void sift_down(int start_index);\n\n void sift_up(int index);\n\npublic:\n\n Pyramid();\n\n Pyramid(const std::vector<T>& v);\n\n void reserve(int new_capacity);\n\n void build_pyramid();\n\n const std::vector<T>& get_vector() const;\n\n void print() const;\n\n int get_size() const;\n\n void extract_root();\n\n const T& get_root() const;\n\n void push(const T& new_value);\n};\n\n\ntemplate <typename T, typename Comparator>\nPyramid<T, Comparator>::Pyramid() : size(0) {}\n\n\ntemplate <typename T, typename Comparator>\nint Pyramid<T, Comparator>::left_child(int index) {\n return 2 * index + 1;\n}\n\n\ntemplate <typename T, typename Comparator>\nint Pyramid<T, Comparator>::right_child(int index) {\n return 2 * index + 2;\n}\n\n\ntemplate <typename T, typename Comparator>\nint Pyramid<T, Comparator>::parent(int index) {\n return (index - 1) / 2;\n}\n\n\ntemplate <typename T, typename Comparator>\nbool Pyramid<T, Comparator>::has_two_children(int index) {\n return right_child(index) < size;\n}\n\n\ntemplate <typename T, typename Comparator>\nbool Pyramid<T, Comparator>::has_the_only_child(int index) {\n return left_child(index) == size - 1;\n}\n\n\ntemplate <typename T, typename Comparator>\nbool Pyramid<T, Comparator>::has_children(int index) {\n return left_child(index) < size;\n}\n\n\ntemplate <typename T, typename Comparator>\nvoid Pyramid<T, Comparator>::sift_down(int start_index) {\n int index = start_index;\n bool swapped = true;\n while (has_children(index) && swapped) {\n if (has_two_children(index)) {\n int max_child_index = (comparator.more(array[left_child(index)], array[right_child(index)])) ? left_child(index) : right_child(index);\n if (comparator.less(array[index], array[max_child_index])) {\n std::swap(array[index], array[max_child_index]);\n index = max_child_index;\n } else {\n swapped = false;\n }\n }\n else if (has_the_only_child(index)) {\n if (comparator.less(array[index], array[left_child(index)])) {\n std::swap(array[index], array[left_child(index)]);\n index = left_child(index);\n } else {\n swapped = false;\n }\n }\n }\n}\n\n\ntemplate <typename T, typename Comparator>\nvoid Pyramid<T, Comparator>::sift_up(int index) {\n while (comparator.less(array[parent(index)], array[index])) {\n std::swap(array[parent(index)], array[index]);\n index = parent(index);\n }\n}\n\n\ntemplate <typename T, typename Comparator>\nPyramid<T, Comparator>::Pyramid(const std::vector<T>& v) : array(v), size(v.size()), comparator() {\n build_pyramid();\n}\n\n\ntemplate <typename T, typename Comparator>\nvoid Pyramid<T, Comparator>::build_pyramid() {\n for (int i = 1; i < size; ++i) {\n sift_up(i);\n }\n}\n\n\ntemplate <typename T, typename Comparator>\nconst std::vector<T>& Pyramid<T, Comparator>::get_vector() const {\n return array;\n}\n\n\ntemplate <typename T, typename Comparator>\nvoid Pyramid<T, Comparator>::print() const {\n std::cout << \"pyramid: \";\n for (int i = 0; i < size; ++i) {\n std::cout << array[i] << ' ';\n }\n std::cout << '\\n';\n}\n\n\ntemplate <typename T, typename Comparator>\nvoid Pyramid<T, Comparator>::extract_root() {\n std::swap(array[0], array[size - 1]);\n array.pop_back();\n --size;\n sift_down(0);\n}\n\n\ntemplate <typename T, typename Comparator>\nconst T& Pyramid<T, Comparator>::get_root() const {\n return array[0];\n}\n\n\ntemplate <typename T, typename Comparator>\nint Pyramid<T, Comparator>::get_size() const {\n return size;\n}\n\n\ntemplate<typename T, typename Comparator>\nvoid Pyramid<T, Comparator>::push(const T& new_value) {\n array.push_back(new_value);\n sift_up(size);\n ++size;\n}\n\n\ntemplate<typename T, typename Comparator>\nvoid Pyramid<T, Comparator>::reserve(int new_capacity) {\n array.reserve(new_capacity);\n}\n\n\nclass Comparator_int {\n public:\n bool less(int first, int second) const {\n return first < second;\n }\n\n bool more(int first, int second) const {\n return first > second;\n }\n };\n\n\n\nclass Solution {\npublic:\n std::vector<int> resultsArray(std::vector<std::vector<int>>& queries, int k) {\n\n std::vector<int> result;\n result.reserve(queries.size());\n\n Pyramid<int, Comparator_int> pyramid;\n pyramid.reserve(k);\n\n for (int i = 0; i < queries.size(); ++i) {\n\n int value = abs(queries[i][0]) + abs(queries[i][1]);\n\n if (i + 1 <= k) {\n pyramid.push(value);\n int new_result = (i + 1 == k) ? pyramid.get_root() : -1;\n result.push_back(new_result);\n } else {\n if (value < pyramid.get_root()) {\n pyramid.extract_root();\n pyramid.push(value);\n }\n result.push_back(pyramid.get_root());\n }\n }\n\n return result;\n }\n};", "memory": "249450" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n if (queries.size() < k) {\n return vector<int>(queries.size(), -1);\n }\n\n int pq[k];\n transform(queries.begin(), next(queries.begin(), k), pq, calcDistance);\n make_heap(pq, pq + k);\n\n int distance = pq[0];\n vector<int> result(k, -1);\n result.back() = distance;\n\n for (int i = k; i < queries.size(); ++i) {\n int newDistance = calcDistance(queries[i]);\n \n if (newDistance < distance) {\n pop_heap(pq, pq + k);\n pq[k-1] = newDistance;\n push_heap(pq, pq + k);\n distance = pq[0];\n }\n \n result.push_back(distance);\n }\n\n return result;\n }\nprivate:\n inline static int calcDistance(const vector<int>& point) {\n return abs(point[0]) + abs(point[1]);\n }\n};", "memory": "250284" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "\nclass Solution {\npublic:\n\ntemplate<class T, class U = greater<T>> class p_queue { vector<T> H; U Cc; size_t S = 0, C;\n void H_U(size_t I) { while (I && Cc(H[I], H[(I - 1) >> 1])) { swap(H[I], H[(I - 1) >> 1]); I = (I - 1) >> 1; }}\n void H_D(size_t I) { while (true) { size_t Lrg = I, L = 2 * I + 1, R = L + 1;\n if (L < S && Cc(H[L], H[Lrg])) Lrg = L; if (R < S && Cc(H[R], H[Lrg])) Lrg = R;\n if (Lrg != I) { swap(H[I], H[Lrg]); I = Lrg; } else { break; } } }\npublic: p_queue(size_t Cp = 2) {C = Cp; H.resize(C);} size_t size() const { return S; }\n void push(const T& value) { if(S == C) {H.resize(C <<= 1); } H[S++] = value; H_U(S - 1); }\n void pop() { if (S) { H[0] = H[S - 1]; S--; H_D(0); } } const T& top() const { return H[0]; }\n};\n vector<int> resultsArray(vector<vector<int>>& ar, int k) {\n int n = ar.size();\n vector<int> ra(n);\n p_queue<int> m(n);\n for(int i = 0; i < n; i++) {\n m.push(abs(ar[i][0]) + abs(ar[i][1]));\n if(m.size() > k){\n m.pop();\n }\n if(m.size() == k) {\n ra[i] = m.top();\n }\n else {\n ra[i] = -1;\n }\n }\n return ra;\n }\n};", "memory": "250284" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "\nclass Solution {\npublic:\n\ntemplate<class T, class U = greater<T>> class p_queue { vector<T> H; U Cc; size_t S = 0, C;\n void H_U(size_t I) { while (I && Cc(H[I], H[(I - 1) >> 1])) { swap(H[I], H[(I - 1) >> 1]); I = (I - 1) >> 1; }}\n void H_D(size_t I) { while (true) { size_t Lrg = I, L = 2 * I + 1, R = L + 1;\n if (L < S && Cc(H[L], H[Lrg])) Lrg = L; if (R < S && Cc(H[R], H[Lrg])) Lrg = R;\n if (Lrg != I) { swap(H[I], H[Lrg]); I = Lrg; } else { break; } } }\npublic: p_queue(size_t Cp = 2) {C = Cp; H.resize(C);} size_t size() const { return S; }\n void push(const T& value) { if(S == C) {H.resize(C <<= 1); } H[S++] = value; H_U(S - 1); }\n void pop() { if (S) { H[0] = H[S - 1]; S--; H_D(0); } } const T& top() const { return H[0]; }\n};\n vector<int> resultsArray(vector<vector<int>>& ar, int k) {\n int n = ar.size();\n vector<int> ra(n);\n p_queue<int> m(n);\n for(int i = 0; i < n; i++) {\n m.push(abs(ar[i][0]) + abs(ar[i][1]));\n if(m.size() > k){\n m.pop();\n }\n if(m.size() == k) {\n ra[i] = m.top();\n }\n else {\n ra[i] = -1;\n }\n }\n return ra;\n }\n};", "memory": "251118" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n\n void swim(vector<int>& vec, int idx) {\n while(idx > 0) {\n int parent_idx = (idx-1)/2;\n if (vec[idx] > vec[parent_idx]) {\n swap(vec[idx], vec[parent_idx]);\n idx = parent_idx;\n }else\n break;\n }\n }\n\n void sink(vector<int>& vec, int idx) {\n int len = vec.size();\n while(idx < len) {\n int next_idx = idx * 2 + 1; // left\n if (next_idx+1 < len && vec[next_idx+1] > vec[next_idx]){\n ++next_idx;\n }\n if (vec[next_idx] > vec[idx])\n swap(vec[next_idx], vec[idx]);\n else\n break;\n idx = next_idx;\n }\n }\n\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int> heap;\n heap.reserve(k);\n vector<int> res(queries.size());\n for(int i = 0; i < queries.size(); ++i){\n heap.push_back(abs(queries[i][0])+abs(queries[i][1]));\n int len = heap.size();\n swim(heap, len-1);\n if (len > k) {\n heap[0] = heap[len-1];\n heap.pop_back();\n sink(heap, 0);\n }\n res[i] = (heap.size() == k) ? heap[0] : -1;\n }\n return res;\n }\n};", "memory": "251118" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n=queries.size();\n priority_queue<int>dist;\n vector<int>res(n);\n for(int i=0;i<n;i++){\n dist.push(abs(queries[i][0])+abs(queries[i][1]));\n if(dist.size()>k) dist.pop();\n res[i] = (dist.size()==k)?dist.top():-1;\n }\n return res;\n }\n};", "memory": "251951" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<int> q;\n vector<int> ans(queries.size(), -1);\n for (int i = 0; i < queries.size(); ++i) {\n int cur_dis = abs(queries[i][0]) + abs(queries[i][1]);\n if (q.size() < k) {\n q.push(cur_dis);\n } else {\n if (cur_dis < q.top()) {\n q.pop();\n q.push(cur_dis);\n }\n }\n if (q.size() == k)\n ans[i] = q.top();\n }\n return ans;\n }\n};", "memory": "251951" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "#include <iostream>\n#include <queue>\n\nstatic const int fast_io = []()\n{\n std::ios::sync_with_stdio(false);\n std::cin.tie(NULL);\n std::cout.tie(NULL);\n return 0;\n}();\n\nusing namespace std;\n\nclass Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<int> PQ;\n vector<int> result;\n result.reserve(queries.size());\n\n for(auto& query: queries){\n int x = query[0];\n int y = query[1];\n int key = abs(x) + abs(y);\n if(PQ.size() < k){\n PQ.push(key);\n }else{\n if(key < PQ.top()){\n PQ.pop();\n PQ.push(key);\n }\n }\n\n if(PQ.size() < k){\n result.emplace_back(-1);\n }else{\n result.emplace_back(PQ.top());\n }\n }\n return result;\n }\n};", "memory": "252785" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n\n priority_queue<int> pq;\n vector<int> ans(queries.size(), -1);\n if (k > queries.size()) {\n return ans;\n }\n\n for (int i = 0; i < k; i++) {\n int dis = abs(queries[i][0]) + abs(queries[i][1]);\n pq.push(dis);\n }\n ans[k - 1] = pq.top();\n\n for (int i = k; i < queries.size(); i++) {\n int dis = abs(queries[i][0]) + abs(queries[i][1]);\n if (pq.top() > dis) {\n pq.pop();\n pq.push(dis);\n }\n ans[i] = pq.top();\n }\n\n return ans;\n }\n};", "memory": "252785" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "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 int n=queries.size();\n vector<int>ans(n);\n vector<int>store(k);\n int now=-1;\n for(int i=0;i<n;i++){\n pq.push(abs(queries[i][0])+abs(queries[i][1]));\n // dummy.push(pq.top());\n if(i<k-1){\n ans[i]=-1;\n }\n // else if(i==k-1){\n // while(pq.size()>1){\n // // store.push(pq.top());\n // pq.pop();\n // }\n // ans[i]=pq.top();\n // // now=ans[i];\n // }\n else{\n int tmp=abs(queries[i][0])+abs(queries[i][1]);\n // cout<<tmp<<endl;\n // pq.push(tmp);\n // dummy.\n if(i>k-1) {pq.pop();}\n ans[i]=pq.top();\n // if(tmp>now){\n // ans[i]=pq.top();\n // }\n // else{\n // pq.push(tmp);\n // pq.pop();\n // ans[i]=pq.top();\n // }\n }\n }\n return ans;\n }\n};", "memory": "253619" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n std::vector<int> kNearDistObst, ans;\n kNearDistObst.reserve(k+1);\n int dist = -1;\n std::make_heap(kNearDistObst.begin(), kNearDistObst.end());\n for(auto &query : queries)\n {\n dist = abs(query[0]) + abs(query[1]);\n kNearDistObst.push_back(dist);\n push_heap(kNearDistObst.begin(), kNearDistObst.end());\n if(kNearDistObst.size() > k)\n {\n pop_heap(kNearDistObst.begin(), kNearDistObst.end());\n kNearDistObst.pop_back();\n ans.push_back(kNearDistObst.front());\n }\n else if(kNearDistObst.size() == k)\n {\n ans.push_back(kNearDistObst.front());\n }\n else\n {\n ans.push_back(-1);\n }\n }\n \n return ans;\n }\n};", "memory": "253619" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL); \n priority_queue<int> pq;\n vector<int> res;\n for (int i=0; i<queries.size(); i++) {\n pq.push(abs(queries[i][0]) + abs(queries[i][1]));\n if (pq.size() < k) {\n res.push_back(-1);\n } else if (pq.size() == k) {\n res.push_back(pq.top());\n } else {\n pq.pop();\n res.push_back(pq.top());\n }\n \n }\n return res;\n\n }\n};", "memory": "257788" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int> ans;\n \n vector<int> d;\n \n if(k>queries.size()){\n for(int i=0; i<queries.size(); i++){\n ans.push_back(-1);\n }\n return ans;\n }\n \n int i=0;\n for(; i<queries.size(); i++){\n d.push_back(abs(queries[i][0])+abs(queries[i][1]));\n \n if(d.size() == k){\n break;\n }else{\n ans.push_back(-1);\n }\n }\n \n priority_queue<int> pq(d.begin(), d.end());\n ans.push_back(pq.top());\n i++;\n \n for(; i<queries.size(); i++){\n int temp = abs(queries[i][0])+abs(queries[i][1]);\n \n if(pq.top() > temp){\n pq.pop();\n pq.push(temp);\n }\n \n ans.push_back(pq.top());\n }\n \n return ans;\n }\n};", "memory": "257788" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "template <typename T>\nstruct Fenwick {\n\tint n;\n\tvector<T> a;\n\n\tFenwick(int n = 0) {\n\t\tinit(n);\n\t}\n\n\tvoid init(int n) {\n\t\tthis->n = n;\n\t\ta.assign(n + 1, T());\n\t}\n\n\tvoid add(int i, T val) {\n\t\tfor (++i; i <= n; i += i & -i) {\n\t\t\ta[i] += val;\n\t\t}\n\t}\n\n\tT query(int i) {\n\t\tT res = T();\n\t\tfor (++i; i; i -= i & -i) {\n\t\t\tres += a[i];\n\t\t}\n\t\treturn res;\n\t}\n\n\tT rangeSum(int L, int R) {\n\t\treturn query(R) - query(L - 1);\n\t}\n};\n\nclass Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int> a;\n for (auto &q : queries) {\n a.push_back(abs(q[0]) + abs(q[1]));\n }\n sort(a.begin(), a.end());\n a.erase(unique(a.begin(), a.end()), a.end());\n \n auto get = [&](int x) -> int {\n return lower_bound(a.begin(), a.end(), x) - a.begin();\n };\n \n const int m = a.size();\n const int q = queries.size();\n Fenwick<int> fen(m);\n vector<int> ans(q, -1);\n for (int i = 0; i < q; ++i) {\n int dis = abs(queries[i][0]) + abs(queries[i][1]);\n fen.add(get(dis), 1);\n if (i + 1 < k) {\n continue;\n }\n int L = 0, R = m - 1;\n while (L < R) {\n int x = (L + R) / 2;\n if (fen.rangeSum(0, x) >= k) {\n R = x;\n } else {\n L = x + 1;\n }\n }\n ans[i] = a[L];\n }\n return ans;\n }\n};", "memory": "258621" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int> result(queries.size());\n int n = queries.size();\n if(n == 1 && k == 1){\n return {abs(queries[0][0]) + abs(queries[0][1])};\n }\n if(k > n){\n for(int i = 0; i < n; i++){\n result[i] = -1;\n }\n return result;\n }\n for(int i = 0; i < k - 1; i++){\n result[i] = -1;\n }\n priority_queue<pair<int, pair<int , int>>> pq;\n for(int i = 0; i < k; i++){\n pq.push({abs(queries[i][0]) + abs(queries[i][1]), {queries[i][0], queries[i][1]}});\n }\n result[k - 1] = pq.top().first;\n for(int i = k; i < n; i++){\n if(abs(queries[i][0]) + abs(queries[i][1]) < pq.top().first){\n pq.pop();\n pq.push({abs(queries[i][0]) + abs(queries[i][1]), {queries[i][0], queries[i][1]}});\n }\n result[i] = pq.top().first;\n }\n return result;\n }\n};", "memory": "259455" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n=queries.size();\n vector<int> result(n);\n priority_queue<pair<int,pair<int,int>>> pq;\n for(int i=0;i<n;i++){\n int dist=abs(queries[i][0])+abs(queries[i][1]);\n pq.push({dist,{queries[i][0],queries[i][1]}});\n if(pq.size()<k){\n result[i]=-1;\n continue;\n }\n while(pq.size()>k){\n pq.pop();\n\n }\n int dist1=pq.top().first;\n result[i]=dist1;\n }\n return result;\n \n\n \n }\n};", "memory": "259455" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n // max heap with size k\n // min heap with other\n priority_queue<int> max_heap;\n priority_queue<int, vector<int>, greater<int>> min_heap;\n vector<int> result(queries.size());\n for(int i=0; i<queries.size(); i++) {\n int p = queries[i][0];\n int q = queries[i][1];\n int dis = abs(p) + abs(q);\n max_heap.push(dis);\n if(max_heap.size() < k) {\n result[i] = -1;\n }\n else if(max_heap.size() == k+1) {\n int val = max_heap.top();\n max_heap.pop();\n min_heap.push(val);\n val = max_heap.top();\n max_heap.pop();\n min_heap.push(val);\n \n val = min_heap.top();\n max_heap.push(val);\n result[i] = max_heap.top();\n }\n else {\n result[i] = max_heap.top();\n }\n }\n return result;\n }\n};", "memory": "260289" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<long long>pq;\n int n = queries.size();\n vector<int>ans;\n if(n<k){\n for(int i=0;i<n;i++)ans.push_back(-1);\n return ans;\n }\n for(int i=0;i<k;i++){\n pq.push(abs(queries[i][0])+abs(queries[i][1]));\n ans.push_back(-1);\n }\n ans.pop_back();\n ans.push_back(pq.top());\n for(int i=k;i<n;i++){\n long long dist = abs(queries[i][0])+abs(queries[i][1]);\n if(dist<pq.top()){\n pq.pop();\n pq.push(dist);\n }\n ans.push_back(pq.top());\n }\n return ans;\n }\n};", "memory": "260289" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\nprivate:\n // int lesser (map<int,int>&distCount,int dist)\n // {\n // int ans =0;\n // for(auto it = distCount.begin();it!=distCount.end();it++)\n // {\n // if(it->first>=dist)return ans;\n // else ans += it->second;\n // }\n // return ans;\n // }\n // int checkNearest (map<int,int>&distCount, int k)\n // {\n // int low =0, high = distCount.size();\n // while(low<high)\n // {\n // int mid = low + (high-low)/2;\n // auto it = advance(distCount.begin(),mid);\n // int less = lesser(distCount,it->first);\n // if(less<k && k<=it->second+less)return it->first;\n // else if(less<k)\n // {\n // low =mid;\n // }\n // else if (less>k)high = mid;\n // }\n // return -1;\n // }\n\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n map<int,int> distCount;\n priority_queue<int> less;\n priority_queue<int,vector<int>,greater<>> more;\n vector<int>ans;\n for (int i =0;i<queries.size();i++)\n {\n int dist = abs(queries[i][0]) + abs(queries[i][1]);\n less.push(dist);\n if(less.size()<k)ans.push_back(-1);\n else if (less.size()==k)\n {\n ans.push_back(less.top());\n }\n else if (less.size()>k)\n {\n more.push(less.top());\n less.pop();\n ans.push_back(less.top());\n } \n }\n return ans;\n }\n};", "memory": "261123" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<int> pq1, pq2;\n vector<int>res;\n int n = queries.size();\n for(int i = 0 ;i < n; i++){\n int d = abs(queries[i][0])+abs(queries[i][1]);\n pq1.push(d);\n while(!pq1.empty() && !pq2.empty() && pq1.top() > -pq2.top()){\n pq2.push(-pq1.top()); pq1.pop();\n }\n while(pq1.size() > k && !pq1.empty()){\n pq2.push(-pq1.top()); pq1.pop();\n }\n while(pq1.size() < k && !pq2.empty()){\n pq1.push(-pq2.top());pq2.pop();\n }\n if(pq1.size()<k)res.push_back(-1);\n else res.push_back(pq1.top());\n }\n return res;\n }\n};\n/*\n[[1,2],[3,4],[2,3],[-3,0]] k = 2\n 3 7 5\n -1\n 7\n 5\n\n*/", "memory": "261123" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int m = queries.size();\n int n = queries[0].size();\n vector <int> ans;\n // for(int i=0;i<k-1;i++) ans.push_back(-1);\n vector <long long> temp(m);\n for(int i=0;i<m;i++){\n temp[i] = abs(queries[i][0]) + abs(queries[i][1]);\n }\n priority_queue <int, vector<int>> gq;\n for(int i=0;i<m;i++){\n // if(i<=k-1) ans.push_back(-1);\n gq.push(temp[i]);\n if(gq.size()<k) ans.push_back(-1);\n else if(gq.size()==k){\n ans.push_back(gq.top());\n }\n else{\n gq.pop();\n ans.push_back(gq.top());\n }\n }\n return ans;\n }\n};", "memory": "261956" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "bool comparator(pair<int, int>& p1, pair<int, int>& p2) {\n if (p1.first == p2.first) {\n return p2.second < p1.second;\n }\n return p1.first < p2.first;\n }\n\nclass Solution {\n \n\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<pair<int, int>> distances;\n int n = queries.size();\n for (int i = 0; i < queries.size(); i++) {\n distances.push_back({abs(queries[i][0]) + abs(queries[i][1]), i});\n }\n\n sort(distances.begin(), distances.end(), comparator);\n // for(auto u: distances) cout<< u.first<<\" \"<<u.second<<endl;\n\n vector<int> result(n, 0);\n for (int i = 0; i < min(k - 1, n); i++) {\n result[i] = -1;\n }\n int po = k - 1, d;\n for (int i = n - 1; i >= k - 1; i--) {\n result[i] = distances[po].first;\n d = abs(queries[i][0]) + abs(queries[i][1]);\n if (d <= distances[po].first) {\n po = min(po + 1, n - 1);\n while (po < n - 1 && distances[po].second > i)\n po++;\n }\n // cout << po << endl;\n }\n return result;\n }\n};", "memory": "262790" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int q = queries.size();\n vector<int> res(q , -1);\n priority_queue<pair<long long, int>> pq;\n \n for(int i = 0; i < q; i++){\n pq.push({abs(queries[i][0]) + abs(queries[i][1]) , i});\n \n while(pq.size() > k)\n pq.pop();\n \n if(pq.size() == k)\n res[i] = pq.top().first;\n }\n return res;\n }\n};", "memory": "263624" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\nprivate:\n priority_queue<long long> maxHeap;\n priority_queue<long long, vector<long long>, greater<long long>> minHeap;\n int K;\n\n void addObstacle(long long distance) {\n if (maxHeap.size() < K) {\n maxHeap.push(distance);\n } else if (distance < maxHeap.top()) {\n minHeap.push(maxHeap.top());\n maxHeap.pop();\n maxHeap.push(distance);\n } else {\n minHeap.push(distance);\n }\n\n balanceHeaps();\n }\n\n void balanceHeaps() {\n while (maxHeap.size() < K && !minHeap.empty()) {\n maxHeap.push(minHeap.top());\n minHeap.pop();\n }\n }\n\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n K = k;\n vector<int> results;\n results.reserve(queries.size());\n\n for (const auto& query : queries) {\n int x = query[0], y = query[1];\n long long distance = static_cast<long long>(abs(x)) + abs(y);\n\n addObstacle(distance);\n\n if (maxHeap.size() < K) {\n results.push_back(-1);\n } else {\n results.push_back(static_cast<int>(maxHeap.top()));\n }\n }\n\n return results;\n }\n};", "memory": "263624" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue <pair<int,pair<int, int>>> p;\n vector <int> ans;\n for(int i = 0; i < queries.size(); i++){\n if(p.size() == k){\n auto it = p.top();\n int dist = it.first;\n if(dist > abs(queries[i][0]) + abs(queries[i][1])){\n p.pop();\n p.push({abs(queries[i][0]) + abs(queries[i][1]), {queries[i][0], queries[i][1]}});\n }\n }\n else {\n p.push({abs(queries[i][0]) + abs(queries[i][1]), {queries[i][0], queries[i][1]}});\n }\n\n\n if(p.size() < k){\n ans.push_back(-1);\n }\n else {\n auto it = p.top();\n ans.push_back(it.first);\n }\n }\n\n return ans;\n }\n};", "memory": "264458" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int dist(int x,int y){\n return abs(x)+abs(y);\n }\n\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int> ans;\n priority_queue<pair<int,pair<int,int>>> pq;\n for(int i=0;i<queries.size();i++){\n int x=queries[i][0],y=queries[i][1];\n\n // Pushing\n if(pq.size()<k) pq.push({dist(x,y),{x,y}});\n else if(pq.size()==k){\n if(pq.top().first>dist(x,y)){\n pq.pop();\n pq.push({dist(x,y),{x,y}});\n }\n }\n\n // Checking\n if(pq.size()<k) ans.push_back(-1);\n else ans.push_back(pq.top().first);\n }\n return ans;\n }\n};", "memory": "264458" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long int dis(long long int a, long long int b){\n return abs(a) + abs(b); \n }\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int> ans;\n vector<long long int> arr(queries.size(),-1);\n for(int i = 0; i<queries.size();i++){\n arr[i] = dis(queries[i][0],queries[i][1]);\n }\n priority_queue<long long int> pq;\n for(int i = 0 ; i<queries.size();i++){\n pq.push(arr[i]);\n if(pq.size()>k){\n pq.pop();\n }\n if(pq.size()==k){\n ans.push_back(pq.top());\n }\n else{\n ans.push_back(-1);\n }\n }\n return ans;\n }\n};", "memory": "265291" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n=queries.size(),i=0;\n vector<int>ans(n,-1);\n priority_queue<int>pq;\n if(k>n){\n return ans;\n }\n\n int l=0;\n for(auto it:queries){\n int a=abs(it[0]);\n int b=abs(it[1]);\n pq.push(a+b);\n l++;\n if(l==k)break;\n ans[i++]=-1;\n\n }\n ans[i++]=pq.top();\n for(int it=k;it<n;it++){\n int b=abs(queries[it][1]);\n int a=abs(queries[it][0]);\n \n if((a+b)<pq.top()){\n pq.pop();\n pq.push(a+b);\n }\n ans[i++]=pq.top();\n \n }\n return ans;\n \n\n\n \n }\n};", "memory": "266125" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue <pair<long long,int>> pq;\n\n vector <int> ans;\n for(int i = 0 ; i < queries.size() ; i++) {\n int r = queries[i][0] , c = queries[i][1];\n\n pq.push({abs(r) + abs(c) , i});\n\n if(pq.size() > k) pq.pop();\n\n if(pq.size() == k) {\n ans.push_back(pq.top().first);\n }\n else ans.push_back(-1);\n\n \n }\n\n return ans;\n }\n};", "memory": "266959" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n \n void output(vector<int> vec) {\n for(int i = 0; i < vec.size(); i++) {\n cout << vec.at(i) << \", \";\n }\n cout << endl;\n }\n \n vector<int> edgeCase1(int size) {\n vector<int> results;\n for(int i = 0; i < size; i++) {\n results.push_back(-1);\n }\n return results;\n }\n \n void insertValue(vector<int>& priorityQueue, int value) {\n int insertIndex = 0;\n while(insertIndex < priorityQueue.size() && \n value > priorityQueue.at(insertIndex)) {\n insertIndex += 1;\n }\n priorityQueue.insert(priorityQueue.begin() + insertIndex, value);\n priorityQueue.pop_back();\n }\n\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n if(k > queries.size()) {\n return edgeCase1(queries.size());\n }\n \n vector<int> dists;\n for(int i = 0; i < queries.size(); i++) {\n int dist = abs(queries.at(i).at(0)) + abs(queries.at(i).at(1));\n dists.push_back(dist);\n }\n output(dists);\n \n vector<int> results;\n priority_queue<int> pq;\n for(const auto& dist: dists) {\n if(pq.size() < k) {\n pq.push(dist);\n }else {\n pq.push(dist);\n pq.pop();\n }\n\n if(pq.size() < k) {\n results.push_back(-1);\n }else{\n results.push_back(pq.top());\n }\n }\n return results;\n }\n \n// [[1,2],[3,4],[2,3],[-3,0]]\n// 2\n// [[5,5],[4,4],[3,3]]\n// 1\n// [[-7,-1]]\n// 3\n// [[-7,9],[-7,-6],[6,7],[5,8],[1,-5]]\n// 2\n};", "memory": "266959" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\nstruct comp1{\n bool operator()(pair<int,long long>&p1, pair<int, long long>&p2)\n {\n if(p1.second>p2.second)return true;\n return false;\n }\n};\nstruct comp2{\n bool operator()(pair<int,long long>&p1, pair<int, long long>&p2)\n {\n if(p1.second<p2.second)return true;\n return false;\n }\n};\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<pair<int,long long>,vector<pair<int, long long>>, comp1>pq1;\n priority_queue<pair<int,long long>,vector<pair<int, long long>>, comp2>pq2;\n vector<int>ans;\n for(int i=0;i<queries.size();i++)\n {\n pq2.push({i,abs(queries[i][0]) + abs(queries[i][1])});\n while(pq2.size()>k)\n {\n pq2.pop();\n }\n if(pq2.size()==k)\n {\n ans.push_back(pq2.top().second);\n }\n else\n {\n ans.push_back(-1);\n }\n }\n return ans;\n }\n};", "memory": "267793" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n = queries.size();\n priority_queue<long double> closestK;\n vector<int> out;\n for(int i = 0; i < n; i++) {\n int dist = abs(queries[i][0]) + abs(queries[i][1]);\n closestK.push(dist);\n if(closestK.size() > k)\n closestK.pop();\n if(closestK.size() == k)\n out.push_back(closestK.top());\n else\n out.push_back(-1);\n }\n return out;\n }\n};", "memory": "267793" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n typedef long long ll;\n\n int find_dist(int x, int y) {\n return abs(x)+abs(y);\n }\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n //kth nearest distance\n\n //2 heaps?\n priority_queue<ll> leftHeap;\n priority_queue<ll, vector<ll>, greater<ll>> rightHeap;\n\n vector<int> res;\n\n for (auto& query : queries) {\n ll dist = find_dist(query[0], query[1]);\n if (leftHeap.size() < k) {\n leftHeap.push(dist);\n } else if (dist < leftHeap.top()) {\n //size k hi hai lh ka\n leftHeap.push(dist);\n rightHeap.push(leftHeap.top());\n leftHeap.pop();\n } else {\n rightHeap.push(dist);\n }\n if(leftHeap.size()==k)res.push_back(leftHeap.top());\n else{\n res.push_back(-1);\n }\n \n }\n\n return res;\n\n\n }\n};", "memory": "268626" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<long long,vector<long long>,greater<long long>> sq;\n priority_queue<long long> lq;\n vector<int> ans;\n int q=queries.size();\n for(int i=0;i<q;i++){\n if(i<k-1){\n lq.push({abs(queries[i][0]) + abs(queries[i][1])});\n }\n else{\n long long curr=abs(queries[i][0]) + abs(queries[i][1]);\n if(k==1 || curr>=lq.top()){\n sq.push(curr);\n }\n else{\n sq.push(lq.top());\n lq.pop();\n lq.push(curr);\n }\n \n }\n ans.push_back(sq.size()?sq.top():-1);\n }\n return ans;\n }\n};", "memory": "268626" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n inline int abs(int x) {return x>0? x: -x;}\n \n int ask(vector<int> &arr, int pos) {\n int ans = 0;\n for(; pos; pos -= pos & (-pos))\n ans += arr[pos];\n return ans;\n }\n void add(vector<int> &arr, int pos, int val = 1) {\n int size = arr.size();\n for(; pos < size; pos += pos & (-pos))\n arr[pos] += val;\n }\n \n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n = queries.size();\n vector<pair<int, int>> q;\n for (int i=0; i<n; ++i) {\n int dist = abs(queries[i][0]) + abs(queries[i][1]);\n q.emplace_back(make_pair(dist, i));\n }\n sort(q.begin(), q.end());\n \n vector<int> rank(n, 0);\n for (int i=0; i<n; ++i)\n rank[q[i].second] = i;\n \n vector<int> arr(n+1, 0);\n vector<int> ans(n, 0);\n \n int L, R, m;\n \n for (int i=0; i<n; ++i) {\n add(arr, rank[i]+1, 1);\n // cout << \"i=\" << i << \", rank=\" << rank[i] << endl;\n \n if (i<k-1)\n ans[i] = -1;\n else {\n // find the minimum m such that ask(m) == k\n L = k, R = n;\n while (L < R) {\n m = (L+R) >> 1;\n if (ask(arr, m) < k)\n L = m+1;\n else\n R = m;\n }\n // answer is L\n // cout << \"Found L=\" << L << \" with ask(\" << L << \")=\" << ask(arr, L) << endl;\n ans[i] = q[L-1].first;\n }\n }\n \n return ans;\n }\n};", "memory": "269460" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n inline int abs(int x) {return x>0? x: -x;}\n \n int ask(vector<int> &arr, int pos) {\n int ans = 0;\n for(; pos; pos -= pos & (-pos))\n ans += arr[pos];\n return ans;\n }\n void add(vector<int> &arr, int pos, int val = 1) {\n int size = arr.size();\n for(; pos < size; pos += pos & (-pos))\n arr[pos] += val;\n }\n \n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n = queries.size();\n vector<pair<int, int>> q;\n for (int i=0; i<n; ++i) {\n int dist = abs(queries[i][0]) + abs(queries[i][1]);\n q.emplace_back(make_pair(dist, i));\n }\n sort(q.begin(), q.end());\n \n vector<int> rank(n, 0);\n for (int i=0; i<n; ++i)\n rank[q[i].second] = i;\n \n vector<int> arr(n+1, 0);\n vector<int> ans(n, 0);\n \n int L, R, m;\n \n for (int i=0; i<n; ++i) {\n add(arr, rank[i]+1, 1);\n // cout << \"i=\" << i << \", rank=\" << rank[i] << endl;\n \n if (i<k-1)\n ans[i] = -1;\n else {\n // find the minimum m such that ask(m) == k\n L = k, R = n;\n while (L < R) {\n m = (L+R) >> 1;\n if (ask(arr, m) < k)\n L = m+1;\n else\n R = m;\n }\n // answer is L\n // cout << \"Found L=\" << L << \" with ask(\" << L << \")=\" << ask(arr, L) << endl;\n ans[i] = q[L-1].first;\n }\n }\n \n return ans;\n }\n};", "memory": "270294" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n inline int abs(int x) {return x>0? x: -x;}\n \n int ask(vector<int> &arr, int pos) {\n int ans = 0;\n for(; pos; pos -= pos & (-pos))\n ans += arr[pos];\n return ans;\n }\n void add(vector<int> &arr, int pos, int val = 1) {\n int size = arr.size();\n for(; pos < size; pos += pos & (-pos))\n arr[pos] += val;\n }\n \n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n = queries.size();\n vector<pair<int, int>> q;\n for (int i=0; i<n; ++i) {\n int dist = abs(queries[i][0]) + abs(queries[i][1]);\n q.emplace_back(make_pair(dist, i));\n }\n sort(q.begin(), q.end());\n \n vector<int> rank(n, 0);\n for (int i=0; i<n; ++i)\n rank[q[i].second] = i;\n \n vector<int> arr(n+1, 0);\n vector<int> ans(n, 0);\n \n int L, R, m;\n \n for (int i=0; i<n; ++i) {\n add(arr, rank[i]+1, 1);\n // cout << \"i=\" << i << \", rank=\" << rank[i] << endl;\n \n if (i<k-1)\n ans[i] = -1;\n else {\n // find the minimum m such that ask(m) == k\n L = k, R = n;\n while (L < R) {\n m = (L+R) >> 1;\n if (ask(arr, m) < k)\n L = m+1;\n else\n R = m;\n }\n // answer is L\n // cout << \"Found L=\" << L << \" with ask(\" << L << \")=\" << ask(arr, L) << endl;\n ans[i] = q[L-1].first;\n }\n }\n \n return ans;\n }\n};", "memory": "270294" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define xl cout<<'\\n';\n#define def template<typename... T>\n\ndef void out(T&&... s) {\n\t(void)initializer_list<int> {(cout << s << '\\n', 0)...};\n}\ndef void outc(T&&... s) {\n\t(void)initializer_list<int> {(cout << s << \" \", 0)...};\n}\ndef void oute(T&&... s) {\n\t(void)initializer_list<int> {(cout << s << ' ', 0)...};\n\txl;\n}\ndef void in(T&... s) {\n\t(void)initializer_list<int> {(cin >> s, 0)...};\n}\ntemplate<class T>\nvoid outv(const T&x, int s = 0, int e = numeric_limits<int>::max()) {\n\tauto it = x.begin();\n\tadvance(it, s);\n\tfor(int i = s; i < e && it != x.end(); ++i, ++it) {\n\t\toutc(*it);\n\t}\n\txl;\n}\n#define tcs ios::sync_with_stdio(0);cin.tie(0); \\\nINT(t); for(int i=1; i<=t; i++){Case(i);solve();bl }\n#define MOD (int)1e9 + 7\n#define sz(x) (int)x.size()\n#define all(x) begin(x), end(x)\n#define rall(x) rbegin(x), rend(x)\n#define pb push_back\n#define eb emplace_back\n#define ppb pop_back\n#define ppf pop_front\n#define ff first\n#define ss second\n#define vi vector<int>\n#define vb vector<bool>\n#define vs vector<string>\n#define vc vector<char>\n#define vii vector<vi>\n#define vcc vector<vc>\n#define vbb vector<vb>\n#define mii map<int, int>\n#define mci map<char, int>\n#define mic map<int, char>\n#define mcvi map<char, vi>\n#define mivi map<int, vi>\n#define pii pair<int, int>\n#define pci pair<char, int>\n#define mipii map<int, pii>\n#define vpii vector<pii>\n#define vpci vector<pci>\n#define si set<int>\n#define sc set<char>\n#define prefix(x) partial_sum(all(x), begin(x))\n#define suffix(x) partial_sum(rall(x), rbegin(x))\n#define maxe(x) *max_element(all(x))\n#define mine(x) *min_element(all(x))\n#define maxeIndex(x) max_element(all(x)) - begin(x)\n#define mineIndex(x) min_element(all(x)) - begin(x)\n#define ub(x, y) upper_bound(all(x), y) - begin(x)\n#define lb(x, y) lower_bound(all(x), y) - begin(x)\n#define vfind(v, x) find(all(v), x)\n#define vcount(s, x) count(all(s), x)\n#define vsum(v) accumulate(all(v), 0LL)\n#define csum(x) (x * (x + 1) >> 1)\n#define mid(l, r) (int)(l + ((r - l) >> 1))\n#define verase(v, x) v.erase(vfind(v, x))\n#define vunique(v) v.erase(unique(all(v), end(v)))\n#define vremove(v, x) v.erase(remove(all(v), x), end(v))\n#define vrev(v) reverse(all(v));\n#define lsb(x) (int)log2(x & -x)\n#define msb(x) (x == 0 ? -1 : 31 - __builtin_clz(x))\n#define setbit(x) __builtin_popcountll(x)\n#define bin(x) bitset<32>(x)\n#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)\n#define VEC(T,v,n) vector<T> v(n);for(auto& x:v) cin>>x\n#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)\ntemplate<class Fun> class _F {\n\tFun fun_;\n public:\n\ttemplate<class T> explicit _F(T &&fun): fun_(std::forward<T>(fun)) {}\n\ttemplate<class ...Args> decltype(auto) operator()(Args &&...args) {\n\t\treturn fun_(std::ref(*this), std::forward<Args>(args)...);\n\t}\n};\ntemplate<class Fun> decltype(auto) F(Fun &&fun) {\n\treturn _F<std::decay_t<Fun>>(std::forward<Fun>(fun));\n}\nvi solve(vpii&v,int k) {\n\tpriority_queue<int> pq;\n\tvi res;\n\tfor(const auto& i:v) {\n\t\tint d=abs(i.ff)+abs(i.ss);\n\t\tif(sz(pq)<k) {\n\t\t\tpq.push(d);\n\t\t}\n\t\telse {\n\t\t\tint x=pq.top();\n\t\t\tif(d<x) {\n\t\t\t\tpq.pop();\n\t\t\t\tpq.push(d);\n\t\t\t}\n\t\t}\n\t\tif(sz(pq)<k)\n\t\t\tres.pb(-1);\n\t\telse\n\t\t\tres.pb(abs(pq.top()));\n\t}\n\treturn res;\n}\nclass Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vpii x;\n for(auto&i:queries)\n x.pb({i[0],i[1]});\n return solve(x,k);\n }\n};", "memory": "271128" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int>result;\n priority_queue<int>max;\n vector<pair<int,int>>obsv;\n for(const auto &i:queries)\n {\n int a=i[0],b=i[1];\n int dist=abs(a)+abs(b);\n obsv.push_back({a,b});\n max.push(dist);\n if(max.size()>k)\n {\n max.pop();\n }\n if(obsv.size()<k)\n {\n result.push_back(-1);\n }\n else{\n result.push_back(max.top());\n }\n }\n return result;\n }\n};", "memory": "271128" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int> res;\n priority_queue<int>maxH;\n vector<pair<int,int>> obs;\n for(const auto &query:queries)\n {\n int x=query[0],y=query[1];\n int dis=abs(x)+abs(y);\n obs.push_back({x,y});\n maxH.push(dis);\n if(maxH.size()>k)\n {\n maxH.pop();\n }\n if(obs.size()<k)\n {\n res.push_back(-1);\n }\n else\n {\n res.push_back(maxH.top());\n }\n }\n return res;\n }\n};", "memory": "271961" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n \n int dis(int a,int b){\n // int abs_dist = abs(a)+abs(b);\n return abs(a)+abs(b);\n \n }\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int>res;\n \n vector<pair<int,int>>obs;\n priority_queue<int>maxi;\n \n for(auto& query : queries){\n int x = query[0];\n int y = query[1];\n \n \n obs.push_back({x,y});\n \n vector<int>dist;\n int dists = dis(x,y);\n maxi.push(dists);\n// for(auto& obst : obs){\n// int distance = dis(obst.first,obst.second);\n// // cout<<distance<<endl\"\n \n// dist.push_back(distance);\n// }\n \n if(maxi.size()>k){\n maxi.pop();\n }\n \n // sort(dist.begin(),dist.end());\n // for(int i=0;i<dist.size();i++){\n // cout<<dist[i]<<\" \";\n // }\n \n if(maxi.size()<k){\n res.push_back(-1);\n }else{\n res.push_back(maxi.top());\n }\n // for(int i=0;i<res.size();i++){\n // cout<<res[i]<<\" \";\n // }\n }\n return res;\n \n }\n};", "memory": "271961" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n void replace(vector<int>& rsq, int mod, int i, int x) {\n i += mod;\n rsq[i] = x;\n i /= 2;\n while (i > 0) {\n rsq[i] = rsq[i + i] + rsq[i + i + 1];\n i /= 2;\n }\n }\n\n int getsum(vector<int>& rsq, int mod, int l, int r) {\n int i = l + mod, j = r + mod, ans = 0;\n while (i <= j) {\n if (i % 2 == 1) ans += rsq[i];\n if (j % 2 == 0) ans += rsq[j];\n i = (i + 1) / 2;\n j = (j - 1) / 2;\n }\n return ans;\n }\n\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n = queries.size();\n vector<int> rsq(2 * n + 2, 0);\n int mod = n + 1;\n vector<pair<int, int> > a;\n int id = 0;\n for (const auto& q : queries) {\n a.push_back({abs(q[0]) + abs(q[1]), id});\n ++id;\n }\n sort(a.begin(), a.end());\n vector<int> ans(n, -1), ids(n, -1);\n for (int i = 0; i < a.size(); ++i) ids[a[i].second] = i;\n int cnt = 0;\n for (int i = 0; i < a.size(); ++i) {\n replace(rsq, mod, ids[i], 1);\n ++cnt;\n if (cnt < k) ans[i] = -1; else {\n int l = 0, r = n - 1;\n while (r - l > 1) {\n int mid = (l + r) / 2;\n if (getsum(rsq, mod, 0, mid) >= k) r = mid; else l = mid;\n }\n int id = r;\n if (getsum(rsq, mod, 0, l) >= k) id = l;\n ans[i] = a[id].first;\n }\n }\n return ans;\n }\n};", "memory": "272795" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\ntemplate <typename T>\nvoid print(const T& container, const std::string& delimiter = \" \") {\n std::copy(container.begin(), container.end(), std::ostream_iterator<typename T::value_type>(std::cout, delimiter.c_str()));\n std::cout << std::endl;\n}\n#define ll long long\n\nclass Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& qs, int k) {\n fast;\n vector<int> dist;\n vector<int> ans;\n int maxdist = 0;\n for(int i = 0; i<min((ll)qs.size(),(ll)k); i++){ \n dist.push_back(abs(qs[i][0])+abs(qs[i][1]));\n maxdist = max(maxdist,dist[i]);\n if(i!=k-1)ans.push_back(-1);\n else \n ans.push_back(maxdist);\n }\n sort(dist.begin(), dist.end());\n\n for(int i = min((ll)qs.size(),(ll)k); i<qs.size(); i++) { \n auto v = qs[i];\n int newdist = abs(v[0])+abs(v[1]);\n if(newdist<dist[k-1]) {\n dist.insert(lower_bound(dist.begin(),dist.begin()+k,newdist), newdist);\n dist.resize(k);\n ans.push_back(dist[k-1]);\n }\n else {\n ans.push_back(dist[k-1]);\n }\n // print(ans);\n // print(dist);\n }\n return ans;\n }\n};", "memory": "273629" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<long long>pq;\n vector<long long>arr;\n for(int i=0;i<queries.size();i++){\n arr.push_back(abs(queries[i][0])+abs(queries[i][1]));\n }\n vector<int>res;\n for(int i=0;i<queries.size();i++){\n if(i<k-1){\n res.push_back(-1);\n pq.push(arr[i]);\n continue;\n }\n \n pq.push(arr[i]);\n \n while(pq.size()>k)pq.pop();\n\n res.push_back(pq.top());\n }\n return res;\n }\n};", "memory": "273629" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<long long int> distance;\n vector<int> ans;\n priority_queue<long long> pq;\n\n for(int i=0;i<queries.size();i++)\n {\n long long int dist = abs(queries[i][0]) + abs(queries[i][1]);\n distance.push_back(dist);\n if(pq.size() == k)\n {\n auto node = pq.top();\n if(node > dist)\n {\n pq.pop();\n pq.push(dist);\n }\n }\n else if(pq.size() < k)\n {\n pq.push(dist);\n }\n if(distance.size() < k)\n {\n ans.push_back(-1);\n }\n else\n {\n auto node = pq.top();\n ans.push_back(node);\n }\n }\n\n return ans;\n }\n};", "memory": "274463" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "#define ll long long int\nclass Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n ll n=queries.size();\n vector<ll> ans;\n priority_queue<ll> pq;\n for(ll i=0;i<n;i++){\n ll x=queries[i][0];\n ll y=queries[i][1];\n ll dis=abs(x-0)+abs(y-0);\n if(pq.size()<k){\n pq.push(dis);\n }else{\n if(pq.top()>dis){\n pq.pop();\n pq.push(dis);\n }\n }\n if(pq.size()<k){\n ans.push_back(-1);\n }else{\n ll xy=pq.top();\n ans.push_back(xy);\n }\n }\n vector<int> answer;\n for(ll i=0;i<ans.size();i++){\n answer.push_back(ans[i]);\n }\n return answer;\n }\n};", "memory": "275296" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& q, int k) {\n vector<int> ans ;\n priority_queue<long long> pq ;\n for(int i = 0 ; i < q.size() ; i++) {\n long long x = q[i][0] ;\n long long y = q[i][1] ;\n long long dis = abs(x) + abs(y) ;\n pq.push(dis) ;\n if( pq.size() < k ) ans.push_back(-1) ;\n else {\n vector<int> temp ;\n if( pq.size() > k ) {\n temp.push_back(pq.top()) ;\n pq.pop() ;\n }\n ans.push_back(pq.top()) ;\n }\n }\n return ans ;\n }\n};", "memory": "276130" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& q, int k) {\n vector<int> ans ;\n priority_queue<long long> pq ;\n for(int i = 0 ; i < q.size() ; i++) {\n long long x = q[i][0] ;\n long long y = q[i][1] ;\n long long dis = abs(x) + abs(y) ;\n pq.push(dis) ;\n if( pq.size() < k ) ans.push_back(-1) ;\n else {\n vector<int> temp ;\n while( pq.size() > k ) {\n temp.push_back(pq.top()) ;\n pq.pop() ;\n }\n ans.push_back(pq.top()) ;\n }\n }\n return ans ;\n }\n};", "memory": "276964" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) \n {\n const size_t size = queries.size();\n vector<int> records(size, 0);\n for (int i = 0; i < size; ++i)\n {\n records[i] = abs(queries[i][0]) + abs(queries[i][1]);\n }\n vector<int> sorted = records;\n sort(sorted.begin(), sorted.end());\n unordered_map<int, int> cnts;\n int ptr = k - 1;\n vector<int> results(size, -1);\n for (int i = size - 1; i >= 0; --i)\n {\n if (ptr < size)\n {\n results[i] = sorted[ptr];\n if (records[i] > sorted[ptr])\n {\n ++cnts[records[i]];\n }\n else\n {\n ++ptr;\n while (ptr < size && cnts[sorted[ptr]] != 0)\n {\n --cnts[sorted[ptr++]];\n }\n }\n }\n }\n return results;\n }\n};", "memory": "276964" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> solve(vector<vector<int>>& queries, int k) {\n vector<int> ans;\n priority_queue<int> pq;\n unordered_set<int> st;\n\n for (const auto& it : queries) {\n\n pq.push(abs(it[0]) + abs(it[1]));\n \n if (pq.size() > k) {\n st.insert(pq.top());\n pq.pop();\n }\n \n while (pq.size() < k && !st.empty()) {\n int z = *st.begin();\n st.erase(st.begin());\n pq.push(z);\n }\n \n int temp = (pq.size() < k) ? -1 : pq.top();\n ans.push_back(temp);\n }\n \n return ans; \n }\n \n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n return solve(queries, k);\n }\n};\n", "memory": "277798" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<int> pq;\n vector<int> ret(queries.size(), -1);\n if (k > queries.size()) return ret;\n\n int index = k - 1;\n for (auto query : queries) {\n int dis = abs(query[0]) + abs(query[1]);\n if (pq.size() < k) {\n pq.push(dis);\n if (pq.size() == k) ret[index++] = pq.top();\n continue;\n } \n \n if (dis <= pq.top()) {\n pq.pop();\n pq.push(dis);\n }\n\n ret[index++] = pq.top();\n }\n return ret;\n }\n};", "memory": "278631" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n //As we need to find the kth nearest distance we have to create a max heap of the size k;\n priority_queue<int>pq;\n vector<int>ans(queries.size());\n int index=0;\n for(auto it:queries){\n int dist=abs(it[0])+abs(it[1]);\n pq.push(dist);\n if(index<k-1){\n ans[index]=-1;\n index++;\n continue;\n }\n if(pq.size()>k){\n pq.pop();\n }\n ans[index]=pq.top();\n index++;\n }\n return ans;\n }\n};", "memory": "279465" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int len = queries.size();\n priority_queue<int> left; // get the largest num\n vector<int> result = vector(len, -1);\n // let left.size() == k\n for(int i = 0; i < len; i++){\n auto q = queries[i];\n int curr_dist = abs(q[0]) + abs(q[1]);\n left.push(curr_dist);\n\n if(left.size() > k){\n left.pop();\n }\n \n if(left.size() == k){\n result[i] = left.top();\n }\n }\n\n return result;\n }\n};\n\n// k * logk + (n - k) * log(n - k) ~= nlogn", "memory": "280299" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n \n int N = queries.size();\n \n priority_queue<int> store;\n vector<int> answer(N, -1);\n \n int index = 0;\n // 3 ,7, 5 ,3 , 18\n for(auto query : queries){\n \n int distance = abs(query[0]) + abs(query[1]);\n store.push(distance);\n \n if(store.size() > k){\n store.pop();\n }\n \n if(store.size() == k){\n answer[index] = store.top();\n }\n \n index+=1; \n }\n \n return answer;\n }\n};", "memory": "281133" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<int>pq;\n int n = queries.size();\n vector<int>v(n,-1);\n for(int i=0;i<n;i++){\n auto it = queries[i];\n int x = it[0];\n int y = it[1];\n int dist = abs(x)+abs(y);\n\n pq.push(dist);\n if(pq.size() > k) pq.pop();\n\n\n if(pq.size() == k){\n v[i] = pq.top();\n }\n }\n\n return v;\n }\n};", "memory": "281966" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<int>pq;\n int n=queries.size();\n vector<int> ans(n,-1);\n int temp=k;\n for(auto qry:queries) {\n if(k) {\n pq.push(abs(qry[0]) + abs(qry[1]));\n k--;\n }\n else {\n break;\n }\n }\n if(k) {\n return ans;\n }\n ans[temp-1]=pq.top();\n for(int i=temp;i<queries.size();i++) {\n auto qry=queries[i];\n int x=abs(qry[0]);\n int y=abs(qry[1]);\n if((x+y)<pq.top()) {\n cout<<x+y<<endl;\n pq.pop();\n pq.push(x+y);\n }\n ans[i]=pq.top();\n }\n return ans;\n }\n};", "memory": "282800" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n \n \n \n vector<int> resultsArray(vector<vector<int>>& q, int k) {\n \n \n priority_queue<int> pq;\n vector<int> ans;\n set<int> st;\n for(int i=0;i<q.size();i++){\n \n int val = abs(q[i][0]) + abs(q[i][1]) ;\n \n \n if(i<k)\n { \n ans.push_back(-1);\n pq.push(val);\n \n st.insert(val);\n \n if(i==k-1){\n ans[i] = *st.rbegin();\n } \n }\n \n else{\n \n if(pq.top() > val){\n \n pq.pop();\n pq.push(val);\n \n }\n ans.push_back(pq.top());\n\n }\n \n }\n \n \n return ans;\n \n }\n};", "memory": "283634" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n template<typename T> struct Tr_Nd {\n T key; int32_t Hg, count, Sz; Tr_Nd* Lt; Tr_Nd* Rt;\n Tr_Nd(T x) : key(x), Lt(nullptr), Rt(nullptr), Hg(1), count(1), Sz(1) {}\n};\ntemplate<typename T> class mset {\n Tr_Nd<T>* root; int32_t cnt = 0; T first = numeric_limits<T>::max(); T last = numeric_limits<T>::min();\n Tr_Nd<T>* BN(Tr_Nd<T>* Nd, T key) {if (!Nd) return new Tr_Nd<T>(key);Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0); int32_t balance = (Nd->Lt ? Nd->Lt->Hg : 0) - (Nd->Rt ? Nd->Rt->Hg : 0);\n if (balance > 1) { if (key < Nd->Lt->key) { Tr_Nd<T>* x = Nd->Lt; Nd->Lt = x->Rt; x->Rt = Nd;\n Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0);x->Hg = 1 + max(x->Lt ? x->Lt->Hg : 0, x->Rt ? x->Rt->Hg : 0);\n x->Sz = x->count + (x->Lt ? x->Lt->Sz : 0) + (x->Rt ? x->Rt->Sz : 0);return x;} else {Tr_Nd<T>* y = Nd->Lt->Rt;Nd->Lt->Rt = y->Lt;\n y->Lt = Nd->Lt;Nd->Lt = y;Nd->Lt->Lt->Hg = 1 + max(Nd->Lt->Lt->Lt ? Nd->Lt->Lt->Lt->Hg : 0, Nd->Lt->Lt->Rt ? Nd->Lt->Lt->Rt->Hg : 0);\n Nd->Lt->Lt->Sz = Nd->Lt->Lt->count + (Nd->Lt->Lt->Lt ? Nd->Lt->Lt->Lt->Sz : 0) + (Nd->Lt->Lt->Rt ? Nd->Lt->Lt->Rt->Sz : 0);\n Tr_Nd<T>* x = Nd->Lt;Nd->Lt = x->Rt;x->Rt = Nd;Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0);x->Hg = 1 + max(x->Lt ? x->Lt->Hg : 0, x->Rt ? x->Rt->Hg : 0);\n x->Sz = x->count + (x->Lt ? x->Lt->Sz : 0) + (x->Rt ? x->Rt->Sz : 0);return x;}}if (balance < -1) {if (key > Nd->Rt->key) {\n Tr_Nd<T>* y = Nd->Rt;Nd->Rt = y->Lt;y->Lt = Nd;Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0);y->Hg = 1 + max(y->Lt ? y->Lt->Hg : 0, y->Rt ? y->Rt->Hg : 0);\n y->Sz = y->count + (y->Lt ? y->Lt->Sz : 0) + (y->Rt ? y->Rt->Sz : 0);return y;} else {\n Tr_Nd<T>* y = Nd->Rt->Lt;Nd->Rt->Lt = y->Rt;y->Rt = Nd->Rt; Nd->Rt = y;\n Nd->Rt->Rt->Hg = 1 + max(Nd->Rt->Rt->Lt ? Nd->Rt->Rt->Lt->Hg : 0, Nd->Rt->Rt->Rt ? Nd->Rt->Rt->Rt->Hg : 0);\n Nd->Rt->Rt->Sz = Nd->Rt->Rt->count + (Nd->Rt->Rt->Lt ? Nd->Rt->Rt->Lt->Sz : 0) + (Nd->Rt->Rt->Rt ? Nd->Rt->Rt->Rt->Sz : 0);\n Tr_Nd<T>* x = Nd->Rt;Nd->Rt = x->Lt;x->Lt = Nd;Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0);x->Hg = 1 + max(x->Lt ? x->Lt->Hg : 0, x->Rt ? x->Rt->Hg : 0);\n x->Sz = x->count + (x->Lt ? x->Lt->Sz : 0) + (x->Rt ? x->Rt->Sz : 0);return x;}}return Nd;\n }\n Tr_Nd<T>* BND(Tr_Nd<T>* Nd) {if (!Nd) return Nd;Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0);int32_t balance = (Nd->Lt ? Nd->Lt->Hg : 0) - (Nd->Rt ? Nd->Rt->Hg : 0);\n if (balance > 1) {if ((Nd->Lt->Rt ? Nd->Lt->Rt->Hg : 0) > (Nd->Lt->Lt ? Nd->Lt->Lt->Hg : 0)) {\n Tr_Nd<T>* y = Nd->Lt->Rt;Nd->Lt->Rt = y->Lt;y->Lt = Nd->Lt;Nd->Lt = y;\n Nd->Lt->Lt->Hg = 1 + max(Nd->Lt->Lt->Lt ? Nd->Lt->Lt->Lt->Hg : 0, Nd->Lt->Lt->Rt ? Nd->Lt->Lt->Rt->Hg : 0);\n Nd->Lt->Lt->Sz = Nd->Lt->Lt->count + (Nd->Lt->Lt->Lt ? Nd->Lt->Lt->Lt->Sz : 0) + (Nd->Lt->Lt->Rt ? Nd->Lt->Lt->Rt->Sz : 0);\n }Tr_Nd<T>* x = Nd->Lt;Nd->Lt = x->Rt;x->Rt = Nd;Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0);x->Hg = 1 + max(x->Lt ? x->Lt->Hg : 0, x->Rt ? x->Rt->Hg : 0);\n x->Sz = x->count + (x->Lt ? x->Lt->Sz : 0) + (x->Rt ? x->Rt->Sz : 0);return x;}if (balance < -1) {\n if ((Nd->Rt->Lt ? Nd->Rt->Lt->Hg : 0) > (Nd->Rt->Rt ? Nd->Rt->Rt->Hg : 0)) {Tr_Nd<T>* y = Nd->Rt->Lt;Nd->Rt->Lt = y->Rt;\n y->Rt = Nd->Rt;Nd->Rt = y;Nd->Rt->Rt->Hg = 1 + max(Nd->Rt->Rt->Lt ? Nd->Rt->Rt->Lt->Hg : 0, Nd->Rt->Rt->Rt ? Nd->Rt->Rt->Rt->Hg : 0);\n Nd->Rt->Rt->Sz = Nd->Rt->Rt->count + (Nd->Rt->Rt->Lt ? Nd->Rt->Rt->Lt->Sz : 0) + (Nd->Rt->Rt->Rt ? Nd->Rt->Rt->Rt->Sz : 0);}\n Tr_Nd<T>* x = Nd->Rt;Nd->Rt = x->Lt;x->Lt = Nd;Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0);x->Hg = 1 + max(x->Lt ? x->Lt->Hg : 0, x->Rt ? x->Rt->Hg : 0);\n x->Sz = x->count + (x->Lt ? x->Lt->Sz : 0) + (x->Rt ? x->Rt->Sz : 0);return x;} return Nd;\n }\n int32_t Gt_Sz(Tr_Nd<T>* node) { return node ? node->Sz : 0; }\n Tr_Nd<T>* insert(Tr_Nd<T>* node, T key) { if (node == nullptr) { cnt++; return new Tr_Nd<T>(key); }\n if (key < node->key) node->Lt = insert(node->Lt, key);\n else if (key > node->key) node->Rt = insert(node->Rt, key);\n else { node->count++; cnt++; node->Sz++; return node;}return BN(node, key);\n }\n Tr_Nd<T>* minValueNode(Tr_Nd<T>* node) { Tr_Nd<T>* current = node;\n while (current && current->Lt != nullptr) current = current->Lt; return current;\n }\n Tr_Nd<T>* deleteNode(Tr_Nd<T>* root, T key) { if (root == nullptr) return root;\n if (key < root->key) root->Lt = deleteNode(root->Lt, key); else if (key > root->key)\n root->Rt = deleteNode(root->Rt, key); else {if (root->count > 1) {root->count--; cnt--;\n root->Sz;} else if (root->Lt == nullptr || root->Rt == nullptr) {\n Tr_Nd<T>* temp = root->Lt ? root->Lt : root->Rt; if (temp == nullptr) {temp = root; root = nullptr;}\n else { *root = *temp; } delete temp; cnt--;} else { Tr_Nd<T>* temp = minValueNode(root->Rt);\n root->key = temp->key; root->count = temp->count; root->Sz = temp->Sz;\n root->Rt = deleteNode(root->Rt, temp->key);} } if (root == nullptr) return root; return BND(root);\n }\n Tr_Nd<T>* searchKey(Tr_Nd<T>* node, T key) const{ if (node == nullptr) return nullptr; if (node->key == key) return node;\n else if (key < node->key) return searchKey(node->Lt, key); else return searchKey(node->Rt, key);\n }\n void updateFirst() { first = minValueNode(root)->key; }\n void updateLast() { Tr_Nd<T>* current = root; while (current && current->Rt != nullptr)\n current = current->Rt; last = current->key;\n }\n vector<T> inNod; void inorder(Tr_Nd<T>* node) { if(node == nullptr) return;inorder(node->Lt);\n for(int32_t i = 0; i < node->count; i++) inNod.emplace_back(node->key); inorder(node->Rt);\n }\n Tr_Nd<T>* FBO(Tr_Nd<T>* node, int32_t ord) { if (node == nullptr || ord <= 0 || ord > Gt_Sz(node)) return nullptr;\n int32_t Ltt_count = Gt_Sz(node->Lt); if (ord <= Ltt_count) return FBO(node->Lt, ord);\n if (ord <= Ltt_count + node->count) return node; return FBO(node->Rt, ord - Ltt_count - node->count);\n }\n int32_t OOK(Tr_Nd<T>* node, T key) { if (node == nullptr) return 0; if (key < node->key) return OOK(node->Lt, key);\n else if (key > node->key) return Gt_Sz(node->Lt) + node->count + OOK(node->Rt, key);\n else return Gt_Sz(node->Lt);//Lt swap Rt and '<' swap '>' for greater equal\n }\n Tr_Nd<T>* LB(Tr_Nd<T>* node, T key){ Tr_Nd<T>* res = nullptr; while (node != nullptr)\n if (node->key >= key) res = node, node = node->Lt; else node = node->Rt; return res;\n }\n Tr_Nd<T>* UB(Tr_Nd<T>* node, T key){ Tr_Nd<T>* res = nullptr; while (node != nullptr)\n if (node->key > key) res = node, node = node->Lt; else node = node->Rt; return res;\n }\n void clear(Tr_Nd<T>* node) {if (node) { clear(node->Lt); clear(node->Rt);delete node;}}\npublic:\n mset() : root(nullptr) {} class Iterate {\n vector<T>* nodes; size_t index;\n public:\n Iterate(vector<T>* nodes, size_t index) : nodes(nodes), index(index) {}\n bool operator!=(const Iterate& other) const { return index != other.index; }\n T operator->() { return (*nodes)[index];} T operator*() const { return (*nodes)[index];}\n Iterate operator++(int32_t) { index++; return *this; } Iterate& operator++() {++index;return *this;}\n Iterate operator+(size_t n) const { return Iterate(nodes, index + n);}\n Iterate operator-(size_t n) const { return Iterate(nodes, index - n);} friend class mset<T>;\n };\n bool find(T key) const {return searchKey (root, key) ? true : false;}\n void insert(T key) { root = insert(root, key); first = min<T>(first, key); last = max<T>(last, key);}\n void erase(T key) { root = deleteNode(root, key); if(cnt == 0){first = numeric_limits<T>::max();\n last = numeric_limits<T>::min();} else{ if(first == key) updateFirst(); if(last == key) updateLast();} }\n friend mset<T>& operator <= (mset<T>& a, int32_t n) { T x; while(n--) { cin >> x; a.insert(x);} return a;}\n friend ostream& operator << (ostream& o, mset<T>& a) { for(auto i : a) o << i << ' '; o << '\\n'; return o;}\n friend mset<T>& operator <= (mset<T>& a, mset<T>& b) {a.clear(); for(auto i : b) a.insert(i); return a;}\n Iterate begin() { inNod.clear();inorder(root);return Iterate(&inNod, 0);}\n void clear(){clear(root);root=nullptr; cnt = 0; T first=numeric_limits<T>::max();T last=numeric_limits<T>::min();}\n Iterate end() { return Iterate(&inNod, inNod.size());} int32_t size() const { return cnt; }\n T front() const{ return first; } T back() const{ return last; }\n auto lower_bound(T key) { return LB(root, key); } auto upper_bound(T key) { return UB(root, key); }\n auto find_by_order(int32_t ord){ return FBO(root, ord);} int32_t order_of_key(T key) { return OOK(root, key);}\n};\n vector<int> resultsArray(vector<vector<int>>& ar, int k) {\n mset<int> m;\n int n = ar.size();\n vector<int> ra(n);\n for(int i = 0; i < n; i++) {\n m.insert(abs(ar[i][0]) + abs(ar[i][1]));\n if(m.size() >= k) {\n ra[i] = m.find_by_order(k)->key;\n } \n else {\n ra[i] = -1;\n }\n }\n return ra;\n }\n};", "memory": "284468" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n template<typename T> struct Tr_Nd {\n T key; int32_t Hg, count, Sz; Tr_Nd* Lt; Tr_Nd* Rt;\n Tr_Nd(T x) : key(x), Lt(nullptr), Rt(nullptr), Hg(1), count(1), Sz(1) {}\n};\ntemplate<typename T> class mset {\n Tr_Nd<T>* root; int32_t cnt = 0; T first = numeric_limits<T>::max(); T last = numeric_limits<T>::min();\n Tr_Nd<T>* BN(Tr_Nd<T>* Nd, T key) {if (!Nd) return new Tr_Nd<T>(key);Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0); int32_t balance = (Nd->Lt ? Nd->Lt->Hg : 0) - (Nd->Rt ? Nd->Rt->Hg : 0);\n if (balance > 1) { if (key < Nd->Lt->key) { Tr_Nd<T>* x = Nd->Lt; Nd->Lt = x->Rt; x->Rt = Nd;\n Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0);x->Hg = 1 + max(x->Lt ? x->Lt->Hg : 0, x->Rt ? x->Rt->Hg : 0);\n x->Sz = x->count + (x->Lt ? x->Lt->Sz : 0) + (x->Rt ? x->Rt->Sz : 0);return x;} else {Tr_Nd<T>* y = Nd->Lt->Rt;Nd->Lt->Rt = y->Lt;\n y->Lt = Nd->Lt;Nd->Lt = y;Nd->Lt->Lt->Hg = 1 + max(Nd->Lt->Lt->Lt ? Nd->Lt->Lt->Lt->Hg : 0, Nd->Lt->Lt->Rt ? Nd->Lt->Lt->Rt->Hg : 0);\n Nd->Lt->Lt->Sz = Nd->Lt->Lt->count + (Nd->Lt->Lt->Lt ? Nd->Lt->Lt->Lt->Sz : 0) + (Nd->Lt->Lt->Rt ? Nd->Lt->Lt->Rt->Sz : 0);\n Tr_Nd<T>* x = Nd->Lt;Nd->Lt = x->Rt;x->Rt = Nd;Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0);x->Hg = 1 + max(x->Lt ? x->Lt->Hg : 0, x->Rt ? x->Rt->Hg : 0);\n x->Sz = x->count + (x->Lt ? x->Lt->Sz : 0) + (x->Rt ? x->Rt->Sz : 0);return x;}}if (balance < -1) {if (key > Nd->Rt->key) {\n Tr_Nd<T>* y = Nd->Rt;Nd->Rt = y->Lt;y->Lt = Nd;Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0);y->Hg = 1 + max(y->Lt ? y->Lt->Hg : 0, y->Rt ? y->Rt->Hg : 0);\n y->Sz = y->count + (y->Lt ? y->Lt->Sz : 0) + (y->Rt ? y->Rt->Sz : 0);return y;} else {\n Tr_Nd<T>* y = Nd->Rt->Lt;Nd->Rt->Lt = y->Rt;y->Rt = Nd->Rt; Nd->Rt = y;\n Nd->Rt->Rt->Hg = 1 + max(Nd->Rt->Rt->Lt ? Nd->Rt->Rt->Lt->Hg : 0, Nd->Rt->Rt->Rt ? Nd->Rt->Rt->Rt->Hg : 0);\n Nd->Rt->Rt->Sz = Nd->Rt->Rt->count + (Nd->Rt->Rt->Lt ? Nd->Rt->Rt->Lt->Sz : 0) + (Nd->Rt->Rt->Rt ? Nd->Rt->Rt->Rt->Sz : 0);\n Tr_Nd<T>* x = Nd->Rt;Nd->Rt = x->Lt;x->Lt = Nd;Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0);x->Hg = 1 + max(x->Lt ? x->Lt->Hg : 0, x->Rt ? x->Rt->Hg : 0);\n x->Sz = x->count + (x->Lt ? x->Lt->Sz : 0) + (x->Rt ? x->Rt->Sz : 0);return x;}}return Nd;\n }\n Tr_Nd<T>* BND(Tr_Nd<T>* Nd) {if (!Nd) return Nd;Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0);int32_t balance = (Nd->Lt ? Nd->Lt->Hg : 0) - (Nd->Rt ? Nd->Rt->Hg : 0);\n if (balance > 1) {if ((Nd->Lt->Rt ? Nd->Lt->Rt->Hg : 0) > (Nd->Lt->Lt ? Nd->Lt->Lt->Hg : 0)) {\n Tr_Nd<T>* y = Nd->Lt->Rt;Nd->Lt->Rt = y->Lt;y->Lt = Nd->Lt;Nd->Lt = y;\n Nd->Lt->Lt->Hg = 1 + max(Nd->Lt->Lt->Lt ? Nd->Lt->Lt->Lt->Hg : 0, Nd->Lt->Lt->Rt ? Nd->Lt->Lt->Rt->Hg : 0);\n Nd->Lt->Lt->Sz = Nd->Lt->Lt->count + (Nd->Lt->Lt->Lt ? Nd->Lt->Lt->Lt->Sz : 0) + (Nd->Lt->Lt->Rt ? Nd->Lt->Lt->Rt->Sz : 0);\n }Tr_Nd<T>* x = Nd->Lt;Nd->Lt = x->Rt;x->Rt = Nd;Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0);x->Hg = 1 + max(x->Lt ? x->Lt->Hg : 0, x->Rt ? x->Rt->Hg : 0);\n x->Sz = x->count + (x->Lt ? x->Lt->Sz : 0) + (x->Rt ? x->Rt->Sz : 0);return x;}if (balance < -1) {\n if ((Nd->Rt->Lt ? Nd->Rt->Lt->Hg : 0) > (Nd->Rt->Rt ? Nd->Rt->Rt->Hg : 0)) {Tr_Nd<T>* y = Nd->Rt->Lt;Nd->Rt->Lt = y->Rt;\n y->Rt = Nd->Rt;Nd->Rt = y;Nd->Rt->Rt->Hg = 1 + max(Nd->Rt->Rt->Lt ? Nd->Rt->Rt->Lt->Hg : 0, Nd->Rt->Rt->Rt ? Nd->Rt->Rt->Rt->Hg : 0);\n Nd->Rt->Rt->Sz = Nd->Rt->Rt->count + (Nd->Rt->Rt->Lt ? Nd->Rt->Rt->Lt->Sz : 0) + (Nd->Rt->Rt->Rt ? Nd->Rt->Rt->Rt->Sz : 0);}\n Tr_Nd<T>* x = Nd->Rt;Nd->Rt = x->Lt;x->Lt = Nd;Nd->Hg = 1 + max(Nd->Lt ? Nd->Lt->Hg : 0, Nd->Rt ? Nd->Rt->Hg : 0);\n Nd->Sz = Nd->count + (Nd->Lt ? Nd->Lt->Sz : 0) + (Nd->Rt ? Nd->Rt->Sz : 0);x->Hg = 1 + max(x->Lt ? x->Lt->Hg : 0, x->Rt ? x->Rt->Hg : 0);\n x->Sz = x->count + (x->Lt ? x->Lt->Sz : 0) + (x->Rt ? x->Rt->Sz : 0);return x;} return Nd;\n }\n int32_t Gt_Sz(Tr_Nd<T>* node) { return node ? node->Sz : 0; }\n Tr_Nd<T>* insert(Tr_Nd<T>* node, T key) { if (node == nullptr) { cnt++; return new Tr_Nd<T>(key); }\n if (key < node->key) node->Lt = insert(node->Lt, key);\n else if (key > node->key) node->Rt = insert(node->Rt, key);\n else { node->count++; cnt++; node->Sz++; return node;}return BN(node, key);\n }\n Tr_Nd<T>* FBO(Tr_Nd<T>* node, int32_t ord) { if (node == nullptr || ord <= 0 || ord > Gt_Sz(node)) return nullptr;\n int32_t Ltt_count = Gt_Sz(node->Lt); if (ord <= Ltt_count) return FBO(node->Lt, ord);\n if (ord <= Ltt_count + node->count) return node; return FBO(node->Rt, ord - Ltt_count - node->count);\n }\npublic:\n mset() : root(nullptr) {}\n void insert(T key) { root = insert(root, key); first = min<T>(first, key); last = max<T>(last, key);}\n auto find_by_order(int32_t ord){ return FBO(root, ord);} int32_t order_of_key(T key) { return OOK(root, key);}\n int size() const {return cnt;}\n};\n vector<int> resultsArray(vector<vector<int>>& ar, int k) {\n mset<int> m;\n int n = ar.size();\n vector<int> ra(n);\n for(int i = 0; i < n; i++) {\n m.insert(abs(ar[i][0]) + abs(ar[i][1]));\n if(m.size() >= k) {\n ra[i] = m.find_by_order(k)->key;\n } \n else {\n ra[i] = -1;\n }\n }\n return ra;\n }\n};", "memory": "284468" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "struct AVLNode {\n int value, subtreeSize;\n AVLNode *left, *right;\n\n AVLNode() {}\n\n AVLNode(int Value, \n int SubtreeSize = 1, \n AVLNode *Left = nullptr, \n AVLNode *Right = nullptr\n ): value(Value), subtreeSize(SubtreeSize), left(Left), right(Right) {}\n};\n\nclass AVLTree {\npublic:\n AVLNode *root;\n\n int GetSize(AVLNode *node) {\n return (node ? node->subtreeSize : 0);\n }\n\n void UpdateSize(AVLNode *node) {\n if (node) {\n node->subtreeSize = GetSize(node->left) + GetSize(node->right) + 1;\n }\n }\n\n AVLNode* RotateLeft(AVLNode *root) {\n AVLNode *newRoot = root->right;\n root->right = newRoot->left;\n newRoot->left = root;\n UpdateSize(root);\n UpdateSize(newRoot);\n return newRoot;\n }\n\n AVLNode* RotateRight(AVLNode *root) {\n AVLNode *newRoot = root->left;\n root->left = newRoot->right;\n newRoot->right = root;\n UpdateSize(root);\n UpdateSize(newRoot);\n return newRoot;\n }\n\n AVLNode* Insert(AVLNode *nowAt, int value) {\n if (!nowAt) {\n return new AVLNode(value);\n }\n if (value < nowAt->value) {\n nowAt->left = Insert(nowAt->left, value);\n }\n else {\n nowAt->right = Insert(nowAt->right, value);\n }\n UpdateSize(nowAt);\n int balance = GetSize(nowAt->left) - GetSize(nowAt->right);\n if (balance > 1) {\n return RotateRight(nowAt);\n }\n else if (balance < -1) {\n return RotateLeft(nowAt);\n }\n return nowAt;\n }\n\n\n AVLTree(): root(nullptr) {}\n\n int size() {\n return this->GetSize(root);\n }\n\n void insert(int value) {\n this->root = this->Insert(this->root, value);\n }\n \n int FindKth(AVLNode *nowAt, int k) {\n if (GetSize(nowAt->left) + 1 == k) {\n return nowAt->value;\n }\n if (GetSize(nowAt->left) + 1 > k) {\n return FindKth(nowAt->left, k);\n }\n return FindKth(nowAt->right, k - GetSize(nowAt->left) - 1);\n }\n};\n\nclass Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n AVLTree AVL;\n int nowDistance;\n vector <int> result(queries.size());\n for (int i = 0; i != queries.size(); ++i) {\n nowDistance = abs(queries[i][0]) + abs(queries[i][1]);\n AVL.insert(nowDistance);\n if (i + 1 < k) {\n result[i] = -1;\n }\n else {\n result[i] = AVL.FindKth(AVL.root, k);\n }\n }\n return result;\n }\n};", "memory": "285301" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n multiset<int> st;\n priority_queue<int> pq;\n vector<int> ans;\n for(int i=0;i<min(k-1,(int)queries.size());i++){\n ans.push_back(-1);\n pq.push(abs(queries[i][0])+abs(queries[i][1]));\n }\n if(k==1){\n for(int i=k-1;i<(int)queries.size();i++){\n int cur=abs(queries[i][0])+abs(queries[i][1]);\n st.insert(cur);\n ans.push_back(*st.begin());\n \n }\n }\n else{\n for(int i=k-1;i<(int)queries.size();i++){\n auto val=pq.top();\n int cur=abs(queries[i][0])+abs(queries[i][1]);\n if(cur<val){\n pq.pop();\n pq.push(cur);\n ans.push_back(val);\n st.insert(val);\n }\n else{\n st.insert(cur);\n ans.push_back(*st.begin());\n }\n }\n }\n \n return ans;\n }\n};", "memory": "286135" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int>anse(queries.size(),-1);\n if(k>queries.size()){\n return anse;\n }\n priority_queue<int>pq;\n vector<int>ans(queries.size());\n int ind=0;\n for(vector<int>it:queries){\n int dist=abs(it[0])+abs(it[1]);\n pq.push(dist);\n if(pq.size()<k){\n ans[ind++]=-1;\n }else{\n if(pq.size()>k){\n pq.pop();\n }\n ans[ind++]=pq.top();\n }\n }\n return ans;\n\n }\n};", "memory": "286135" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<long> pq;\n int n = queries.size();\n vector<int> ans(n, -1);\n if(k > n) return ans;\n for(int i=0; i<n; i++){\n auto q = queries[i];\n long d = abs(q[0]) + abs(q[1]);\n // if(pq.size() < k){\n pq.push(d);\n // }\n // else{\n while(pq.size() > k){\n pq.pop();\n }\n if(pq.size() == k) ans[i] = pq.top();\n \n }\n return ans;\n }\n};", "memory": "286969" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<long> pq;\n int n = queries.size();\n vector<int> ans(n, -1);\n if(k > n) return ans;\n for(int i=0; i<n; i++){\n auto q = queries[i];\n long d = abs(q[0]) + abs(q[1]);\n pq.push(d);\n if(pq.size() > k){\n pq.pop();\n }\n if(pq.size() == k) ans[i] = pq.top(); \n }\n return ans;\n }\n};", "memory": "287803" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "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 \n priority_queue<long long>s;\n for(int i=0;i<n;i++){\n auto it=queries[i];\n s.push(abs(it[0])+abs(it[1]));\n \n if(s.size()>k)\n s.pop();\n if(s.size()==k)ans[i]=(s.top());\n }\n return ans;\n }\n};", "memory": "287803" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<int,vector<int>>pq;\n vector<int>res;\n for(auto it:queries){\n int x=it[0];\n int y=it[1];\n if(pq.empty()||pq.size()<k){\n pq.push(abs(x)+abs(y));\n }\n else{\n if(pq.top()>abs(x)+abs(y)){\n pq.pop();\n pq.push(abs(x)+abs(y));\n }\n }\n if(pq.size()<k){\n res.push_back(-1);\n }\n else res.push_back(pq.top());\n }\n return res;\n }\n};", "memory": "288636" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n=queries.size();\n\n priority_queue<int> pq;\n vector<int> ans;\n for(auto query:queries){\n int dist=abs(query[0]) + abs(query[1]);\n\n while(pq.size()>=k && dist<pq.top()){\n pq.pop();\n }\n\n if(pq.size()<k)\n pq.push(dist);\n\n if(pq.size()==k)\n ans.push_back(pq.top());\n else\n ans.push_back(-1);\n }\n\n return ans;\n }\n};", "memory": "288636" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n std::ios_base::sync_with_stdio(false);std::cin.tie(nullptr);\n int n=queries.size();\n priority_queue<int>pq;\n vector<int>result;\n for(auto it:queries){\n int dist=abs(it[0])+abs(it[1]);\n\n if(pq.size() < k){\n pq.push(dist);\n }\n else if(pq.top() > dist){\n pq.pop();\n pq.push(dist);\n }\n\n if(pq.size()<k) result.push_back(-1);\n else result.push_back(pq.top());\n }\n return result;\n }\n};", "memory": "289470" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\nclass Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n vector<int> res;\n\n for(auto q:queries){\n int a=abs(q[0]), b=abs(q[1]);\n int lo=0,hi=res.size();\n int x=a+b;\n res.push_back(x); \n }\n\n sort(res.begin(),res.end());\n\n // for(auto a:res) cout<<a<<\" \";\n // cout<<\"\\n\";\n\n int vi=k-1;\n int n=res.size();\n vector<int> ans(n);\n vector<int> dead(n,0);\n\n \n for(int i=0;i<n;i++){\n while(vi<n and dead[vi]) vi++;\n\n if(vi>=n) ans[n-1-i]=-1;\n else ans[n-1-i]=res[vi];\n\n\n int qi=n-1-i;\n int qres=abs(queries[qi][0])+abs(queries[qi][1]);\n int x=lower_bound(res.begin(),res.end(),qres)-res.begin();\n // if(x+1<n) res[x]=res[x+1];\n\n // cout<<qres<<\" -qres\\n\";\n // for(auto a:res) cout<<a<<\" \";\n // cout<<\"\\n\";\n // cout<<x<<\" \"<<vi<<\" -x vi\\n\";\n\n while(x<n and dead[x]) x++;\n\n if(x<=vi) vi++;\n if(x<n) dead[x]=1;\n\n // for(auto a:res) cout<<a<<\" \";\n // cout<<\"\\n\";\n // for(auto a:dead) cout<<a<<\" \";\n // cout<<\"\\n\";\n // cout<<vi<<\" -vi\\n\";\n }\n\n return ans;\n }\n};", "memory": "289470" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> ans;\n priority_queue<int> pq;\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n for(auto x: queries){\n int dist= abs(x[0])+ abs(x[1]);\n if(pq.size()<k) pq.push(dist);\n else if(dist < pq.top()){\n pq.pop();\n pq.push(dist);\n }\n if(pq.size()<k) ans.push_back(-1);\n else ans.push_back(pq.top());\n }\n return ans;\n }\n};", "memory": "290304" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 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>v;\n for(auto x:q){\n v.push_back(abs(x[0])+abs(x[1]));\n }\n priority_queue<int>pq;\n vector<int>ans(q.size());\n for(int i=0;i<q.size();++i){\n pq.push(v[i]);\n if(pq.size()>k)pq.pop();\n if(pq.size()==k)ans[i] = pq.top();\n else ans[i] = -1;\n }\n return ans;\n }\n};", "memory": "291138" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#define piii pair<int, pair<int, int>>\nstruct myCmp{\n bool operator()(piii& a, piii& b){\n return a.first < b.first;\n }\n};\nclass Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n int n = queries.size();\n priority_queue<piii, vector<piii>, myCmp> pq;\n vector<int> res;\n\n for(int i=0; i<n; i++){\n auto t = queries[i];\n int dist = abs(t[0]) + abs(t[1]);\n if(pq.size() < k){\n pq.push({dist, {t[0], t[1]}});\n if(pq.size() == k) res.push_back(pq.top().first);\n else res.push_back(-1);\n continue;\n }\n if(pq.top().first > dist){\n pq.pop();\n pq.push({dist, {t[0], t[1]}});\n }\n res.push_back(pq.top().first);\n }\n\n return res;\n }\n};", "memory": "293639" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) \n {\n priority_queue< pair<int,pair<int,int>> > q;\n vector<int> ans;\n\n for(auto query : queries)\n {\n int x = query[0];\n int y = query[1];\n\n int dist = abs(x) + abs(y);\n q.push({dist,{x,y}});\n\n if(q.size()>k)\n {\n q.pop();\n }\n\n if(q.size()==k)\n ans.push_back(q.top().first);\n else\n ans.push_back(-1);\n } \n\n return ans;\n }\n};", "memory": "294473" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 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<pair<int, pair<int, int>>> pq;\n vector<int>ans;\n for(auto point: queries)\n {\n int dist = abs(point[0]) + abs(point[1]);\n pq.push({dist, {point[0], point[1]}});\n\n if(pq.size() < k)\n ans.push_back(-1);\n else if(pq.size() > k)\n {\n pq.pop();\n ans.push_back(pq.top().first);\n }\n else\n ans.push_back(pq.top().first);\n \n }\n return ans;\n }\n};", "memory": "295306" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 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<pair<int, pair<int, int>>> distances; // (distance, (x, y))\n vector<int> result;\n for (auto query : queries) {\n auto x = query[0]; auto y = query[1];\n auto query_distance = abs(x) + abs(y);\n \n if (distances.size() < k) {\n distances.push({query_distance, {x, y}});\n } else if (distances.top().first > query_distance) {\n distances.pop();\n distances.push({query_distance, {x, y}});\n } else {\n // do nothing\n }\n\n if (distances.size() == k) {\n result.push_back(distances.top().first);\n } else {\n result.push_back(-1);\n }\n }\n\n return result;\n }\n};", "memory": "295306" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "struct Obstacle\n{\n int x, y;\n int d;\n\n Obstacle(int _x, int _y)\n {\n x = _x;\n y = _y;\n d = abs(x) + abs(y);\n }\n};\n\nlong distance(Obstacle &a)\n{\n return abs(a.x) + abs(a.y);\n}\n\nbool operator <(Obstacle a, Obstacle b)\n{\n return distance(a) < distance(b);\n}\n\nclass Solution\n{\n private:\n\n public:\n\n vector<int> resultsArray(vector<vector<int>> &a, int k)\n {\n vector<int> distances;\n priority_queue<Obstacle> Q;\n\n for(auto query : a)\n {\n int x = query[0];\n int y = query[1];\n Obstacle obstacle(x, y);\n Q.push(obstacle);\n\n if(Q.size() < k)\n {\n distances.push_back(-1);\n }\n else if(Q.size() == k)\n {\n Obstacle top = Q.top();\n distances.push_back(top.d);\n }\n else\n {\n Q.pop();\n Obstacle top = Q.top();\n distances.push_back(top.d);\n }\n }\n\n return distances;\n }\n};", "memory": "296140" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 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> nums;\n for(auto a:queries) {\n int sum = abs(a[0]);\n sum += abs(a[1]);\n\n nums.push_back(sum);\n }\n\n priority_queue<int> maxHeap;\n vector<int> ans;\n\n for(int i=0; i<nums.size(); i++) {\n if(maxHeap.size() < k) \n maxHeap.push(nums[i]);\n else if(maxHeap.size() == k && nums[i] <= maxHeap.top()) {\n maxHeap.push(nums[i]);\n maxHeap.pop();\n }\n\n\n if(maxHeap.size() == k) ans.push_back(maxHeap.top());\n else ans.push_back(-1);\n }\n\n return ans;\n }\n};", "memory": "296140" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 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> max_heap;\n unordered_set<int> distance_set;\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 if (max_heap.size() < k) {\n max_heap.push(distance);\n distance_set.insert(distance);\n } else {\n if (distance < max_heap.top()) {\n int removed_distance = max_heap.top();\n max_heap.pop();\n distance_set.erase(removed_distance);\n\n max_heap.push(distance);\n distance_set.insert(distance);\n }\n }\n if (max_heap.size() < k) {\n results.push_back(-1);\n } else {\n results.push_back(max_heap.top());\n }\n }\n\n return results;\n }\n};", "memory": "296974" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 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;\n priority_queue<int> m;\n unordered_set<int> d;\n for (const auto& query : q) {\n int x = query[0];\n int y = query[1];\n int dist = abs(x) + abs(y);\n\n if (m.size() < k) {\n m.push(dist);\n d.insert(dist);\n if(m.size()==k) {\n ans.push_back(m.top()); \n }\n else {\n ans.push_back(-1);\n }\n } else {\n if (dist < m.top()) {\n int largest = m.top();\n m.pop();\n d.erase(largest);\n\n m.push(dist);\n d.insert(dist);\n }\n ans.push_back(m.top());\n }\n }\n return ans;\n }\n};", "memory": "296974" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n set<int> st;\n vector<pair<int,int>> v;\n int i = 0;\n int n = queries.size();\n priority_queue<int> pq;\n for(auto it: queries){\n pq.push(abs(it[0])+abs(it[1]));\n if(pq.size()>k) pq.pop();\n if(pq.size()==k){\n v.push_back({i,pq.top()});\n }\n else v.push_back({i,-1});\n i++;\n }\n vector<int> v1(n,-1);\n for(auto it: v){\n if(it.second!=-1){\n v1[it.first] = it.second;\n }\n }\n return v1;\n }\n};", "memory": "297808" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#define ll long long\nclass Solution {\n\n ll dist(vector<int> &point) {\n return abs(point[0]) + abs(point[1]);\n }\n\n\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<pair<ll,ll>> small;\n \n int idx = 0;\n vector<int> ans;\n for(auto q:queries) {\n small.push({dist(q), idx} );\n while(small.size()>k) {\n small.pop();\n }\n if(small.size()==k)\n ans.push_back(small.top().first);\n else\n ans.push_back(-1);\n idx++;\n }\n return ans;\n }\n};", "memory": "298641" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // long long get_dis(long long a) {\n // return a * a;\n // }\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n priority_queue<pair<long long, int>> pq;\n vector<int> ans;\n for (int i = 0; i < queries.size(); i++) {\n vector<int> q = queries[i];\n long long dist = abs(q[0]) + abs(q[1]);\n pq.push(make_pair(dist, i));\n if (pq.size()>k) {\n pq.pop();\n }\n if (pq.size()==k) {\n ans.push_back(pq.top().first);\n }\n else {\n ans.push_back(-1); \n }\n }\n return ans;\n }\n};", "memory": "298641" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#define debug(...) debug_helper (#__VA_ARGS__, __VA_ARGS__)\n#define cerr cout\n\n//Used to print value of variable(s) at a given point in time\ntemplate <typename Arg1>\nvoid debug_helper (const char* name, Arg1&& arg1) {\n\tcerr << name << \" : \" << arg1 << endl;\n}\n\ntemplate <typename Arg1, typename... Args>\nvoid debug_helper (const char* names, Arg1&& arg1, Args&&... args) {\n\tconst char* comma = strchr (names + 1, ',');\n\tcerr.write (names, comma - names) << \" : \" << arg1 << \" | \"; debug_helper (comma + 1, args...);\n}\n\ntemplate <class T>\nvoid debug_helper (const char* name, priority_queue<T> pq) {\n\tcerr << name << endl;\n\twhile(!pq.empty()) cerr << pq.top() << \" \", pq.pop();\n\tcerr << endl;\n}\n\ntemplate <class T>\nvoid debug_helper (const char* name, priority_queue<T, vector<T>, greater<T>> pq) {\n\tcerr << name << \": \" << endl;\n\twhile(!pq.empty()) cerr << pq.top() << \" \", pq.pop();\n\tcerr << endl;\n}\n\nclass Solution {\npublic:\n priority_queue<int, vector<int>, greater<int>> minpq;\n priority_queue<int> maxpq;\n int k;\n vector<int> resultsArray(vector<vector<int>>& queries, int k1) {\n vector<int> nums;\n k = k1;\n for(auto it: queries) nums.emplace_back(abs(it[0]) + abs(it[1]));\n \n vector<int> ans;\n for(int i: nums) {\n ans.emplace_back(add(i));\n }\n \n return ans;\n }\n \n int add(int val) {\n if(!maxpq.empty() && (maxpq.size() == k)) {\n maxpq.push(val);\n minpq.push(maxpq.top());\n maxpq.pop();\n }\n else maxpq.push(val);\n \n // debug(val);\n // debug(minpq);\n // debug(maxpq);\n if(maxpq.size() == k) return maxpq.top();\n return -1;\n }\n};", "memory": "299475" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 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(), i;\n vector<int> ans(n, -1);\n if(k > n)\n return ans;\n priority_queue<vector<int>> pq;\n for(i=0;i<k; i++)\n pq.push({(abs(queries[i][0]) + abs(queries[i][1])), i});\n ans[k-1] = pq.top()[0];\n for(i = k; i < n; i++)\n {\n int dist = abs(queries[i][0]) + abs(queries[i][1]);\n if(dist >= pq.top()[0])\n {\n ans[i] = pq.top()[0];\n }\n else\n {\n pq.pop();\n pq.push({dist, i});\n ans[i] = pq.top()[0];\n }\n }\n return ans;\n }\n};", "memory": "300309" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 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>, vector<vector<int>>, cmp> pq;\n int n = queries.size();\n vector<int> result(n, -1);\n for (int i = 0; i < n; i++) {\n pq.push(queries[i]);\n if (pq.size() > k) {\n pq.pop();\n }\n if (pq.size() == k) {\n result[i] = abs(pq.top()[0]) + abs(pq.top()[1]);\n }\n }\n return result;\n }\nprivate:\n struct cmp {\n bool operator() (const vector<int>& v1, const vector<int>& v2) {\n return abs(v1[0]) + abs(v1[1]) < abs(v2[0]) + abs(v2[1]);\n } \n };\n};", "memory": "301143" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n auto queries_size = queries.size();\n if (static_cast<int>(queries_size) < k) {\n return std::vector<int>(queries_size, -1);\n }\n std::map<int,int> m;\n std::vector<int> results;\n results.reserve(queries.size());\n for(int idx=0; idx<k-1; ++idx) {\n results.push_back(-1);\n m[std::abs(queries[idx][0]) + std::abs(queries[idx][1])]++;\n }\n m[std::abs(queries[k-1][0]) + std::abs(queries[k-1][1])]++;\n results.push_back(m.rbegin()->first);\n for(int idx=k; idx<queries_size; ++idx) {\n auto dis = std::abs(queries[idx][0]) + std::abs(queries[idx][1]);\n auto back = m.rbegin()->first;\n if (back <= dis) {\n results.push_back(back);\n } else {\n auto rb = m.rbegin();\n if (rb->second == 1) {\n m.erase(rb->first);\n } else {\n rb->second--;\n }\n m[dis]++;\n results.push_back(m.rbegin()->first);\n }\n }\n\n return results;\n }\n};", "memory": "301976" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 2 * 10<sup>5</sup></code></li> <li>All <code>queries[i]</code> are unique.</li> <li><code>-10<sup>9</sup> &lt;= queries[i][0], queries[i][1] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution {\npublic:\n vector<int> resultsArray(vector<vector<int>>& queries, int k) {\n auto queries_size = queries.size();\n if (static_cast<int>(queries_size) < k) {\n return std::vector<int>(queries_size, -1);\n }\n std::map<int,int> m;\n std::vector<int> results;\n results.reserve(queries.size());\n for(int idx=0; idx<k-1; ++idx) {\n results.push_back(-1);\n m[std::abs(queries[idx][0]) + std::abs(queries[idx][1])]++;\n }\n m[std::abs(queries[k-1][0]) + std::abs(queries[k-1][1])]++;\n results.push_back(m.rbegin()->first);\n for(int idx=k; idx<queries_size; ++idx) {\n auto dis = std::abs(queries[idx][0]) + std::abs(queries[idx][1]);\n auto back = m.rbegin()->first;\n if (back <= dis) {\n results.push_back(back);\n } else {\n auto rb = m.rbegin();\n if (rb->second == 1) {\n m.erase(rb->first);\n } else {\n rb->second--;\n }\n m[dis]++;\n results.push_back(m.rbegin()->first);\n }\n }\n\n return results;\n }\n};", "memory": "302810" }