id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\nprivate:\n vector<vector<int>> grid;\n Node* retTree(int startx, int starty, int length){\n if(length == 1) return new Node(grid[startx][starty], true);\n else{\n int nxLength = length/2, endx = startx + nxLength, endy = starty + nxLength;\n Node *topLeft = retTree(startx, starty, nxLength), *topRight = retTree(startx, endy, nxLength), *bottomLeft = retTree(endx, starty, nxLength), *bottomRight = retTree(endx, endy, nxLength);\n int tlv = topLeft->val, trv = topRight->val, blv = bottomLeft->val, brv = bottomRight->val;\n bool allLeaves = (topLeft->isLeaf && topRight->isLeaf && bottomLeft->isLeaf && bottomRight->isLeaf);\n if(tlv == trv && blv == brv && tlv == blv && allLeaves) return new Node(tlv, true);\n else return new Node(tlv, false, topLeft, topRight, bottomLeft, bottomRight);\n }\n }\n\npublic:\n Node* construct(vector<vector<int>>& _grid) {\n grid = vector<vector<int>> (_grid);\n return retTree(0, 0, grid.size());\n }\n};", "memory": "23700" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size();\n return constructTree(grid, {{0, 0}, {n - 1, n - 1}});\n }\n\n Node* constructTree(vector<vector<int>>& grid, vector<vector<int>> boundary) {\n int x1 = boundary[0][0], y1 = boundary[0][1];\n int x2 = boundary[1][0], y2 = boundary[1][1];\n // cout << x1 << \" \" << y1 << endl;\n // cout << x2 << \" \" << y2 << endl << endl; \n Node* root = new Node(1, false);\n int n = (x2 - x1) + 1;\n int val = grid[x1][y1];\n bool isLeafNode = true; \n for(int i = x1; i <= x2; ++i) {\n for(int j = y1; j <= y2; ++j) {\n if(val != grid[i][j]) {\n isLeafNode = false;\n break;\n }\n }\n if(!isLeafNode) break;\n }\n if(isLeafNode) {\n // cout << x1 << \" \" << y1 << endl;\n // cout << x2 << \" \" << y2 << endl << endl;\n root->isLeaf = true;\n root->val = val;\n return root;\n }\n // cout << \"topLeft => \" << x1 << \" \" << y1 << \" \" << (x1 + (n >> 1) - 1) << \" \" << (y1 + (n >> 1) - 1) << endl;\n // cout << \"topRight => \" << x1 << \" \" << (y1 + (n >> 1)) << \" \" << (x1 + (n >> 1)) << \" \" << (y2) << endl;\n // cout << \"bottomLeft => \" << (x1 + (n >> 1)) << \" \" << y1 << \" \" << x2 << \" \" << (y1 + (n >> 1) - 1) << endl;\n // cout << \"bottomRight => \" << (x1 + (n >> 1)) << \" \" << (y1 + (n >> 1)) << \" \" << x2 << \" \" << y2 << endl;\n\n // cout << endl;\n\n root->topLeft = constructTree(grid, {{x1, y1}, {x1 + (n >> 1) - 1, y1 + (n >> 1) - 1}});\n root->topRight = constructTree(grid, {{x1, y1 + (n >> 1)}, {x1 + (n >> 1) - 1, y2}});\n root->bottomLeft = constructTree(grid, {{x1 + (n >> 1), y1}, {x2, y1 + (n >> 1) - 1}});\n root->bottomRight = constructTree(grid, {{x1 + (n >> 1), y1 + (n >> 1)}, {x2, y2}});\n root->isLeaf = false;\n return root;\n }\n};", "memory": "23800" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n return construct(grid, 0, grid.size() - 1, 0, grid[0].size() - 1);\n }\n\nprivate:\n Node* construct(vector<vector<int>>& grid, int x0, int x1, int y0, int y1)\n {\n if(x0 == x1) return new Node(grid[x0][y0] == 1, true);\n Node* root = new Node();\n Node* topLeft = construct(grid, x0, (x1 + x0) / 2, y0, (y0 + y1) / 2);\n Node* bottomLeft = construct(grid, (x1 + x0) / 2 + 1, x1, y0, (y0 + y1) / 2);\n Node* topRight = construct(grid, x0, (x1 + x0) / 2, (y0 + y1) / 2 + 1, y1);\n Node* bottomRight = construct(grid, (x1 + x0) / 2 + 1, x1, (y0 + y1) / 2 + 1, y1);\n if(topLeft->isLeaf && topRight->isLeaf && bottomLeft->isLeaf && bottomRight->isLeaf && topLeft->val == topRight->val && topRight->val == bottomLeft->val && bottomLeft->val == bottomRight->val)\n {\n root->val = topLeft->val;\n root->isLeaf = true;\n delete topLeft;\n delete topRight;\n delete bottomLeft;\n delete bottomRight;\n }\n else\n {\n root->isLeaf = false;\n root->topLeft = topLeft;\n root->topRight = topRight;\n root->bottomLeft = bottomLeft;\n root->bottomRight = bottomRight;\n }\n return root;\n }\n};", "memory": "23900" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n \n return dfs(grid, 0, 0, grid.size());\n }\nprivate:\n Node* dfs(vector<vector<int>>& grid, int row, int col, int size)\n {\n if(size == 1)\n {\n return new Node(grid[row][col] == 1, true);\n }\n\n int newsize = size / 2;\n\n Node* topLeft = dfs(grid, row, col, newsize);\n Node* topRight = dfs(grid, row, col + newsize, newsize);\n Node* bottomLeft = dfs(grid, row + newsize, col, newsize);\n Node* bottomRight = dfs(grid, row + newsize, col + newsize, newsize);\n\n if(topLeft->isLeaf && topRight->isLeaf && bottomLeft->isLeaf && bottomRight->isLeaf &&\n topLeft->val == topRight->val && topRight->val == bottomLeft->val && bottomLeft->val == bottomRight->val)\n {\n bool val = topLeft->val;\n\n delete topLeft;\n delete topRight;\n delete bottomLeft;\n delete bottomRight;\n\n return new Node(val, true);\n }\n \n return new Node(true, false, topLeft, topRight, bottomLeft, bottomRight);\n }\n\n};", "memory": "24000" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int n = (int) grid.size();\n return build(grid, 0, 0, n);\n }\n \n Node* build(vector<vector<int>>& grid, int i, int j, int n) {\n if(n == 1) return new Node(grid[i][j], true);\n Node* res = new Node(0, false, \n build(grid, i, j, n / 2),\n build(grid, i, j + n / 2, n / 2),\n build(grid, i + n / 2, j, n / 2),\n build(grid, i + n / 2, j + n / 2, n / 2));\n if(\n res->topLeft->isLeaf && \n res->topRight->isLeaf && \n res->bottomLeft->isLeaf && \n res->bottomRight->isLeaf && \n res->topLeft->val == res->topRight->val && \n res->topLeft->val == res->bottomLeft->val && \n res->topLeft->val == res->bottomRight->val\n ) {\n res->val = res->topLeft->val;\n res->isLeaf = true;\n delete res->topLeft;\n delete res->topRight;\n delete res->bottomLeft;\n delete res->bottomRight;\n res->topLeft = NULL;\n res->topRight = NULL;\n res->bottomLeft = NULL;\n res->bottomRight = NULL;\n }\n return res;\n }\n};", "memory": "24000" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n\n return dfs(grid, 0, 0, grid.size());\n }\nprivate:\n Node* dfs(vector<vector<int>>& grid, int row, int col, int size)\n {\n if(size == 1)\n {\n return new Node(grid[row][col] == 1, true);\n }\n\n int newsize = size / 2;\n\n Node* topLeft = dfs(grid, row, col, newsize);\n Node* topRight = dfs(grid, row, col + newsize, newsize);\n Node* bottomLeft = dfs(grid, row + newsize, col, newsize);\n Node* bottomRight = dfs(grid, row + newsize, col + newsize, newsize);\n\n if(topLeft->isLeaf && topRight->isLeaf && bottomLeft->isLeaf && bottomRight->isLeaf &&\n topLeft->val == topRight->val && topRight->val == bottomLeft->val && bottomLeft->val == bottomRight->val)\n {\n bool val = topLeft->val;\n\n delete topLeft;\n delete topRight;\n delete bottomLeft;\n delete bottomRight;\n\n return new Node(val, true);\n }\n return new Node(true, false, topLeft, topRight, bottomLeft, bottomRight);\n }\n};", "memory": "24100" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n\n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n\n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n\n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node*\n_bottomLeft, Node* _bottomRight) { val = _val; isLeaf = _isLeaf; topLeft =\n_topLeft; topRight = _topRight; bottomLeft = _bottomLeft; bottomRight =\n_bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* mergeTree(vector<vector<int>>& grid, int x, int y, int len) {\n if (len == 1)\n return new Node(grid[y][x] == 1 ? true : false, true);\n\n int half = len / 2;\n Node* topLeft = mergeTree(grid, x, y, half);\n Node* topRight = mergeTree(grid, x + half, y, half);\n Node* bottomLeft = mergeTree(grid, x, y + half, half);\n Node* bottomRight = mergeTree(grid, x + half, y + half, half);\n\n if (topLeft->isLeaf && topRight->isLeaf && bottomLeft->isLeaf &&\n bottomRight->isLeaf &&\n ((topLeft->val && topRight->val && bottomLeft->val && bottomRight->val) ||\n (!topLeft->val && !topRight->val && !bottomLeft->val && !bottomRight->val))) {\n Node* ret = new Node(topLeft->val, true);\n delete topLeft;\n delete topRight;\n delete bottomLeft;\n delete bottomRight;\n return ret;\n }\n\n return new Node(1, false, topLeft, topRight, bottomLeft, bottomRight);\n }\n Node* construct(vector<vector<int>>& grid) {\n return mergeTree(grid, 0, 0, grid.size());\n }\n};", "memory": "24200" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n \n return dfs(grid, 0, 0, grid.size());\n }\nprivate:\n Node* dfs(vector<vector<int>>& grid, int row, int col, int size)\n {\n if(size == 1)\n {\n return new Node(grid[row][col] == 1, true);\n }\n\n int newsize = size / 2;\n\n Node* topleft = dfs(grid, row, col, newsize);\n Node* topright = dfs(grid, row, col + newsize, newsize);\n Node* bottomleft = dfs(grid, row + newsize, col, newsize);\n Node* bottomright = dfs(grid, row + newsize, col + newsize, newsize);\n\n if(topleft->isLeaf && topright->isLeaf && bottomleft->isLeaf && bottomright->isLeaf &&\n topleft->val == topright->val && topright->val == bottomleft->val && bottomleft->val == bottomright->val)\n {\n bool val = topleft->val;\n\n delete topleft;\n delete topright;\n delete bottomleft;\n delete bottomright;\n\n return new Node(val, true);\n }\n\n return new Node(true, false, topleft, topright, bottomleft, bottomright);\n }\n};", "memory": "24200" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n \n return dfs(grid, 0, 0, grid.size());\n }\nprivate:\n Node* dfs(vector<vector<int>>& grid, int row, int col, int size)\n {\n if(size == 1)\n {\n return new Node(grid[row][col] == 1, true);\n }\n\n int newsize = size / 2;\n\n Node* topleft = dfs(grid, row, col, newsize);\n Node* topright = dfs(grid, row, col + newsize, newsize);\n Node* bottomleft = dfs(grid, row + newsize, col, newsize);\n Node* bottomright = dfs(grid, row + newsize, col + newsize, newsize);\n\n if(topleft->isLeaf && topright->isLeaf && bottomleft->isLeaf && bottomright->isLeaf &&\n topleft->val == topright->val && topright->val == bottomleft->val && bottomleft->val == bottomright->val)\n {\n bool val = topleft->val;\n\n delete topleft;\n delete topright;\n delete bottomleft;\n delete bottomright;\n\n return new Node(val, true);\n }\n \n return new Node(true, false, topleft, topright, bottomleft, bottomright);\n }\n\n};", "memory": "24300" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n \n return dfs(grid, 0, 0, grid.size());\n }\nprivate:\n Node* dfs(vector<vector<int>>& grid, int row, int col, int size)\n {\n if(size == 1)\n {\n return new Node(grid[row][col] == 1, true);\n }\n\n int newsize = size / 2;\n\n Node* topleft = dfs(grid, row, col, newsize);\n Node* topright = dfs(grid, row, col + newsize, newsize);\n Node* bottomleft = dfs(grid, row + newsize, col, newsize);\n Node* bottomright = dfs(grid, row + newsize, col + newsize, newsize);\n\n if(topleft->isLeaf && topright->isLeaf && bottomleft->isLeaf && bottomright->isLeaf && \n topleft->val == topright->val && topright->val == bottomleft->val && bottomleft->val == bottomright->val)\n {\n bool val = topleft->val;\n\n delete topleft;\n delete topright;\n delete bottomleft;\n delete bottomright;\n\n return new Node(val, true);\n }\n\n return new Node(true, false, topleft, topright, bottomleft, bottomright);\n }\n};", "memory": "24300" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* helper(vector<vector<int>>& grid, int i, int j, int m) {\n if(m == 1) {\n return new Node(grid[i][j], true);\n }\n Node* topLeft = helper(grid, i, j, m / 2);\n Node* topRight = helper(grid, i, j + m / 2, m / 2);\n Node* bottomLeft = helper(grid, i + m / 2, j, m / 2);\n Node* bottomRight = helper(grid, i + m / 2, j + m / 2, m / 2);\n Node* curr = new Node(grid[i][j], false);\n if(topLeft->isLeaf && topRight->isLeaf && bottomLeft->isLeaf && bottomRight->isLeaf &&\n topLeft->val == topRight->val && topRight->val == bottomLeft->val && bottomLeft->val == bottomRight->val) {\n return new Node(grid[i][j], true);\n } else {\n return new Node(grid[i][j], false, topLeft, topRight, bottomLeft, bottomRight);\n }\n }\n Node* construct(vector<vector<int>>& grid) {\n int m = grid.size();\n return helper(grid, 0, 0, m);\n }\n};", "memory": "24400" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* helper(vector<vector<int>>& grid, int i1, int i2, int j1, int j2) {\n Node* node = new Node();\n if (i1 == i2 && j1 == j2) {\n node->isLeaf = true;\n node->val = grid[i1][j1] == 1;\n return node;\n }\n int i_mid = (i1+i2)/2;\n int j_mid = (j1+j2)/2;\n node->topLeft = helper(grid, i1, i_mid, j1, j_mid);\n node->topRight = helper(grid, i1, i_mid, j_mid+1, j2);\n node->bottomLeft = helper(grid, i_mid+1, i2, j1, j_mid);\n node->bottomRight = helper(grid, i_mid+1, i2, j_mid+1, j2);\n if (node->topLeft->isLeaf && node->topRight->isLeaf && node->bottomLeft->isLeaf && node->bottomRight->isLeaf &&\n node->topLeft->val == 1 && node->topRight->val == 1 && node->bottomLeft->val == 1 && node->bottomRight->val == 1) {\n node->val = 1;\n delete(node->topLeft);\n delete(node->topRight);\n delete(node->bottomLeft);\n delete(node->bottomRight);\n node->topLeft = NULL;\n node->topRight = NULL;\n node->bottomLeft = NULL;\n node->bottomRight = NULL;\n node->isLeaf = true;\n }\n else if (node->topLeft->isLeaf && node->topRight->isLeaf && node->bottomLeft->isLeaf && node->bottomRight->isLeaf &&\n node->topLeft->val == 0 && node->topRight->val == 0 && node->bottomLeft->val == 0 && node->bottomRight->val == 0) {\n node->val = 0;\n delete(node->topLeft);\n delete(node->topRight);\n delete(node->bottomLeft);\n delete(node->bottomRight);\n node->topLeft = NULL;\n node->topRight = NULL;\n node->bottomLeft = NULL;\n node->bottomRight = NULL;\n node->isLeaf = true;\n }\n return node;\n }\n Node* construct(vector<vector<int>>& grid) {\n return helper(grid, 0, grid.size()-1, 0, grid[0].size()-1);\n }\n};", "memory": "24500" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n function<Node *(int, int, int)> construct = [&](int x, int y, int size) -> Node * {\n if (size == 1) return new Node(grid[x][y], true);\n\n size /= 2;\n\n Node *top_left = construct(x, y, size);\n Node *top_right = construct(x, y + size, size);\n Node *bottom_left = construct(x + size, y, size);\n Node *bottom_right = construct(x + size, y + size, size);\n\n int val = top_left->val;\n\n if (\n top_left->isLeaf && top_right->isLeaf && bottom_left->isLeaf && bottom_right->isLeaf &&\n val == top_left->val && val == top_right->val && val == bottom_left->val && val == bottom_right->val\n ) {\n return new Node(val, true);\n } else {\n return new Node(false, false, top_left, top_right, bottom_left, bottom_right);\n }\n };\n\n return construct(0, 0, grid.size());\n }\n};", "memory": "24600" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n \n return constructImpl(grid, 0, grid.size()-1, 0, grid.size()-1);\n }\n\n Node* constructImpl(vector<vector<int>>& grid, int rStart, int rEnd, int cStart, int cEnd) {\n\n if (cStart==cEnd) {\n return new Node(grid[rStart][cEnd], true);\n }\n\n Node* topLeft = constructImpl(grid, rStart, (rStart + rEnd)/2, cStart, (cStart + cEnd)/2);\n Node* topRight = constructImpl(grid, rStart, (rStart + rEnd)/2, (cStart + cEnd)/2 +1, cEnd);\n Node* bottomLeft = constructImpl(grid, (rStart + rEnd)/2 + 1, rEnd, cStart, (cStart + cEnd)/2);\n Node* bottomRight = constructImpl(grid, (rStart + rEnd)/2 + 1, rEnd, (cStart + cEnd)/2 +1, cEnd);\n\n if (topLeft->val == 0 && topLeft->isLeaf\n && topRight->val == 0 && topRight->isLeaf\n && bottomLeft->val == 0 && bottomLeft->isLeaf\n && bottomRight->val == 0 && bottomRight->isLeaf) {\n \n delete topLeft;\n delete topRight;\n delete bottomLeft;\n delete bottomRight;\n return new Node(0, true);\n }\n\n if (topLeft->val == 1 && topLeft->isLeaf\n && topRight->val == 1 && topRight->isLeaf\n && bottomLeft->val == 1 && bottomLeft->isLeaf\n && bottomRight->val == 1 && bottomRight->isLeaf) {\n \n delete topLeft;\n delete topRight;\n delete bottomLeft;\n delete bottomRight;\n return new Node(1, true);\n }\n\n return new Node(0, false, topLeft, topRight, bottomLeft, bottomRight);\n }\n};", "memory": "24800" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n \n return constructImpl(grid, 0, grid.size()-1, 0, grid.size()-1);\n }\n\n Node* constructImpl(vector<vector<int>>& grid, int rStart, int rEnd, int cStart, int cEnd) {\n\n if (cStart==cEnd) {\n return new Node(grid[rStart][cEnd], true);\n }\n\n Node* topLeft = constructImpl(grid, rStart, (rStart + rEnd)/2, cStart, (cStart + cEnd)/2);\n Node* topRight = constructImpl(grid, rStart, (rStart + rEnd)/2, (cStart + cEnd)/2 +1, cEnd);\n Node* bottomLeft = constructImpl(grid, (rStart + rEnd)/2 + 1, rEnd, cStart, (cStart + cEnd)/2);\n Node* bottomRight = constructImpl(grid, (rStart + rEnd)/2 + 1, rEnd, (cStart + cEnd)/2 +1, cEnd);\n\n if (topLeft->val == 0 && topLeft->isLeaf\n && topRight->val == 0 && topRight->isLeaf\n && bottomLeft->val == 0 && bottomLeft->isLeaf\n && bottomRight->val == 0 && bottomRight->isLeaf) {\n \n delete topLeft;\n delete topRight;\n delete bottomLeft;\n delete bottomRight;\n return new Node(0, true);\n }\n\n if (topLeft->val == 1 && topLeft->isLeaf\n && topRight->val == 1 && topRight->isLeaf\n && bottomLeft->val == 1 && bottomLeft->isLeaf\n && bottomRight->val == 1 && bottomRight->isLeaf) {\n \n delete topLeft;\n delete topRight;\n delete bottomLeft;\n delete bottomRight;\n return new Node(1, true);\n }\n\n return new Node(0, false, topLeft, topRight, bottomLeft, bottomRight);\n }\n};", "memory": "24800" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n return util(0, 0, grid.size(), grid);\n }\n\n Node* util(int r, int c, int size, vector<vector<int>>& grid) {\n if (size == 1) return new Node(grid[r][c], true);\n auto half = size / 2;\n auto ul = util(r, c, half, grid);\n auto ur = util(r, c + half, half, grid);\n auto dl = util(r + half, c, half, grid);\n auto dr = util(r + half, c + half, half, grid);\n\n vector<Node*> subs{ul, ur, dl, dr};\n auto val_ul = ul->val;\n bool all_same = all_of(subs.begin(), subs.end(), [&](auto sub) {\n return sub->isLeaf && sub->val == val_ul;\n });\n\n if (all_same) for_each(subs.begin(), subs.end(), [&](auto sub) { delete sub; });\n return all_same ? new Node(val_ul, true) : new Node(1, false, ul, ur, dl, dr);\n }\n};", "memory": "26100" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\n struct Limits{\n int left, right, top, bottom;\n bool operator==(const Limits& other) const noexcept{\n return left == other.left and right == other.right and top == other.top and bottom == other.bottom;\n }\n };\n struct LimitsHash{\n size_t operator()(const Limits& limits) const noexcept{\n return limits.left + 100 * limits.right + 10000 * limits.top + 1000000 * limits.bottom;\n }\n };\n unordered_map<Limits, pair<int, bool>, LimitsHash> same_values;\n pair<int, bool> same_value(const vector<vector<int>>& grid, int left, int right, int top, int bottom){\n Limits l = {left, right, top, bottom};\n auto it = same_values.find(l);\n if(it != same_values.cend()){\n return it->second;\n }\n\n int size = right - left;\n int val;\n bool is_same = true;\n\n if(size > 1){\n int size_2 = size / 2;\n \n auto top_left = same_value(grid, left, right - size_2, top, bottom - size_2);\n auto top_right = same_value(grid, left + size_2, right, top, bottom - size_2);\n auto bottom_left = same_value(grid, left, right - size_2, top + size_2, bottom);\n auto bottom_right = same_value(grid, left + size_2, right, top + size_2, bottom);\n\n if(not top_left.second or not top_right.second or not bottom_left.second or not bottom_right.second){\n is_same = false;\n }else if(top_left.first != top_right.first or top_right.first != bottom_left.first or bottom_left.first != bottom_right.first){\n is_same = false;\n }\n \n val = top_left.first;\n }else{\n val = grid[top][left];\n }\n pair<int, bool> result = {val, is_same};\n same_values.emplace(l, result);\n return result;\n }\n Node* helper(const vector<vector<int>>& grid, int left, int right, int top, int bottom){\n Node* node = new Node();\n auto is_same_obj = same_value(grid, left, right, top, bottom);\n\n if(not is_same_obj.second){\n int size_2 = (right - left) / 2;\n\n node->topLeft = helper(grid, left, right - size_2, top, bottom - size_2);\n node->topRight = helper(grid, left + size_2, right, top, bottom - size_2);\n node->bottomLeft = helper(grid, left, right - size_2, top + size_2, bottom);\n node->bottomRight = helper(grid, left + size_2, right, top + size_2, bottom);\n\n node->isLeaf = false;\n node->val = 1;\n }else{\n node->isLeaf = true;\n node->val = is_same_obj.first;\n }\n return node;\n }\npublic:\n Node* construct(vector<vector<int>>& grid) {\n size_t size = grid.size();\n return helper(grid, 0, size, 0, size);\n }\n};", "memory": "26400" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n unordered_set<int> gridVals;\n for (vector<int>& row : grid) {\n for (int num : row) {\n gridVals.insert(num);\n }\n }\n\n Node* node;\n bool isLeaf = (gridVals.size() == 1);\n bool val = false;\n if (isLeaf) {\n val = (*gridVals.begin() == 1);\n node = new Node(val, isLeaf);\n }\n else {\n int halfH = grid.size() / 2;\n int halfW = grid[0].size() / 2;\n vector<vector<int>> topLeft(halfH, vector<int>(halfW));\n vector<vector<int>> topRight(halfH, vector<int>(halfW));\n vector<vector<int>> bottomLeft(halfH, vector<int>(halfW));\n vector<vector<int>> bottomRight(halfH, vector<int>(halfW));\n\n for (int i = 0; i < grid.size(); i++) {\n for (int j = 0; j < grid[0].size(); j++) {\n if (i < halfH && j < halfW) {\n topLeft[i][j] = grid[i][j];\n }\n else if (i < halfH) {\n topRight[i][j-halfW] = grid[i][j];\n }\n else if (j < halfW) {\n bottomLeft[i-halfH][j] = grid[i][j];\n }\n else {\n bottomRight[i-halfH][j-halfW] = grid[i][j];\n }\n }\n }\n\n node = new Node(\n val,\n isLeaf,\n construct(topLeft),\n construct(topRight),\n construct(bottomLeft),\n construct(bottomRight)\n );\n }\n\n return node;\n }\n};", "memory": "26900" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* rec(vector<vector<int>>& grid, int i, int j, int len) {\n if (len == 1) {\n return new Node(grid[i][j] == 1, true);\n }\n std::vector<Node*> nodes;\n nodes.push_back(rec(grid, i, j, len/2));\n nodes.push_back(rec(grid, i, j + len/2, len/2));\n nodes.push_back(rec(grid, i+len/2, j, len/2));\n nodes.push_back(rec(grid, i+len/2, j+len/2, len/2));\n\n auto same_value = std::all_of(nodes.begin(), nodes.end(), [&](auto node) { return node->val == nodes.front()->val;});\n auto all_leaf = std::all_of(nodes.begin(), nodes.end(), [](auto node) { return node->isLeaf;}); \n if (same_value && all_leaf) {\n return new Node(nodes.front()->val, true);\n }\n return new Node(false, false, nodes[0], nodes[1], nodes[2], nodes[3]);\n }\n\n Node* construct(vector<vector<int>>& grid) {\n if (grid.size() == 0) return nullptr;\n return rec(grid, 0, 0, grid.size());\n }\n};", "memory": "27300" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\n Node *helper(vector<vector<int>> &grid, int top, int left, int height, int width) {\n if (height == 1 || width == 1) {\n return new Node(grid[top][left] == 1, true);\n }\n int half_height = height / 2;\n int half_width = width / 2;\n Node *node = new Node(false, false,\n helper(grid, top, left, half_height, half_width),\n helper(grid, top, left + half_width, half_height, half_width),\n helper(grid, top + half_height, left, half_height, half_width),\n helper(grid, top + half_height, left + half_width, half_height, half_width));\n vector<Node *> childs = {node->topLeft, node->topRight, node->bottomLeft, node->bottomRight};\n for (int i = 0; i < childs.size(); ++i) {\n if (!childs[i]->isLeaf) {\n return node;\n }\n }\n for (int i = 1; i < childs.size(); ++i) {\n if (childs[i - 1]->val != childs[i]->val) {\n return node;\n }\n }\n bool value = childs[0]->val;\n for (int i = 0; i < childs.size(); ++i) {\n delete childs[i];\n }\n delete node;\n return new Node(value, true);\n }\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n return helper(grid, 0, 0, m, n);\n }\n};", "memory": "27400" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int val = grid[0][0];\n //cout << \"val check: \" << val << endl;\n bool splitQuad = false;\n for(vector<int>& v : grid){\n for(int& i : v){\n cout << i << \", \";\n if(i == val) continue;\n splitQuad = true;\n break;\n }\n cout << endl;\n if(splitQuad) break;\n }\n //if(splitQuad) cout << \"splitting grid\" << endl;\n //else cout << \"leaf found, not splitting\" << endl;\n Node * root = new Node(splitQuad ? 1 : val, !splitQuad);\n //cout << \"node isLeaf: \" << root->isLeaf << \" node val: \" << root-> val << endl;\n if(splitQuad){\n //split grid\n vector<vector<int>> topLeft;\n for(int i = 0; i < grid.size()/2; i++){\n vector<int> v;\n for(int j = 0; j < grid.size()/2; j++){\n //cout << grid[i][j] << \", \";\n v.push_back(grid[i][j]);\n }\n //cout << endl;\n topLeft.push_back(v);\n }\n root->topLeft = construct(topLeft);\n vector<vector<int>> topRight;\n for(int i = 0; i < grid.size()/2; i++){\n vector<int> v;\n for(int j = grid.size()/2; j < grid.size(); j++){\n v.push_back(grid[i][j]);\n }\n topRight.push_back(v);\n }\n root->topRight = construct(topRight);\n vector<vector<int>> bottomLeft;\n for(int i = grid.size()/2; i < grid.size(); i++){\n vector<int> v;\n for(int j = 0; j < grid.size()/2; j++){\n v.push_back(grid[i][j]);\n }\n bottomLeft.push_back(v);\n }\n root->bottomLeft = construct(bottomLeft);\n vector<vector<int>> bottomRight;\n for(int i = grid.size()/2; i < grid.size(); i++){\n vector<int> v;\n for(int j = grid.size()/2; j < grid.size(); j++){\n v.push_back(grid[i][j]);\n }\n bottomRight.push_back(v);\n }\n root->bottomRight = construct(bottomRight);\n }\n return root;\n }\n};", "memory": "28100" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int val = grid[0][0];\n bool splitQuad = false;\n for(vector<int>& v : grid){\n for(int& i : v){\n cout << i << \", \";\n if(i == val) continue;\n splitQuad = true;\n break;\n }\n cout << endl;\n if(splitQuad) break;\n }\n Node * root = new Node(splitQuad ? 1 : val, !splitQuad);\n if(splitQuad){\n vector<vector<int>> topLeft;\n for(int i = 0; i < grid.size()/2; i++){\n vector<int> v;\n for(int j = 0; j < grid.size()/2; j++){\n v.push_back(grid[i][j]);\n }\n topLeft.push_back(v);\n }\n root->topLeft = construct(topLeft);\n vector<vector<int>> topRight;\n for(int i = 0; i < grid.size()/2; i++){\n vector<int> v;\n for(int j = grid.size()/2; j < grid.size(); j++){\n v.push_back(grid[i][j]);\n }\n topRight.push_back(v);\n }\n root->topRight = construct(topRight);\n vector<vector<int>> bottomLeft;\n for(int i = grid.size()/2; i < grid.size(); i++){\n vector<int> v;\n for(int j = 0; j < grid.size()/2; j++){\n v.push_back(grid[i][j]);\n }\n bottomLeft.push_back(v);\n }\n root->bottomLeft = construct(bottomLeft);\n vector<vector<int>> bottomRight;\n for(int i = grid.size()/2; i < grid.size(); i++){\n vector<int> v;\n for(int j = grid.size()/2; j < grid.size(); j++){\n v.push_back(grid[i][j]);\n }\n bottomRight.push_back(v);\n }\n root->bottomRight = construct(bottomRight);\n }\n return root;\n }\n};", "memory": "28200" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* rec(vector<vector<int>>& grid, int a, int b, int c, int d) {\n Node* top = new Node();\n bool can_end = true;\n for (int i = a; i <= c && can_end; i++){\n for (int j = b; j <= d && can_end; j++){\n if (grid[i][j] != grid[0][0]){\n can_end = false;\n }\n }\n }\n if (can_end){\n top->isLeaf = true; top->val = grid[a][b];\n return top;\n }\n top->topLeft = rec(grid, a, b, (c + a)/2 - 1, (d + b)/2 - 1);\n top->topRight = rec(grid, a, (d + b)/2, (c + a)/2 - 1, d);\n top->bottomLeft = rec(grid, (c + a)/2, b, c, (d + b)/2 - 1);\n top->bottomRight = rec(grid, (c + a)/2, (d + b)/2, c, d);\n return top;\n }\n Node* construct(vector<vector<int>>& grid) {\n Node* top = new Node();\n bool can_end = true;\n for (int i = 0; i < grid.size() && can_end; i++){\n for (int j = 0; j < grid[i].size() && can_end; j++){\n if (grid[i][j] != grid[0][0]){\n can_end = false;\n }\n }\n }\n if (can_end){\n top->isLeaf = true; top->val = grid[0][0];\n return top;\n }\n vector<vector<int>> tL, tR, bL, bR;\n for (int i = 0; i < grid.size()/2; i++){\n vector <int> tmp;\n for (int j = 0; j < grid[i].size()/2; j++){\n tmp.push_back(grid[i][j]);\n }\n tL.push_back(tmp);\n }\n for (int i = grid.size()/2; i < grid.size(); i++){\n vector <int> tmp;\n for (int j = 0; j < grid[i].size()/2; j++){\n tmp.push_back(grid[i][j]);\n }\n bL.push_back(tmp);\n }\n for (int i = 0; i < grid.size()/2; i++){\n vector <int> tmp;\n for (int j = grid[i].size()/2; j < grid[i].size(); j++){\n tmp.push_back(grid[i][j]);\n }\n tR.push_back(tmp);\n }\n for (int i = grid.size()/2; i < grid.size(); i++){\n vector <int> tmp;\n for (int j = grid[i].size()/2; j < grid[i].size(); j++){\n tmp.push_back(grid[i][j]);\n }\n bR.push_back(tmp);\n }\n top->topLeft = construct(tL);\n top->topRight = construct(tR);\n top->bottomLeft = construct(bL);\n top->bottomRight = construct(bR);\n return top;\n }\n};", "memory": "28300" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "static const int speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0;}();\n\nclass Solution {\npublic:\n std::vector<int> getAverages(std::vector<int>& nums, int k) {\n long long sum = 0;\n int bound = 2 * k + 1;\n std::vector<int> ans;\n ans.resize(nums.size(), -1);\n for (int i = 0; i < nums.size(); ++i) {\n sum += nums[i];\n if (i >= bound - 1) {\n ans[i - k] = static_cast<int>(sum / bound);\n sum -= nums[i - bound + 1];\n }\n }\n return ans;\n }\n};", "memory": "132280" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"O3\")\nclass Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n int n = nums.size();\n int len = 2*k + 1;\n vector<int> result(n, -1);\n if (n < len) return result;\n long long sum = accumulate(nums.begin(), nums.begin()+len, 0LL);\n result[k] = sum/len;\n for (int center = k+1; center+k < n; ++center) {\n sum += nums[center+k] - nums[center-k-1];\n result[center] = sum/len;\n } return result;\n }\n};", "memory": "132280" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n std::vector<int> output(nums.size(),-1);\n long int initial_sum = 0;\n for(size_t i =0; i<(2*k) && i<nums.size();i++){\n initial_sum += nums[i];\n //std::cout << \"Adding \" << nums[i] << std::endl;\n }\n \n \n for(size_t j = 0; (j+2*k)<nums.size();j++){\n //std::cout << \"Adding1 \" << nums[j+2*k] << std::endl;\n //std::cout << \"SUbtracting1 \" << nums[j] << std::endl;\n initial_sum += nums[j+2*k];\n output[j+k]=(initial_sum/(2*k+1)); \n initial_sum -= nums[j]; \n }\n\n return output;\n }\n};", "memory": "132640" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n std::vector<int> output(nums.size(),-1);\n long int initial_sum = 0;\n for(size_t i =0; i<(2*k) && i<nums.size();i++){\n initial_sum += nums[i];\n //std::cout << \"Adding \" << nums[i] << std::endl;\n }\n \n \n for(size_t j = 0; (j+2*k)<nums.size();j++){\n //std::cout << \"Adding1 \" << nums[j+2*k] << std::endl;\n //std::cout << \"SUbtracting1 \" << nums[j] << std::endl;\n initial_sum += nums[j+2*k];\n output[j+k]=(initial_sum/(2*k+1)); \n initial_sum -= nums[j]; \n }\n\n return output;\n }\n};", "memory": "132640" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "#include <algorithm>\n\nclass Solution {\npublic:\n\n/*\nn = 4;\n l x r \n l c r\n[1,1,1,1]\nk = 1\nwindowSize = 3;\n\ncurrTotal = nums[0....6]\ncentre = 1\n\navgs [-1, 1,-1,-1]\n \nright = 2\ncurrTotal = 3 \n\n l c r\n[1,1,1,1]\n\n*/\n\n vector<int> getAverages(vector<int>& nums, int k) {\n // build the averages array\n int n = nums.size();\n vector<int> avgs(n, -1); \n int windowSize = k*2 + 1; \n if (n < windowSize) \n return avgs;\n if (k == 0)\n return nums;\n long long currTotal = 0;\n // sliding window - build the initial window \n for (int i = 0; i < windowSize; i++)\n {\n currTotal += nums[i]; \n }\n int centre = k;\n int left = 0;\n int right = centre + k;\n\n avgs[centre] = (int) (currTotal / windowSize);\n\n while (right < n - 1)\n {\n right++; \n currTotal += nums[right];\n currTotal -= nums[left];\n left++;\n centre++;\n avgs[centre] = currTotal / windowSize;\n }\n return avgs;\n }\n};\n\n", "memory": "133000" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> avgs(n, -1);\n if (k == 0) return nums;\n if (2 * k + 1 > n) return avgs;\n long long sum = 0;\n for (int i = 0; i < 2 * k + 1; ++i) {\n sum += nums[i];\n }\n avgs[k] = sum / (2 * k + 1);\n for (int i = k + 1; i < n - k; ++i) {\n sum += nums[i + k] - nums[i - k - 1];\n avgs[i] = sum / (2 * k + 1);\n }\n return avgs;\n }\n};", "memory": "133000" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n=nums.size();\n long long int pref[n+1];\n vector<int> ans(n);\n pref[0]=0;\n for(int i=0;i<n;i++){\n pref[i+1]=pref[i]+nums[i];\n }\n for(int i=0;i<n;i++){\n if(i-k<0||i+k>=n){\n ans[i]=-1;\n }\n else{\n long long int d=pref[i+k+1]-pref[i-k];\n ans[i]=(d/(2*k+1));\n }\n }\n return ans;\n }\n};", "memory": "133360" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n vector<int> avgs(nums.size());\n long long running = 0;\n if (k == 0) {\n return nums;\n }\n bool tf = (nums.size() < 2*k + 1);\n std::cout << tf << endl;;\n if (nums.size() < 2*k + 1) {\n return vector<int>(nums.size(), -1);\n }\n for (int i = 0; i < k; i++) {\n avgs[i] = -1;\n }\n int end = 0;\n for (int i = 0; i < 2 * k + 1; i++) {\n running += nums.at(i);\n\n }\n \n int max = running;\n avgs[k] = running / (2*k + 1);\n for (int i = 0; i < nums.size() - 2*k - 1; i++) {\n running -= nums[i];\n running += nums[2*k + 1 + i];\n avgs[k + i + 1] = running / (2*k + 1);\n }\n for (int i = nums.size() - k; i < nums.size(); i++) {\n avgs[i] = -1;\n }\n\n return avgs;\n \n }\n};", "memory": "133360" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int len = nums.size();\n int step = 2*k+1;\n vector<int> avg(len, -1);\n if(k == 0) return nums;\n if(len < step) \n {\n vector<int> res(len, -1);\n return res;\n } else{\n \n long long sum = 0;\n for(int i = 0; i < step; i++)\n {\n sum += nums[i];\n }\n avg[k] = sum / step;\n for(int i = k+1; i < len - k; i++)\n {\n sum = (sum - nums[i-k-1] + nums[i+k]);\n avg[i] = sum / step;\n }\n \n }\n \n return avg;\n }\n};", "memory": "133720" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n vector<int> ans(nums.size(), -1); \n if(ans.size() < 2*k+1) return ans; \n stack<int> st; long long int t = 0; \n int s = 1, e = 2*k+1; \n for(int i=0; i<=2*k; i++) t += nums[i]; st.push(t/(2*k+1)); \n while(e <nums.size()){\n t -= nums[s-1]; t += nums[e]; s++; e++;\n st.push(t/(2*k+1)); \n }\n\n int start = k, end = nums.size()-k-1;\n for(int i=end; i>=start ; i--) {\n ans[i] = st.top(); st.pop(); \n }\n\n return ans; \n }\n};", "memory": "133720" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int> nums, int k) {\n int n=nums.size();\n vector<int>ans(n,-1);\n int j=0, i=0;\n long long sum=0;\n while(j<n){\n sum +=nums[j];\n if(j-i+1<2*k+1){\n j++;\n }\n else if(j-i+1==2*k+1){ \n ans[i+k]=sum/(2*k+1);\n sum-=nums[i];\n i++;\n j++;\n \n }\n else {\n break;\n }\n\n }\n return ans;\n \n }\n};", "memory": "134080" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n \n int totalSize = k+k+1;\n int N = nums.size();\n \n vector<int> answer(N,-1);\n queue<int> numbers;\n long long sum = 0;\n \n if(k >= N){\n \n return answer;\n }\n \n for(int i=0; i<k; i++){\n cout << nums[i] << endl;\n numbers.push(nums[i]);\n sum += (long long)(nums[i]);\n }\n \n for(int i=0; i<N; i++){\n \n if((i+k) < N){\n numbers.push(nums[i+k]);\n sum += (long long)nums[i+k];\n }\n \n if(numbers.size() == totalSize){\n answer[i] = sum/totalSize;\n int frontNum = numbers.front();\n numbers.pop();\n sum -= (long long)frontNum;\n }\n }\n \n return answer;\n }\n};", "memory": "134440" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> ans(n, -1);\n\n if (k > n / 2)\n return ans;\n\n queue<int> q; \n\n int start = k * 2;\n long sum = 0;\n \n for(int i = 0; i < start; i++){\n sum += nums[i];\n q.push(nums[i]);\n }\n\n for(int i = start; i < nums.size(); i++){\n \n // Add to the back of Sliding Window\n sum += nums[i];\n q.push(nums[i]);\n\n ans[i - k] = sum / (k * 2 + 1);\n\n // Remove Front of Sliding Window\n sum -= q.front();\n q.pop();\n }\n\n return ans;\n\n }\n};", "memory": "134800" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n vector<int> ans;\n if(2*k+1>nums.size()){\n return vector<int>(nums.size(),-1);\n }\n if(k==0){\n return nums;\n }\n int n=nums.size();\n for(int i=0;i<k;i++)ans.push_back(-1);\n double sum=0;\n for(int i=0;i<min(n,2*k+1);i++){\n sum+=nums[i];\n }\n int j=0;\n for(int i=k;i<n-k;i++){\n ans.push_back(sum/(2*k+1));\n if(i+k+1<n)sum+=nums[i+k+1];\n sum-=nums[i-k];\n }\n for(int i=0;i<k;i++){\n if(ans.size()==n)break;\n ans.push_back(-1);\n }\n return ans;\n }\n};", "memory": "135160" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size() ;\n long long csum = 0 ;\n vector<int>ans ;\n int l = 0 ;\n int r = 0 ;\n\n if ( 2*k+1 > n )\n return vector<int>(n,-1) ;\n \n for ( int i = 0 ; i < k ; i++ )\n ans.push_back(-1) ;\n while ( r < n )\n {\n csum += nums[r] ;\n // cout<<l<<\" \"<<r<<\" \"<<csum<<endl ;\n if ( r - l + 1 == 2*k+1 )\n {\n ans.push_back(csum/(2*k+1)) ;\n csum = csum - nums[l] ;\n r++ ;\n l++ ;\n }\n else\n {\n r++ ;\n }\n }\n for ( int i = 0 ; i < k ; i++ )\n ans.push_back(-1) ;\n return ans ;\n }\n};", "memory": "135520" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size() ;\n long long csum = 0 ;\n vector<int>ans ;\n int l = 0 ;\n int r = 0 ;\n\n if ( 2*k+1 > n )\n return vector<int>(n,-1) ;\n \n for ( int i = 0 ; i < k ; i++ )\n ans.push_back(-1) ;\n while ( r < n )\n {\n csum += nums[r] ;\n // cout<<l<<\" \"<<r<<\" \"<<csum<<endl ;\n if ( r - l + 1 == 2*k+1 )\n {\n ans.push_back(csum/(2*k+1)) ;\n csum = csum - nums[l] ;\n r++ ;\n l++ ;\n }\n else if ( r-l+1 < 2*k+1 )\n {\n r++ ;\n }\n }\n for ( int i = 0 ; i < k ; i++ )\n ans.push_back(-1) ;\n return ans ;\n }\n};", "memory": "135520" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size() ;\n long long csum = 0 ;\n vector<int>ans ;\n int l = 0 ;\n int r = 0 ;\n\n if ( 2*k+1 > n )\n return vector<int>(n,-1) ;\n \n for ( int i = 0 ; i < k ; i++ )\n ans.push_back(-1) ;\n while ( r < n )\n {\n csum += nums[r] ;\n //cout<<l<<\" \"<<r<<\" \"<<csum<<endl ;\n if ( r - l + 1 == 2*k+1 )\n {\n ans.push_back(csum/(2*k+1)) ;\n csum = csum - nums[l] ;\n r++ ;\n l++ ;\n }\n else if ( r-l+1 < 2*k+1 )\n {\n r++ ;\n }\n }\n for ( int i = 0 ; i < k ; i++ )\n ans.push_back(-1) ;\n return ans ;\n }\n};", "memory": "135880" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n \n int n = nums.size();\n if(n < 2*k+1)\n {\n vector<int>temp(n,-1);\n return temp;\n }\n\n vector<int>ans;\n long long frange = 2*k;\n long long sum = 0;\n if(frange < n)\n {\n for(int i=0; i<=frange; i++)\n sum += nums[i];\n }\n for(int i=0; i<n; i++)\n {\n if(i<k)\n ans.push_back(-1);\n else\n {\n if(i==k)\n ans.push_back(sum/(frange+1));\n else\n {\n if(i+k < n)\n {\n sum = sum + nums[i+k];\n sum = sum - nums[i-k-1];\n ans.push_back(sum/(frange+1));\n }\n else\n {\n ans.push_back(-1);\n }\n }\n }\n }\n return ans;\n }\n};", "memory": "136240" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size() ;\n long long csum = 0 ;\n vector<int>ans ;\n int l = 0 ;\n int r = 0 ;\n\n if ( 2*k+1 > n )\n return vector<int>(n,-1) ;\n \n for ( int i = 0 ; i < k ; i++ )\n ans.push_back(-1) ;\n while ( r < n )\n {\n csum += nums[r] ;\n // cout<<l<<\" \"<<r<<\" \"<<csum<<endl ;\n if ( r - l + 1 == 2*k+1 )\n {\n ans.push_back(csum/(2*k+1)) ;\n csum = csum - nums[l] ;\n r++ ;\n l++ ;\n }\n else if ( r-l+1 < 2*k+1 )\n {\n r++ ;\n }\n }\n for ( int i = 0 ; i < k ; i++ )\n ans.push_back(-1) ;\n return ans ;\n }\n};", "memory": "136600" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n if(nums.size()<=2*k){\n vector<int>ans(nums.size(),-1);\n return ans;\n }\n vector<int>ans;\n for(int i=0;i<k;i++){\n ans.push_back(-1);\n }\n long long sum=0;\n for(int i=0;i<=2*k;i++){\n sum+=nums[i];\n }\n int j=2*k+1;\n \n ans.push_back(sum/(2*k+1));\n \n while(j<nums.size()){\n cout<<sum<<endl;\n sum+=nums[j];\n sum-=nums[j-2*k-1];\n j++;\n \n ans.push_back(sum/(2*k+1));\n }\n for(int i=0;i<k;i++){\n ans.push_back(-1);\n }\n return ans;\n }\n};", "memory": "136960" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(false); cin.tie(0);\n //NOTE. ban dau push k con -1 vao, ti nua phai push k con -1 nua\n if (nums.size()<2*k+1) return vector<int> (nums.size(), -1);\n vector<int> ans(k, -1);\n long long s=0;\n for (int i=0;i<nums.size();++i){\n s+=nums[i];\n if (i==2*k) ans.push_back(s/(2*k+1));\n else if (i>2*k){\n s-=nums[i-(2*k+1)];\n ans.push_back(s/(2*k+1));\n }\n }\n for (int i=0;i<k;++i) ans.push_back(-1);\n return ans;\n }\n};", "memory": "137320" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(false); cin.tie(0);\n //NOTE. ban dau push k con -1 vao, ti nua phai push k con -1 nua\n if (nums.size()<2*k+1) return vector<int> (nums.size(), -1);\n vector<int> ans(k, -1);\n long long s=0;\n for (int i=0;i<nums.size();++i){\n s+=nums[i];\n if (i==2*k) ans.push_back(s/(2*k+1));\n else if (i>2*k){\n s-=nums[i-(2*k+1)];\n ans.push_back(s/(2*k+1));\n }\n }\n for (int i=0;i<k;++i) ans.push_back(-1);\n return ans;\n }\n};", "memory": "137320" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n // requires a full window, else -1\n // so start at k, end at n - k\n // rolling window, adjust total per iter\n \n if (k == 0) {\n return nums;\n }\n \n std::vector<int32_t> out;\n \n size_t n = nums.size();\n \n if (k * 2 > n) {\n for (int i = 0; i < n; i++) {\n out.push_back(-1);\n }\n return out;\n }\n \n int64_t total = 0;\n \n // [0 1 2 3 4 5 6 7], 2\n \n // build initial window\n for (int i=0; i < k && i < n; i++) {\n out.push_back(-1);\n total += nums[i];\n }\n for (int i=k; i<=k*2 && i < n; i++) {\n total += nums[i];\n }\n \n // rolling window\n for (int i=k; i < n-k; i++) {\n out.push_back(total / (k * 2 + 1));\n total -= nums[i-k];\n if (i + k + 1 < n) {\n total += nums[i + k + 1];\n }\n }\n \n for (int i=0; i<k; i++) {\n out.push_back(-1);\n }\n \n return out;\n }\n};", "memory": "137680" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n if(k==0){\n return nums;\n }\n int n=nums.size();\n int i=0;\n int j=0;\n long long presum=0;\n vector<int> ans;\n while(j<n){\n presum+=nums[j];\n if(j-i+1>k && j+k<n){\n long long postsum=0,z=j+1;\n while(z<=j+k){\n postsum+=nums[z];\n z++;\n }\n int avg=(postsum+presum)/(2*k+1);\n ans.push_back(avg);\n presum-=nums[i];\n i++;\n }\n else{\n ans.push_back(-1);\n }\n j++;\n\n }\n return ans;\n }\n};", "memory": "138040" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int windowSize = 2*k+1, n = nums.size();\n if(n <= windowSize && k>1) {\n vector<int> ans(n, -1);\n return ans;\n } \n \n vector<int> ans(k, -1);\n long long l = 0, r = 0, sum = 0;\n while(r < n) {\n sum += nums[r];\n if(r-l+1 == windowSize) {\n int avg = sum/windowSize;\n ans.push_back(avg);\n sum-=nums[l];\n l++;\n r++;\n }\n else r++;\n }\n for(int i = 0; i<k; ++i) ans.push_back(-1);\n return ans;\n }\n};", "memory": "138400" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n \n vector<int> result;\n int win_len = 2*k+1;\n if(!(2*k+1 <= nums.size())) \n {\n result.insert(result.begin(), nums.size(), -1);\n return result; \n }\n\n long long sum=0;\n for(int i=0;i<2*k+1;i++)\n {\n sum=sum+nums[i];\n }\n \n result.insert(result.begin(), k, -1);\n \n int start=0, end=2*k+1;\n\n while(end < nums.size())\n { \n cout<<nums.size()<< \" \";\n result.push_back(sum/win_len);\n sum=sum-nums[start];\n sum=sum+nums[end];\n start++;\n end++;\n }\n result.push_back(sum/win_len);\n for(int i=0;i<k;i++)\n {\n result.push_back(-1);\n }\n\n return result;\n }\n};", "memory": "138760" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "#define ll long long\n\nclass Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n vector<int>ans;\n int n=nums.size(),l=0;\n ll sum=0;\n if(2*k+1>n){\n for(int i=0;i<n;i++){\n ans.push_back(-1);\n }\n return ans;\n }\n for(int i=0;i<k;i++){\n ans.push_back(-1);\n sum+=nums[i];\n }\n for(int j=k;j<=2*k;j++){\n sum+=nums[j];\n }\n ans.push_back(sum/(2*k+1));\n for(int j=2*k+1;j<n;j++){\n sum-=nums[l++];\n sum+=nums[j];\n ans.push_back(sum/(2*k+1));\n }\n for(int j=0;j<k;j++){\n ans.push_back(-1);\n }\n return ans;\n }\n};", "memory": "138760" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n vector<int> ans;\n int i = 0,t = k+k+1, n = nums.size();\n while(i<k&&i<n){\n ans.push_back(-1);\n i++;\n }\n long long winsum = 0;\n for(int j = 0; j<t && j<n;j++){\n winsum+=nums[j];\n }\n for( i=k ;i<n-k;i++){\n int d = winsum/t;\n ans.push_back(d);\n winsum -= nums[i-k];\n if(i+1+k<n)winsum += nums[i+k+1];\n }\n while(i<n){\n ans.push_back(-1);\n i++;\n }\n return ans;\n }\n};", "memory": "139120" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n vector<int> ans;\n int i = 0,t = k+k+1, n = nums.size();\n while(i<k&&i<n){\n ans.push_back(-1);\n i++;\n }\n long long winsum = 0;\n for(int j = 0; j<t && j<n;j++){\n winsum+=nums[j];\n }\n for( i=k ;i<n-k;i++){\n int d = winsum/t;\n ans.push_back(d);\n winsum -= nums[i-k];\n if(i+1+k<n)winsum += nums[i+k+1];\n }\n while(i<n){\n ans.push_back(-1);\n i++;\n }\n return ans;\n }\n};", "memory": "139120" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n=nums.size();\n if(k==0){\n return nums;\n }\n vector<int>result(n,-1);\n if(n<2*k+1)\n return result;\n vector<long long >prefixsum(n,0);\n prefixsum[0]=nums[0];\n for(int i=1;i<n;i++){\n prefixsum[i]+=prefixsum[i-1]+nums[i];\n }\n for(int i=k;i<=n-k-1;i++){\n int left_idx=i-k;\n int right_idx=i+k;\n long long sum=prefixsum[right_idx];\n if(left_idx>0){\n sum-=prefixsum[left_idx-1];\n }\n result[i]=sum/(2*k+1);\n }\n return result;\n }\n};", "memory": "139480" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> result(n, -1);\n \n // Edge case when k is 0, return the nums array itself\n if (k == 0) return nums;\n \n // Edge case when the window size is larger than the array\n if (2 * k + 1 > n) return result;\n \n // Create the prefix sum array\n vector<long long> prefix(n + 1, 0);\n for (int i = 0; i < n; i++) {\n prefix[i + 1] = prefix[i] + nums[i];\n }\n \n // Calculate averages for each valid index\n for (int i = k; i < n - k; i++) {\n long long sum = prefix[i + k + 1] - prefix[i - k];\n int avg = sum / ((k * 2) + 1);\n result[i] = avg;\n }\n \n return result;\n }\n};\n", "memory": "139480" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n=nums.size();\n\n vector<int> result(n,-1);\n if(k==0){\n return nums;\n }\n if(n<(2*k+1)){\n return result;\n }\n\n vector<long long> prefixsum(n,0);\n prefixsum[0]=nums[0];\n\n for(int i=1;i<n;i++){\n prefixsum[i]=prefixsum[i-1]+nums[i];\n }\n\n for(int i=k;i<=n-k-1;i++){\n int left_Sum=i-k;\n int right_Sum=i+k;\n \n long long sum=prefixsum[i+k];\n if(i-k>0){\n sum=sum-prefixsum[i-k-1];\n }\n\n sum=sum/(2*k+1);\n\n result[i]=sum;\n }\n return result;\n }\n};", "memory": "139840" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> avgVec(n, -1);\n if (k == 0) {\n return nums;\n }\n if (2 * k + 1 > n) {\n return avgVec; // Not enough elements to form any k-radius subarrays\n }\n\n // Create prefix sum array\n vector<long long> prefixSum(n + 1, 0);\n for (int i = 0; i < n; i++) {\n prefixSum[i + 1] = prefixSum[i] + nums[i];\n }\n\n // Calculate k-radius averages\n for (int i = k; i < n - k; i++) {\n long long sum = prefixSum[i + k + 1] - prefixSum[i - k];\n avgVec[i] = sum / (2 * k + 1);\n }\n\n return avgVec;\n }\n};", "memory": "139840" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> avgs(n, -1);\n if (n > 2 * k) {\n vector<long> sums(n, 0);\n sums[0] = nums[0];\n \n for (int i = 1; i < n; i++) {\n sums[i] = sums[i - 1] + nums[i];\n }\n \n avgs[k] = sums[2 * k]/(2 * k + 1);\n for (int i = k + 1; i < n - k; i++) {\n avgs[i] = (sums[i + k] - sums[i - (k + 1)])/(2 * k + 1);\n }\n }\n return avgs;\n }\n};", "memory": "140200" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> avgs(n ,-1);\n if (2*k+1 >n){\n return avgs;\n }\n \n vector<long long> prefix (n,0);\n prefix[0] = nums[0];\n for (int i=1; i<n; i++){\n prefix[i] = nums[i] + prefix[i-1];\n }\n \n for(int i = k; i<n-k; i++){\n int left_bound = i-k;\n int right_bound = i+k;\n long long total;\n if (left_bound==0){\n total = prefix[right_bound];\n } else{\n total = prefix[right_bound]-prefix[left_bound-1];\n }\n \n int count = right_bound-left_bound+1;\n avgs[i] = static_cast<int>(total/count);\n }\n \n return avgs;\n\n \n }\n};", "memory": "140200" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n if (k == 0) {\n return nums;\n }\n\n int n = nums.size();\n vector<unsigned long long> prefix(n + 1);\n vector<int> result(n, -1);\n\n // no index will have 'k' radius if 2 * k + 1 > n\n // this means that our windowSize will simply not fit\n if (2 * k + 1 > n) {\n return result;\n }\n \n for (int i = 0; i < n; i++) {\n prefix[i + 1] = prefix[i] + nums[i];\n }\n \n for (int i = 0; i < n; i++) {\n if (i - k < 0 || i + k >= n) {\n result[i] = -1;\n continue;\n }\n \n unsigned long long left_sum = 0;\n if (i - k > 0) {\n left_sum = prefix[i - k]; \n }\n \n result[i] = (prefix[i + k + 1] - left_sum) / (2 * k + 1);\n }\n\n return result;\n }\n};", "memory": "140560" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n // so for every index < i-k and > k+i is -1\n if(k == 0) return nums;\n int n = nums.size();\n vector<int> res(n, -1);\n vector<long long> prefixSum(n, 0);\n prefixSum[0] = nums[0];\n for (int i = 1; i < n; i++) {\n prefixSum[i] = prefixSum[i - 1] + nums[i];\n }\n for (int i = k; i < n - k; i++) {\n if (i - k - 1 < 0) {\n res[i] = (prefixSum[i + k]) / (k * 2 + 1);\n } else {\n res[i] = (prefixSum[i + k] - prefixSum[i - k-1]) / (k * 2 + 1);\n }\n }\n return res;\n }\n};", "memory": "140560" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n if (k == 0) {\n return nums;\n }\n\n int n = nums.size();\n vector<unsigned long long> prefix(n + 1);\n vector<int> result(n, -1);\n\n // no index will have 'k' radius if 2 * k + 1 > n\n // this means that our windowSize will simply not fit\n if (2 * k + 1 > n) {\n return result;\n }\n \n for (int i = 0; i < n; i++) {\n prefix[i + 1] = prefix[i] + nums[i];\n }\n \n for (int i = 0; i < n; i++) {\n if (i - k < 0 || i + k >= n) {\n result[i] = -1;\n continue;\n }\n\n // HERE, I CASTED UNSIGNED LONG LONG TO INT!\n // BE CAREFUL! \n unsigned long long left_sum = 0;\n if (i - k > 0) {\n left_sum = prefix[i - k]; \n }\n \n result[i] = (prefix[i + k + 1] - left_sum) / (2 * k + 1);\n }\n\n return result;\n }\n};", "memory": "140920" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size(); \n vector<int>ans;\n long long pre[n], suff[n];\n\n pre[0] = nums[0];\n for(int i = 1; i<n; i++){\n pre[i] = pre[i-1] + nums[i];\n }\n\n suff[n-1] = nums[n-1];\n for(int i = n-2; i>=0; i--){\n suff[i] = suff[i+1] + nums[i];\n }\n\n for(int i = 0; i<n; i++){\n if(i<k || i+k >=n){ \n ans.push_back(-1);\n }\n else {\n long long sumOfNextKElements = pre[i+k] - pre[i];\n long long sumOfPrevKElements = suff[i-k] - suff[i];\n long long sum = sumOfNextKElements + sumOfPrevKElements + nums[i];\n sum = sum /(2*k +1);\n ans.push_back(sum);\n }\n }\n return ans;\n\n }\n};", "memory": "140920" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n // [7,4,3,9,1,8,5,2,6] and k = 3\n // n = 9, o/p = 9.\n // divide by 2k+1 \n // sum of 2k+1 numbers-> preSum[2k+1 elements]\n // pivot -> pivot-k>0 and pivot+k<n -> i(+/-)(2k+1)\n // take avg when above satisfies (ps[pivot+k] - ps[pivot-k])/2k+1\n // [-1,-1,-1, __ , __ , __ , -1,-1,-1]\n\n vector<int> getAverages(vector<int>& nums, int k) {\n \n int n = nums.size();\n vector<int> ans(n,-1);\n\n if(k>n) ans;\n if(k==0) return nums;\n\n vector<long long> preSum(n+1,0);\n preSum[0]=0;\n ans[0]=-1;\n\n for(int i =1;i<n+1;i++)\n {\n\n preSum[i] = nums[i-1]+preSum[i-1]; \n if((i<k || i+k+1 > n) && i<n)\n ans[i]=-1;\n \n }\n\n // for(int i =0;i<n+1;i++)\n // cout<<preSum[i]<<\" \"; \n // cout<<endl;\n\n for(int p = k;p<n-k;p++)\n {\n long long sum = preSum[p+k+1]-preSum[p-k];\n // cout<<preSum[p+k+1]<<\" \"<<preSum[p-k]<<endl;\n long long den = (2*k)+1;\n ans[p]=sum/den; \n }\n return ans;\n\n \n }\n};", "memory": "141280" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n // [7,4,3,9,1,8,5,2,6] and k = 3\n // n = 9, o/p = 9.\n // divide by 2k+1 \n // sum of 2k+1 numbers-> preSum[2k+1 elements]\n // pivot -> pivot-k>0 and pivot+k<n -> i(+/-)(2k+1)\n // take avg when above satisfies (ps[pivot+k] - ps[pivot-k])/2k+1\n // [-1,-1,-1, __ , __ , __ , -1,-1,-1]\n\n vector<int> getAverages(vector<int>& nums, int k) {\n \n int n = nums.size();\n vector<int> ans(n,-1);\n\n if(k>n) return ans;\n if(k==0) return nums;\n\n vector<long long> preSum(n+1,0);\n preSum[0]=0;\n ans[0]=-1;\n\n for(int i =1;i<n+1;i++)\n {\n\n preSum[i] = nums[i-1]+preSum[i-1]; \n if((i<k || i+k+1 > n) && i<n)\n ans[i]=-1;\n \n }\n\n // for(int i =0;i<n+1;i++)\n // cout<<preSum[i]<<\" \"; \n // cout<<endl;\n\n for(int p = k;p<n-k;p++)\n {\n long long sum = preSum[p+k+1]-preSum[p-k];\n // cout<<preSum[p+k+1]<<\" \"<<preSum[p-k]<<endl;\n long long den = (2*k)+1;\n ans[p]=sum/den; \n }\n return ans;\n\n \n }\n};", "memory": "141280" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n vector<long long> prefixArray(nums.size(), 0);\n\nint n = nums.size();\nprefixArray[0] = nums[0];\n//7,4,3,9,1,8,5,2,6\n//7,11,14,23,24,32,37,39,45\nfor (int i = 1; i < n; i++)\n{\n\tprefixArray[i] = prefixArray[i-1] + nums[i];\n}\n\nfor (int i = 0; i < n; i++)\n{\n\tstd::cout << prefixArray[i] << \" \";\n}\n\nstd::vector<int> result(n, -1);\nfor (int i= k; i < n - k; i++)\n{\n\tlong long sum = prefixArray[i + k];\n\tif (i - k > 0)\n\t{\n\t\tsum -= prefixArray[i - k - 1];\n\t}\n\n\tresult[i] = (sum / ((2*k) + 1));\n}\n\nreturn result;\n }\n};", "memory": "141640" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n vector<int> averages(nums.size(), -1);\n vector<long> prefixSum(nums.size() + 1, 0);\n const int divisor = (k << 1) + 1;\n \n for (int i = 0; i < nums.size(); ++i) {\n prefixSum[i + 1] = nums[i] + prefixSum[i];\n }\n\n for (int i = k; i + k < nums.size(); ++i) {\n averages[i] = (prefixSum[i + k + 1] - prefixSum[i - k]) / divisor;\n }\n\n return averages;\n }\n};", "memory": "141640" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n long long n=nums.size();\n vector<int> ans(n,-1);\n vector<long long> pre(n);\n if(k>n) return ans;\n if(k==0) return nums;\n pre[0]=nums[0];\n for(int i=1;i<n;i++){\n pre[i]=pre[i-1]+nums[i];\n }\n for(int i=k;i<=(n-k-1);i++){\n \n long long val=(pre[i+k]- (i-k == 0 ? 0 : pre[i-k-1]));\n cout<<val<<\" \";\n ans[i]=val/(2*k+1);\n \n }\n return ans;\n }\n};", "memory": "142000" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n long long n=nums.size();\n vector<int> ans(n,-1);\n vector<long long> pre(n);\n if(k>n) return ans;\n if(k==0) return nums;\n pre[0]=nums[0];\n for(int i=1;i<n;i++){\n pre[i]=pre[i-1]+nums[i];\n }\n for(int i=0;i<n;i++){\n if(i>=k && i<=(n-k-1)){\n long long val=(pre[i+k]- (i-k == 0 ? 0 : pre[i-k-1]));\n cout<<val<<\" \";\n ans[i]=val/(2*k+1);\n }\n }\n return ans;\n }\n};", "memory": "142000" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size();\n vector<long long> prefix(n);\n vector<int> avgs(n, -1);\n \n if(n < (2*k - 1))\n return avgs;\n \n if(k == 0)\n return nums;\n \n prefix[0] = nums[0];\n for(int i = 1; i < n; i++) {\n prefix[i] = prefix[i - 1] + nums[i];\n }\n \n for(int i = k; i < n - k; i++) {\n long long sum = prefix[i + k];\n \n if(i != k) {\n sum -= prefix[i - k - 1];\n }\n \n avgs[i] = sum/(2*k + 1);\n }\n \n return avgs;\n }\n};", "memory": "142360" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n=nums.size();\n if(n<=k)\n {\n vector<int> ans(n,-1);\n return ans;\n }\n vector<long long> pref(n);\n for(int i=0;i<n;i++)\n {\n if(i==0)\n {\n pref[i]=nums[i];\n }\n else\n {\n pref[i]=nums[i]+pref[i-1];\n }\n }\n\n vector<int> ans;\n int i=0;\n for(;i<k&&i<n;i++)\n {\n ans.push_back(-1);\n }\n\n for(;i<n-k&&i<n;i++)\n {\n long long temp=pref[i+k];\n if(i-k-1>=0)\n {\n temp-=pref[i-k-1];\n }\n ans.push_back(temp/(2*k+1));\n }\n\n for(;i<n;i++)\n {\n ans.push_back(-1);\n }\n return ans;\n }\n};", "memory": "142360" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n vector<int> ans;\n int sum = 0;\n if(k == 0)\n return nums;\n int n = nums.size();\n vector<long long int> pre(n, 0);\n pre[0] = nums[0];\n for(int i = 1; i < n; i++){\n pre[i] = pre[i - 1] + nums[i];\n }\n\n // for(int i = 0; i < k && i < n; i++){\n // sum += nums[i];\n // ans.push_back(-1);\n\n // }\n for(int i = 0; i < n; i++){\n if(i + k >= n || i - k < 0){\n ans.push_back(-1);\n continue;\n }\n long long int avg = pre[i + k] - (i - k <= 0 ? 0 : pre[i - k - 1]);\n ans.push_back(avg/(2*k + 1));\n }\n return ans;\n \n }\n};\n", "memory": "142720" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& v, int k) {\n int len=2*k+1;\n int n=v.size();\n if(k==0){\n return v;\n }\n vector<int>ans;\n vector<long long>pre(n);\n pre[0]=v[0];\n for(int i=1;i<n;i++){\n pre[i]=v[i]+pre[i-1];\n }\n int i=0,j=0;\n while(i<n){\n if(i+k>=n || i-k<0){\n ans.push_back(-1);\n \n }\n else{\n long long x=pre[i+k];\n if(i-k>0){\n x-=pre[i-k-1];\n }\n ans.push_back(x/len);\n }\n i++;\n\n }\n return ans;\n }\n};", "memory": "143080" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n \n vector<int> getAverages(vector<int>& nums, int k) {\n vector<int>avgs;\n int n = nums.size();\n\n vector<long long>pref(n);\n \n pref[0]=nums[0];\n for(int i = 1; i < n; i++){\n pref[i] = nums[i] + pref[i-1];\n }\n\n\n\n // int n = nums.size();\n int windowSize = 2 * k + 1;\n for(int i = 0; i < n; i++){\n if(i - k >= 0 && i + k < n){\n int value = (pref[i + k] - (i - k - 1 >= 0 ? pref[i-k-1] : 0)) / (windowSize);\n avgs.push_back(value);\n } else{\n avgs.push_back(-1);\n }\n }\n\n return avgs;\n\n }\n};", "memory": "145240" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n\n vector<int> ans; \n\n // prefixSum array \n vector<long long> prefixSum(nums.size(), 0);\n\n // pre-processing: build the prefix Sum\n prefixSum[0] = nums[0]; \n\n for (int i = 1; i < nums.size(); i++)\n {\n prefixSum[i] = nums[i] + prefixSum[i - 1];\n }\n\n for (int i = 0; i < nums.size(); i++)\n {\n // check validity of bounds\n if (((i - k) >= 0) && ((i + k) < nums.size()))\n {\n // calculate and populate the average\n ans.push_back(( prefixSum[i + k] - prefixSum[i - k] + nums[i - k])/(2*k + 1));\n }\n else\n {\n ans.push_back(-1);\n }\n }\n\n return ans; \n \n }\n};", "memory": "145600" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n \n // if k = 0 avg is the original / input array\n if(0 == k) return nums;\n\n int len = nums.size();\n vector<int> ans(len, -1);\n \n // It is possible that the sum of all the elements does not fit in a 32-bit integer type.\n vector<long long> sums;\n long long currSum = 0;\n\n // window size is defined as the length of [i-k, i+k]\n int windowSize = 2*k + 1;\n\n // all elements of answer/response will be -1 in case the\n // input length is smaller than the windows size\n if(nums.size() < windowSize) return ans;\n \n // calculate the sums of all elements prior to nums[i] including nums[i]\n for(int i = 0; i < len; i++){\n currSum += nums[i];\n sums.push_back(currSum);\n } \n \n for(int j = k; j < (len - k); j++){\n int right = j+k, left = j-k;\n ans[j] = (sums[right] - sums[left] + nums[left])/(windowSize);\n \n }\n \n return ans;\n \n }\n};", "memory": "145960" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n \n if(k < 1){\n return nums;\n }else if((k*2+1) > nums.size()){\n vector<int> ans(nums.size(), -1);\n \n return ans;\n }\n \n vector<long long> preSum = {nums[0]};\n \n for(int i = 1; i < nums.size(); i++){\n preSum.push_back(preSum[i-1] + nums[i]);\n }\n \n vector<int> ans(nums.size(),-1);\n \n for(int i = k; i < nums.size() - k; i++){\n \n long long sum = preSum[i + k] - preSum[i - k] + nums[i - k];\n ans[i] = (sum / (k * 2 + 1));\n \n \n }\n \n return ans;\n \n \n \n \n \n }\n};", "memory": "146320" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n \n // if k = 0 avg is the original / input array\n if(0 == k) return nums;\n\n int len = nums.size();\n vector<int> ans(len, -1);\n \n // It is possible that the sum of all the elements does not fit in a 32-bit integer type.\n vector<long long> sums;\n long long currSum = 0;\n\n // window size is defined as the length of [i-k, i+k]\n int windowSize = 2*k + 1;\n\n // all elements of answer/response will be -1 in case the\n // input length is smaller than the windows size\n if(nums.size() < windowSize) return ans;\n \n // calculate the sums of all elements prior to nums[i] including nums[i]\n for(int i = 0; i < len; i++){\n currSum += nums[i];\n sums.push_back(currSum);\n } \n \n for(int j = k; j < (len - k); j++){\n int right = j+k, left = j-k;\n ans[j] = (sums[right] - sums[left] + nums[left])/(windowSize);\n \n }\n \n return ans;\n \n }\n};", "memory": "146680" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n \n // if k = 0 avg is the original / input array\n if(0 == k) return nums;\n\n int len = nums.size();\n vector<int> ans(len, -1);\n \n // It is possible that the sum of all the elements does not fit in a 32-bit integer type.\n vector<long long> sums;\n long long currSum = 0;\n\n // window size is defined as the length of [i-k, i+k]\n int windowSize = 2*k + 1;\n\n // all elements of answer/response will be -1 in case the\n // input length is smaller than the windows size\n if(nums.size() < windowSize) return ans;\n \n // calculate the sums of all elements prior to nums[i] including nums[i]\n for(int i = 0; i < len; i++){\n currSum += nums[i];\n sums.push_back(currSum);\n } \n \n for(int j = k; j < (len - k); j++){\n \n ans[j] = (sums[j+k] - sums[j-k] + nums[j-k])/(windowSize);\n \n }\n \n return ans;\n \n }\n};", "memory": "146680" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n vector<int > averages(nums.size(), -1);\n if(k == 0) {\n return nums;\n }\n \n int windowSize = 2*k + 1;\n \n if(windowSize > nums.size()) {\n return averages;\n }\n \n // Compute the prefix sum\n vector<long long> prefix_sum = {nums[0]};\n for(int i = 0; i < nums.size(); i++){\n prefix_sum.push_back(prefix_sum.back() + nums[i]);\n }\n \n for(int i = k; i < nums.size()-k; i++) {\n int leftBound = i-k;\n int rightBound = i+k;\n long long int Sum = (prefix_sum[rightBound + 1] - prefix_sum[leftBound]);\n long long int average = Sum / windowSize;\n averages[i] = average;\n }\n \n return averages;\n }\n};", "memory": "147040" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n \n vector<int> ans(nums.size(), -1);\n \n // It is possible that the sum of all the elements does not fit in a 32-bit integer type.\n vector<long long> sums;\n long long currSum = 0;\n\n // window size is defined as the length of [i-k, i+k]\n int windowSize = 2*k + 1;\n \n // if k = 0 avg is the original / input array\n if(0 == k) return nums;\n\n // all elements of answer/response will be -1 in case the\n // input length is smaller than the windows size\n if(nums.size() < windowSize) return ans;\n \n // calculate the sums of all elements prior to nums[i] including nums[i]\n for(int i = 0; i < nums.size(); i++){\n currSum += nums[i];\n sums.push_back(currSum);\n } \n \n for(int j = k; j < (nums.size() - k); j++){\n \n ans[j] = (sums[j+k] - sums[j-k] + nums[j-k])/(windowSize);\n \n }\n \n return ans;\n \n }\n};", "memory": "147400" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n \n vector<int> ans(nums.size(), -1);\n vector<long long> sums;\n long long currSum = 0;\n \n // if k = 0 avg is the original / input array\n if(0 == k) return nums;\n if(nums.size() < 2*k + 1) return ans;\n \n // calculate the sums of all elements prior to nums[i] including nums[i]\n for(int i = 0; i < nums.size(); i++){\n currSum += nums[i];\n sums.push_back(currSum);\n } \n \n for(int j = k; j < (nums.size() - k); j++){\n \n ans[j] = (sums[j+k] - sums[j-k] + nums[j-k])/(2*k+1);\n \n }\n \n return ans;\n \n }\n};", "memory": "147760" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n \n vector<int> ans(nums.size(), -1);\n vector<long long> sums;\n\n // int can't hold the sum of all elements\n long long currSum = 0;\n\n // window size is defined as the length of [i-k, i+k]\n int windowSize = 2*k + 1;\n \n // if k = 0 avg is the original / input array\n if(0 == k) return nums;\n\n // all elements of answer/response will be -1 in case the\n // input length is smaller than the windows size\n if(nums.size() < windowSize) return ans;\n \n // calculate the sums of all elements prior to nums[i] including nums[i]\n for(int i = 0; i < nums.size(); i++){\n currSum += nums[i];\n sums.push_back(currSum);\n } \n \n for(int j = k; j < (nums.size() - k); j++){\n \n ans[j] = (sums[j+k] - sums[j-k] + nums[j-k])/(windowSize);\n \n }\n \n return ans;\n \n }\n};", "memory": "147760" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n long long sum = 0;\n int n = nums.size();\n int windowSize = k * 2 + 1;\n vector<int> averages(n, -1);\n\n if(k == 0) return nums;\n\n if(windowSize > n) return averages;\n\n vector<long long> prefixSum = {nums[0]};\n for(int i = 1; i < n; i++){\n prefixSum.push_back(prefixSum[i - 1] + nums[i]);\n }\n\n for(int i = k; i < (n - k); i++){\n sum = prefixSum[i + k] - prefixSum[i - k] + nums[i - k];\n averages[i] = sum / windowSize;\n }\n\n return averages;\n }\n};", "memory": "148120" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n long long sum = 0;\n int n = nums.size();\n int windowSize = k * 2 + 1;\n vector<int> averages(n, -1);\n\n if(k == 0) return nums;\n\n if(windowSize > n) return averages;\n\n vector<long long> prefixSum = {nums[0]};\n for(int i = 1; i < n; i++){\n prefixSum.push_back(prefixSum[i - 1] + nums[i]);\n }\n\n for(int i = k; i < (n - k); i++){\n sum = prefixSum[i + k] - prefixSum[i - k] + nums[i - k];\n averages[i] = sum / windowSize;\n }\n\n return averages;\n }\n};", "memory": "148120" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size();\n \n \n if(k == 0) return nums;\n \n vector<int> ans(n, -1);\n if (n < 2 * k + 1) return vector<int>(n, -1);\n \n \n vector<long long> prefix{nums[0]};\n \n for(int i = 1; i < n; i++)\n {\n prefix.push_back(prefix.back() + nums[i]);\n }\n \n // 0 1 2 3 4 5 6, 7, 8\n // 7,4, 3, 9, 1, 8, 5, 2, 6\n // 7,11 14,23,24,32,37,39,45 -- output 3\n //[40527, 53696, 10730, 66491, 62141, 83909, 78635, 18560\n //[40527, 94223, 104953, 171444, 233585, 317494, 396129, 414689\n int divisor = (k*2)+1;\n int endPtr = nums.size()-k;\n for(int i = k; i < endPtr; i++)\n {\n long long value = prefix[i+k] - prefix[i-k] + nums[i-k];\n ans[i] = value/(divisor);\n }\n \n return ans;\n }\n};", "memory": "148480" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size();\n \n \n if(k == 0) return nums;\n \n vector<int> ans(n, -1);\n if (n < 2 * k + 1) return vector<int>(n, -1);\n \n \n vector<long long> prefix{nums[0]};\n \n for(int i = 1; i < n; i++)\n {\n prefix.push_back(prefix.back() + nums[i]);\n }\n \n // 0 1 2 3 4 5 6, 7, 8\n // 7,4, 3, 9, 1, 8, 5, 2, 6\n // 7,11 14,23,24,32,37,39,45 -- output 3\n //[40527, 53696, 10730, 66491, 62141, 83909, 78635, 18560\n //[40527, 94223, 104953, 171444, 233585, 317494, 396129, 414689\n int divisor = (k*2)+1;\n int endPtr = nums.size()-k;\n for(int i = k; i < endPtr; i++)\n {\n long long value = prefix[i+k] - prefix[i-k] + nums[i-k];\n ans[i] = value/(divisor);\n }\n \n return ans;\n }\n};", "memory": "148840" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n vector<int> ans;\n long long sum=0;\n\n int i=0,j=0;\n while(i<=j && j<nums.size()){\n sum+=nums[j];\n\n while(j-i+1 > (2*k +1)){\n sum = sum - nums[i];\n i++;\n }\n\n if(j-i+1 == (2*k +1)){\n ans.push_back(sum/(2*k +1));\n }\n else{\n ans.push_back(-1);\n }\n\n j++;\n }\n if(k>nums.size()){\n return ans;\n }\n vector<int> a2;\n for(int i=k;i<nums.size();i++){\n a2.push_back(ans[i]);\n }\n for(int i=0;i<k;i++){\n a2.push_back(ans[i]);\n }\n return a2;\n }\n};", "memory": "149200" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n bool validIdx(int idx, int n) {\n return idx >= 0 && idx < n;\n } \npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> res(n, -1);\n\n vector<long long> pref(n, 0);\n pref[0] = nums[0];\n for (int i = 1; i < n; i++) {\n pref[i] = pref[i - 1] + nums[i];\n }\n\n \n vector<long long> suf(n, 0);\n suf[n - 1] = nums[n - 1];\n for (int i = n - 2; i >= 0; i--) {\n suf[i] = suf[i + 1] + nums[i];\n }\n\n for (int i = k; i < n - k; i++) {\n \n long long sum = (i - k > 0 ? pref[i + k] - pref[i - k - 1] : pref[i + k]);\n res[i] = sum / (2 * k + 1);\n }\n\n return res;\n }\n};\n", "memory": "149560" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size();\n vector<long long> pref(n, 0), suff(n, 0); // Use long long to avoid overflow\n\n // Build prefix sum array\n pref[0] = nums[0];\n for (int i = 1; i < n; i++) {\n pref[i] = pref[i - 1] + nums[i];\n }\n\n // Build suffix sum array\n suff[n - 1] = nums[n - 1];\n for (int i = n - 2; i >= 0; i--) {\n suff[i] = suff[i + 1] + nums[i];\n }\n\n vector<int> ans(n, -1); // This remains int, as the averages are integers\n\n // Calculate k-radius averages using prefix and suffix sums\n for (int i = k; i < n - k; i++) {\n long long leftSum = pref[i] - (i - k - 1 >= 0 ? pref[i - k - 1] : 0);\n long long rightSum = suff[i] - (i + k + 1 < n ? suff[i + k + 1] : 0);\n long long totalSum = leftSum + rightSum - nums[i];\n ans[i] = totalSum / (2 * k + 1); // Integer division\n }\n\n return ans;\n }\n};\n", "memory": "149920" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n=nums.size();\n vector<long long> prek(n);\n long long sum=0;\n for(int i=0;i<n;i++){\n sum+=nums[i];\n if(i-k-1>=0)sum-=nums[i-k-1];\n prek[i]=sum;\n }\n vector<long long> sufk(n);\n sum=0;\n for(int i=n-1;i>=0;i--){\n sum+=nums[i];\n if(i+k+1<n)sum-=nums[i+k+1];\n sufk[i]=sum;\n }\n vector<int> ans(n,-1);\n for(int i=k;i<=n-1-k;i++){\n long long avg=(prek[i]+sufk[i]-nums[i])/((2*k)+1);\n ans[i]=avg;\n } \n return ans;\n }\n};", "memory": "149920" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n = nums.size();\n vector<long long> pre(n, 0), suf(n, 0);\n pre[0] = nums[0];\n suf[n-1] = nums[n-1];\n for(int i = 1; i < n; i++) {\n pre[i] = nums[i] + pre[i-1];\n }\n for(int i = n-2; i >= 0; i--) {\n suf[i] = nums[i] + suf[i+1];\n }\n vector<int> ans(n, -1);\n for(int i = k; i < n-k; i++) {\n long long sumleft = pre[i+k] - pre[i];\n long long sumright = suf[i-k] - suf[i];\n long long avg = (sumleft + sumright + nums[i]) / (k*2 + 1);\n ans[i] = avg;\n }\n return ans;\n }\n};", "memory": "150280" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n=nums.size();\n vector<long long >prefix(n,0);\n vector<long long>suffix(n,0);\n long long sum=0;\n for(int i=0;i<n;i++){\nsum+=nums[i];\nprefix[i]=sum;\n\n }\nsum=0;\n for(int i=n-1;i>=0;i--){\nsum+=nums[i];\nsuffix[i]=sum;\n }\n vector<int>ans(n,-1);\n for(int i=0;i<n;i++){\n int left=i-k;\n int right=i+k;\n if(left>=0&&right<n){\n \nans[i]=(prefix[i]-prefix[left]+nums[left]+suffix[i]-suffix[right]+nums[right]-nums[i])/(2*k+1);\n\n\n }\n\n }return ans;\n }\n};", "memory": "150640" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int>ans(n,-1);\n\n if(nums.size() <= (2*k))\n {\n return ans;\n } \n else\n {\n int right=0, left=0;\n vector<long long> sum;\n sum.push_back(nums[0]);\n for(int i=1; i<nums.size(); i++)\n {\n sum.push_back(sum.back() + nums[i]);\n }\n\n for(int right=(2*k); right<nums.size(); right++)\n {\n ans[left+k] = ((sum[right]-sum[left]+nums[left])/(2*k+1));\n left++;\n }\n }\n\n return ans;\n \n }\n};", "memory": "151000" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int>ans(n,-1);\n\n if(nums.size() <= (2*k))\n {\n return ans;\n } \n else\n {\n int right=0, left=0;\n vector<long long> sum;\n sum.push_back(nums[0]);\n for(int i=1; i<nums.size(); i++)\n {\n sum.push_back(sum.back() + nums[i]);\n }\n\n for(int right=(2*k); right<nums.size(); right++)\n {\n if((right-left) == (2*k))\n {\n ans[left+k] = ((sum[right]-sum[left]+nums[left])/(2*k+1));\n left++;\n }\n }\n }\n\n return ans;\n \n }\n};", "memory": "151000" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n vector<long long >sum;\n if(k==0)\n return nums;\n \n long long acc=0;\n for(int i=0;i<nums.size();i++){\n acc=acc+nums[i];\n sum.push_back(acc);\n }\n vector<int>ans(nums.size(),-1);\n if(nums.size()<=k*2)\n return ans;\n\n \n for(int i=k;i<nums.size()-k;i++){\n long long prev=0;\n if(i-k-1>=0)\n prev=sum[i-k-1];\n ans[i]=(sum[i+k]-prev)/(2*k+1);\n }\n\n\n return ans;\n }\n};", "memory": "151360" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n \n if (k == 0) {\n return nums;\n }\n \n int n = nums.size();\n std::vector<long> prefix = {nums[0]};\n for (int i = 1; i < n; i++) {\n prefix.push_back(prefix.back() + nums[i]);\n }\n \n std::vector<int> ans(n, -1);\n \n if (2 * k >= n) {\n return ans;\n }\n \n for (int i = k; i < n - k; i++) {\n long k_sum = prefix[i + k] - prefix[i - k] + nums[i - k];\n int k_avg = k_sum / (2 * k + 1);\n ans[i] = k_avg;\n }\n \n return ans;\n }\n};", "memory": "151720" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n \n if (k==0)\n {\n return nums; // average of every single element is just itself\n }\n \n int n = nums.size();\n \n int denominator = 2*k + 1;\n \n vector<int> avgs(n);\n \n // create a prefix sum array\n vector<long> prefix = {nums[0]};\n for (int i=1; i<n; i++)\n {\n prefix.push_back(nums[i] + prefix.back());\n }\n \n for (int i=0; i<n; i++)\n {\n if ((k>i) || (k+i >= n))\n {\n avgs[i] = -1; \n }\n else\n {\n long total;\n if ((i-k-1) < 0)\n {\n total = prefix[i+k];\n }\n else\n {\n total = prefix[i+k] - prefix[i-k-1];\n }\n \n avgs[i] = total / denominator;\n }\n \n }\n \n return avgs; \n \n }\n};", "memory": "152080" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n vector<int> result(nums.size(), -1);\n if( k == 0) return nums;\n\n vector<long> prefixSum { nums[0]};\n for(int i = 1 ; i < nums.size(); ++i)\n {\n prefixSum.push_back(prefixSum.back() + nums[i]);\n }\n\n long sumDeduct = 0;\n for(int i = k; i + k < nums.size(); ++i)\n {\n if( i > k)\n {\n sumDeduct += nums[i - k - 1];\n cout << i << \" \" << k << \" deduct : \" << sumDeduct << endl;\n }\n result[i] = (prefixSum[i + k] - sumDeduct)/ (2*k + 1);\n }\n return result;\n }\n};", "memory": "152440" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n \n if(k < 1){\n\n return nums;\n }else if((k*2+1) > nums.size()){\n vector<int> ans(nums.size(), -1);\n \n return ans;\n }\n \n vector<long long> preSum = {nums[0]};\n \n for(int i = 1; i < nums.size(); i++){\n preSum.push_back(preSum[i-1] + nums[i]);\n }\n \n vector<int> ans;\n \n for(int i = 0; i < nums.size(); i++){\n \n if(i < k){\n ans.push_back(-1);\n }else if((i+k) >= nums.size()){\n ans.push_back(-1);\n }else{\n long long sum = preSum[i + k] - preSum[i - k] + nums[i - k];\n ans.push_back(sum / (k * 2 + 1));\n }\n \n \n \n }\n \n return ans;\n \n \n \n \n \n }\n};", "memory": "152440" }
2,211
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> (<strong>inclusive</strong>). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array </em><code>avgs</code><em> of length </em><code>n</code><em> where </em><code>avgs[i]</code><em> is the <strong>k-radius average</strong> for the subarray centered at index </em><code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" style="width: 343px; height: 119px;" /> <pre> <strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3 <strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1] <strong>Explanation:</strong> - avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index. - The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37. Using <strong>integer division</strong>, avg[3] = 37 / 7 = 5. - For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4. - For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4. - avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [100000], k = 0 <strong>Output:</strong> [100000] <strong>Explanation:</strong> - The sum of the subarray centered at index 0 with radius 0 is: 100000. avg[0] = 100000 / 1 = 100000. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [8], k = 100000 <strong>Output:</strong> [-1] <strong>Explanation:</strong> - avg[0] is -1 because there are less than k elements before and after index 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i], k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getAverages(vector<int>& nums, int k) {\n vector<long long> sums;\n \n sums.push_back(0);\n int div = 2*k+1;\n \n for (int i = 0; i < nums.size(); i++) {\n sums.push_back(nums[i] + sums[i]);\n }\n \n vector<int> avgs(nums.size());\n \n for (int i = 0; i < nums.size(); i++) {\n if (i - k < 0 || i + k >= nums.size()) {\n avgs[i] = -1;\n }\n else {\n avgs[i] = (sums[i+k+1] - sums[i-k]) / (div);\n }\n }\n \n return avgs;\n }\n};", "memory": "152800" }