id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n int dp[n];\n if(nums[0] == 0){dp[0] = 1;}\n else{dp[0] = 0;}\n for(int i = 1; i < n; i++){\n if(nums[i] != nums[i - 1]){\n dp[i] = dp[i - 1] + 1;\n }\n else{\n dp[i] = dp[i - 1];\n }\n }\n return dp[n - 1];\n }\n};", "memory": "156100" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n \n\n int const n = nums.size();\n int z[n+1];\n int o[n+1];\n\n z[n] = 0;\n o[n] = 0;\n\n \n\n for (int i = n-1; i>=0; i--){\n cout<<i;\n if (nums[i]){\n z[i] = o[i+1] +1;\n o[i] = o[i+1];\n } else{\n o[i] = z[i+1] +1;\n z[i] = z[i+1];\n }\n }\n\n return o[0];\n \n \n }\n};", "memory": "156500" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n int const n = 100000;\n bitset<n+1> bits;\n bitset<n+1> mask;\n\n for(int i = 0;i<n;i++){\n mask[i] = 1;\n }\n\n int i = nums.size()-1;\n for(auto it:nums){\n bits[i--] = it;\n }\n\n int cnt = 0;\n for(int i=nums.size()-1;i>=0;i--){\n int shift = nums.size() - i;\n if(bits[i] != 1){\n bits = bits^(mask>>shift);\n cnt++;\n }\n }\n\n return cnt;\n }\n};", "memory": "156700" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "\n#define endl '\\n'\n#define pint pair<int, int>\n#define vint vector<int>\n#define vpint vector<pair<int, int>>\n#define vstr vector<string>\n#define uset unordered_set\n#define umap unordered_map\n#define vbool vector<bool>\n#define vvint vector<vector<int>>\n#define vvvint vector<vector<vector<int>>>\n#define ll long long\n#define MOD 1000000007\n\nclass Solution {\npublic:\n int helper(vint& nums, int pos, bool flipAll) {\n int n = nums.size();\n\n if (pos >= n)\n return 0;\n\n int ans = 1000000;\n\n int curr = flipAll ? nums[pos] ^ 1 : nums[pos];\n\n if (curr == 1)\n ans = min(ans, helper(nums, pos + 1, flipAll));\n else\n ans = min(ans, 1 + helper(nums, pos + 1, !flipAll));\n\n return ans;\n }\n\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n\n int ans = helper(nums, 0, false);\n return ans;\n }\n};", "memory": "157000" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "#define ll long long int\nclass Solution {\npublic:\n // for this we have \n ll solve(ll idx,vector<int>&nums,ll n,int f)\n {\n if(idx==n)\n return 0;\n ll ans=INT_MAX;\n if((nums[idx]==0)&&(f%2))\n {\n ans=min(ans,solve(idx+1,nums,n,f)); \n }\n else if((nums[idx]==0)&&(f%2==0))\n {\n ans=min(ans,1+solve(idx+1,nums,n,f^1));\n }\n else if((nums[idx]==1)&&(f%2))\n {\n ans=min(ans,1+solve(idx+1,nums,n,f^1));\n }\n else\n {\n ans=min(ans,solve(idx+1,nums,n,f));\n }\n return ans;\n }\n int minOperations(vector<int>& nums) \n {\n ll n=nums.size();\n int f=0;\n return solve(0,nums,n,f);\n }\n};", "memory": "157100" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution1 {\n int f1(int i,vector<int> &nums){\n if(i==nums.size())return 0;\n\n if(nums[i]==0){\n //flip from i to n-1\n for(int idx=i;idx<nums.size();idx++)nums[idx]=!nums[idx];\n\n return 1+f1(i+1,nums);\n }\n\n return f1(i+1,nums);\n }\n public:\n int minOperations(vector<int>& nums) {\n return f1(0,nums);\n }\n};\n\n\nclass Solution {\n int f1(int i, bool flipped , vector<int> &nums){\n if(i==nums.size())return 0;\n\n if( (flipped && nums[i]==1) or (!flipped && nums[i]==0)){\n return 1+f1(i+1,!flipped,nums);\n }\n\n return f1(i+1,flipped,nums);\n }\npublic:\n int minOperations(vector<int>& nums) {\n return f1(0,false,nums);\n }\n};", "memory": "157200" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n\n int solve(vector<int>&nums,int i,int cnt){\n if(i == nums.size()) return 0;\n int res = 0;\n if(cnt%2 != nums[i]) res = solve(nums,i+1,cnt);\n else res = 1 + solve(nums,i+1,cnt+1);\n return res;\n }\n int minOperations(vector<int>& nums) {\n return solve(nums,0,0);\n }\n};", "memory": "158000" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n static const int mn = 1e5 + 10;\n int dp[mn];\n int minOpsByIndex(int i, vector<int>& nums, int flip = 0) {\n int n = nums.size();\n if(i == n - 1) {\n if(flip == 0)\n return dp[i] = (nums[n - 1] == 0? 1 : 0);\n return dp[i] = (1 - nums[n - 1] == 0 ? 1 : 0);\n }\n if(dp[i] >= 0) return dp[i];\n int ans = 1e9 + 7;\n int j = i + 1;\n if(flip == 1) {\n if(1 - nums[i] == 0)\n ans = min(ans, 1 + minOpsByIndex(j, nums, flip = 1 - flip)); \n else ans = min(ans, minOpsByIndex(j, nums, flip = flip));\n } else {\n if(nums[i] == 0)\n ans = min(ans, 1 + minOpsByIndex(j, nums, 1 - flip)); \n else ans = min(ans, minOpsByIndex(j, nums, flip));\n }\n \n dp[i] = ans;\n return ans;\n\n }\n int minOperations(vector<int>& nums) {\n int n = nums.size();\n\n for(int i = 0; i < n; i++) dp[i] = -1;\n int ans = minOpsByIndex(0, nums, 0);\n for(int i = 0; i < n; i++) cout << dp[i] << endl;\n return ans;\n }\n};", "memory": "159100" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n int dp[100005][2];\n int rec(int level, int target, vector<int>& nums){\n if(level==n){\n return 0;\n }\n if(dp[level][target]!=-1){\n return dp[level][target];\n }\n\n int ans = 1e9;\n if(nums[level]==target){\n ans = min(ans,rec(level+1,target,nums));\n }\n else{\n ans = min(ans,1+rec(level+1,!target,nums));\n }\n\n return dp[level][target] = ans;\n }\n int minOperations(vector<int>& nums) {\n n=nums.size();\n memset(dp,-1,sizeof(dp));\n int ans = rec(0,1,nums);\n return ans;\n }\n};", "memory": "159400" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dp[100001][2];\n int solve(int level, int op,vector<int>&nums ){\n if(level==nums.size()){\n return 0;\n }\n if(dp[level][op] != -1){\n return dp[level][op];\n }\n int ans;\n if(op == 0){\n if(nums[level]==0){\n ans = 1+solve(level+1,(op+1)%2,nums);\n }\n else{\n ans = solve(level+1,op,nums);\n }\n }\n else{\n if(nums[level]==1){\n ans = 1+solve(level+1,(op+1)%2,nums);\n \n }\n else{\n ans = solve(level+1,op,nums);\n }\n }\n return dp[level][op] = ans;\n }\n int minOperations(vector<int>& nums) {\n memset(dp,-1,sizeof(dp));\n return solve(0,0,nums); \n }\n};", "memory": "159600" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dp[100001][2];\n int solve(int level, int op,vector<int>&nums ){\n if(level==nums.size()){\n return 0;\n }\n if(dp[level][op] != -1){\n return dp[level][op];\n }\n int ans;\n if(op == 0){\n if(nums[level]==0){\n ans = 1+solve(level+1,(op+1)%2,nums);\n }\n else{\n ans = solve(level+1,op,nums);\n }\n }\n else{\n if(nums[level]==1){\n ans = 1+solve(level+1,(op+1)%2,nums);\n \n }\n else{\n ans = solve(level+1,op,nums);\n }\n }\n return dp[level][op] = ans;\n }\n int minOperations(vector<int>& nums) {\n memset(dp,-1,sizeof(dp));\n return solve(0,0,nums); \n }\n};", "memory": "159700" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& n) {\n string nums = \"\";\n for(int i=0;i<n.size();i++) nums+=(n[i]+'0');\n int total = unique(nums.begin(),nums.end()) - nums.begin();\n cout<<total<<\" \"<<nums;\n if(nums[0]=='1') total--;\n return total;\n }\n};", "memory": "159900" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n deque<int>dq;\n int ans=0;\n\n for(int i=0;i<nums.size();i++){\n if( (nums[i]+dq.size()) % 2 == 0){ //True value is zero\n if(i>nums.size()){\n return -1;\n }\n ans++;\n dq.push_back(i);\n }\n }\n\n return ans;\n }\n};", "memory": "160000" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n queue<int> q;\n int ans=0;\n int n=nums.size();\n for(int i=0;i<n;i++){\n if((nums[i]+q.size())%2==0){\n q.push(i);\n ans++;\n }\n }\n return ans;\n \n }\n};", "memory": "160100" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n Solution(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n }\n int minOperations(vector<int>& nums) {\n int ans=0,n=nums.size();\n queue<int>q;\n for(int i=0; i<n; i++){\n if((q.size() + nums[i])%2 == 0){\n ans++;\n q.push(i);\n }\n }\n return ans;\n }\n};", "memory": "160300" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "#define ll long long int\nclass Solution {\npublic:\n // for this we have \n ll dp[100005][2];\n ll solve(ll idx,vector<int>&nums,ll n,int f)\n {\n if(idx==n)\n return 0;\n if(dp[idx][f]!=-1)\n return dp[idx][f];\n ll ans=INT_MAX;\n if((nums[idx]==0)&&(f%2))\n {\n ans=min(ans,solve(idx+1,nums,n,f)); \n }\n else if((nums[idx]==0)&&(f%2==0))\n {\n ans=min(ans,1+solve(idx+1,nums,n,f^1));\n }\n else if((nums[idx]==1)&&(f%2))\n {\n ans=min(ans,1+solve(idx+1,nums,n,f^1));\n }\n else\n {\n ans=min(ans,solve(idx+1,nums,n,f));\n }\n dp[idx][f]=ans;\n return ans;\n }\n int minOperations(vector<int>& nums) \n {\n memset(dp,-1,sizeof dp);\n ll n=nums.size();\n int f=0;\n return solve(0,nums,n,f);\n }\n};", "memory": "160500" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums) {\n\n int n=nums.size();\n vector<int> rgtz(n,0);\n int z=0;\n for(int i=n-1;i>=0;i--)\n { \n if(!(nums[i]))\n {\n z++;\n }\n rgtz[i]=z;\n }\n int c=0;\n for(int i=0;i<n;i++)\n {\n if(nums[i]&&(c&1))\n {\n c++;\n }\n else if(!nums[i] && (c%2==0))\n {\n c++;\n }\n }\n return c;\n \n }\n};", "memory": "162100" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint minOperations(vector<int> &nums)\n{\n int size = nums.size();\n int operations = 0;\n int i = 0;\n vector<int> flippingCounts(size, 0);\n int mod = 0;\n while (i < size)\n {\n if (nums[i] == mod)\n {\n flippingCounts[i]++;\n operations++;\n mod = (mod == 0) ? 1 : 0;\n }\n i++;\n }\n\n // print1d(flippingCounts);\n int totalFlipsRequired = 0;\n\n for (i = 0; i < size; i++)\n {\n totalFlipsRequired += flippingCounts[i];\n if (totalFlipsRequired % 2 == 1)\n {\n nums[i] = (nums[i] == 0) ? 1 : 0;\n }\n }\n\n for (int i = 0; i < size; i++)\n {\n if (nums[i] == 0)\n {\n return -1;\n }\n }\n\n return operations;\n}\n};", "memory": "162200" }
3,477
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
3
{ "code": "class Solution \n{\npublic:\n int minOperations(vector<int>&v) \n {\n int ans=0;\n vector<int>t(v.size()+1,0);\n for(int i=0;i<v.size();i++)\n {\n if(i==0)\n {\n if(v[i])\n {\n //pass\n }\n else\n {\n t[i]++;\n ans++;\n t.back()--;\n }\n }\n else\n {\n t[i]+=t[i-1];\n if(t[i]%2)\n {\n v[i]=!v[i];\n }\n if(v[i])\n {\n //pass\n }\n else\n {\n t[i]++;\n ans++;\n t.back()--;\n }\n }\n }\n return ans;\n }\n};", "memory": "162300" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& req) {\n vector<int> a(n, -1);\n int m = req.size();\n for(int i = 0; i < m; i++)\n a[req[i][0]] = req[i][1];\n if(a[0] != -1 and a[0] != 0)\n return 0;\n int mod = 1e9 + 7;\n int k = 401;\n int dpprev[k], dpcurr[k];\n for(int l = 0; l < k; l++)\n dpprev[l] = 0, dpcurr[l] = 0;\n dpprev[0] = 1;\n for(int i = 1; i < n; i++){\n if(a[i] == -1){\n for(int j = 0; j <= i; j++)\n for(int l = 0; l < k; l++)\n if(l + i - j < k)\n dpcurr[l + i - j] = (dpcurr[l + i - j] + dpprev[l]) % mod;\n }\n else{\n for(int j = 0; j <= i; j++){\n int l = a[i] + j - i;\n if(0 <= l and l < k)\n dpcurr[a[i]] = (dpcurr[a[i]] + dpprev[l]) % mod;\n }\n }\n for(int l = 0; l < k; l++)\n dpprev[l] = dpcurr[l], dpcurr[l] = 0;\n }\n int ans = 0;\n return dpprev[a[n - 1]];\n }\n};", "memory": "27200" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& req) {\n vector<int> a(n, -1);\n int m = req.size();\n for(int i = 0; i < m; i++)\n a[req[i][0]] = req[i][1];\n if(a[0] != -1 and a[0] != 0)\n return 0;\n int mod = 1e9 + 7;\n int k = 401;\n int dpprev[k], dpcurr[k];\n for(int l = 0; l < k; l++)\n dpprev[l] = 0, dpcurr[l] = 0;\n dpprev[0] = 1;\n for(int i = 1; i < n; i++){\n if(a[i] == -1){\n // cout << \"here\" << endl;\n for(int j = 0; j <= i; j++)\n for(int l = 0; l < k; l++)\n if(l + i - j < k)\n dpcurr[l + i - j] = (dpcurr[l + i - j] + dpprev[l]) % mod;\n }\n else{\n // cout << \"here\" << endl;\n for(int j = 0; j <= i; j++)\n for(int l = 0; l < k; l++)\n if(l + i - j == a[i])\n dpcurr[l + i - j] = (dpcurr[l + i - j] + dpprev[l]) % mod;\n }\n for(int l = 0; l < k; l++)\n dpprev[l] = dpcurr[l], dpcurr[l] = 0;\n }\n int ans = 0;\n return dpprev[a[n - 1]];\n }\n};", "memory": "27200" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& requirements) {\n vector<long long> dp(401, 0), dp_prev(401, 0);\n long long mdl = 1000000007;\n unordered_map<int, int> req;\n for (const auto &r : requirements) {\n if (r[0] == 0 && r[1] > 0) {\n return 0;\n }\n req[r[0]] = r[1];\n }\n dp[0] = 1;\n for (int i = 1 ; i < n ; ++i) {\n for (int j = 0 ; j <= 400 ; ++j) {\n dp_prev[j] = dp[j];\n dp[j] = 0;\n }\n if (req.find(i) != req.end()) {\n int j = req[i];\n for (int k = max(0, j-i) ; k <= j ; ++k) {\n dp[j] += dp_prev[k];\n dp[j] %= mdl;\n }\n if (i == n-1) return dp[j];\n continue;\n }\n for (int j = 0 ; j <= 400 ; ++j) {\n for (int k = max(0, j-i) ; k <= j ; ++k) {\n dp[j] += dp_prev[k];\n dp[j] %= mdl;\n }\n }\n }\n return -1;\n }\n};", "memory": "30801" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
1
{ "code": "class Solution {\n const int M = 1e9 + 7;\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& requirements) {\n vector<int> req(n, -1);\n req[0] = 0;\n for (auto& r : requirements) {\n req[r[0]] = r[1];\n }\n if (req[0] > 0) return 0;\n\n int m = ranges::max(req);\n vector<vector<int>> f(n, vector<int>(m + 1));\n f[0][0] = 1;\n for (int i = 1; i < n; ++i) {\n int mx = req[i] >= 0 ? req[i] : m;\n int r = req[i - 1];\n if (r >= 0) {\n for (int j = r; j <= min(i + r, mx); ++j) {\n f[i][j] = f[i - 1][r];\n }\n } else {\n for (int j = 0; j <= mx; ++j) {\n for (int k = 0; k <= min(i, j); ++k) {\n f[i][j] = (f[i][j] + f[i - 1][j - k]) % M;\n }\n }\n }\n }\n return f[n - 1][req[n - 1]];\n }\n};", "memory": "30801" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
1
{ "code": "class Solution {\n using ll = long long;\n const static ll mod = 1e9 + 7;\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& requirements) {\n // dp[i][j]\n vector<int> r(n, -1);\n int mx = 0;\n for (auto &v : requirements) {\n r[v[0]] = v[1];\n mx = max(mx, v[1]);\n }\n if (r[0] > 0) return 0;\n vector<vector<int>> dp(n, vector<int>(mx + 1));\n dp[0][0] = 1;\n for (int i = 1; i < n; ++i) {\n if (r[i] != -1) {\n int j = r[i];\n for (int k = 0; k <= min(i,j); ++k) {\n dp[i][j] = (dp[i][j] + dp[i - 1][j - k]) % mod;\n }\n } else {\n for (int j = 0; j <= mx; ++j) {\n for (int k = 0; k <= min(i,j); ++k) {\n dp[i][j] = (dp[i][j] + dp[i - 1][j - k]) % mod;\n }\n }\n }\n }\n return dp[n - 1][r[n - 1]];\n }\n};", "memory": "34403" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& requirements) {\n constexpr int MOD = 1'000'000'007;\n std::vector<int> end_to_count(n, -1);\n int max_inversions = 0;\n for (const auto &requirement : requirements)\n {\n end_to_count[requirement[0]] = requirement[1];\n max_inversions = std::max(max_inversions, requirement[1]);\n }\n if (end_to_count[0] > 0) return 0;\n end_to_count[0] = 0;\n std::vector<std::vector<int> > dp(n, std::vector<int>(max_inversions + 1, 0));\n dp[0][0] = 1;\n for (int i = 1; i < n; ++i)\n {\n int l = 0, r = max_inversions;\n if (end_to_count[i] >= 0)\n l = r = end_to_count[i];\n for (int j = l; j <= r; ++j)\n for (int k = 0; k <= std::min(i, j); ++k)\n dp[i][j] = (dp[i][j] + dp[i - 1][j - k]) % MOD;\n }\n return dp[n - 1][end_to_count[n - 1]];\n }\n};", "memory": "34403" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
2
{ "code": "int mod = int(1e9) + 7;\nclass Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& r) {\n sort(r.begin(), r.end());\n int k = r.back()[1];\n vector<long long> dp(k+1);\n dp[0] = 1;\n for(int i=1;i<r.size();i++) if(r[i-1][1] > r[i][1]) return 0; // check increasing cnt\n int pre = 0;\n for(int i=0;i<r.size();i++){\n int e = r[i][0], c = r[i][1];\n // appending element with array size from pre to end_i\n for(int l=pre;l <= e;l++){\n vector<long long> ndp(k+1);\n long long su = 0;\n // windowing for appending element to array with length = l\n for(int m=0;m <= k;m++){ \n su += dp[m];\n if(m >= l + 1) su -= dp[m-l-1];\n su %= mod;\n if(su < 0) su += mod;\n ndp[m] = su;\n }\n dp = std::move(ndp);\n }\n for(int i=0;i<=k;i++) if(i != c) dp[i] = 0; // only take cnt_i\n pre = e + 1;\n \n }\n long long ans = 0;\n for(int i=0;i<=k;i++) ans += dp[i];\n return ans;\n \n }\n};", "memory": "38004" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
2
{ "code": "int mod = int(1e9) + 7;\nclass Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& r) {\n sort(r.begin(), r.end());\n int k = r.back()[1];\n vector<long long> dp(k+1);\n dp[0] = 1;\n for(int i=1;i<r.size();i++) if(r[i-1][1] > r[i][1]) return 0; // check increasing cnt\n int pre = 0;\n for(int i=0;i<r.size();i++){\n int e = r[i][0], c = r[i][1];\n // appending element with array size from pre to end_i\n for(int l=pre;l <= e;l++){\n vector<long long> ndp(k+1);\n long long su = 0;\n // windowing for appending element to array with length = l\n for(int m=0;m <= k;m++){ \n su += dp[m];\n if(m >= l + 1) su -= dp[m-l-1];\n su %= mod;\n if(su < 0) su += mod;\n ndp[m] = su;\n }\n dp = std::move(ndp);\n }\n for(int i=0;i<=k;i++) if(i != c) dp[i] = 0; // only take cnt_i\n pre = e + 1;\n \n }\n long long ans = 0;\n for(int i=0;i<=k;i++) ans += dp[i];\n return ans;\n \n }\n};", "memory": "41605" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
2
{ "code": "#include \"bits/stdc++.h\"\nusing namespace std;\ntemplate <class T, size_t size=tuple_size<T>::value>\nstring to_debug(T, string s=\"\")\n\trequires(not ranges::range<T>);\nstring to_debug(auto x)\n\trequires requires(ostream &os) { os<<x; }\n{\n\treturn static_cast<ostringstream>(ostringstream()<<x).str();\n}\nstring to_debug(ranges::range auto x, string s=\"\")\n\trequires(not is_same_v<decltype(x), string>)\n{\n\tfor (auto xi:x) s+=\", \"+to_debug(xi);\n\n\treturn \"[\"+s.substr(s.empty()?0:2)+\"]\";\n}\ntemplate <class T, size_t size>\nstring to_debug(T x, string s)\n\trequires(not ranges::range<T>)\n{\n\t[&] <size_t... I>(index_sequence<I...>) { ((s+=\", \"+to_debug(get<I>(x))), ...); }(make_index_sequence<size>());\n\treturn \"(\"+s.substr(s.empty()?0:2)+\")\";\n}\n#define dbg(...) cout << __LINE__ << \": (\" #__VA_ARGS__ \") = \" << to_debug(tuple(__VA_ARGS__)) << endl\ntemplate<typename T1, typename T2> istream &operator>>(istream &cin, pair<T1, T2> &a) { return cin>>a.first>>a.second; }\ntemplate<typename T1> istream &operator>>(istream &cin, vector<T1> &a) { for (auto &x:a) cin>>x; return cin; }\ntemplate<typename T1> istream &operator>>(istream &cin, valarray<T1> &a) { for (auto &x:a) cin>>x; return cin; }\ntemplate<typename T1, typename T2> ostream &operator<<(ostream &cout, const pair<T1, T2> &a) { return cout<<a.first<<' '<<a.second; }\ntemplate<typename T1, typename T2> ostream &operator<<(ostream &cout, const vector<pair<T1, T2>> &a) { for (auto &x:a) cout<<x<<'\\n'; return cout; }\ntemplate<typename T1> ostream &operator<<(ostream &cout, const vector<T1> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }\ntemplate<typename T1, typename T2> bool cmin(T1 &x, const T2 &y) { if (y<x) { x=y; return 1; } return 0; }\ntemplate<typename T1, typename T2> bool cmax(T1 &x, const T2 &y) { if (x<y) { x=y; return 1; } return 0; }\ntemplate<typename T1> vector<T1> range(T1 l, T1 r, T1 step=1) { assert(step>0); int n=(r-l+step-1)/step, i; vector<T1> res(n); for (i=0; i<n; i++) res[i]=l+step*i; return res; }\ntemplate<typename T1> basic_string<T1> operator*(const basic_string<T1> &s, int m) { auto r=s; m*=s.size(); r.resize(m); for (int i=s.size(); i<m; i++) r[i]=r[i-s.size()]; return r; }\ntypedef unsigned ui;\ntypedef long long ll;\n#define all(x) (x).begin(), (x).end()\nconst ll p=1e9+7;\nclass Solution\n{\npublic:\n\tint numberOfPermutations(int n, vector<vector<int>> &reqq)\n\t{\n\t\tvector<int> req(n, -1);\n\t\tfor (auto &v:reqq)\n\t\t{\n\t\t\treq[v[0]]=v[1];\n\t\t}\n\t\tassert(req[n-1]>=0);\n\t\tint m=req[n-1]+1, i, j, k;\n\t\tvector f(n, vector<ll>(m));\n\t\tf[0][0]=1;\n\t\tfor (i=0; i+1<n; i++)\n\t\t{\n\t\t\tif (req[i]!=-1)\n\t\t\t{\n\t\t\t\tfor (j=0; j<m; j++) if (j!=req[i]) f[i][j]=0;\n\t\t\t}\n\t\t\tfor (k=0; k<=i+1; k++) for (j=0; j+k<m; j++) (f[i+1][j+k]+=f[i][j])%=p;\n\t\t}\n\t\treturn f[n-1][req[n-1]];\n\t}\n};", "memory": "41605" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
2
{ "code": "#define ll long long\nclass Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& b) {\n const int mod=1e9+7;\n auto add=[&](ll x, ll y){\n ll val= (x%mod+y%mod)%mod;\n while(val<0) val+=mod;\n return val;\n };\n const int k = 4e2;\n vector<int> a(n,-1);\n for(int i=0;i<b.size();i++){\n a[b[i][0]]=b[i][1];\n }\n vector<int> dp1(k+1);\n dp1[0]=1;\n for(int i = 1; i <= n; i++){\n vector<int>dp2(k+1);\n for(int j = 0; j <= (a[i-1]==-1?k:a[i-1]); j++){\n if(i<=1 || (i>1&&a[i-2]==-1)){\n dp2[j]=add(dp2[j],dp1[j]);\n if(j-1>=0) dp2[j]=add(dp2[j], dp2[j-1]);\n if(j-i>=0) dp2[j]=add(dp2[j], -dp1[j-i]);\n }\n else {\n int l=j-a[i-2];\n if(l<i&&l>=0) dp2[j]=add(dp2[j],dp1[a[i-2]]);\n }\n \n }\n swap(dp1,dp2);\n }\n return dp1[a[n-1]];\n }\n};\n\n/*\n1 0 0 0 0 0 \n1 1 0 0 0 0 \n1 1 1 1 0 0 \n1 2 3 4 3 2 \n1 3 6 10 0 0 \n\n1 0 0 0 0 0 \n1 1 0 0 0 0 \n0 1 1 1 0 0 \n0 1 2 3 3 2 \n0 1 3 6 0 0 \n*/", "memory": "45206" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
2
{ "code": "#define ll long long\nclass Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& b) {\n const int mod=1e9+7;\n auto add=[&](ll x, ll y){\n ll val= (x%mod+y%mod)%mod;\n while(val<0) val+=mod;\n return val;\n };\n const int k = 4e2;\n vector<int> a(n,-1);\n for(int i=0;i<b.size();i++){\n a[b[i][0]]=b[i][1];\n }\n b.clear();\n vector<int> dp1(k+1);\n dp1[0]=1;\n for(int i = 1; i <= n; i++){\n vector<int>dp2(k+1);\n for(int j = 0; j <= (a[i-1]==-1?k:a[i-1]); j++){\n if(i<=1 || (i>1&&a[i-2]==-1)){\n dp2[j]=add(dp2[j],dp1[j]);\n if(j-1>=0) dp2[j]=add(dp2[j], dp2[j-1]);\n if(j-i>=0) dp2[j]=add(dp2[j], -dp1[j-i]);\n }\n else {\n int l=j-a[i-2];\n if(l<i&&l>=0) dp2[j]=add(dp2[j],dp1[a[i-2]]);\n }\n \n }\n swap(dp1,dp2);\n }\n return dp1[a[n-1]];\n }\n};\n\n/*\n1 0 0 0 0 0 \n1 1 0 0 0 0 \n1 1 1 1 0 0 \n1 2 3 4 3 2 \n1 3 6 10 0 0 \n\n1 0 0 0 0 0 \n1 1 0 0 0 0 \n0 1 1 1 0 0 \n0 1 2 3 3 2 \n0 1 3 6 0 0 \n*/", "memory": "45206" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& r) {\n vector<int> f(n+1,-1);\n int M=1e9+7;\n for(int i=0;i<r.size();i++)f[r[i][0]]=r[i][1];\n vector<vector<int>>dp(n+1,vector<int>(402));\n for(int i=1;i<=400;i++)dp[0][i]=1;\n for(int i=1;i<=n;i++){\n for(int j=0;j<=400;j++){\n dp[i][j+1]=dp[i][j]%M;\n int ind=max(0,j-i+1);\n dp[i][j+1]+=((dp[i-1][j+1]-dp[i-1][ind])%M+M)%M;\n dp[i][j+1]%=M;\n }\n if(f[i-1]==-1)continue;\n int val=((dp[i][f[i-1]+1]-dp[i][f[i-1]])%M+M)%M;\n for(int j=0;j<=400;j++){\n if(f[i-1]<j)dp[i][j+1]=dp[i][j];\n else if(f[i-1]>j)dp[i][j+1]=0;\n else{\n dp[i][j+1]=val;\n }\n }\n }\n return dp[n][401]%M;\n }\n};", "memory": "48808" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& req) \n {\n\n\n vector<vector<int>>dp(n,vector<int>(401,0));\n\n \n dp[0][0]=1;\n\n map<int,int>mp;\n\n\n for(auto q:req)\n {\n if(q[1]>((q[0]*(q[0]+1))/2))\n return 0;\n\n mp[q[0]]=q[1];\n }\n\n for(int i=1;i<n;i++)\n {\n if(mp.find(i)==mp.end())\n {\n for(int k=0;k<401;k++)\n {\n \n\n int p=min(i,k);\n\n int sum=0;\n\n for(int v=0;v<=p;v++)\n {\n sum+=dp[i-1][k-v];\n sum%=1000000007;\n }\n\n dp[i][k]=sum%1000000007;\n }\n }\n else\n {\n int k=mp[i];\n int p=min(i,k);\n\n int sum=0;\n\n for(int v=0;v<=p;v++)\n {\n sum+=dp[i-1][k-v];\n sum%=1000000007;\n }\n\n dp[i][k]=sum%1000000007;\n }\n }\n\n int i=0,k=0;\n for(auto q:req)\n {\n if(q[0]>=i)\n {\n i=q[0];\n k=q[1];\n }\n }\n\n\n\n return dp[n-1][k];\n \n }\n};", "memory": "48808" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "class Solution {\npublic:\n const int mod=1e9+7;\n vector<vector<int>> dp;\n int rec(int idx,int inv,int j,int& n,vector<vector<int>>& v)\n {\n if(idx==n)\n return 1;\n if(dp[idx][inv]!=-1)\n return dp[idx][inv];\n int ans=0;\n for(int i=0;i<=idx;i++)\n {\n if(j<v.size() && v[j][0]==idx)\n {\n if(inv+i==v[j][1])\n ans=(ans+rec(idx+1,inv+i,j+1,n,v))%mod;\n }\n else\n ans=(ans+rec(idx+1,min(700,inv+i),j,n,v))%mod;\n }\n return dp[idx][inv]=ans;\n }\n int numberOfPermutations(int n, vector<vector<int>>& requirements) {\n sort(requirements.begin(),requirements.end());\n dp=vector<vector<int>> (n,vector<int> (701,-1));\n return rec(0,0,0,n,requirements);\n }\n};", "memory": "52409" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "#define P ((int)1e9 + 7)\nclass Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& requirements) {\n vector<int> count(n+1, -1);\n for(int i = 0; i < requirements.size(); i++) {\n count[requirements[i][0]] = requirements[i][1];\n }\n if (count[0] > 0)\n return 0;\n vector<vector<long long>> dp(n, vector<long long>(401, 0));\n dp[0][0] = 1;\n for(int i = 1; i < n; i++) {\n if (count[i] == -1) {\n long long cumulate = 0;\n for(int j = 0; j < 401; j++) {\n cumulate += dp[i-1][j];\n if (j >= i+1) {\n cumulate += P - dp[i-1][j-(i+1)];\n }\n cumulate %= P;\n dp[i][j] = cumulate;\n }\n }else {\n long long cumulate = 0;\n for(int j = max(0, count[i]-i); j <= count[i]; j++) {\n cumulate += dp[i-1][j];\n }\n cumulate %= P;\n dp[i][count[i]] = cumulate;\n }\n }\n\n if (count[n-1] != -1) {\n return dp[n-1][count[n-1]];\n } else {\n long long res = 0;\n for(int i = 0; i < 401; i++) {\n res += dp[n-1][i];\n res %= P;\n }\n return res;\n }\n }\n};", "memory": "56010" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& b) {\n const int mod=1e9+7;\n auto add=[&](ll x, ll y){\n ll val= (x%mod+y%mod)%mod;\n while(val<0) val+=mod;\n return val;\n };\n const int k = 4e2;\n vector<int> a(n,-1);\n for(int i=0;i<b.size();i++){\n a[b[i][0]]=b[i][1];\n }\n vector<ll> dp1(k+1);\n dp1[0]=1;\n for(int i = 1; i <= n; i++){\n vector<ll>dp2(k+1);\n for(int j = 0; j <= (a[i-1]==-1?k:a[i-1]); j++){\n if(i<=1 || (i>1&&a[i-2]==-1)){\n dp2[j]=add(dp2[j],dp1[j]);\n if(j-1>=0) dp2[j]=add(dp2[j], dp2[j-1]);\n if(j-i>=0) dp2[j]=add(dp2[j], -dp1[j-i]);\n }\n else {\n int l=j-a[i-2];\n if(l<i&&l>=0) dp2[j]=add(dp2[j],dp1[a[i-2]]);\n }\n \n }\n swap(dp1,dp2);\n }\n return dp1[a[n-1]];\n }\n};\n\n/*\n1 0 0 0 0 0 \n1 1 0 0 0 0 \n1 1 1 1 0 0 \n1 2 3 4 3 2 \n1 3 6 10 0 0 \n\n1 0 0 0 0 0 \n1 1 0 0 0 0 \n0 1 1 1 0 0 \n0 1 2 3 3 2 \n0 1 3 6 0 0 \n*/", "memory": "59611" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "const long long mod = 1e9+7;\n\nclass Solution {\n vector<vector<long long>> dp;\n\n long long dfs(int i, int reqs_idx, int cnt, vector<vector<int>> &reqs) {\n if(reqs_idx >= 0 && reqs[reqs_idx][0] == i) {\n if(reqs[reqs_idx][1] != cnt) return 0;\n\n reqs_idx--;\n }\n\n // if max possible inversions is < cnt, return 0\n if(i*(i+1)/2 < cnt) return 0;\n if(i == 0) return cnt == 0;\n \n if(dp[i][cnt] != -1)\n return dp[i][cnt];\n \n long long ans = 0;\n for(int k = 0; k <= min(i, cnt); k++) {\n ans = (ans + dfs(i-1, reqs_idx, cnt-k, reqs)) % mod;\n }\n // ans--;\n\n return dp[i][cnt] = ans;\n }\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& requirements) {\n sort(requirements.begin(), requirements.end());\n dp.clear(); dp.resize(n, vector<long long>(401, -1));\n\n return dfs(n-1, requirements.size()-1, requirements.back()[1], requirements);\n }\n};", "memory": "63213" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "typedef long long ll;\nll mod = 1000000007;\n\nclass Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& requirements) {\n int lim = 401;\n vector<ll> dp(lim, 0);\n map<int,int> reqs;\n for (vector<int> req : requirements) reqs[req[0]] = req[1];\n dp[0] = 1;\n for (int i = 0; i < n; i++) {\n vector<ll> nx(lim, 0);\n bool cancel = (reqs.find(i) != reqs.end());\n for (int j = 0; j < lim; j++) {\n for (int k = max(0, j - i); k <= j; k++) {\n nx[j] = (nx[j] + dp[k]) % mod;\n }\n if (cancel && reqs[i] != j) nx[j] = 0;\n }\n dp = nx;\n }\n return dp[reqs[n-1]];\n }\n};", "memory": "66814" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "// let dp[i][j] find the number of permutations that have max element i and j inversions\n// dp[i][j] = sum (dp[i-1][k], 0<=k=j)\ntypedef long long ll;\nconst ll mod = 1e9+7;\nconst ll mx = 401;\nclass Solution {\npublic:\n int numberOfPermutations(ll n, vector<vector<int>>& requirements) {\n vector<vector<ll>> dp(n,vector<ll> (mx, 0));\n map<ll,ll> mp;\n for(auto g:requirements){\n mp[g[0]]=g[1];\n }\n if(mp.count(0) and mp[0]!=0) return 0;\n dp[0][0] = 1; \n for(ll i=1;i<n;i++){\n for(int j=0;j<mx;j++){\n if(mp.count(i) and mp[i]!=j) {\n continue;\n }\n for (int k=0;k<=i;++k) \n {\n if (j>=k)\n dp[i][j]=(dp[i][j]+dp[i-1][j-k])%mod;\n }\n // cout<<i<<\" \"<<j<<\" \"<<dp[i][j]<<endl;\n }\n }\n return dp[n-1][mp[n-1]];\n }\n};", "memory": "66814" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "int mod = 1e9 + 7;\nclass Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& requirements) {\n vector<long long> req(n + 1, -1);\n for (vector<int>& x : requirements) {\n req[x[0]] = x[1];\n }\n vector<vector<long long>> dp(n + 1, vector<long long>(401, 0));\n dp[0][0] = 1;\n for (long long len = 1; len <= n; len++) {\n for (long long inv = 0; inv <= 400; inv++) {\n for (long long last = 0; last < len; last++) {\n if (req[len - 1] != -1 and req[len - 1] != inv)\n continue;\n if (inv >= last) {\n dp[len][inv] += dp[len - 1][inv - last];\n dp[len][inv] %= mod;\n }\n }\n }\n }\n // cout << dp[1][0] << \" \" << dp[1][1] << endl;\n long long ans = 0;\n for (long long inv = 0; inv <= 400; inv++) {\n if (req[n - 1] == -1 or req[n - 1] == inv) {\n ans += dp[n][inv];\n ans %= mod;\n }\n }\n return ans;\n }\n};", "memory": "70415" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "class Solution {\npublic:\n const int MOD = 1000000007;\n\n int numberOfPermutations(int n, vector<vector<int>>& requirements) {\n // dp[endi][cnti] represents the number of permutations of length endi+1 with cnti inversions\n vector<vector<int>> dp(n + 1, vector<int>(401, 0));\n \n // Initial dp state\n dp[0][0] = 1; // There's only one way to have a permutation of length 1 with 0 inversions, which is [0]\n \n for (int length = 1; length <= n; ++length) {\n for (int invs = 0; invs < 401; ++invs) {\n for (int last = 0; last < length; ++last) {\n if (invs >= last) {\n dp[length][invs] = (dp[length][invs] + dp[length - 1][invs - last]) % MOD;\n }\n }\n }\n }\n\n // Create a lookup table for the requirements\n vector<int> required_inversions(n, -1);\n for (const auto& req : requirements) {\n required_inversions[req[0]] = req[1];\n }\n \n // Initialize dp2 to filter out invalid sequences\n vector<vector<int>> dp2(n + 1, vector<int>(401, 0));\n dp2[0][0] = 1;\n \n for (int length = 1; length <= n; ++length) {\n for (int invs = 0; invs < 401; ++invs) {\n if (dp[length][invs] == 0) continue; // Skip impossible configurations\n if (required_inversions[length - 1] != -1 && required_inversions[length - 1] != invs) continue; // Skip if it doesn't match requirement\n\n for (int last = 0; last < length; ++last) {\n if (invs >= last) {\n dp2[length][invs] = (dp2[length][invs] + dp2[length - 1][invs - last]) % MOD;\n }\n }\n }\n }\n \n // The final answer is the number of permutations of length n with the required number of inversions\n int result = 0;\n for (int invs = 0; invs < 401; ++invs) {\n if (required_inversions[n - 1] == -1 || required_inversions[n - 1] == invs) {\n result = (result + dp2[n][invs]) % MOD;\n }\n }\n \n return result;\n }\n};", "memory": "70415" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& requirements) {\n int mod = 1e9 + 7;\n sort(requirements.begin(),requirements.end());\n int idx = 0 ;\n if(requirements[0][0]==0){\n if(requirements[0][1]==0){\n idx=1;\n }else{\n return 0;\n }\n }\n int r_len = requirements.size();\n int limit = requirements[idx][1];\n map<int,int> state;\n state[0]=1;\n for(int i=2;i<=n+1;i++){\n map<int,int> new_state;\n for(auto item : state){\n if(item.first>limit){continue;}\n for(int j=0;j<i;j++){\n new_state[item.first + j]=(new_state[item.first + j]+state[item.first])%mod;\n }\n }\n state.clear();\n if(idx<r_len && (i-1) == requirements[idx][0]){\n if(new_state.find(requirements[idx][1])==new_state.end()){\n return 0;\n }\n state[requirements[idx][1]] = new_state[requirements[idx][1]];\n idx++;\n if(idx<r_len){\n limit = requirements[idx][1];\n }else{\n limit = 400;\n }\n }else{\n state = new_state;\n }\n }\n return state[requirements[r_len-1][1]]%mod;\n\n }\n};", "memory": "74016" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& requirements) \n {\n \n int mod = 1e9 + 7;\n sort(requirements.begin(),requirements.end());\n int idx = 0 ;\n \n if(requirements[0][0]==0)\n {\n if(requirements[0][1]==0)\n idx=1;\n else\n return 0;\n }\n\n int r_len = requirements.size();\n int limit = requirements[idx][1];\n map<int,int> state;\n state[0]=1;\n \n for( int i = 2 ; i <= n + 1 ; i++ )\n {\n map<int,int> new_state;\n\n for(auto item : state)\n {\n if(item.first>limit)\n continue;\n \n for(int j=0;j<i;j++)\n new_state[item.first + j]=(new_state[item.first + j]+state[item.first])%mod;\n \n }\n\n state.clear();\n\n if( idx < r_len && (i-1) == requirements[idx][0] )\n {\n if(new_state.find(requirements[idx][1])==new_state.end())\n return 0;\n \n state[requirements[idx][1]] = new_state[requirements[idx][1]];\n idx++;\n \n if( idx < r_len )\n limit = requirements[idx][1];\n else\n limit = 400;\n }\n else\n state = new_state;\n }\n \n return state[requirements[r_len-1][1]]%mod;\n\n }\n};", "memory": "77618" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "class Solution {\npublic:\n const long long MOD=1e9 + 7;\n struct cmp{\n bool operator() (vector<int>&a, vector<int>&b){\n return a[0]<b[0];\n }\n };\n int numberOfPermutations(int m, vector<vector<int>>& r) {\n long long ans;\n r.insert(r.begin(),{0,0});\n int n = r.size();\n sort(r.begin(),r.end(),cmp());\n bool first = true;\n for(int i=1;i<n;i++){\n if(first){\n ans = help(r[i-1][0],r[i][0],r[i-1][1],r[i][1]);\n first = false;\n if(ans==0) return 0;\n }\n else {\n ans= ans*help(r[i-1][0],r[i][0],r[i-1][1],r[i][1])%MOD;\n if(ans==0) return 0;\n }\n }\n return (int)ans;\n }\n long long help(int a, int b, int cnta, int cntb){\n if(a==b&&cnta!=cntb) return 0;\n vector<vector<int>>dp(b+1,vector<int>(cntb+1,0));\n for(int i=b;i>=a;i--){\n for(int j=cnta;j<=cntb;j++){\n if(i==b){\n if(j==cntb) dp[i][j]=1;\n continue;\n }\n else{\n if(j==cnta){\n for(int k=j;k<=min(j+i+1,cntb);k++){\n long long temp = ((long long)dp[i][j] + (long long)dp[i+1][k]) % MOD;\n dp[i][j] = (int)temp;\n }\n }\n else{\n long long temp =((long long)dp[i][j-1]+(j+i+1<=cntb?(long long)dp[i+1][j+i+1]:0)-(long long)dp[i+1][j-1]+MOD)%MOD;\n dp[i][j] = (int)temp;\n }\n }\n \n }\n }\n return (long long)dp[a][cnta];\n }\n};", "memory": "81219" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "class Solution {\npublic:\n const long long MOD=1e9 + 7;\n struct cmp{\n bool operator() (vector<int>&a, vector<int>&b){\n return a[0]<b[0];\n }\n };\n int numberOfPermutations(int m, vector<vector<int>>& r) {\n long long ans;\n r.insert(r.begin(),{0,0});\n int n = r.size();\n sort(r.begin(),r.end(),cmp());\n bool first = true;\n for(int i=1;i<n;i++){\n if(first){\n ans = help(r[i-1][0],r[i][0],r[i-1][1],r[i][1]);\n first = false;\n if(ans==0) return 0;\n }\n else {\n ans= ans*help(r[i-1][0],r[i][0],r[i-1][1],r[i][1])%MOD;\n if(ans==0) return 0;\n }\n }\n return (int)ans;\n }\n long long help(int a, int b, int cnta, int cntb){\n if(a==b&&cnta!=cntb) return 0;\n vector<vector<int>>dp(b+1,vector<int>(cntb+1,0));\n for(int i=b;i>=a;i--){\n for(int j=cnta;j<=cntb;j++){\n if(i==b){\n if(j==cntb) dp[i][j]=1;\n continue;\n }\n else{\n if(j==cnta){\n for(int k=j;k<=min(j+i+1,cntb);k++){\n long long temp = ((long long)dp[i][j] + (long long)dp[i+1][k]) % MOD;\n dp[i][j] = (int)temp;\n }\n }\n else{\n long long temp =((long long)dp[i][j-1]+(j+i+1<=cntb?(long long)dp[i+1][j+i+1]:0)-(long long)dp[i+1][j-1]+MOD)%MOD;\n dp[i][j] = (int)temp;\n }\n }\n \n }\n }\n return (long long)dp[a][cnta];\n }\n};", "memory": "84820" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "class Solution {\npublic:\n const long long MOD=1e9 + 7;\n struct cmp{\n bool operator() (vector<int>&a, vector<int>&b){\n if(a[0]==b[0]) return a[1]<b[1];\n return a[0]<b[0];\n }\n };\n int numberOfPermutations(int m, vector<vector<int>>& r) {\n long long ans;\n r.insert(r.begin(),{0,0});\n int n = r.size();\n sort(r.begin(),r.end(),cmp());\n bool first = true;\n for(int i=1;i<n;i++){\n if(first){\n ans = help(r[i-1][0],r[i][0],r[i-1][1],r[i][1]);\n first = false;\n if(ans==0) return 0;\n }\n else {\n ans= ans*help(r[i-1][0],r[i][0],r[i-1][1],r[i][1])%MOD;\n if(ans==0) return 0;\n }\n }\n return (int)ans;\n }\n long long help(int a, int b, int cnta, int cntb){\n vector<vector<int>>dp(b+1,vector<int>(cntb+1,0));\n for(int i=b;i>=a;i--){\n for(int j=cnta;j<=cntb;j++){\n if(i==b){\n if(j==cntb) dp[i][j]=1;\n continue;\n }\n else{\n if(j==cnta){\n for(int k=j;k<=min(j+i+1,cntb);k++){\n long long temp = ((long long)dp[i][j] + (long long)dp[i+1][k]) % MOD;\n dp[i][j] = (int)temp;\n }\n }\n else{\n long long temp =((long long)dp[i][j-1]+(j+i+1<=cntb?(long long)dp[i+1][j+i+1]:0)-(long long)dp[i+1][j-1]+MOD)%MOD;\n dp[i][j] = (int)temp;\n }\n }\n \n }\n }\n return (long long)dp[a][cnta];\n }\n};", "memory": "84820" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "class Solution {\npublic:\n const long long mod = 1e9 + 7;\n\n int solve(int numDistinct, int inv, unordered_map<int, int> &req, vector<vector<int>> &memo) {\n if (inv < 0)\n return 0;\n\n if (req.find(numDistinct) != req.end() && req[numDistinct] != inv)\n return 0;\n\n if (numDistinct == 0) {\n if (inv == 0)\n return 1;\n else\n return 0;\n }\n\n if (memo[numDistinct][inv] != -1)\n return memo[numDistinct][inv];\n\n long long result = 0;\n\n for (int i = 1; i <= numDistinct + 1; ++i) {\n int inversionsCaused = numDistinct - i + 1;\n\n result = (result + solve(numDistinct - 1, inv - inversionsCaused, req, memo) % mod) % mod;\n }\n\n return memo[numDistinct][inv] = result;\n }\n\n int numberOfPermutations(int n, vector<vector<int>>& requirements) {\n sort(requirements.begin(), requirements.end());\n vector<vector<int>> memo(305, vector<int>(405, -1));\n unordered_map<int, int> req;\n\n for (int i = 0; i < requirements.size(); ++i) {\n req[requirements[i][0]] = requirements[i][1];\n }\n\n return solve(n - 1, requirements[requirements.size() - 1][1], req, memo);\n }\n};", "memory": "88421" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "class Solution {\npublic:\n const long long mod = 1e9 + 7;\n\n int solve(int pos, int inv, int &n, unordered_map<int, int> &req, vector<vector<int>> &memo) {\n if (pos == n)\n return 1;\n\n if (inv > 400)\n return 0;\n\n if (memo[pos][inv] != -1)\n return memo[pos][inv];\n\n long long result = 0;\n int maxInv = inv + pos;\n\n for (int i = inv; i <= maxInv; ++i) {\n if (req.find(pos) != req.end() && req[pos] != i) {\n continue;\n }\n\n result = (result + solve(pos + 1, i, n, req, memo) % mod) % mod;\n }\n\n return memo[pos][inv] = result;\n }\n\n int numberOfPermutations(int n, vector<vector<int>>& requirements) {\n vector<vector<int>> memo(305, vector<int>(405, -1));\n unordered_map<int, int> req;\n\n for (int i = 0; i < requirements.size(); ++i) {\n req[requirements[i][0]] = requirements[i][1];\n }\n\n if (req.find(0) != req.end() && req[0] != 0)\n return 0;\n\n return solve(1, 0, n, req, memo);\n }\n};", "memory": "88421" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "class Solution {\npublic:\n const long long mod = 1e9 + 7;\n\n int solve(int numDistinct, int inv, unordered_map<int, int> &req, vector<vector<int>> &memo) {\n if (inv < 0)\n return 0;\n\n if (req.find(numDistinct) != req.end() && req[numDistinct] != inv)\n return 0;\n\n if (numDistinct == 0) {\n if (inv == 0)\n return 1;\n else\n return 0;\n }\n\n if (memo[numDistinct][inv] != -1)\n return memo[numDistinct][inv];\n\n long long result = 0;\n\n for (int i = 1; i <= numDistinct + 1; ++i) {\n int inversionsCaused = numDistinct - i + 1;\n\n result = (result + solve(numDistinct - 1, inv - inversionsCaused, req, memo) % mod) % mod;\n }\n\n return memo[numDistinct][inv] = result;\n }\n\n int numberOfPermutations(int n, vector<vector<int>>& requirements) {\n sort(requirements.begin(), requirements.end());\n vector<vector<int>> memo(305, vector<int>(405, -1));\n unordered_map<int, int> req;\n\n for (int i = 0; i < requirements.size(); ++i) {\n req[requirements[i][0]] = requirements[i][1];\n }\n\n return solve(n - 1, requirements[requirements.size() - 1][1], req, memo);\n }\n};", "memory": "92023" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "class Solution {\npublic:\nvector<vector<int>>dp;\nint dfs(int i, int j, int cnt, vector<vector<int>>& reqs) \n{\n if ( j >= 0 && reqs[j][0] == i ) \n {\n if (reqs[j][1] != cnt)\n return 0;\n j--;\n }\n\n if ( i == 0 || i * (i + 1) / 2 < cnt )\n return cnt == 0; \n \n if( dp[i][cnt] != -1 )\n return dp[i][cnt];\n\n int tot = 0;\n for (int k = 0; k <= min(i, cnt); ++k)\n tot = ( tot + dfs(i - 1, j, cnt - k, reqs)) % 1000000007;\n \n return dp[i][cnt] = tot;\n\n}\n\nint numberOfPermutations(int n, vector<vector<int>>& reqs) \n{\n dp.assign(301,vector<int>(401,-1));\n sort(begin(reqs), end(reqs));\n return dfs(n - 1, reqs.size() - 1, reqs.back()[1], reqs);\n}\n};", "memory": "95624" }
3,460
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
3
{ "code": "int MOD = 1e9 + 7;\nclass Solution {\n vector<vector<int>> dp;\npublic:\n int numberOfPermutations(int n, vector<vector<int>>& reqs) {\n sort(reqs.begin(), reqs.end());\n dp = vector<vector<int>>(301, vector<int>(401, 0));\n\n dp[0][0] = 1; \n if (reqs[0][0] == 0 && reqs[0][1] != 0)\n return 0;\n\n int idx = (reqs[0][0] == 0) ? 1 : 0;\n for (int i=1; i<=n; ++i) {\n dp[i][0] = dp[i-1][0];\n\n for (int j=1; j<=400; ++j) {\n dp[i][j] = ((long long)dp[i][j-1] + dp[i-1][j]) % MOD;\n if (j > i)\n dp[i][j] = (MOD + (long long)dp[i][j] - dp[i-1][j-i-1]) % MOD;\n }\n\n if (idx < reqs.size() && reqs[idx][0] == i) {\n for (int j=0; j<=400; ++j) {\n if (j != reqs[idx][1])\n dp[i][j] = 0;\n }\n ++idx;\n }\n }\n return dp[reqs.back()[0]][reqs.back()[1]];\n }\n};", "memory": "99225" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& v) {\n int n=v.size();\n sort(v.begin(),v.end());\n int i=0,j=n-1;\n double ans=INT_MAX;\n while(i<j) {\n double k = (v[i]+v[j])/(2*1.0);\n ans=min(ans,k);\n i++;\n j--;\n }\n return ans;\n }\n};", "memory": "24800" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n double res = DBL_MAX;\n int l = 0;\n int r = nums.size() - 1;\n\n sort(nums.begin(), nums.end());\n\n while (l < r) {\n\n res = min(res, (double)(nums[l] + nums[r])/2);\n l++;\n r--;\n }\n return res;\n }\n};", "memory": "24900" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n sort(nums.begin(), nums.end()); \n int left = 0;\n int right = nums.size() - 1;\n double minAvg = INT_MAX;\n while(left < right){\n double avg = (nums[left] + nums[right]) / 2.0; \n minAvg = min(minAvg, avg); \n left++; \n right--; \n }\n return minAvg; \n }\n};", "memory": "25000" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n double res=INT_MAX;\n sort(nums.begin(), nums.end());\n int l=0, r=nums.size()-1;\n while(l<r){\n double avg = (nums[l++] +nums[r--])/2.0;\n res=min(res, avg);\n }\n return res;\n }\n};", "memory": "25100" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n \n sort(nums.begin(),nums.end());\n \n int n = nums.size();\n int i = 0,j= n-1;\n double ans = 3000;\n\n while(i<j){\n double sum = (nums[i]+nums[j])/double(2.0);\n ans = min(ans,sum);\n i++;\n j--;\n }\n\n return ans;\n }\n};", "memory": "25100" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n sort(begin(nums), end(nums));\n\n double avg = FLT_MAX;\n int n=nums.size();\n\n //[7,8,3,4,15,13,4,1]\n // 1,3,4,4,7,8,13,15\n for (int i = 0; i < n/2; i++)\n {\n double avg1 = double(nums[i] + nums[n-i-1])/2.0;\n if ( avg1< avg)\n {\n avg = avg1;\n \n }\n }\n return avg;\n }\n};", "memory": "25200" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n double num=10000000;\n int n=nums.size();\n sort(nums.begin(),nums.end());\n int i=0, j=n-1;\n while(i<j)\n {\n num = min(num , (double)(nums[j]+nums[i])/2);\n i++;\n j--;\n }\n return num;\n }\n};", "memory": "25200" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n vector<double> averages ;\n sort(nums.begin(), nums.end()) ;\n double avg ;\n int i = 0 ;\n int j = nums.size()-1 ;\n double maxAvg = DBL_MAX ;\n while(i < j){\n avg = (double)((nums.at(i)+nums.at(j))/2.0) ;\n avg = min(avg, maxAvg) ;\n maxAvg = avg ;\n ++i ;\n --j ;\n }\n return avg ;\n }\n};", "memory": "25300" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n sort(nums.begin(), nums.end()) ;\n double avg ;\n int i = 0 ;\n int j = nums.size()-1 ;\n double maxAvg = DBL_MAX ;\n while(i < j){\n avg = (double)((nums.at(i)+nums.at(j))/2.0) ;\n avg = min(avg, maxAvg) ;\n maxAvg = avg ;\n ++i ;\n --j ;\n }\n return avg ;\n }\n};", "memory": "25300" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n sort(nums.begin() , nums.end());\n int n= nums.size();\n int low=0, high=n-1;\n vector<double>avg;\n double maxi = INT_MAX;\n while(low<high){\n double average = double(nums[high--] + nums[low++])/2;\n // avg.push_back(average);\n if(average < maxi) maxi =average;\n }\n return maxi;\n // return *min_element(avg.begin(),avg.end());\n }\n};", "memory": "25400" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n vector<double>ans;\n double avg;\n int n=nums.size();\n int start=0,end=n-1;\n while(start<end){\n avg=(nums[start]+nums[end])/2.0;\n ans.push_back(avg);\n start++;\n end--;\n }\n return *min_element(ans.begin(),ans.end());\n \n }\n};", "memory": "25400" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n sort(nums.begin() , nums.end());\n int n= nums.size();\n int low=0, high=n-1;\n vector<double>avg;\n double maxi = INT_MAX;\n while(low<high){\n double average = double(nums[high--] + nums[low++])/2;\n avg.push_back(average);\n }\n return *min_element(avg.begin(),avg.end());\n }\n};", "memory": "25500" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n vector<double> v;\n sort(nums.begin(), nums.end());\n int l=0, r=nums.size()-1;\n while(l<=r) v.push_back(((double)nums[l++]+(double)nums[r--])/(double)2);\n return *min_element(v.begin(), v.end());\n }\n};", "memory": "25500" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n vector<double> av;\n \n while (nums.size() > 1) {\n auto min_it = min_element(begin(nums), end(nums));\n auto max_it = max_element(begin(nums), end(nums));\n \n double left = *min_it;\n double right = *max_it;\n \n nums.erase(min_it);\n if (min_it < max_it){\n max_it--;\n }\n nums.erase(max_it);\n \n av.push_back((left + right) / 2.0);\n }\n if (nums.size() == 1) {\n av.push_back(nums[0]);\n }\n \n double mini = *min_element(begin(av), end(av));\n return mini;\n }\n};\n", "memory": "25600" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n vector<double> v;\n sort(nums.begin(), nums.end());\n int n=nums.size();\n int l=0, r=n-1;\n while(l<=r){\n double avg= ((double)nums[l++]+(double)nums[r--])/(double)2;\n v.push_back(avg);\n }\n return *min_element(v.begin(), v.end());\n }\n};", "memory": "25600" }
3,471
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n double minimumAverage(vector<int>& nums) {\n int time = nums.size()/2;\n vector<double>avg;\n sort(nums.begin(),nums.end());\n int s = 0 ;\n int e = nums.size()-1;\n while(time>0 && s<=e){\n double d = double(nums[s]+nums[e])/2;\n avg.push_back(d);\n s=s+1;\n e=e-1;\n time = time-1;\n }\n sort(avg.begin(),avg.end());\n return avg[0];\n }\n};", "memory": "25700" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
0
{ "code": "static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\nint parse_input_and_solve(const std::string& s) {\n const int S = s.size();\n int cols = 0;\n int rows = 0;\n int k = 2;\n\n int min_row = INT_MAX;\n int max_row = 0;\n int min_col = INT_MAX;\n int max_col = 0;\n\n int i = 0;\n int j = 0;\n while (k < S) {\n if (s[k] == '1') {\n min_row = std::min(min_row, i);\n max_row = std::max(max_row, i);\n min_col = std::min(min_col, j);\n max_col = std::max(max_col, j);\n ++j;\n } else if (s[k] == '0') {\n ++j;\n } else if (s[k] == ']') {\n cols = std::max(cols, j);\n j = 0;\n ++i;\n }\n ++k;\n }\n return (max_row - min_row + 1) * (max_col - min_col + 1);\n}\n\nstatic bool Solve = [](){\n std::ofstream out(\"user.out\");\n std::string grid_str;\n while (std::getline(std::cin, grid_str)) {\n out << parse_input_and_solve(grid_str) << \"\\n\";\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\npublic:\n int minimumArea(std::vector<std::vector<int>>& grid) {\n const int rows = grid.size();\n const int cols = grid.front().size();\n int min_row = rows;\n int max_row = 0;\n int min_col = cols;\n int max_col = 0;\n for (int i = 0; i < rows; ++i) {\n for (int j = 0; j < cols; ++j) {\n if (grid[i][j] == 1) {\n min_row = std::min(min_row, i);\n max_row = std::max(max_row, i);\n min_col = std::min(min_col, j);\n max_col = std::max(max_col, j);\n }\n }\n }\n return (max_row - min_row + 1) * (max_col - min_col + 1);\n }\n};", "memory": "12400" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
0
{ "code": "static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\nint parse_input_and_solve(const std::string& s) {\n const int S = s.size();\n int cols = 0;\n int rows = 0;\n int k = 2;\n\n int min_row = INT_MAX;\n int max_row = 0;\n int min_col = INT_MAX;\n int max_col = 0;\n\n int i = 0;\n int j = 0;\n while (k < S) {\n if (s[k] == '1') {\n min_row = std::min(min_row, i);\n max_row = std::max(max_row, i);\n min_col = std::min(min_col, j);\n max_col = std::max(max_col, j);\n ++j;\n } else if (s[k] == '0') {\n ++j;\n } else if (s[k] == ']') {\n cols = std::max(cols, j);\n j = 0;\n ++i;\n }\n ++k;\n }\n return (max_row - min_row + 1) * (max_col - min_col + 1);\n}\n\nstatic bool Solve = [](){\n std::ofstream out(\"user.out\");\n std::string grid_str;\n while (std::getline(std::cin, grid_str)) {\n out << parse_input_and_solve(grid_str) << \"\\n\";\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\npublic:\n int minimumArea(std::vector<std::vector<int>>& grid) {\n const int rows = grid.size();\n const int cols = grid.front().size();\n int min_row = rows;\n int max_row = 0;\n int min_col = cols;\n int max_col = 0;\n for (int i = 0; i < rows; ++i) {\n for (int j = 0; j < cols; ++j) {\n if (grid[i][j] == 1) {\n min_row = std::min(min_row, i);\n max_row = std::max(max_row, i);\n min_col = std::min(min_col, j);\n max_col = std::max(max_col, j);\n }\n }\n }\n return (max_row - min_row + 1) * (max_col - min_col + 1);\n }\n};", "memory": "12500" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
0
{ "code": "static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\nint parse_input_and_solve(const std::string& s) {\n const int S = s.size();\n int cols = 0;\n int rows = 0;\n int k = 2;\n\n int min_row = INT_MAX;\n int max_row = 0;\n int min_col = INT_MAX;\n int max_col = 0;\n\n int i = 0;\n int j = 0;\n while (k < S) {\n if (s[k] == '1') {\n min_row = std::min(min_row, i);\n max_row = std::max(max_row, i);\n min_col = std::min(min_col, j);\n max_col = std::max(max_col, j);\n ++j;\n } else if (s[k] == '0') {\n ++j;\n } else if (s[k] == ']') {\n cols = std::max(cols, j);\n j = 0;\n ++i;\n }\n ++k;\n }\n return (max_row - min_row + 1) * (max_col - min_col + 1);\n}\n\nstatic bool Solve = [](){\n std::ofstream out(\"user.out\");\n std::string grid_str;\n while (std::getline(std::cin, grid_str)) {\n out << parse_input_and_solve(grid_str) << \"\\n\";\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\npublic:\n int minimumArea(std::vector<std::vector<int>>& grid) {\n const int rows = grid.size();\n const int cols = grid.front().size();\n int min_row = rows;\n int max_row = 0;\n int min_col = cols;\n int max_col = 0;\n for (int i = 0; i < rows; ++i) {\n for (int j = 0; j < cols; ++j) {\n if (grid[i][j] == 1) {\n min_row = std::min(min_row, i);\n max_row = std::max(max_row, i);\n min_col = std::min(min_col, j);\n max_col = std::max(max_col, j);\n }\n }\n }\n return (max_row - min_row + 1) * (max_col - min_col + 1);\n }\n};", "memory": "12500" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
0
{ "code": "static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\nclass Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int m=grid.size();\n int n=grid[0].size();\n int minrow=m,mincol=n,maxrow=0,maxcol=0;\n for(int i=0;i<m;i++)\n {\n for(int j=0;j<n;j++)\n {\n if(grid[i][j]==1)\n {\n minrow=min(minrow,i);\n maxrow=max(maxrow,i);\n mincol=min(mincol,j);\n maxcol=max(maxcol,j);\n }\n }\n }\n return (maxrow-minrow+1)*(maxcol-mincol+1);\n }\n};", "memory": "131100" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n int up = grid.size();\n int bottom = 0;\n int left = grid[0].size();\n int right = 0;\n for(int i = 0; i < grid.size(); i++){\n for(int j = 0; j < grid[0].size(); j++){\n if(grid[i][j] == 1){\n left = min(left , j);\n right = max(right , j+ 1);\n bottom = max(bottom , i+ 1);\n up = min(up , i);\n }\n }\n }\n return (bottom-up)*(right-left);\n \n }\n};", "memory": "131200" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
0
{ "code": "static const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n#include<iostream>\n\nclass Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int min_x=grid.size(),min_y=grid[0].size(),max_x=0,max_y=0;\n for(int i=0;i<grid.size();i++){\n vector<int>& v = grid[i];\n for(int j=0;j<v.size();j++)\n if(v[j]){\n max_x = i>max_x?i:max_x;\n max_y = j>max_y?j:max_y;\n min_x = i<min_x?i:min_x;\n min_y = j<min_y?j:min_y;\n }\n }\n return(max_x-min_x+1)*(max_y-min_y+1);\n }\n};", "memory": "131300" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n int lRow=INT_MAX;\n int hRow=INT_MIN;\n int lCol=INT_MAX;\n int hCol=INT_MIN;\n int n=grid.size();\n int m=grid[0].size();\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(grid[i][j]==1){\n lRow=min(i,lRow);\n hRow=max(i,hRow);\n lCol=min(j,lCol);\n hCol=max(j,hCol);\n }\n }\n }\n return (hRow-lRow+1)*(hCol-lCol+1);\n }\n};", "memory": "131400" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int max_r=0, max_c=0;\n int min_r=grid.size(), min_c=grid[0].size();\n for(int i=0;i<grid.size();i++){\n for(int j=0;j<grid[0].size();j++){\n if(grid[i][j]==1){\n max_r=max(i+1,max_r);\n max_c=max(j+1,max_c);\n min_r=min(i+1,min_r);\n min_c=min(j+1,min_c);\n }\n }\n }\n \n int rectangle=(max_r-min_r+1)* (max_c-min_c+1);\n return rectangle;\n }\n};", "memory": "132600" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int n =grid.size();\n int m =grid[0].size();\n int mini_row=INT_MAX,mini_col=INT_MAX;\n int maxi_row=0,maxi_col=0;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(grid[i][j]==1){\n mini_row=min(mini_row,i);\n mini_col=min(mini_col,j);\n maxi_row=max(maxi_row,i);\n maxi_col=max(maxi_col,j);\n }\n }\n }\n return (maxi_row-mini_row+1)*(maxi_col-mini_col+1);\n \n }\n};", "memory": "132700" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& v) {\n int n=v.size();\n int m=v[0].size();\n int r1=INT_MAX,c1=INT_MAX,r2=INT_MIN,c2=INT_MIN;\n for(int i=0;i<n;i++) {\n for(int j=0;j<m;j++) {\n if(v[i][j]==1) {\n r1=min(r1,i);\n r2=max(r2,i);\n c1=min(c1,j);\n c2=max(c2,j);\n }\n }\n }\n int r=r2-r1+1;\n int c=c2-c1+1;\n return r*c;\n }\n};", "memory": "132700" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
0
{ "code": "class Solution {\n\n typedef pair<int,int> pi;\npublic:\n\n int row_sum(int x, int y1, int y2, vector<vector<int>>& mat){\n for(int j = y1; j <= y2; j++){\n if(mat[x][j]){\n return 1;\n }\n }\n\n return 0;\n }\n\n int row_col(int y, int x1, int x2, vector<vector<int>>& mat){\n for(int i = x1; i <= x2; i++){\n if(mat[i][y]){\n return 1;\n }\n }\n\n return 0;\n }\n\n int minimumArea(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n\n pi tl{0, 0}, br{n - 1, m - 1};\n while(true){\n int x1 = tl.first;\n int y1 = tl.second;\n int x2 = br.first;\n int y2 = br.second;\n\n int sum_r_up = row_sum(x1, y1, y2, grid);\n int sum_r_down = row_sum(x2, y1, y2, grid);\n int sum_c_lf = row_col(y1, x1, x2, grid);\n int sum_c_rg = row_col(y2, x1, x2, grid);\n //cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << '\\n';\n // cout << sum_r_up << ' ' << sum_r_down << ' ' << sum_c_lf << ' ' << sum_c_rg << '\\n';\n //cout << \"_______________\\n\";\n if(!sum_r_up) x1++;\n if(!sum_r_down) x2--;\n if(!sum_c_lf) y1++;\n if(!sum_c_rg) y2--;\n\n if(sum_r_up && sum_r_down && sum_c_lf && sum_c_rg){\n break;\n }\n\n tl = {x1, y1};\n br = {x2, y2}; \n }\n\n int x1 = tl.first;\n int y1 = tl.second;\n int x2 = br.first;\n int y2 = br.second;\n int area = (x2 - x1 + 1) * (y2 - y1 + 1);\n return area;\n }\n};", "memory": "132800" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int rowStartIndex = -1, rowEndIndex = -1;\n int jStartIndex = -1, jEndIndex = -1;\n int rows = grid.size();\n int cols = grid[0].size();\n \n // Find the topmost and bottommost rows with a '1'\n for (int i = 0; i < rows; ++i) {\n for (int j = 0; j < cols; ++j) {\n if (grid[i][j] == 1) {\n if (rowStartIndex == -1) rowStartIndex = i;\n rowEndIndex = i; // Will keep updating until we reach the last row with '1'\n }\n }\n }\n \n // Find the leftmost and rightmost columns with a '1'\n for (int j = 0; j < cols; ++j) {\n for (int i = 0; i < rows; ++i) {\n if (grid[i][j] == 1) {\n if (jStartIndex == -1) jStartIndex = j;\n jEndIndex = j; // Will keep updating until we reach the last column with '1'\n }\n }\n }\n\n // If no '1' is found, return 0 (no area)\n if (rowStartIndex == -1 || jStartIndex == -1) {\n return 0;\n }\n\n // Calculate the minimum area\n int area = (rowEndIndex - rowStartIndex + 1) * (jEndIndex - jStartIndex + 1);\n return area;\n }\n};\n", "memory": "132800" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int r_min=INT_MAX, c_min=INT_MAX, r_max=INT_MIN, c_max=INT_MIN;\n for(int i=0;i<grid.size();i++){\n for(int j=0;j<grid[0].size();j++){\n if(grid[i][j]){\n r_min=min(r_min, i);\n r_max=max(r_max, i);\n c_min=min(c_min, j);\n c_max=max(c_max, j);\n }\n }\n }\n\n return (r_max-r_min+1) * (c_max-c_min+1);\n }\n};", "memory": "132900" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int minRow = INT_MAX, maxRow = INT_MIN;\n int minCol = INT_MAX, maxCol = INT_MIN;\n\n for (int row = 0; row < grid.size(); ++row) {\n for (int col = 0; col < grid[row].size(); ++col) {\n if (grid[row][col] == 1) {\n if (row < minRow) minRow = row;\n if (row > maxRow) maxRow = row;\n if (col < minCol) minCol = col;\n if (col > maxCol) maxCol = col;\n }\n }\n }\n int rowDiff=maxRow-minRow+1;\n int colDiff=maxCol-minCol+1;\n int area=rowDiff*colDiff;\n return area;\n }\n};", "memory": "132900" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int lRow=INT_MAX;\n int hRow=INT_MIN;\n int lCol=INT_MAX;\n int hCol=INT_MIN;\n int n=grid.size();\n int m=grid[0].size();\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(grid[i][j]==1){\n lRow=min(i,lRow);\n hRow=max(i,hRow);\n lCol=min(j,lCol);\n hCol=max(j,hCol);\n }\n }\n }\n return (hRow-lRow+1)*(hCol-lCol+1);\n }\n};", "memory": "133000" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& g) {\n int n=g.size(),m=g[0].size();\n int t=0,b=n-1,l=0,r=m-1;\n while(t<n){\n bool x=0;\n for(int i=0;i<m;i++){\n if(g[t][i]==1){\n x=1;\n break;\n }\n }\n if(x)break;\n t++;\n }\n // cout<<\"top- \"<<t<<endl;\n while(b>=0){\n bool x=0;\n for(int i=0;i<m;i++){\n if(g[b][i]==1){\n x=1;\n break;\n }\n }\n if(x)break;\n b--;\n }\n // cout<<\"bottom- \"<<b<<endl;\n while(l<m){\n bool x=0;\n for(int i=0;i<n;i++){\n if(g[i][l]==1){\n x=1;\n break;\n }\n }\n // cout<<l<<\" \"<<x<<endl;\n if(x)break;\n l++;\n }\n // cout<<\"left- \"<<l<<endl;\n while(r>=0){\n bool x=0;\n for(int i=0;i<n;i++){\n if(g[i][r]==1){\n x=1;\n break;\n }\n }\n if(x)break;\n r--;\n }\n if(t>=n || b<0 || l>=m || r<0)return 0;\n return abs(abs(t-b)+1)*abs(abs(l-r)+1);\n }\n};", "memory": "133000" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n \n int rows=grid.size();\n int cols=grid[0].size();\n int above_row=rows,down_row=rows,left_col,right_col;\n // int rows=grid.size();\n // int cols=grid[0].size();\n for(int i=0;i<rows;i++)\n {\n int flag=0;\n for(int j=0;j<cols;j++)\n {\n if(grid[i][j])\n {\n flag=1;\n above_row=i;\n break;\n }\n }\n if(flag)\n break;\n }\n \n for(int i=rows-1;i>=0;i--)\n {\n int flag=0;\n for(int j=0;j<cols;j++)\n {\n if(grid[i][j])\n {\n flag=1;\n down_row=i;\n break;\n }\n }\n if(flag)\n break;\n }\n \n for(int i=0;i<cols;i++)\n {\n int flag=0;\n for(int j=0;j<rows;j++)\n {\n if(grid[j][i])\n {\n flag=1;\n left_col=i;\n break;\n }\n }\n if(flag)\n break;\n }\n \n for(int i=cols-1;i>=0;i--)\n {\n int flag=0;\n for(int j=0;j<rows;j++)\n {\n if(grid[j][i])\n {\n flag=1;\n right_col=i;\n break;\n }\n }\n if(flag)\n break;\n }\n \n int width=right_col-left_col+1;\n int height=down_row-above_row+1;\n \n return width*height;\n \n \n \n }\n};", "memory": "133100" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n vector<int> row(grid.size(),0);\n vector<int> col(grid[0].size(),0);\n for(int i=0;i<grid.size();i++){\n for(int j=0;j<grid[0].size();j++){\n if(grid[i][j]==1){\n row[i]=1;\n col[j]=1;\n }\n }\n }\n int a1=-1;int a2=-1; int b1=-1; int b2=-1;\n for(int i=0;i<row.size();i++){\n if(a1==-1&& row[i]==1) a1=i;\n }\n for(int i=row.size()-1;i>=0;i--){\n if(a2==-1&& row[i]==1) a2=i;\n }\n for(int i=0;i<col.size();i++){\n if(b1==-1&& col[i]==1) b1=i;\n }\n for(int i=col.size()-1;i>=0;i--){\n if(b2==-1&& col[i]==1) b2=i;\n }\n return (b2-b1+1)*(a2-a1+1);\n }\n};", "memory": "133200" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int r=grid.size();\n int c=grid[0].size();\n vector<int> hz(c,0);\n vector<int> vr(r,0);\n for(int i=0;i<r;i++){\n for(int j=0;j<c;j++){\n if(grid[i][j]==1){\n hz[j]=1;\n vr[i]=1;\n }\n }\n }\n for(int i=0;i<r;i++){\n cout<<vr[i]<<\" \";\n }\n cout<<endl;\n for(int i=0;i<c;i++){\n cout<<hz[i]<<\" \";\n }\n cout<<endl;\n \n // for(int i=0;i<r;i++){\n // for(int j=0;j<c;j++){\n // cout<<grid[i][j]<<\" \";\n // }\n // cout<<endl;\n // }\n // cout<<\"Grid\"<<endl;\n int r1=-1,r2=-1;\n for(int i=0;i<r;i++){\n if(vr[i]==1 && r1==-1){\n r1=i;\n }\n if(vr[r-1-i]==1 && r2==-1){\n r2=r-1-i;\n }\n }\n int c1=-1,c2=-1;\n for(int i=0;i<c;i++){\n if(hz[i]==1 && c1==-1){\n c1=i;\n }\n if(hz[c-1-i]==1 && c2==-1){\n c2=c-1-i;\n }\n }\n cout<<r2<<\" \"<<r1<<\" \"<<c1<<\" \"<<c2<<endl; \n return (r2-r1+1)*(c2-c1+1);\n // return 0;\n }\n};", "memory": "133300" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int rs=int(grid.size()), cs=int(grid[0].size());\n vector<int> r1(rs), c1(cs);\n for (int r=0; r<rs; ++r) {\n for (int c=0; c<cs; ++c) {\n r1[r]+=grid[r][c];\n c1[c]+=grid[r][c];\n }\n }\n int a=0;\n while (a<rs and r1[a]==0) ++a;\n if (a==rs) return 0;\n int b=rs-1;\n while (b>a and r1[b]==0) --b;\n int c=0;\n while (c<cs and c1[c]==0) ++c;\n if (c==cs) return 0;\n int d=cs-1;\n while (d>c and c1[d]==0) --d;\n return (b-a+1)*(d-c+1);\n }\n};", "memory": "133400" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int n = grid.size(),m=grid[0].size();\n vector<int> vert(n,0),hor(m,0);\n\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(grid[i][j] == 1){\n vert[i]=(1);\n break;\n }\n }\n }\n\n for(int j=0;j<m;j++){\n for(int i=0;i<n;i++){\n if(grid[i][j] == 1){\n hor[j]=(1);\n break;\n }\n }\n }\n\n for(auto x: vert) cout<<x<<\" \";\n cout<<endl;\n for(auto x: hor) cout<<x<<\" \";\n cout<<endl;\n\n int s=0,e=n-1;\n while(s<e){\n if(vert[s] == 0) s++;\n if(vert[e] == 0) e--;\n\n if(vert[s] == 1 && vert[e] == 1) break; \n }\n int l1 = e-s+1;\n\n s=0,e=m-1;\n while(s<e){\n if(hor[s] == 0) s++;\n if(hor[e] == 0) e--;\n\n if(hor[s] == 1 && hor[e] == 1) break; \n }\n int l2 = e-s+1;\n\n return l1*l2;\n }\n};", "memory": "133500" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n \n // checking for rows\n\n int n = grid.size();\n int m = grid[0].size();\n \n vector<int> row;\n for(int i =0;i<n;i++){\n int sum = 0;\n for(int j =0;j<m;j++){\n if(grid[i][j] == 1) sum++;\n }\n row.push_back(sum);\n }\n\n int startr= -1,endr=n;\n for(int i =0;i<n;i++){\n if(row[i] !=0) {\n startr = i;\n break;\n }\n }\n for(int i =n-1;i>=0;i--){\n if(row[i] !=0) {\n endr = i;\n break;\n }\n }\n\n int row_length = endr-startr+1;\n\n\n // column\n vector<int> col;\n\n for(int i =0;i<m;i++){\n int sum = 0;\n for(int j =0;j<n;j++){\n if(grid[j][i] == 1) sum++;\n }\n col.push_back(sum);\n }\n\n int startc= -1,endc=m;\n for(int i =0;i<m;i++){\n if(col[i] !=0) {\n startc = i;\n break;\n }\n }\n\n for(int i =m-1;i>=0;i--){\n if(col[i] !=0) {\n endc = i;\n break;\n }\n }\n\n int col_length= endc-startc+1;\n\n return row_length*col_length;\n\n\n\n\n }\n};", "memory": "133600" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int r = grid.size();\n int c = grid[0].size();\n vector<int> rows;\n vector<int> cols;\n\n for (int i = 0; i < grid.size(); i++){\n int sum = 0;\n for (int j = 0; j < grid[0].size(); j++){\n sum += grid[i][j];\n }\n rows.push_back(sum);\n }\n\n for (int j = 0; j < grid[0].size(); j++){\n int sum = 0;\n for (int i = 0; i < grid.size(); i++){\n sum += grid[i][j];\n }\n cols.push_back(sum);\n }\n\n for (int i = 0; i < rows.size(); i++){\n if (rows[i] > 0){\n break;\n }\n else if (r > 0){\n r -= 1;\n }\n }\n\n for (int i = 0; i < cols.size(); i++){\n if (cols[i] > 0){\n break;\n }\n else if (c > 0){\n c -= 1;\n }\n }\n\n for (int i = rows.size()-1; i >= 0; i--){\n if (rows[i] > 0){\n break;\n }\n else if (r > 0){\n r -= 1;\n }\n }\n\n for (int i = cols.size()-1; i >= 0; i--){\n if (cols[i] > 0){\n break;\n }\n else if (c > 0){\n c -= 1;\n }\n }\n return r * c;\n }\n};", "memory": "133700" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int n=grid.size();\n int m=grid[0].size();\n int i=-1,j=-1;\n vector<int>row(n,0);\n vector<int>col(m,0);\n for(int x=0;x<n;x++){\n for(int y=0;y<m;y++){\n if(grid[x][y]==1){\n row[x]=1;\n col[y]=1;\n }\n }\n }\n int ind1=-1;\n vector<int>ch1;\n for(int x=0;x<n;x++){\n if(row[x]==1){\n ch1.push_back(x);\n }\n }\n int ind2=-1;\n vector<int>ch2;\n \n \n for(int x=0;x<m;x++){\n if(col[x]==1){\n ch2.push_back(x);\n }\n }\n \n \n \n int b=*max_element(ch1.begin(),ch1.end())-*min_element(ch1.begin(),ch1.end())+1;\n int l=*max_element(ch2.begin(),ch2.end())-*min_element(ch2.begin(),ch2.end())+1;\n // cout<<l<<\" \"<<b<<endl;\n return l*b;\n \n }\n};", "memory": "133800" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool ch(vector<int> v1){\n for(int i=0;i<v1.size();i++){\n if(v1[i]==1) return false;\n }\n return true;\n }\n\n int minimumArea(vector<vector<int>>& grid) {\n int len=grid[0].size();\n int hei=grid.size();\n\n int a=0;\n while(a<grid.size()&&ch(grid[a])==true){\n a++;\n hei--;\n }\n \n a=grid.size()-1;\n while(a>-1&&ch(grid[a])==true){\n a--;\n hei--;\n }\n vector<int> uv(grid.size());\n vector<int> bv(grid.size());\n for (int i = 0; i < grid.size(); i++) {\n uv[i] = grid[i][0];\n bv[i] = grid[i][grid[0].size() - 1];\n }\n a=grid[0].size()-1;\n while(a>-1&&ch(bv)==true){\n a--;\n len--;\n if(a>-1){\n\n for(int i=0;i<grid.size();i++){\n bv[i]=(grid[i][a]);\n }\n\n }\n }\n\n a=0;\n while(a<grid[0].size()&&ch(uv)==true){\n a++;\n len--;\n if(a<grid[0].size()){\n\n for(int i=0;i<grid.size();i++){\n uv[i]=(grid[i][a]);\n }\n\n }\n }\n\n \n\n return max(hei,0)*max(len,0); }\n};", "memory": "134100" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool ch(vector<int> v1){\n for(int i=0;i<v1.size();i++){\n if(v1[i]==1) return false;\n }\n return true;\n }\n\n int minimumArea(vector<vector<int>>& grid) {\n int len=grid[0].size();\n int hei=grid.size();\n\n int a=0;\n while(a<grid.size()&&ch(grid[a])==true){\n a++;\n hei--;\n }\n \n a=grid.size()-1;\n while(a>-1&&ch(grid[a])==true){\n a--;\n hei--;\n }\n vector<int> uv(grid.size());\n vector<int> bv(grid.size());\n for (int i = 0; i < grid.size(); i++) {\n uv[i] = grid[i][0];\n bv[i] = grid[i][grid[0].size() - 1];\n }\n a=grid[0].size()-1;\n while(a>-1&&ch(bv)==true){\n a--;\n len--;\n if(a>-1){\n\n for(int i=0;i<grid.size();i++){\n bv[i]=(grid[i][a]);\n }\n\n }\n }\n\n a=0;\n while(a<grid[0].size()&&ch(uv)==true){\n a++;\n len--;\n if(a<grid[0].size()){\n\n for(int i=0;i<grid.size();i++){\n uv[i]=(grid[i][a]);\n }\n\n }\n }\n\n \n\n return max(hei,0)*max(len,0); }\n};", "memory": "134200" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool ch(vector<int> v1){\n for(int i=0;i<v1.size();i++){\n if(v1[i]==1) return false;\n }\n return true;\n }\n\n int minimumArea(vector<vector<int>>& grid) {\n int len=grid[0].size();\n int hei=grid.size();\n\n int a=0;\n while(a<grid.size()&&ch(grid[a])==true){\n a++;\n hei--;\n }\n \n a=grid.size()-1;\n while(a>-1&&ch(grid[a])==true){\n a--;\n hei--;\n }\n vector<int> uv(grid.size());\n vector<int> bv(grid.size());\n for (int i = 0; i < grid.size(); i++) {\n uv[i] = grid[i][0];\n bv[i] = grid[i][grid[0].size() - 1];\n }\n a=grid[0].size()-1;\n while(a>-1&&ch(bv)==true){\n a--;\n len--;\n if(a>-1){\n\n for(int i=0;i<grid.size();i++){\n bv[i]=(grid[i][a]);\n }\n\n }\n }\n\n a=0;\n while(a<grid[0].size()&&ch(uv)==true){\n a++;\n len--;\n if(a<grid[0].size()){\n\n for(int i=0;i<grid.size();i++){\n uv[i]=(grid[i][a]);\n }\n\n }\n }\n\n \n\n return max(hei,0)*max(len,0); }\n};", "memory": "134200" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n set<int>row; set<int>col;\n int minimumArea(vector<vector<int>>& grid) {\n int n = grid.size();int m = grid[0].size();\n for(int i = 0;i<n;i++){\n for(int j =0;j<m;j++){\n if(grid[i][j]==1){ row.insert(i);col.insert(j);}\n\n }\n }\n int l,b;\n l = abs(*row.begin()-*(--row.end()))+1;\n b = abs(*col.begin()-*(--col.end()))+1; \n\n return l*b; \n }\n};", "memory": "134300" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int n=grid.size();\n int m=grid[0].size();\n set<int> row,col;\n for(int i=0;i<n;i++)\n {\n for(int j=0;j<m;j++)\n {\n if(grid[i][j])\n {\n row.insert(i);\n col.insert(j);\n }\n }\n }\n int len=*row.rbegin()-*row.begin()+1;\n int width = *col.rbegin()-*col.begin()+1;\n return len*width;\n }\n};", "memory": "134600" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n unordered_set<int>row;\n unordered_set<int>col;\n int n = grid.size();\n int m = grid[0].size();\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(grid[i][j]==1){\n row.insert(i+1);\n col.insert(j+1);\n }\n }\n }\n vector<int>drow(row.begin(),row.end());\n vector<int>dcol(col.begin(),col.end());\n sort(drow.begin(),drow.end());\n sort(dcol.begin(),dcol.end());\n int l = drow[drow.size()-1]-drow[0]+1;\n int w = dcol[dcol.size()-1]-dcol[0]+1;\n return l*w;\n }\n};", "memory": "135300" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int n = grid.size();\n int m = grid[0].size();\n queue<pair<int,int>>q;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(grid[i][j]==1){\n q.push({i,j});\n }\n }\n }\n int lmin = 1e9;\n int lmax = -1e9;\n int wmin = 1e9;\n int wmax = -1e9;\n int ans = -1e9;\n while(!q.empty()){\n auto it = q.front();\n q.pop();\n lmin = min(lmin,it.first);\n lmax = max(lmax, it.first+1);\n wmin = min(wmin,it.second);\n wmax =max(wmax, it.second+1);\n int len = lmax - lmin;\n int wid = wmax - wmin;\n ans = max(ans , len*wid);\n }\n return ans;\n }\n};", "memory": "148900" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int minx = INT_MAX, maxx = INT_MIN, miny = INT_MAX, maxy = INT_MIN;\n int i = 0;\n for (auto row: grid) {\n int j = 0;\n for (auto element:row) {\n if (element == 1) {\n minx = min(minx, i);\n maxx = max(maxx, i);\n miny = min(miny, j);\n maxy = max(maxy, j);\n }\n j++;\n }\n i++;\n }\n if (minx == INT_MAX) return 0;\n return (maxx - minx + 1) * (maxy - miny + 1);\n }\n};", "memory": "151400" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n int m = grid.size();\n int n = grid[0].size();\n // transpose of grid\n vector<vector<int>>transpose(n, vector<int>(m, 0));\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n transpose[j][i]= grid[i][j];\n }\n }\n int len = m;\n int width = n;\n \n \n int i=0;\n while(i<m){\n if(isRowzero(grid[i])){\n i++;\n len--;\n }\n else break;\n }\n \n int I=m-1;\n while(I>=0){\n if(isRowzero(grid[I])){\n I--;\n len--;\n }\n else break;\n }\n \n int j=0;\n while(j<n){\n if(isRowzero(transpose[j])){\n j++;\n width--;\n }\n else break;\n }\n int J=n-1;\n while(J>=0){\n if(isRowzero(transpose[J])){\n J--;\n width--;\n }\n else break;\n }\n \n return len*width;\n \n \n \n }\nprivate:\n bool isRowzero(vector<int>arr){\n for(int i=0;i<arr.size();i++){\n if(arr[i]!=0) return false;\n }\n return true;\n }\n \n \n};", "memory": "152800" }
3,461
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n int minimumArea(vector<vector<int>>& grid) {\n\n vector<pair<int, int>> v;\n for (int i = 0; i < grid.size(); i++) {\n for (int j = 0; j < grid[0].size(); j++) {\n if (grid[i][j] == 1) {\n v.push_back({i, j});\n }\n }\n }\n\n int minr = INT_MAX, maxr = INT_MIN, minc = INT_MAX, maxc = INT_MIN;\n for (auto it : v) {\n minr = min(minr, it.first);\n maxr = max(maxr, it.first);\n minc = min(minc, it.second);\n maxc = max(maxc, it.second);\n }\n\n int l = maxc - minc + 1, b = maxr - minr + 1;\n return l * b;\n }\n};", "memory": "157000" }