id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& a, int k) {\n // important idea : min decreases/max increases when size of subarray increases\n // nxt[x] : first idx to the right of \"k\" s.t. a[idx] < x\n // prv[x] : first idx to the left of \"k\" s.t. a[idx] < x\n\n int n = a.size();\n\n vector<int> nxt(a[k]+1 , n-1) , prv(a[k]+1 , 0);\n\n int done = a[k];\n for(int i=k+1;i<n;i++){\n for(int j=done;j>a[i];j--){\n nxt[j] = i-1;\n }\n done = min(done , a[i]);\n }\n\n done = a[k];\n for(int i=k-1;i>=0;i--){\n for(int j=done;j>a[i];j--){\n prv[j] = i+1;\n }\n done = min(done , a[i]);\n }\n\n int ans = 0;\n for(int i=a[k];i>=0;i--){\n // considering i is the minimum\n int l = prv[i];\n int r = nxt[i];\n // [l.. k ..r]\n ans = max(ans , (r-l+1) * i);\n }\n return ans;\n }\n};",
"memory": "96600"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "#define FOR(i, n) for(int i=0; i<n; i++)\n#define IFOR(i, a, b) for(int i=a; i<b; i++)\n\nclass Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n int ans = nums[k];\n // Itertaion 1\n stack<int> st1;\n FOR(i, k+1) st1.push(i);\n int mn1 = nums[k];\n IFOR(i, k, n) {\n mn1 = min(mn1, nums[i]);\n while(!st1.empty()) {\n int tp = st1.top();\n if(nums[tp] >= mn1) st1.pop();\n else break;\n }\n if(st1.size() == 0) {\n ans = max(ans, mn1*(i+1));\n } else {\n ans = max(ans, mn1*(i-st1.top()));\n }\n }\n\n // Itertaion 2\n stack<int> st2;\n for(int i=n-1; i>=k; i--) st2.push(i);\n int mn2 = nums[k];\n for(int i=k; i>=0; i--) {\n mn2 = min(mn2, nums[i]);\n while(!st2.empty()) {\n int tp = st2.top();\n if(nums[tp] >= mn2) st2.pop();\n else break;\n }\n if(st2.size() == 0) {\n ans = max(ans, mn2*(n-i));\n } else {\n ans = max(ans, mn2*(st2.top()-i));\n }\n }\n\n return ans;\n }\n};",
"memory": "97700"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size(),res=0;\n vector<int> range(n);\n\n stack<int> st;\n\n for(int i=0;i<n;i++){\n while(!st.empty() && nums[i]<nums[st.top()]){\n int t = st.top();\n st.pop();\n range[t] = i-1;\n }\n st.push(i);\n }\n while(!st.empty() ){\n int t = st.top();\n st.pop();\n range[t] = n-1;\n }\n\n // for(int i=n-1;i>=0;i--){\n // cout<<range[i]<<\" \";\n // }cout<<endl;\n\n for(int i=n-1;i>=0;i--){\n while(!st.empty() && nums[i]<nums[st.top()]){\n int t = st.top();\n st.pop();\n // cout<<t<<\" \";\n if(k>i && k<=range[t])\n {\n int prod = (range[t] - i) * nums[t];\n res = max(res,prod);\n }\n }\n st.push(i);\n }\n while(!st.empty() ){\n int t = st.top();\n st.pop();\n if(k>=0 && k<=range[t])\n {\n int prod = (range[t] +1) * nums[t];\n res = max(res,prod);\n }\n \n }\n return res;\n }\n};",
"memory": "99100"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> l(n);\n stack<int> s;\n for (int i = 0; i < n; i++) {\n while (!s.empty() && (nums[s.top()] >= nums[i])) s.pop();\n l[i] = s.empty() ? -1 : s.top();\n s.push(i);\n }\n while (!s.empty()) s.pop();\n int result = 0;\n for (int i = n - 1; i >= 0; i--) {\n while (!s.empty() && (nums[s.top()] >= nums[i])) s.pop();\n int r = s.empty() ? n : s.top();\n if ((l[i] < k) && (k < r)) result = max(result, (r - l[i] - 1) * nums[i]);\n s.push(i);\n }\n return result;\n }\n};",
"memory": "99300"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n stack<int>s;\n vector<int>nxt(n);\n for(int i=0;i<n;++i){\n while(!s.empty() && nums[s.top()]>nums[i]){\n nxt[s.top()]=i;\n s.pop();\n }\n s.push(i);\n }\n while(!s.empty()){\n nxt[s.top()]=n;\n s.pop();\n }\n int ans=0;\n for(int i=n-1;i>=0;--i){\n while(!s.empty() && nums[s.top()]>nums[i]){\n if(nxt[s.top()]>k && i<k){\n ans=max(ans,(nxt[s.top()]-i-1)*nums[s.top()]);\n }\n s.pop();\n }\n s.push(i);\n }\n while(!s.empty()){\n if(nxt[s.top()]>k){\n ans=max(ans,nxt[s.top()]*nums[s.top()]);\n }\n s.pop();\n }\n return ans;\n }\n};",
"memory": "99400"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n vector<int>pref(k+1 , 0) , suff ; \n pref[k] = nums[k] ;\n for(int i=k-1 ; i>=0 ;i--){\n pref[i] = (min(pref[i+1] , nums[i])) ;\n }\n suff.push_back(nums[k]) ;\n \n for(int i=k+1 ;i<nums.size() ;i++){\n suff.push_back(min(nums[i] , suff[i-k-1] )) ;\n }\n \n int left=pref.size() , right=0 ;\n int ans =INT_MIN ;\n \n while (left > 0 || right < suff.size() - 1) {\n if (left == 0 || (right < suff.size() - 1 && suff[right + 1] > pref[left - 1])) {\n right++;\n } else {\n left--;\n }\n int min_val = min(pref[left] , suff[right]) ;\nans= max(ans , min_val*(right-left+k+1)) ;\n } \n \n\n return ans;\n }\n};",
"memory": "100000"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int ans = nums[k];\n int i = 0;\n int n = nums.size();\n int j = n - 1;\n\n vector<int> left(n, INT_MAX), right(n, INT_MAX);\n left[k] = nums[k];\n right[k] = nums[k];\n for (int i = k - 1; i >= 0; i--) {\n left[i] = min(nums[i], left[i + 1]);\n }\n for (int i = k + 1; i < n; i++) {\n right[i] = min(nums[i], right[i - 1]);\n }\n while(i < k && j > k) {\n ans = max(ans, min(left[i], right[j]) * (j - i + 1));\n if (left[i] < right[j]) \n i++;\n else \n j--;\n }\n while(j > k) {\n ans = max(ans, min(left[i], right[j]) * (j - i + 1));\n j--;\n }\n while(i < k) {\n ans = max(ans, min(left[i], right[j]) * (j - i + 1));\n i++;\n }\n return ans;\n }\n};",
"memory": "100300"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size(),maxi=0;\n vector<int> pre(n),suff(n);\n int mini=nums[k];\n for(int i=k;i>=0;i--){\n mini=min(mini,nums[i]);\n pre[i]=mini;\n }\n mini=nums[k];\n for(int i=k;i<n;i++){\n mini=min(mini,nums[i]);\n suff[i]=mini;\n }\n for(int i=0;i<n;i++){\n int left=i,right=i;\n int l=0,r=k;\n while(l<=r){\n int mid=(l+r)/2;\n if(nums[i]<=pre[mid]){\n left=mid;\n r=mid-1;\n }else{\n l=mid+1;\n }\n }\n l=k,r=n-1;\n while(l<=r){\n int mid=(l+r)/2;\n if(nums[i]<=suff[mid]){\n right=mid;\n l=mid+1;\n }else{\n r=mid-1;\n }\n }\n \n if(left<=k && k<=right){\n maxi=max(maxi,abs(right-left+1)*nums[i]);\n }\n }\n return maxi;\n }\n};",
"memory": "100400"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& A, int k) {\n const int N = A.size();\n \n // monostack\n vector<int> rs(N);\n for (int i = N - 1; i >= 0; i --){\n int n = i + 1;\n\n // strictly greater\n while (n < N && A[n] >= A[i]){\n n = rs[n];\n }\n\n rs[i] = n;\n }\n\n vector<int> ls(N);\n for (int i = 0; i < N; i++){\n int n = i - 1;\n \n // strictly greater\n while (n >= 0 && A[n] >= A[i]){\n n = ls[n];\n }\n\n ls[i] = n;\n }\n\n int res = 0;\n for (int i = 0; i < A.size(); i++){\n int l = ls[i];\n int r = rs[i];\n l++; r--;\n\n if (l <= k && k <= r){\n res = max(res,\n (r - l + 1) * A[i]\n );\n }\n }\n return res;\n }\n};",
"memory": "100500"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n \n // Arrays to store the left and right boundaries for each element\n vector<int> left(n), right(n);\n \n // Initialize left boundaries\n for (int i = 0; i < n; i++) {\n left[i] = i;\n while (left[i] > 0 && nums[left[i] - 1] >= nums[i]) {\n left[i] = left[left[i] - 1]; // Move leftwards while nums are greater than or equal\n }\n }\n \n // Initialize right boundaries\n for (int i = n - 1; i >= 0; i--) {\n right[i] = i;\n while (right[i] < n - 1 && nums[right[i] + 1] >= nums[i]) {\n right[i] = right[right[i] + 1]; // Move rightwards while nums are greater than or equal\n }\n }\n \n // Calculate the maximum score for subarrays that include index k\n int maxScore = 0;\n for (int i = 0; i < n; i++) {\n if (left[i] <= k && right[i] >= k) { // Check if the subarray includes k\n int score = nums[i] * (right[i] - left[i] + 1); // Calculate score for subarray\n maxScore = max(maxScore, score);\n }\n }\n \n return maxScore;\n }\n};\n",
"memory": "100600"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& a, int k) {\n int n=a.size();\n vector<int> st;\n st.push_back(-1);\n int ans=0;\n vector<int> right(n+1);\n for(int i=n-1;i>=0;i--){\n while(st.back()!=-1 and a[st.back()]>=a[i]){\n st.pop_back();\n }\n int x=st.back();\n right[i]=(x==-1)?n:x;\n st.push_back(i);\n }\n\n while(st.back()!=-1) st.pop_back();\n\n vector<int> left(n+1);\n for(int i=0;i<n;i++){\n while(st.back()!=-1 and a[st.back()]>=a[i]){\n st.pop_back();\n }\n int x=st.back();\n left[i]=(x==-1)?-1:x;\n st.push_back(i);\n }\n for(int i=0;i<n;i++){\n if(left[i]<k and right[i]>k)ans=max(ans,(right[i]-left[i]-1)*a[i]);\n }\n\n return ans;\n }\n};",
"memory": "101500"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> left(n, -1);\n vector<int> stack;\n \n for (int i = n - 1; i >= 0; i--) {\n while (!stack.empty() && nums[stack.back()] > nums[i]) {\n left[stack.back()] = i;\n stack.pop_back();\n }\n \n stack.push_back(i);\n }\n \n vector<int> right(n, n);\n stack.clear();\n for (int i = 0; i < n; i++) {\n while (!stack.empty() && nums[stack.back()] > nums[i]) {\n right[stack.back()] = i;\n stack.pop_back();\n }\n \n stack.push_back(i);\n }\n \n int ans = 0;\n for (int i = 0; i < n; i++) {\n if (left[i] < k && right[i] > k) {\n ans = max(ans, nums[i] * (right[i] - left[i] - 1));\n }\n }\n \n return ans;\n }\n};",
"memory": "101800"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> left(n, -1);\n vector<int> stack;\n \n for (int i = n - 1; i >= 0; i--) {\n while (!stack.empty() && nums[stack.back()] > nums[i]) {\n left[stack.back()] = i;\n stack.pop_back();\n }\n \n stack.push_back(i);\n }\n \n vector<int> right(n, n);\n stack.clear();\n for (int i = 0; i < n; i++) {\n while (!stack.empty() && nums[stack.back()] > nums[i]) {\n right[stack.back()] = i;\n stack.pop_back();\n }\n \n stack.push_back(i);\n }\n \n int ans = 0;\n for (int i = 0; i < n; i++) {\n if (left[i] < k && right[i] > k) {\n ans = max(ans, nums[i] * (right[i] - left[i] - 1));\n }\n }\n \n return ans;\n }\n};",
"memory": "101900"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> left(n, -1);\n vector<int> stack;\n \n for (int i = n - 1; i >= 0; i--) {\n while (!stack.empty() && nums[stack.back()] > nums[i]) {\n left[stack.back()] = i;\n stack.pop_back();\n }\n \n stack.push_back(i);\n }\n \n vector<int> right(n, n);\n stack.clear();\n for (int i = 0; i < n; i++) {\n while (!stack.empty() && nums[stack.back()] > nums[i]) {\n right[stack.back()] = i;\n stack.pop_back();\n }\n \n stack.push_back(i);\n }\n \n int ans = 0;\n for (int i = 0; i < n; i++) {\n if (left[i] < k && right[i] > k) {\n ans = max(ans, nums[i] * (right[i] - left[i] - 1));\n }\n }\n \n return ans;\n }\n};",
"memory": "102000"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> left(n, -1);\n vector<int> stack;\n \n for (int i = n - 1; i >= 0; i--) {\n while (!stack.empty() && nums[stack.back()] > nums[i]) {\n left[stack.back()] = i;\n stack.pop_back();\n }\n \n stack.push_back(i);\n }\n \n vector<int> right(n, n);\n stack.clear();\n for (int i = 0; i < n; i++) {\n while (!stack.empty() && nums[stack.back()] > nums[i]) {\n right[stack.back()] = i;\n stack.pop_back();\n }\n \n stack.push_back(i);\n }\n \n int ans = 0;\n for (int i = 0; i < n; i++) {\n if (left[i] < k && right[i] > k) {\n ans = max(ans, nums[i] * (right[i] - left[i] - 1));\n }\n }\n \n return ans;\n }\n};",
"memory": "102100"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nSolution(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n}\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n stack<int> sl, sr;\n vector<int> left(n,-1), right(n,n);\n for(int i=0; i<n; i++){\n while(!sl.empty() && nums[sl.top()]>=nums[i]) sl.pop();\n if(!sl.empty()) left[i] = sl.top();\n sl.push(i);\n }\n\n for(int i=n-1; i>=0; i--){\n while(!sr.empty() && nums[sr.top()]>=nums[i]) sr.pop();\n if(!sr.empty()) right[i] = sr.top();\n sr.push(i);\n }\n int res = 0;\n for(int i=0; i<n; i++){\n int l = left[i], r = right[i];\n if(k>=r || k<= l) continue;\n res = max(res, nums[i]*(r-l-1));\n }\n return res;\n }\n};",
"memory": "102800"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nSolution(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n}\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n stack<int> sl, sr;\n vector<int> left(n,-1), right(n,n);\n for(int i=0; i<n; i++){\n while(!sl.empty() && nums[sl.top()]>=nums[i]) sl.pop();\n if(!sl.empty()) left[i] = sl.top();\n sl.push(i);\n }\n\n for(int i=n-1; i>=0; i--){\n while(!sr.empty() && nums[sr.top()]>=nums[i]) sr.pop();\n if(!sr.empty()) right[i] = sr.top();\n sr.push(i);\n }\n int res = 0;\n for(int i=0; i<n; i++){\n int l = left[i], r = right[i];\n if(k>=r || k<= l) continue;\n res = max(res, nums[i]*(r-l-1));\n }\n return res;\n }\n};",
"memory": "102900"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n Solution(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n }\n \n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> left(n), right(n);\n\n // Combine left and right boundary calculation in a single pass\n stack<int> st;\n \n for (int i = 0; i < n; ++i) {\n while (!st.empty() && nums[st.top()] >= nums[i]) st.pop();\n left[i] = (st.empty() ? -1 : st.top());\n st.push(i);\n }\n\n // Clear the stack to reuse it for the right boundary\n while (!st.empty()) st.pop();\n \n for (int i = n - 1; i >= 0; --i) {\n while (!st.empty() && nums[st.top()] >= nums[i]) st.pop();\n right[i] = (st.empty() ? n : st.top());\n st.push(i);\n }\n\n int res = 0;\n \n // Calculate the maximum score for each element\n for (int i = 0; i < n; ++i) {\n if (left[i] < k && right[i] > k) { // Only consider ranges that include index k\n res = max(res, nums[i] * (right[i] - left[i] - 1));\n }\n }\n\n return res;\n }\n};\n",
"memory": "103000"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nSolution(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n}\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n stack<int> sl, sr;\n vector<int> left(n,-1), right(n,n);\n for(int i=0; i<n; i++){\n while(!sl.empty() && nums[sl.top()]>=nums[i]) sl.pop();\n if(!sl.empty()) left[i] = sl.top();\n sl.push(i);\n }\n\n for(int i=n-1; i>=0; i--){\n while(!sr.empty() && nums[sr.top()]>=nums[i]) sr.pop();\n if(!sr.empty()) right[i] = sr.top();\n sr.push(i);\n }\n int res = 0;\n for(int i=0; i<n; i++){\n int l = left[i], r = right[i];\n if(k>=r || k<= l) continue;\n res = max(res, nums[i]*(r-l-1));\n }\n return res;\n }\n};",
"memory": "103100"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int> l(n),r(n);\n stack<int> st;\n for(int i=0;i<n;i++){\n while(!st.empty() && nums[i]<=nums[st.top()]){\n st.pop();\n }\n l[i]=st.empty()?0:st.top()+1;\n st.push(i);\n }\n while(!st.empty()) st.pop();\n for(int i=n-1;i>=0;i--){\n while(!st.empty() && nums[i]<nums[st.top()]){\n st.pop();\n }\n r[i]=st.empty()?n-1:st.top()-1;\n st.push(i);\n }\n int maxi=0;\n for(int i=0;i<n;i++){\n if(r[i]>=k && k>=l[i]){\n int sum=(r[i]-l[i]+1)*nums[i];\n maxi=max(maxi,sum);\n }\n }\n return maxi;\n }\n};",
"memory": "103200"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "#include <vector>\n#include <stack>\n#include <algorithm>\n#include <climits>\n#include <iostream>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> nsl(n + 1, n); \n vector<int> psl(n + 1, -1); \n stack<int> st;\n\n \n st.push(n - 1);\n for (int i = n - 2; i >= 0; i--) {\n while (!st.empty() && nums[i] <= nums[st.top()]) {\n st.pop();\n }\n nsl[i] = st.empty() ? n : st.top();\n st.push(i);\n }\n\n while (!st.empty()) {\n st.pop();\n }\n\n \n st.push(0);\n for (int i = 1; i < n; i++) {\n while (!st.empty() && nums[i] <= nums[st.top()]) {\n st.pop();\n }\n psl[i] = st.empty() ? -1 : st.top();\n st.push(i);\n }\n\n int l = k, r = k;\n long long int ans = nums[k];\n\n for (int i = 0; i < n; i++) {\n if (psl[i] < k && nsl[i] > k) {\n ans=max(ans,1ll*nums[i]*(nsl[i]-psl[i]-1));\n }\n }\n return ans;\n }\n};\n",
"memory": "103300"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n stack<int>st;\n int n = nums.size();\n vector<int>left(n,-1), right(n,n);\n\n for(int i = 0 ; i < n ; i++){\n while(!st.empty() && nums[st.top()] >= nums[i]) {\n st.pop();\n }\n\n if(!st.empty()) left[i] = st.top();\n\n st.push(i);\n }\n\n while (!st.empty()) st.pop();\n for(int i = n-1 ; i >=0 ; i--){\n while(!st.empty() && nums[st.top()] >= nums[i]) {\n st.pop();\n }\n\n if(!st.empty()) right[i] = st.top();\n\n st.push(i);\n }\n\n\n int ans = 0;\n\n for(int i = 0 ; i < n ; i++){\n if(left[i] < k && right[i] > k){\n ans = max(ans , (right[i] - left[i] - 1)* nums[i] );\n }\n }\n\n return ans;\n }\n};",
"memory": "103400"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n stack<int> s,ss;\n vector<int> nse(n,n),pse(n,-1);\n for(int i=0;i<n;i++){\n while(!s.empty() && nums[i]<=nums[s.top()]){\n nse[s.top()]=i;\n s.pop();\n }\n s.push(i);\n }\n for(int i=n-1;i>=0;i--){\n while(!ss.empty() && nums[i]<nums[ss.top()]){\n pse[ss.top()]=i;\n ss.pop();\n }\n ss.push(i);\n }\n int ans=0;\n for(int i=0;i<n;i++){\n if((pse[i]+1)<=k && (nse[i]-1)>=k ){\n ans=max(ans,(nse[i]-pse[i]-1)*nums[i]);\n }\n }\n return ans;\n }\n};",
"memory": "103500"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n\n int N = nums.size();\n vector<int> left_smallest_num(N);\n vector<int> right_smallest_num(N);\n\n stack<int> l_stc{};\n stack<int> r_stc{};\n\n for (int i = 0; i < N; i++) {\n while (!l_stc.empty() && nums[l_stc.top()] >= nums[i]) {\n l_stc.pop();\n }\n if (l_stc.empty())\n left_smallest_num[i] = -1;\n else\n left_smallest_num[i] = l_stc.top();\n\n l_stc.push(i);\n }\n\n for (int i = N - 1; i >= 0; i--) {\n while (!r_stc.empty() && nums[r_stc.top()] >= nums[i]) {\n r_stc.pop();\n }\n if (r_stc.empty())\n right_smallest_num[i] = N;\n else\n right_smallest_num[i] = r_stc.top();\n\n r_stc.push(i);\n }\n\n int max_area = 0;\n\n for (int i = 0; i < N; i++) {\n if (left_smallest_num[i] < k and right_smallest_num[i] > k) {\n int width = right_smallest_num[i] - left_smallest_num[i] - 1;\n int area = nums[i] * width;\n max_area = max(max_area, area);\n }\n }\n\n return max_area;\n }\n};",
"memory": "103500"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvector<int>nse(vector<int >&nums){\nstack<int>st;\nint i;\nint n=nums.size();\nvector<int>ans(n);\nfor(i=n-1;i>=0;i--){\n while(!st.empty()&&nums[st.top()]>=nums[i]){\n st.pop();\n }\n if(st.empty()){\nans[i]=n-1;\n }\n else{\n ans[i]=st.top()-1;\n }\nst.push(i);\n}\nreturn ans;\n}\nvector<int>pse(vector<int >&nums){\nstack<int>st;\nint i;\nint n=nums.size();\nvector<int>ans(n);\nfor(i=0;i<n;i++){\n while(!st.empty()&&nums[st.top()]>=nums[i]){\n // cout<<nums[i]<<\" \"<<st.top()<<endl;\n st.pop();\n }\n if(st.empty()){\nans[i]=0;\n }\n else{\n ans[i]=st.top()+1;\n }\nst.push(i);\n}\nreturn ans;\n}\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int>ns= nse(nums);\n vector<int>ls=pse(nums);\n // for(auto it:ns) cout<<it<<\" \";\n // cout<<endl;\n // for(auto it:ls) cout<<it<<\" \";\n int ans=0;\n int i;\n for(i=0;i<n;i++){\n int j=(ns[i]);\n int l= (ls[i]);\n if((l)<=k&&(j)>=k){\n j=max(ns[i],k);\n l= min(ls[i],k);\n int p=abs(j-l+1)*nums[i];\n cout<<p<<\" \" <<i<<endl;\n ans=max(ans,p);\n }\n }\n return ans;\n \n }\n};",
"memory": "103600"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> leftSmaller(vector<int>& arr){\n int n = arr.size();\n stack<int> st;\n vector<int> ans(n);\n\n for(int i = 0; i < n; i++){\n while(!st.empty() && arr[i] < arr[st.top()]){\n st.pop();\n }\n\n if(st.empty())ans[i] = -1;\n else ans[i] = st.top();\n\n st.push(i);\n }\n \n return ans;\n }\n\n vector<int> rightSmaller(vector<int>& arr){\n int n = arr.size();\n stack<int> st;\n vector<int> ans(n);\n\n for(int i = n - 1; i >= 0; i--){\n while(!st.empty() && arr[i] <= arr[st.top()]){\n st.pop();\n }\n\n if(st.empty())ans[i] = n;\n else ans[i] = st.top();\n\n st.push(i);\n }\n \n return ans;\n }\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> ls = leftSmaller(nums);\n vector<int> rs = rightSmaller(nums);\n\n int ans = 0;\n for(int i = 0; i < n; i++){\n if(k > ls[i] && k < rs[i]){\n int value = (rs[i] - ls[i] - 1) * nums[i];\n ans = max(ans, value);\n }\n }\n\n return ans;\n }\n};",
"memory": "103600"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n void stack_op(int i, stack<int> &st, vector<int> &heights, vector<int> &nse){\n if (st.empty()){\n st.push(i);\n }\n else{\n while (!st.empty() && heights[st.top()] > heights[i]){\n nse[st.top()] = i;\n st.pop();\n }\n st.push(i);\n }\n }\n\n void find_nse(vector<int> &nse, bool forward, vector<int> &heights, int n){\n stack<int> st;\n if (forward){\n for (int i = 0; i < n; i++){\n stack_op(i, st, heights, nse);\n }\n }\n else{\n for (int i = n - 1; i >= 0; i--){\n stack_op(i, st, heights, nse);\n }\n }\n }\n\n int largestRectangleArea(vector<int>& heights, int k) {\n int n = heights.size();\n vector<int> nse_forward(n, -1), nse_backward(n, -1);\n find_nse(nse_forward, true, heights, n);\n find_nse(nse_backward, false, heights, n);\n\n int max_area = -1;\n for (int i = 0; i < n; i++){\n int cur_area = 0;\n int right_index = nse_forward[i], left_index = nse_backward[i];\n if (left_index == -1 && right_index == -1){\n left_index = -1;\n right_index = n;\n cur_area = heights[i] * n;\n }\n else if (left_index == -1){\n left_index = -1;\n cur_area = heights[i] * (right_index);\n }\n else if (right_index == -1){\n right_index = n;\n cur_area = heights[i] * (i - left_index + n - 1 - i);\n }\n else\n cur_area = heights[i] * (right_index - left_index - 1);\n \n if (left_index + 1 > k || right_index - 1 < k)\n continue;\n \n max_area = max(max_area, cur_area);\n }\n\n return max_area;\n }\n\npublic:\n int maximumScore(vector<int>& nums, int k) {\n return largestRectangleArea(nums, k);\n }\n};",
"memory": "104500"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> li(n),ri(n);\n\n vector<int> st(n);\n int stp = 0;\n li[0] = -1;\n st[stp] = 0;\n int i = 1;\n while(stp != -1 && i < n){\n while(stp != -1 && nums[st[stp]] >= nums[i] ){\n stp--;\n }\n if(stp != -1) li[i] = st[stp];\n else li[i] = -1;\n stp++;\n st[stp] = i;\n i++;\n }\n\n stp = 0;\n ri[n-1] = n;\n st[stp] = n-1;\n i = n-2;\n while(stp != -1 && i>-1){\n while(stp != -1 && nums[st[stp]] >= nums[i] ){\n stp--;\n }\n if(stp != -1) ri[i] = st[stp];\n else ri[i] = n;\n stp++;\n st[stp] = i;\n i--;\n }\n for(auto e:li){\n cout<<e<<\" \";\n }\n cout<<endl;\n for(auto e:ri){\n cout<<e<<\" \";\n }\n\n int gs = 0;\n for(int i =0; i < n; i++){\n if(li[i]+1 <= k && ri[i]-1 >= k){\n gs = max(gs, nums[i] * ( (ri[i] - 1) - (li[i] +1) + 1));\n }\n }\n \n return gs;\n\n }\n};",
"memory": "104600"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int ans = solve(nums, k);\n reverse(nums.begin(), nums.end());\n return max(ans, solve(nums, nums.size() - k - 1));\n }\n \n int solve(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> left(k, 0);\n int currMin = INT_MAX;\n for (int i = k - 1; i >= 0; i--) {\n currMin = min(currMin, nums[i]);\n left[i] = currMin;\n }\n \n vector<int> right;\n currMin = INT_MAX;\n for (int i = k; i < n; i++) {\n currMin = min(currMin, nums[i]);\n right.push_back(currMin);\n }\n \n int ans = 0;\n for (int j = 0; j < right.size(); j++) {\n currMin = right[j];\n int i = lower_bound(left.begin(), left.end(), currMin) - left.begin();\n int size = (k + j) - i + 1;\n ans = max(ans, currMin * size);\n }\n \n return ans;\n }\n};",
"memory": "108000"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int ans = solve(nums, k);\n reverse(nums.begin(), nums.end());\n return max(ans, solve(nums, nums.size() - k - 1));\n }\n \n int solve(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> left(k, 0);\n int currMin = INT_MAX;\n for (int i = k - 1; i >= 0; i--) {\n currMin = min(currMin, nums[i]);\n left[i] = currMin;\n }\n \n vector<int> right;\n currMin = INT_MAX;\n for (int i = k; i < n; i++) {\n currMin = min(currMin, nums[i]);\n right.push_back(currMin);\n }\n \n int ans = 0;\n for (int j = 0; j < right.size(); j++) {\n currMin = right[j];\n int i = lower_bound(left.begin(), left.end(), currMin) - left.begin();\n int size = (k + j) - i + 1;\n ans = max(ans, currMin * size);\n }\n \n return ans;\n }\n};",
"memory": "108000"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\n class SegmentTree {\n vector<int> tree;\n int n;\n\n // Build the segment tree from the given array\n void buildTree(vector<int>& arr, int start, int end, int node) {\n if (start == end) {\n tree[node] = arr[start];\n } else {\n int mid = (start + end) / 2;\n buildTree(arr, start, mid, 2 * node + 1);\n buildTree(arr, mid + 1, end, 2 * node + 2);\n tree[node] = min(tree[2 * node + 1], tree[2 * node + 2]);\n }\n }\n\n // Query the segment tree to find the minimum in range [L, R]\n int queryTree(int start, int end, int L, int R, int node) {\n if (R < start || L > end) { // range completely outside\n return INT_MAX;\n }\n if (L <= start && end <= R) { // range completely inside\n return tree[node];\n }\n\n int mid = (start + end) / 2;\n int leftMin = queryTree(start, mid, L, R, 2 * node + 1);\n int rightMin = queryTree(mid + 1, end, L, R, 2 * node + 2);\n return min(leftMin, rightMin);\n }\n\n public:\n // Initialize the segment tree with the given array\n SegmentTree(vector<int>& arr) {\n n = arr.size();\n tree.resize(4 * n, INT_MAX);\n buildTree(arr, 0, n - 1, 0);\n }\n\n // Query the segment tree to find the minimum in range [L, R]\n int getMin(int L, int R) {\n return queryTree(0, n - 1, L, R, 0);\n }\n };\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n int res = 0;\n SegmentTree segTree(nums);\n\n int lo = k;\n int hi = k;\n\n while (lo >= 0 && hi < n) {\n int mn = segTree.getMin(lo, hi);\n res = max(res, (hi - lo + 1) * mn);\n\n if (lo == 0) {\n hi++;\n } else if (hi == n-1) {\n lo--;\n } else if (nums[lo-1] < nums[hi+1]) {\n hi++;\n } else {\n lo--;\n }\n }\n\n return res;\n }\n};",
"memory": "108100"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "#define ll long long\n#define pb push_back\nclass Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n ll n = nums.size();\n vector<ll>prev(n);\n vector<ll>next(n);\n prev[k]=nums[k];\n next[k]=nums[k];\n for(ll i=k+1;i<n;i++){\n prev[i]=min(prev[i-1],1LL*nums[i]);\n }\n for(ll i=k-1;i>=0;i--){\n prev[i]=min(prev[i+1],1LL*nums[i]);\n }\n\n ll beg = 0;\n ll end = n-1;\n ll maxi = nums[k];\n while(beg<=end){\n maxi=max(maxi,(end-beg+1)*min(prev[beg],prev[end]));\n if(prev[beg]<=prev[end]){\n beg++;\n }else{\n end--;\n }\n }\n return maxi;\n\n }\n};",
"memory": "108300"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& a, int k) {\n int n = a.size();\n map<int,int> mp2;map<int,int> mp1;\n int mi = a[k],ans=a[k];mp2[mi]=k;\n if(n==1){return ans;}\n vector<int> vmi(n,a[k]);\n for(int i=k;i<n;i++){\n mi = min(mi,a[i]);\n mp2[mi]=i;vmi[i]=mi;\n }\n mi = a[k];mp1[mi]=k;\n for(int i=k;i>=0;i--){\n mi= min(mi,a[i]);\n vmi[i] = mi;mp1[mi]=i;\n }\n for(int i=0;i<=k;i++){\n if(i<n-1&&vmi[i]==vmi[i+1])continue;\n int tp = vmi[i];\n auto it = mp2.lower_bound(tp);\n int id = ((it==mp2.end())?k:(*it).second);\n ans = max(ans,tp*(id-mp1[tp]+1));\n }\n for(int i=k;i<n;i++){\n if(i<n-1&&vmi[i]==vmi[i+1])continue;\n int tp = vmi[i];\n auto it = mp1.lower_bound(tp);\n int id = ((it==mp2.end())?k:(*it).second);\n ans = max(ans,tp*(mp2[tp]-id+1));\n }\n return ans;\n }\n};",
"memory": "108800"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n\n vector<int> v1;\n vector<int> v2;\n\n int find(int target,int k,int n){\n\n // find elements from k to 0 for which target is min\n // find elements from k+1 to n for which target is min\n\n \n\n int elemt1 = k+1 - (lower_bound(v1.begin(),v1.end(),target)-v1.begin());\n int elemt2 = v2.size() - (lower_bound(v2.begin(),v2.end(),target)-v2.begin());\n\n return target * (elemt1 +elemt2 );\n\n\n }\n\n int maximumScore(vector<int>& nums, int k) {\n\n stack<int> s;\n\n int n = nums.size();\n\n vector<int> minArr(n,-1);\n\n minArr[k] = nums[k];\n\n for(int i = k;i>=0;i--){\n\n while(!s.empty() && nums[i] < s.top()){\n s.pop();\n }\n \n if(s.empty() || nums[i] < s.top())\n s.push(nums[i]);\n minArr[i] = s.top();\n\n }\n\n while(!s.empty()) s.pop();\n\n for(int i = k;i<n;i++){\n\n while(!s.empty() && nums[i] < s.top()){\n s.pop();\n }\n \n if(s.empty() || nums[i] < s.top())\n s.push(nums[i]);\n minArr[i] = s.top();\n\n }\n\n for(int i = 0;i<n;i++){\n\n cout<<minArr[i]<<\" \";\n\n if(i<=k){\n v1.push_back(minArr[i]);\n \n }else{\n v2.push_back(minArr[i]);\n }\n }\n\n reverse(v2.begin(),v2.end());\n \n\n int ans = 0;\n\n for(int i =0;i<n;i++){\n ans = max(ans , find(minArr[i],k,n));\n } \n\n\n return ans;\n \n }\n};\n\n/**\n\nnums = [1,4,3,7,4,5], k = 3\n\nnum = [1,4,3,7,4,5]\nminar = [1,3,3,7,4,4]\n\n\n\n\n**/",
"memory": "109000"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n int ans=0;\n stack<int> st;\n stack<int> st_r;\n vector<int> NSL;\n vector<int> NSR(n,0);\n for(int i=0;i<n;i++){\n while(!st.empty() && nums[i]<=nums[st.top()]){\n st.pop();\n }\n if(st.empty()) NSL.push_back(-1);\n else NSL.push_back(st.top());\n st.push(i);\n }\n // st.clear();\n for(int i=n-1;i>=0;i--){ // decreasing monotonic stack\n while(!st_r.empty() && nums[i]<=nums[st_r.top()]){\n st_r.pop();\n }\n if(st_r.empty()) NSR[i]=n;\n else NSR[i]=st_r.top();\n st_r.push(i);\n }\n for(int i=0;i<n;i++){\n if(NSL[i]+1<=k && NSR[i]-1>=k){ // i<=k && j>=k // here i = NSL[i]+1. and j=NSR[i]-1\n ans=max(ans,nums[i]*(NSR[i]-NSL[i]-1));\n }\n }\n return ans;\n }\n};",
"memory": "110300"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int>left;\n vector<int>right(n);\n stack<int>st;\n for(int i=0;i<n;i++){\n while(!st.empty()&&nums[st.top()]>=nums[i]) st.pop();\n if(st.empty()) left.push_back(-1);\n else left.push_back(st.top());\n st.push(i);\n }\n st=stack<int>();\n for(int i=n-1;i>=0;i--){\n while(!st.empty()&&nums[st.top()]>=nums[i]) st.pop();\n if(st.empty()) right[i]=n;\n else right[i]=st.top();\n st.push(i);\n }\n int ans=0;\n for(int i=0;i<n;i++){\n cout<<left[i]<<\" \"<<right[i]<<endl;\n if(left[i]+1<=k&&right[i]-1>=k){\n ans=max(ans,(abs(left[i]-right[i])-1)*nums[i]);\n }\n }\n return ans;\n }\n};",
"memory": "110400"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int>left;\n vector<int>right(n);\n stack<int>st;\n for(int i=0;i<n;i++){\n while(!st.empty()&&nums[st.top()]>=nums[i]) st.pop();\n if(st.empty()) left.push_back(-1);\n else left.push_back(st.top());\n st.push(i);\n }\n st=stack<int>();\n for(int i=n-1;i>=0;i--){\n while(!st.empty()&&nums[st.top()]>=nums[i]) st.pop();\n if(st.empty()) right[i]=n;\n else right[i]=st.top();\n st.push(i);\n }\n int ans=0;\n for(int i=0;i<n;i++){\n // cout<<left[i]<<\" \"<<right[i]<<endl;\n if(left[i]+1<=k&&right[i]-1>=k){\n ans=max(ans,(abs(left[i]-right[i])-1)*nums[i]);\n }\n }\n return ans;\n }\n};",
"memory": "110400"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n int ans=0;\n stack<int> st;\n stack<int> st_r;\n vector<int> NSL;\n vector<int> NSR(n,0);\n for(int i=0;i<n;i++){\n while(!st.empty() && nums[i]<=nums[st.top()]){\n st.pop();\n }\n if(st.empty()) NSL.push_back(-1);\n else NSL.push_back(st.top());\n st.push(i);\n }\n // st.clear();\n for(int i=n-1;i>=0;i--){ // decreasing monotonic stack\n while(!st_r.empty() && nums[i]<=nums[st_r.top()]){\n st_r.pop();\n }\n if(st_r.empty()) NSR[i]=n;\n else NSR[i]=st_r.top();\n st_r.push(i);\n }\n for(int i=0;i<n;i++){\n if(NSL[i]+1<=k && NSR[i]-1>=k){\n ans=max(ans,nums[i]*(NSR[i]-NSL[i]-1));\n }\n }\n return ans;\n }\n};",
"memory": "110500"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n\n stack<int>s;\n vector<long long>left(n,0),right(n,0);\n for(int i=n-1;i>=0;i--){\n while(s.size() && nums[s.top()]>=nums[i]){\n s.pop();\n }\n if(s.size()){\n right[i]=s.top()-i;\n }\n else{\n right[i]=n-i;\n }\n s.push(i);\n }\n\n while(s.size()){\n s.pop();\n }\n\n for(int i=0;i<n;i++){\n while(s.size() && nums[s.top()]>=nums[i]){\n s.pop();\n }\n if(s.size()){\n left[i]=i-s.top();\n }\n else{\n left[i]=i+1;\n }\n s.push(i);\n }\n\n long long ans=0;\n\n for(int i=0;i<n;i++){\n if((i>=k && i-left[i]+1<=k) || (i<=k && i+right[i]-1>=k)){\n ans=max(ans,(left[i]+right[i]-1)*nums[i]);\n }\n }\n\n return ans;\n }\n};",
"memory": "111000"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n struct subarray {\n size_t l;\n size_t r;\n };\n\n int maximumScore(vector<int>& nums, int k) {\n stack<size_t> st; // <num, idx>\n vector<subarray> mp(nums.size()); // idx -> {l, r}\n\n for (size_t i = 0; i < nums.size(); i++) {\n if (st.empty()) {\n mp[i] = {i, nums.size()-1};\n } else {\n while (!st.empty() && nums[i] < nums[st.top()]) {\n mp[st.top()].r = i-1;\n st.pop();\n }\n mp[i] = {(st.empty() ? -1 : st.top()) + 1, nums.size()-1};\n }\n st.push(i);\n }\n int res = INT_MIN;\n for (size_t i = 0; i < nums.size(); i++) {\n if (mp[i].l <= k && mp[i].r >= k) {\n int temp = nums[i] * (1 + mp[i].r - mp[i].l);\n res = max(res, temp);\n }\n }\n return res;\n }\n};",
"memory": "111100"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n=nums.size();\n\n stack<int>s;\n vector<long long>left(n,0),right(n,0);\n for(int i=n-1;i>=0;i--){\n while(s.size() && nums[s.top()]>=nums[i]){\n s.pop();\n }\n if(s.size()){\n right[i]=s.top()-i;\n }\n else{\n right[i]=n-i;\n }\n s.push(i);\n }\n\n while(s.size()){\n s.pop();\n }\n\n for(int i=0;i<n;i++){\n while(s.size() && nums[s.top()]>=nums[i]){\n s.pop();\n }\n if(s.size()){\n left[i]=i-s.top();\n }\n else{\n left[i]=i+1;\n }\n s.push(i);\n }\n\n long long ans=0;\n\n for(int i=0;i<n;i++){\n if(i-left[i]+1<=k && i+right[i]-1>=k){\n ans=max(ans,(left[i]+right[i]-1)*nums[i]);\n }\n }\n\n return ans;\n }\n};",
"memory": "111300"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n int ans = 0;\n\n vector<int> nsr(n), nsl(n);\n \n stack<int> st;\n for (int i = 0; i < n; ++i) {\n while (!st.empty() && nums[st.top()] >= nums[i]) {\n st.pop();\n }\n nsl[i] = st.empty() ? -1 : st.top();\n st.push(i);\n }\n \n stack<int> stk;\n for (int i = n - 1; i >= 0; --i) {\n while (!stk.empty() && nums[stk.top()] >= nums[i]) {\n stk.pop();\n }\n nsr[i] = stk.empty() ? n : stk.top();\n stk.push(i);\n }\n \n vector<long long> score(n, 0);\n score[0] = nums[0];\n for (int i = 1; i < n; ++i) {\n score[i] = score[i - 1] + nums[i];\n }\n \n for (int i = 0; i < n; ++i) {\n int min = nums[i];\n int l = nsl[i] + 1;\n int r = nsr[i] - 1;\n if (l <= k && r >= k) {\n int len = r - l + 1;\n int product = min * len;\n ans = max(ans, product);\n }\n }\n\n return ans;\n }\n};\n",
"memory": "111400"
} |
1,918 | <p>You are given an array of integers <code>nums</code> <strong>(0-indexed)</strong> and an integer <code>k</code>.</p>
<p>The <strong>score</strong> of a subarray <code>(i, j)</code> is defined as <code>min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)</code>. A <strong>good</strong> subarray is a subarray where <code>i <= k <= j</code>.</p>
<p>Return <em>the maximum possible <strong>score</strong> of a <strong>good</strong> subarray.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,3,7,4,5], k = 3
<strong>Output:</strong> 15
<strong>Explanation:</strong> The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,5,4,5,4,1,1,1], k = 0
<strong>Output:</strong> 20
<strong>Explanation:</strong> The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 2 * 10<sup>4</sup></code></li>
<li><code>0 <= k < nums.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \nvector<int>Nearest_Smallest_left(vector<int>v){\n stack<int>st;\n int n = v.size();\n vector<int>left_min(n);\n left_min[0] = -1;\n st.push(0);\n for(int i=1; i<n; i++){\n while(st.size() && v[st.top()] >= v[i]) st.pop();\n if(st.empty()) left_min[i] = -1;\n else left_min[i] = st.top();\n st.push(i);\n }\n return left_min;\n}\n\n\n \nvector<int>Nearest_Smallest_right(vector<int>v){\n stack<int>st;\n int n = v.size();\n vector<int>right_min(n);\n right_min[n-1] = n;\n st.push(n-1);\n for(int i=n-2; i>=0; i--){\n while(st.size() && v[st.top()] >= v[i]) st.pop();\n if(st.empty()) right_min[i] = n;\n else right_min[i] = st.top();\n st.push(i);\n }\n return right_min;\n}\nint maximumScore(vector<int>& nums, int k) {\n int n = nums.size();\n int ans = 0;\n vector<int>left_smallest = Nearest_Smallest_left(nums);\n vector<int>right_smallest = Nearest_Smallest_right(nums);\n \n for(int i = 0; i < n; i++){\n int left = left_smallest[i] , right = right_smallest[i];\n if(k > left && k <right){\n ans = max(ans, nums[i] * (right - left - 1)); // upto what num[i] is minimum \n }\n }\n return ans;\n }\n};",
"memory": "111500"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* deleteNode(TreeNode* root, int key) {\n stack<tuple<TreeNode*, bool, bool>> searchPath;\n TreeNode* searcher = root;\n if (searcher == nullptr) {\n return nullptr;\n }\n searchPath.push(make_tuple(searcher, false, false));\n bool keyFound = false;\n vector<TreeNode*> nodes;\n while (true) {\n if (searchPath.size() == 0) {\n break;\n }\n\n bool goneLeft = get<1>(searchPath.top());\n bool goneRight = get<2>(searchPath.top());\n\n if (goneLeft && goneRight) {\n if (!(searcher->val == key)) {\n nodes.push_back(searcher);\n }\n else {\n keyFound = true;\n }\n\n searchPath.pop();\n if (searchPath.size() > 0) {\n searcher = get<0>(searchPath.top());\n }\n }\n\n if (!goneLeft) {\n if (searcher->left == nullptr) {\n get<1>(searchPath.top()) = true;\n }\n else {\n searcher = searcher->left;\n get<1>(searchPath.top()) = true;\n searchPath.push(make_tuple(searcher, false, false));\n }\n continue;\n }\n if (!goneRight) {\n if (searcher->right == nullptr) {\n get<2>(searchPath.top()) = true;\n }\n else {\n searcher = searcher->right;\n get<2>(searchPath.top()) = true;\n searchPath.push(make_tuple(searcher, false, false));\n }\n continue;\n }\n }\n if (keyFound) {\n if (nodes.size() == 0) {\n return nullptr;\n }\n\n std::sort(nodes.begin(), nodes.end(), [](const auto& lhs, const auto& rhs){\n return lhs->val < rhs->val;\n });\n\n for (int i = 0; i < nodes.size(); i++) {\n nodes[i]->left = nullptr;\n nodes[i]->right = nullptr;\n }\n\n int nodesSize = nodes.size();\n int headIdx = (nodesSize / 2);\n\n root = nodes[headIdx];\n TreeNode* parent = root;\n for (int i = headIdx + 1; i < nodes.size(); i++) {\n parent->right = nodes[i];\n parent = nodes[i];\n }\n parent = root;\n for (int i = headIdx - 1; i >= 0; i--) {\n parent->left = nodes[i];\n parent = nodes[i];\n }\n }\n return root;\n }\n};",
"memory": "23300"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* deleteNode(TreeNode* root, int key) {\n vector<int> inOrder;\n populate(inOrder, root, key);\n return create(inOrder);\n }\n\n void populate(vector<int> &inOrder, TreeNode* root, int key) {\n if (root == NULL) {\n return;\n }\n\n populate(inOrder, root->left, key);\n if (root->val != key)\n inOrder.push_back(root->val);\n populate(inOrder, root->right, key);\n }\n\n TreeNode* create(vector<int> &inOrder){\n TreeNode *start, *curr;\n start = curr = NULL;\n for(int val: inOrder){\n TreeNode *node = new TreeNode(val);\n if(curr == NULL){\n curr = start = node;\n }\n else{\n curr->right = node;\n curr = node;\n }\n }\n return start;\n }\n};",
"memory": "25100"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<TreeNode*>nodes;\n void inorder(TreeNode*root, int key){\n if(!root)return;\n inorder(root->left, key);\n if((root->val)!=key){\n nodes.push_back(root);\n }\n inorder(root->right, key);\n }\n TreeNode* deleteNode(TreeNode* root, int key) {\n if(!root)return NULL;\n inorder(root, key);\n int n=nodes.size();\n if(n==0)return NULL;\n for(int i=0; i<n-1; i++){\n nodes[i]->right =nodes[i+1];\n nodes[i]->left=NULL;\n }\n nodes[n-1]->right=NULL;\n nodes[n-1]->left=NULL;\n return nodes[0];\n }\n};",
"memory": "25200"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> in,pre;\n map<int,int> mp;\n void maping(){\n for(int i=0;i<in.size();i++){\n mp[in[i]] = i;\n }\n }\n TreeNode* solve(int i,int s,int e){\n int n = in.size();\n if(i>=n || s>e) return NULL;\n\n int ele = pre[i];\n TreeNode* root = new TreeNode(ele);\n int pos = mp[ele];\n\n root->left = solve(i+1,0,pos-1);\n root->right = solve(i+1,pos+1,e);\n\n return root;\n \n }\n void findIn(TreeNode* root,int v){\n if(!root) return;\n\n if(root->val != v) in.push_back(root->val);\n\n findIn(root->left,v);\n findIn(root->right,v);\n }\n void findpre(TreeNode* root,int v){\n if(!root) return;\n\n findpre(root->left,v);\n if(root->val != v) pre.push_back(root->val);\n findpre(root->right,v);\n }\n TreeNode* deleteNode(TreeNode* root, int key) {\n findIn(root,key);\n findpre(root,key);\n int n = pre.size();\n\n return solve(0,0,n-1);\n }\n};",
"memory": "27900"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n TreeNode* constructBST(int left, int right, const vector<TreeNode*> &vec) {\n if (left > right) {\n return NULL;\n }\n\n int idx = (left + right) / 2 + 0.5;\n TreeNode* root = vec[idx];\n cout<<root->val<<endl;\n\n root->left = constructBST(left, idx - 1, vec);\n root->right = constructBST(idx + 1, right, vec);\n return root;\n }\n\n bool bfs(TreeNode* root, int key, vector<TreeNode*> & vec) {\n if (!root) return false;\n \n bool res = false;\n res |= bfs(root->left, key, vec);\n if (root->val != key) {\n vec.push_back(root);\n } else {\n res = true;\n }\n res |= bfs(root->right, key, vec);\n\n return res;\n } \n\n TreeNode* deleteNode(TreeNode* root, int key) {\n \n vector<TreeNode*> vec;\n bool succ = bfs(root, key, vec);\n\n if (!succ) return root; \n return constructBST(0, vec.size() -1, vec);\n }\n};",
"memory": "29300"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool searchForNode(TreeNode* root, int key) {\n if(!root)\n return false;\n \n if(root -> val == key)\n return true;\n \n bool found = false;\n \n if(key > root -> val)\n found = searchForNode(root -> right, key);\n else \n found = searchForNode(root -> left, key);\n\n return found;\n }\n\n void inorder(TreeNode* root, int key, vector<TreeNode*>& nodes) {\n if(!root)\n return ;\n\n inorder(root -> left, key, nodes);\n \n if(root -> val != key)\n nodes.push_back(root);\n\n inorder(root -> right, key, nodes);\n }\n\n TreeNode* buildBST(int left, int right, vector<TreeNode*>& nodes) {\n if(right < left)\n return nullptr;\n \n int mid = (left + right) / 2;\n\n nodes[mid] -> left = buildBST(left, mid - 1, nodes);\n nodes[mid] -> right = buildBST(mid + 1, right, nodes);\n\n return nodes[mid];\n }\n\n TreeNode* deleteNode(TreeNode* root, int key) {\n if(!root || !searchForNode(root, key)) {\n // cout << \"not found\";\n return root;\n }\n\n vector<TreeNode*> nodes;\n inorder(root, key, nodes);\n\n // for(auto &it : nodes)\n // cout << it -> val << ' ';\n\n return buildBST(0, nodes.size() - 1, nodes);\n }\n};",
"memory": "29400"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n TreeNode* constructBST(int left, int right, const vector<TreeNode*> &vec) {\n if (left > right) {\n return NULL;\n }\n\n int idx = (left + right) / 2 + 0.5;\n TreeNode* root = vec[idx];\n cout<<root->val<<endl;\n\n root->left = constructBST(left, idx - 1, vec);\n root->right = constructBST(idx + 1, right, vec);\n return root;\n }\n\n bool bfs(TreeNode* root, int key, vector<TreeNode*> & vec) {\n if (!root) return false;\n \n bool res = false;\n res |= bfs(root->left, key, vec);\n if (root->val != key) {\n vec.push_back(root);\n } else {\n res = true;\n }\n res |= bfs(root->right, key, vec);\n\n return res;\n } \n\n TreeNode* deleteNode(TreeNode* root, int key) {\n \n vector<TreeNode*> vec;\n bool succ = bfs(root, key, vec);\n\n if (!succ) return root; \n return constructBST(0, vec.size() -1, vec);\n }\n};",
"memory": "29500"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* sortedArrayToBST(vector<int>& nums){\n if (nums.empty())return NULL;\n \n int n = nums.size();\n int mid = n / 2;\n TreeNode* root = new TreeNode(nums[mid]);\n queue<pair<TreeNode*, pair<int, int> > > q;\n q.push({ root, { 0, mid - 1 } });\n q.push({ root, { mid + 1, n - 1 } });\n \n while (!q.empty()) {\n pair<TreeNode*, pair<int, int> > curr = q.front();\n q.pop();\n TreeNode* parent = curr.first;\n int left = curr.second.first;\n int right = curr.second.second;\n \n // if there are elements to process and parent node\n // is not NULL\n if (left <= right && parent != NULL) {\n int mid = (left + right) / 2;\n TreeNode* child = new TreeNode(nums[mid]);\n \n // set the child node as left or right child of\n // the parent node\n if (nums[mid] < parent->val) {\n parent->left = child;\n }\n else {\n parent->right = child;\n }\n \n // push the left and right child and their\n // indices to the queue\n q.push({ child, { left, mid - 1 } });\n q.push({ child, { mid + 1, right } });\n }\n }\n \n return root;\n }\n void helper(TreeNode* root, int key,vector<int>&v){\n if(root==NULL)return;\n helper(root->left,key,v);\n if(root->val!=key)v.push_back(root->val);\n helper(root->right,key,v);\n return;\n }\n TreeNode* deleteNode(TreeNode* root, int key) {\n vector<int>v;\n helper(root,key,v);\n TreeNode* ans=sortedArrayToBST(v);\n return ans;\n }\n};",
"memory": "31300"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n void inorder(TreeNode* root, vector<int>&tree, int key)\n {\n if(root == NULL) return ;\n \n inorder(root->left, tree, key);\n \n if(root->val != key)\n tree.push_back(root->val);\n \n inorder(root->right, tree, key);\n }\n \n TreeNode* maketree(vector<int>&tree, int start, int end)\n {\n if(start > end)\n return NULL;\n \n TreeNode *node;\n \n if(start == end)\n {\n node = new TreeNode(tree[start]);\n return node;\n }\n \n int mid = (start+end)/2;\n \n node = new TreeNode(tree[mid], maketree(tree,start,mid-1), maketree(tree,mid+1,end));\n return node;\n }\n \n TreeNode* deleteNode(TreeNode* root, int key) {\n \n vector<int>tree;\n inorder(root,tree,key);\n return maketree(tree,0,tree.size()-1);\n }\n};",
"memory": "31400"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "// /**\n// * Definition for a binary tree node.\n// * struct TreeNode {\n// * int val;\n// * TreeNode *left;\n// * TreeNode *right;\n// * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n// * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n// * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n// * };\n// */\n// class Solution {\n// public:\n// void solve(TreeNode*root,int key ){\n// if(root==nullptr){\n// return ;\n// }\n// if(root->val==key){\n// if(root->right){\n// root->val=root->right->val;\n// delete(root->right);\n// }\n// else{\n// root->val=root->left->val;\n// delete(root->left);\n// }\n// }\n// solve(root->left,key);\n// solve(root->right,key);\n// }\n// TreeNode* deleteNode(TreeNode* root, int key) {\n// solve(root,key);\n// return root;\n\n// }\n// };\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void solve(vector<int>&inorder,TreeNode*root,int key){\n if(root==nullptr)return;\n solve(inorder,root->left,key);\n if(root->val!=key){\n inorder.push_back(root->val);\n\n }\n solve(inorder,root->right,key);\n\n }\n\n TreeNode* makeBST(vector<int>&vec,int left,int right){\n if(left>right){\n return nullptr;\n }\n int mid=left+(right-left)/2;\n TreeNode*root=new TreeNode(vec[mid]);\n root->left=makeBST(vec,left,mid-1);\n root->right=makeBST(vec,mid+1,right);\n\n return root;\n }\n TreeNode* deleteNode(TreeNode* root, int key) {\n vector<int>inorder;\n solve(inorder,root,key);\n TreeNode*ans=makeBST(inorder,0,inorder.size()-1);\n return ans;\n }\n};",
"memory": "31500"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n vector<int> arr;\n void inorder(TreeNode* node){\n if(node == NULL) return;\n\n inorder(node->left);\n arr.push_back(node->val);\n inorder(node->right);\n }\n\n TreeNode* convert(int st, int en){\n if(st > en) return NULL;\n \n int mid = (st+en)/2;\n\n TreeNode* root = new TreeNode(arr[mid]);\n root->left = convert(st,mid-1);\n root->right = convert(mid+1,en);\n return root;\n }\n\n TreeNode* deleteNode(TreeNode* root, int val) {\n inorder(root);\n int n = arr.size();\n int found = false;\n for(int i = 0; i<n; i++){\n if(arr[i] == val){\n found = true;\n for(int j = i; j<n-1; j++){\n swap(arr[j], arr[j+1]);\n }\n break;\n }\n }\n if(found){\n arr.pop_back();\n return convert(0,arr.size()-1);\n }\n else return root;\n }\n};",
"memory": "31600"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> ans;\n\n void inOrderTraversal(TreeNode* root) {\n if(!root) return;\n inOrderTraversal(root->left);\n ans.push_back(root->val);\n inOrderTraversal(root->right);\n }\n\n int findElement(int num) {\n int left = 0, right = ans.size();\n while(left <= right) {\n int mid = left + ((right - left) / 2);\n if(ans[mid] == num) return mid;\n if(ans[mid] < num) left = mid + 1;\n else right = mid - 1;\n }\n return -1;\n }\n\n TreeNode* sortedArrayToBST(int start, int end) {\n if(start > end) return NULL;\n int mid = start + ((end - start) / 2);\n TreeNode* currRoot = new TreeNode(ans[mid]);\n currRoot->left = sortedArrayToBST(start, mid - 1);\n currRoot->right = sortedArrayToBST(mid + 1, end);\n return currRoot;\n }\n\n TreeNode* deleteNode(TreeNode* root, int key) {\n if(!root) return NULL;\n inOrderTraversal(root);\n int index = findElement(key);\n cout<<index<<\"\\n\";\n if(index != -1) ans.erase(ans.begin() + index);\n // cout<<ans<<\"\\n\";\n return sortedArrayToBST(0, ans.size() - 1);\n }\n};",
"memory": "31700"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvoid dfs(TreeNode* root,vector<int>& nodes,int key){\n if(root==nullptr) return;\n if(root->val!=key) nodes.push_back(root->val);\n dfs(root->left,nodes,key);\n dfs(root->right,nodes,key);\n}\nTreeNode* create(vector<int>& nodes,int s,int e){\n int mid=(s+e)/2;\n TreeNode* ans=new TreeNode(nodes[mid]);\n if(s<mid){\n ans->left=create(nodes,s,mid-1);\n }\n else{\n ans->left=nullptr;\n }\n if(e>mid){\n ans->right=create(nodes,mid+1,e);\n }\n else {\n ans->right=nullptr;\n }\n return ans;\n}\n TreeNode* deleteNode(TreeNode* root, int key) {\n vector<int>nodes;\n dfs(root,nodes,key);\n if(nodes.size()==0) return nullptr;\n sort(nodes.begin(),nodes.end());\n return create(nodes,0,nodes.size()-1);\n }\n};",
"memory": "31700"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* deleteNode(TreeNode* root, int key) {\n // Step 1: Traverse the BST and store nodes in a vector\n vector<int> nodes;\n inorderTraversal(root, nodes);\n\n // Step 2: Remove the node with the given key from the vector\n removeNode(nodes, key);\n\n // Step 3: Rebuild the BST from the modified vector\n return buildBSTFromArray(nodes);\n }\n\nprivate:\n // In-order traversal to store nodes in a vector\n void inorderTraversal(TreeNode* root, vector<int>& nodes) {\n if (root == nullptr) return;\n inorderTraversal(root->left, nodes);\n nodes.push_back(root->val);\n inorderTraversal(root->right, nodes);\n }\n\n // Remove a node from the vector\n void removeNode(vector<int>& nodes, int key) {\n auto it = find(nodes.begin(), nodes.end(), key);\n if (it != nodes.end()) {\n nodes.erase(it);\n }\n }\n\n // Build a balanced BST from a sorted array\n TreeNode* sortedArrayToBST(const vector<int>& nodes, int start, int end) {\n if (start > end) return nullptr;\n int mid = start + (end - start) / 2;\n TreeNode* root = new TreeNode(nodes[mid]);\n root->left = sortedArrayToBST(nodes, start, mid - 1);\n root->right = sortedArrayToBST(nodes, mid + 1, end);\n return root;\n }\n\n // Helper function to initiate the BST construction\n TreeNode* buildBSTFromArray(const vector<int>& nodes) {\n return sortedArrayToBST(nodes, 0, nodes.size() - 1);\n }\n};\n\n",
"memory": "31800"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* helper(vector<int>& arr, int lo ,int hi){\n if(lo>hi) return nullptr;\n int mid=lo+(hi-lo)/2;\n TreeNode* root=new TreeNode(arr[mid]);\n root->left=helper(arr, lo, mid-1);\n root->right=helper(arr, mid+1, hi);\n return root;\n }\n void inorder(TreeNode* root, vector<int> &ans){\n if(root==nullptr) return;\n inorder(root->left, ans);\n ans.push_back(root->val);\n inorder(root->right, ans);\n }\n TreeNode* deleteNode(TreeNode* root, int key) {\n if(root==NULL) return root;\n vector<int> ans;\n inorder(root, ans);\n vector<int> temp;\n for(int i=0;i<ans.size();i++){\n if(key==ans[i]) continue;\n else{\n temp.push_back(ans[i]);\n }\n }\n if(temp.size()!=ans.size()){\n return helper(temp, 0, temp.size()-1);\n }\n return root;\n }\n};",
"memory": "31900"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n Solution(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n }\n vector<int> ans;\n void inOrder(TreeNode* root){\n if(!root)return;\n\n if(root->left)inOrder(root->left);\n ans.push_back(root->val);\n if(root->right)inOrder(root->right);\n }\n int bst(vector<int>& arr,int key){\n int lo=0,hi=(int)arr.size()-1;\n while(lo<=hi){\n int mid = lo + (hi-lo)/2;\n if(arr[mid]==key)return mid;\n else if(arr[mid]<key)lo=mid+1;\n else hi=mid-1;\n }\n return -1;\n }\n TreeNode* createBst(int l,int r,vector<int> &ans){\n if(l>r)return NULL;\n int mid = l+(r-l)/2;\n auto newRoot = new TreeNode(ans[mid]);\n newRoot->left = createBst(l,mid-1,ans);\n newRoot->right = createBst(mid+1,r,ans);\n return newRoot;\n }\n TreeNode* deleteNode(TreeNode* root, int key) {\n if(!root)return root;\n inOrder(root);\n int index=bst(ans,key);\n if(index==-1)return root;\n ans.erase(ans.begin()+index);\n int n=ans.size();\n return createBst(0,n-1,ans);\n }\n};",
"memory": "32000"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvector<int>arr;\nvoid solve(TreeNode*r){\n if(r==NULL) return;\n solve(r->left);\n arr.push_back(r->val);\n solve(r->right);\n}\nTreeNode*make(int s,int e,vector<int>&ekor){\n if(s>e) return NULL;\n int mid=(s+e)>>1;\n TreeNode* nn=new TreeNode(ekor[mid]);\n if(s==e) return nn;\n nn->left=make(s,mid-1,ekor);\n nn->right=make(mid+1,e,ekor);\n return nn;\n}\n TreeNode* deleteNode(TreeNode* root, int key) {\n solve(root);\n vector<int>ekor;\n for(auto &i:arr){\n if(i==key) continue;\n ekor.push_back(i);\n }\n for(auto &i:ekor) cout<<i<<\" \";\n return make(0,ekor.size()-1,ekor);\n\n }\n};",
"memory": "32100"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root , vector<int>&arr)\n {\n if(root == NULL) return;\n inorder(root->left , arr);\n arr.push_back(root->val);\n inorder(root->right, arr);\n }\n TreeNode*BSTfromIno(vector<int> &arr , int s , int e)\n {\n if(s>e) return NULL;\n int mid = s + (e-s)/2;\n TreeNode* root = new TreeNode(arr[mid]);\n root->left = BSTfromIno(arr , s , mid-1);\n root->right = BSTfromIno(arr , mid+1 , e);\n return root;\n }\n vector<int> solve(vector<int> &arr , int key)\n {\n vector<int> temp;\n for(int i = 0 ; i < arr.size() ; i++)\n {\n if(arr[i] != key)\n temp.push_back(arr[i]);\n }\n return temp;\n }\n TreeNode* deleteNode(TreeNode* root, int key) {\n vector<int> nums , arr;\n inorder(root , nums);\n arr = solve(nums , key);\n return BSTfromIno(arr , 0 , arr.size()-1);\n }\n};",
"memory": "32200"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root, vector<int>&val)\n {\n if(!root) return;\n\n inorder(root->left,val);\n val.push_back(root->val);\n inorder(root->right,val);\n }\n\n TreeNode* toBST(TreeNode* node,int start, int end, vector<int>&val)\n {\n if(start>end) return NULL;\n\n int mid=(start+end)/2;\n node=new TreeNode(val[mid]);\n\n node->left=toBST(node,start,mid-1,val);\n node->right=toBST(node,mid+1,end,val);\n\n return node;\n }\n\n TreeNode* deleteNode(TreeNode* root, int key) {\n \nios::sync_with_stdio(false); cin.tie(nullptr); if(root==NULL || (!root->left and !root->right && root->val!=key)) return root;\n if(root->val==key && !root->left and !root->right) return NULL;\n\n vector<int>v;\n inorder(root,v);\n\n vector<int>res;\n\n for(auto i:v)if(i!=key)res.push_back(i);\n \n TreeNode *node=NULL;\n return toBST(node,0,res.size()-1,res);\n return NULL;\n }\n};",
"memory": "32400"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int key) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* getSuccessor(TreeNode* curr){\n curr=curr->right;\n while(curr!=NULL && curr->left!=NULL)curr=curr->left;\n return curr;\n }\n TreeNode* deleteNode(TreeNode* root, int key) {\n if(root==NULL)return root;\n if(root->val<key){\n root->right=deleteNode(root->right,key);\n }else if(root->val>key){\n root->left=deleteNode(root->left,key);\n }else{\n if(root->left==NULL){\n TreeNode* temp = root->right;\n delete(root);\n return temp;\n }else if(root->right==NULL){\n TreeNode* temp=root->left;\n delete root;\n return temp;\n }else{\n TreeNode* succ=getSuccessor(root);\n root->val=succ->val;\n root->right=deleteNode(root->right,succ->val);\n }\n }\n return root;\n }\n};",
"memory": "33100"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* deleteNode(TreeNode* root, int key) {\n if (!root)\n return NULL;\n if (root->val < key)\n root->right = deleteNode(root->right, key);\n else if (root->val > key)\n root->left = deleteNode(root->left, key);\n else {\n if (!root->right && !root->left)\n return NULL;\n else if (!root->right)\n return root->left;\n else if (!root->left)\n return root->right;\n else {\n TreeNode* temp = root->right;\n while (temp->left)\n temp = temp->left;\n temp->left = root->left;\n return root->right;\n }\n }\n return root;\n }\n};",
"memory": "33100"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int findmax(TreeNode* root) {\n if(!root->right) return root->val;\n return findmax(root->right);\n }\n TreeNode* deleteNode(TreeNode* root, int key) {\n if(!root) return NULL;\n if(key < root->val) {\n root->left = deleteNode(root->left, key);\n } \n else if(key > root->val) {\n root->right = deleteNode(root->right, key); \n }\n else {\n \n if(!root->left) {\n TreeNode* tmp = root->right;\n delete root;\n return tmp;\n }\n else if(!root->right) {\n TreeNode* tmp = root->left;\n delete root;\n return tmp;\n }\n else {\n int mx = findmax(root->left);\n root->val = mx;\n root->left = deleteNode(root->left, mx); \n }\n \n }\n\n return root; \n }\n};",
"memory": "33200"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* minValueNode(TreeNode* node) {\n TreeNode* current = node;\n while (current && current->left != nullptr) {\n current = current->left;\n }\n return current;\n }\n TreeNode* deleteNode(TreeNode* root, int key) {\n if (!root) return NULL;\n if (key < root->val) {\n root->left = deleteNode(root->left, key);\n } else if (key > root->val) {\n root->right = deleteNode(root->right, key);\n } else {\n if (!root->left) {\n TreeNode* temp = root->right;\n delete root;\n return temp;\n } else if (!root->right) {\n TreeNode* temp = root->left;\n delete root;\n return temp;\n }\n TreeNode* temp = minValueNode(root->right);\n root->val = temp->val;\n root->right = deleteNode(root->right, temp->val);\n }\n return root;\n }\n};",
"memory": "33300"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* minVal(TreeNode* root){\n if(root==NULL){\n return NULL;\n }\n\n TreeNode* tmp = root;\n while(tmp->left!=NULL){\n tmp = tmp->left;\n }\n return tmp;\n }\n\n TreeNode* deleteNode(TreeNode* root, int key) {\n //base case\n if(root==NULL){\n return root;\n } \n\n //algo starts here\n if(root->val==key){\n // 0 Child case\n if(root->left==NULL and root->right==NULL){\n return NULL;\n }\n\n // 1 Child case\n //if it has only leftchild\n if(root->left!=NULL and root->right==NULL){\n TreeNode* temp = root->left;\n delete root;\n return temp;\n }\n\n //if it has only rightchild\n if(root->left==NULL and root->right!=NULL){\n TreeNode* temp = root->right;\n delete root;\n return temp;\n }\n\n // 2 Child case\n if(root->left!=NULL and root->right!=NULL){\n int mini = minVal(root->right)->val;\n root->val = mini;\n\n root->right = deleteNode(root->right, mini);\n return root;\n }\n }\n else if(root->val>key){\n //search in LST\n root->left = deleteNode(root->left, key);\n return root;\n }\n else{\n //search in RST\n root->right = deleteNode(root->right, key);\n return root;\n }\n\n return root;\n }\n};",
"memory": "33300"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n // TreeNode* Succ(TreeNode* node){\n // TreeNode* temp = node->right;\n // while(temp->left != nullptr){\n // temp = temp->left;\n // }\n // return temp;\n\n // }\n // TreeNode* deleteNode(TreeNode* root, int key) {\n // TreeNode* temp = root;\n // TreeNode* node = nullptr;\n // TreeNode* parent = nullptr;\n // while(temp != nullptr){\n // if(temp->val == key) node=temp;\n // else if(temp->val > key) {parent = temp; temp = temp->left;}\n // else {parent = temp; temp = temp->right;}\n // }\n\n // if(node == nullptr) return root;\n // if(node->right != nullptr && node->left != nullptr){\n // TreeNode* successor = Succ(node);\n // node->val = successor->val;\n // successor->val = key;\n // TreeNode* temp = node;\n // node = successor;\n // successor = temp;\n // }\n \n // if(node->left == nullptr){\n // if(parent == nullptr) root = node->right;\n // else if(parent->left == node) parent->left = node->right;\n // else parent->right = node->right;\n // }\n // else if(node->right == nullptr){\n // if(parent == nullptr) root= node->left;\n // else if(parent->left == node) parent->left = node->left;\n // else parent->right = node->left;\n // }\n // return root;\n\n \n // }\n TreeNode* deleteNode(TreeNode* root, int key) {\n if(root)\n if(key< root->val) root->left = deleteNode(root->left, key);\n else if(key> root->val) root->right = deleteNode(root->right, key);\n else {\n if(!root->left && !root->right) return NULL; //no children\n if(!root->left || !root->right) return root->left ? root->left : root->right;\n\n TreeNode* temp = root->right;\n while(temp->left != nullptr){\n temp = temp->left;\n }\n root->val = temp->val;\n root->right = deleteNode(root->right, temp->val);\n\n }\n return root;\n }\n};",
"memory": "33400"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* minValNode(TreeNode* root){\n TreeNode* temp = root;\n while(temp->left) temp = temp->left;\n return temp;\n }\n TreeNode* deleteNode(TreeNode* root, int key) {\n if(root == NULL) return root;\n if(root->val > key){\n root->left = deleteNode(root->left,key);\n }else if(root->val < key){\n root->right = deleteNode(root->right,key);\n }else if(root->val == key){\n if(root->left == NULL && root->right == NULL){\n return NULL;\n }else if(root->right == NULL){\n return root->left;\n }else if(root->left == NULL){\n return root->right;\n }else{\n TreeNode* minVal = minValNode(root->right);\n root->val = minVal->val;\n root->right = deleteNode(root->right,minVal->val);\n }\n }\n return root;\n }\n};",
"memory": "33400"
} |
450 | <p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return <em>the <strong>root node reference</strong> (possibly updated) of the BST</em>.</p>
<p>Basically, the deletion can be divided into two stages:</p>
<ol>
<li>Search for a node to remove.</li>
<li>If the node is found, delete the node.</li>
</ol>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
<strong>Output:</strong> [5,4,6,2,null,null,7]
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
<strong>Output:</strong> [5,3,6,2,4,null,7]
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [], key = 0
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li><code>root</code> is a valid binary search tree.</li>
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* f(TreeNode* root,int key){\n if(root==NULL)return NULL;\n if(root->val==key)return root;\n if(root->val>key)return f(root->left,key);\n return f(root->right,key);\n }\n void delnode(TreeNode* root,TreeNode* node){\n if(root==NULL)return;\n if(root->left==node){\n if(node->left==NULL&&node->right==NULL){\n root->left=NULL;\n }\n else if(node->left==NULL)root->left=node->right;\n else root->left=node->left;\n return;\n }\n if(root->right==node){\n if(node->left==NULL&&node->right==NULL)root->right=NULL;\n else if(node->left==NULL)root->right=node->right;\n else root->right=node->left;\n return;\n }\n if(root->val>node->val)delnode(root->left,node);\n else delnode(root->right,node);\n }\n TreeNode* deleteNode(TreeNode* root, int key) {\n if(root==NULL)return root;\n TreeNode* node=f(root,key);\n if(node==NULL)return root;\n if((node->left==NULL)||(node->right==NULL)){\n if(node==root){\n if(node->left==NULL)return node->right;\n else return node->left;\n }\n delnode(root,node);\n return root;\n }\n TreeNode* temp=node->right;\n TreeNode* parent=node;\n while((temp->left!=NULL)){\n parent=temp;\n temp=temp->left;\n }\n swap(node->val,temp->val);\n if(parent==node)parent->right=temp->right;// whien parent->right==node or node->right==temp or parent hi node ho ->agar jis node ko delete krna hai uska right hi inorder successor ho or in simple terms jb node->right ka koi bhi left child ho hi na in this case parent->right=temp->right or node->right=temp->right or node->right=node->right->right kucch bhi bol skte\n else parent->left=temp->right;\n delete temp;\n return root;\n }\n};",
"memory": "33500"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minMoves(vector<int>& nums) {\n int n = nums.size();\n int count = 0;\n int min = nums[0];\n\n for (int i = 0; i < n; i++) {\n if (nums[i] < min) {\n min = nums[i];\n }\n }\n for (int i = 0; i < n; i++) {\n nums[i] = nums[i] - min;\n }\n for (int i = 0; i < n; i++) {\n count = count + nums[i];\n }\n return count;\n }\n};",
"memory": "30600"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minMoves(vector<int>& nums) {\n if (nums.empty()) return 0;\n \n // Find the minimum element\n int minElement = *min_element(nums.begin(), nums.end());\n \n // Calculate the sum of differences from min element\n int moves = 0;\n for (int num : nums) {\n moves += num - minElement;\n }\n \n return moves;\n }\n};",
"memory": "30700"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "\tclass Solution{\n\tpublic://Remember--whenever someone trash talks your country, state, city, home, community, etc--their feet are usually telling you that they're a liar.\n\t\tint minMoves(vector<int>& nums){\n\t\t\tint m=INT_MAX;\n\t\t\tfor(int n:nums) m=min(m,n);\n\t\t\tint ans=0;\n\t\t\tfor(int n:nums) ans+=n-m;\n\t\t\treturn ans;}};",
"memory": "30800"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minMoves(vector<int>& nums) {\n int mini = INT_MAX;\n for (const auto& num : nums) mini = min(mini, num);\n int moves = 0;\n for (const auto& num : nums) moves += (num - mini);\n return moves;\n }\n};",
"memory": "30900"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int minMoves(vector<int>& nums) {\n int val = *std::min_element(nums.cbegin(), nums.cend());\n int ans = 0;\n for(const auto i : nums) {\n ans += i - val;\n }\n return ans;\n }\n};",
"memory": "30900"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "\tclass Solution{\n\tpublic://Remember--whenever someone trash talks your country, state, city, home, community, etc--their feet are usually telling you that they're a liar.\n\t\tint minMoves(vector<int>& nums){\n\t\t\tint m=INT_MAX;\n\t\t\tfor(int n:nums) m=min(m,n);\n\t\t\tint ans=0;\n\t\t\tfor(int n:nums) ans+=n-m;\n\t\t\treturn ans;}};",
"memory": "31000"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 0 | {
"code": "\tclass Solution{\n\tpublic://Remember--whenever someone trash talks your country, state, city, home, community, etc--their feet are usually telling you that they're a liar.\n\t\tint minMoves(vector<int>& nums){\n\t\t\tint m=INT_MAX;\n\t\t\tfor(int n:nums) m=min(m,n);\n\t\t\tint ans=0;\n\t\t\tfor(int n:nums) ans+=n-m;\n\t\t\treturn ans;}};",
"memory": "31000"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 2 | {
"code": "class Solution\n{\npublic:\n int minMoves(vector<int> &nums)\n {\n int sum = accumulate(nums.begin(), nums.end(), 0);\n int minElement = *min_element(nums.begin(), nums.end());\n return sum - minElement * nums.size();\n }\n};",
"memory": "31100"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minMoves(vector<int>& nums) {\n int sum = 0;\n int mini = INT_MAX;\n for(auto i:nums){\n sum += i;\n mini = min(mini,i);\n }\n return sum - (mini * nums.size());\n }\n};",
"memory": "31100"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minMoves(vector<int>& nums) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL), cout.tie(NULL);\n\n int mini = *min_element(nums.begin(), nums.end());\n int ans = 0;\n for(int num: nums){\n ans += (num-mini);\n }\n\n return ans;\n }\n};",
"memory": "31200"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minMoves(vector<int>& nums) {\n int ans = 0;\n int prev = 0;\n stable_sort(nums.begin(),nums.end());\n for(int i=1;i<nums.size();i++){\n int temp = ans;\n ans += prev + nums[i]-nums[i-1];\n prev = ans - temp;\n }\n return ans;\n }\n // 1,2,3,4,5 0\n // 2,2,4,5,6 1\n // 3,3,4,6,7 \n // 4,4,4,7,8 3\n // 5,5,5,7,9\n // 6,6,6,7,10\n // 7,7,7,7,11 6\n};",
"memory": "31600"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minMoves(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n if(nums.size()==1) return 0;\n if(nums.size()==2) return abs(nums[1]-nums[0]);\n int n = nums.size();\n int ans = nums[n-1] - nums[n-2];\n int k = 1;\n for(int i = n-2 ; i>0 ; i--){\n ans += ((k+1)*(nums[i] - nums[i-1]));\n k++;\n }\n return ans;\n\n \n }\n};",
"memory": "31900"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int minMoves(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int maxi=nums[nums.size()-1];\n if(nums[0]==maxi){\n return 0;\n }\n int cnt=0;\n for(int i=1;i<nums.size();i++){\n cout<<nums[i]<<endl;\n cout<<nums[0]<<endl;\n cnt+=nums[i]-nums[0];\n }\n return cnt;\n }\n};",
"memory": "32000"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minMoves(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int maxi=nums[nums.size()-1];\n if(nums[0]==maxi){\n return 0;\n }\n int cnt=0;\n for(int i=1;i<nums.size();i++){\n cout<<nums[i]<<endl;\n cout<<nums[0]<<endl;\n cnt+=nums[i]-nums[0];\n }\n return cnt;\n }\n};",
"memory": "32100"
} |
453 | <p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minMoves(vector<int>& nums) {\n std::sort(nums.begin(), nums.end());\n int ans = 0;\n for(std::size_t i = nums.size() - 1 ; i > 0 ; --i) {\n ans += nums[i] - nums[0];\n }\n return ans;\n }\n};",
"memory": "32100"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> mp;\n int ans = 0, canVec = 0, offset = 2e5, m1 = INT_MAX, m2 = INT_MAX, n1 = INT_MIN, n2 = INT_MIN;\n for(int a: nums1) {m1 = min(m1, a); n1 = max(n1, a);}\n for(int a: nums2) {m2 = min(m2, a); n2 = max(n2, a);}\n int left = m1 + m2, right = n1 + n2;\n vector<int> v(right - left + 1, 0);\n for(int a: nums1) for(int b: nums2) v[a + b - left]++;\n for(int a: nums3) for(int b: nums4) if (-a - b >= left && -a - b <= right) ans += v[-a - b - left];\n return ans;\n }\n};",
"memory": "24200"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> mp;\n int ans = 0, canVec = 0, offset = 2e5, m1 = INT_MAX, m2 = INT_MAX, n1 = INT_MIN, n2 = INT_MIN;\n for(int a: nums1) {m1 = min(m1, a); n1 = max(n1, a);}\n for(int a: nums2) {m2 = min(m2, a); n2 = max(n2, a);}\n int left = m1 + m2, right = n1 + n2;\n vector<int> v(right - left + 1, 0);\n for(int a: nums1) for(int b: nums2) v[a + b - left]++;\n for(int a: nums3) for(int b: nums4) if (-a - b >= left && -a - b <= right) ans += v[-a - b - left];\n return ans;\n }\n};",
"memory": "24200"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> mp;\n int ans = 0, canVec = 0, offset = 2e5, m1 = INT_MAX, m2 = INT_MAX, n1 = INT_MIN, n2 = INT_MIN;\n for(int a: nums1) {m1 = min(m1, a); n1 = max(n1, a);}\n for(int a: nums2) {m2 = min(m2, a); n2 = max(n2, a);}\n int left = m1 + m2, right = n1 + n2;\n vector<int> v(right - left + 1, 0);\n for(int a: nums1) for(int b: nums2) v[a + b - left]++;\n for(int a: nums3) for(int b: nums4) if (-a - b >= left && -a - b <= right) ans += v[-a - b - left];\n return ans;\n }\n};",
"memory": "24300"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> mp;\n int ans = 0, canVec = 0, offset = 2e5, m1 = INT_MAX, m2 = INT_MAX, n1 = INT_MIN, n2 = INT_MIN;\n for(int a: nums1) {m1 = min(m1, a); n1 = max(n1, a);}\n for(int a: nums2) {m2 = min(m2, a); n2 = max(n2, a);}\n int left = m1 + m2, right = n1 + n2;\n vector<int> v(right - left + 1, 0);\n for(int a: nums1) for(int b: nums2) v[a + b - left]++;\n for(int a: nums3) for(int b: nums4) if (-a - b >= left && -a - b <= right) ans += v[-a - b - left];\n return ans;\n }\n};",
"memory": "24300"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> mp;\n int ans = 0, canVec = 0, offset = 2e5, m1 = INT_MAX, m2 = INT_MAX, n1 = INT_MIN, n2 = INT_MIN;\n for(int a: nums1) {m1 = min(m1, a); n1 = max(n1, a);}\n for(int a: nums2) {m2 = min(m2, a); n2 = max(n2, a);}\n int left = m1 + m2, right = n1 + n2;\n vector<int> v(right - left + 1, 0);\n for(int a: nums1) for(int b: nums2) v[a + b - left]++;\n for(int a: nums3) for(int b: nums4) if (-a - b >= left && -a - b <= right) ans += v[-a - b - left];\n return ans;\n }\n};",
"memory": "24400"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> mp;\n int ans = 0, canVec = 0, offset = 2e5, m1 = INT_MAX, m2 = INT_MAX, n1 = INT_MIN, n2 = INT_MIN;\n for(int a: nums1) {m1 = min(m1, a); n1 = max(n1, a);}\n for(int a: nums2) {m2 = min(m2, a); n2 = max(n2, a);}\n int left = m1 + m2, right = n1 + n2;\n vector<int> v(right - left + 1, 0);\n for(int a: nums1) for(int b: nums2) v[a + b - left]++;\n for(int a: nums3) for(int b: nums4) if (-a - b >= left && -a - b <= right) ans += v[-a - b - left];\n return ans;\n }\n};",
"memory": "24400"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)\n\nint minE(const vector<int> &v){\n return *min_element(v.begin(),v.end());\n}\nint maxE(const vector<int> &v){\n return *max_element(v.begin(),v.end());\n}\n\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3,\n vector<int>& nums4) {\n fast;\n int n=nums1.size();\n // unordered_map<int, int> hash;\n const int left=min(minE(nums1)+minE(nums2),-maxE(nums3)-maxE(nums4));\n const int right=max(maxE(nums1)+maxE(nums2),-minE(nums3)-minE(nums4));\n vector<unsigned int> hash(right-left+1,0);\n for (auto& i : nums1) {\n for (auto& j : nums2) {\n hash[i + j-left]++;\n }\n }\n\n int ans = 0;\n for (auto& i : nums3) {\n for (auto& j : nums4) {\n ans += hash[-(i + j)-left];\n }\n }\n return ans;\n }\n};",
"memory": "24500"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> mp;\n int ans = 0, canVec = 0, offset = 2e5, m1 = INT_MAX, m2 = INT_MAX, n1 = INT_MIN, n2 = INT_MIN;\n for(int a: nums1) {m1 = min(m1, a); n1 = max(n1, a);}\n for(int a: nums2) {m2 = min(m2, a); n2 = max(n2, a);}\n int left = m1 + m2, right = n1 + n2;\n vector<int> v(right - left + 1, 0);\n for(int a: nums1) for(int b: nums2) v[a + b - left]++;\n for(int a: nums3) for(int b: nums4) if (-a - b >= left && -a - b <= right) ans += v[-a - b - left];\n return ans;\n }\n};",
"memory": "24600"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n void sort(vector<int> &nums){\n std::sort(nums.begin(), nums.end());\n }\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n int n = nums1.size();\n sort(nums1);\n sort(nums2);\n sort(nums3);\n sort(nums4);\n int ans = 0;\n for(int i = 0; i < n; i++){\n int a = 1;\n for(int x = i+1; x < n && nums1[x] == nums1[i]; i++, x++) a++;\n for(int j = 0; j < n; j++){\n int b = 1;\n for(int x = j+1; x < n && nums2[x] == nums2[j]; j++, x++) b++;\n int k = 0, l = n-1;\n while(k < n && l >= 0){\n int sum = nums1[i] + nums2[j] + nums3[k] + nums4[l];\n if(sum > 0) l--;\n else if(sum < 0) k++;\n else{\n int c = 1;\n int d = 1;\n for(int x = k+1; x < n && nums3[x] == nums3[k]; k++, x++) c++;\n for(int x = l-1; x >= 0 && nums4[x] == nums4[l]; l--, x--) d++;\n ans += a*b*c*d;\n k++, l--;\n }\n }\n \n }\n }\n return ans;\n }\n};",
"memory": "24700"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n \n // Sort all input arrays\n sort(nums1.begin(), nums1.end());\n sort(nums2.begin(), nums2.end());\n sort(nums3.begin(), nums3.end());\n sort(nums4.begin(), nums4.end());\n\n const int n = nums1.size();\n\n // Calculate possible range for the sums\n int lowerBound = min(nums1[0] + nums2[0], -nums3[n - 1] - nums4[n - 1]);\n int upperBound = max(nums1[n - 1] + nums2[n - 1], -nums3[0] - nums4[0]);\n\n // Create a vector to store the count of each sum\n vector<int> sumCounts(upperBound - lowerBound + 1, 0);\n\n int result = 0;\n\n // Calculate and count sums for nums1 and nums2\n for (auto num1 : nums1) {\n for (auto num2 : nums2) {\n int sum = num1 + num2;\n sumCounts[sum - lowerBound]++;\n }\n }\n\n // Calculate sums for nums3 and nums4, check for complements\n for (auto num3 : nums3) {\n for (auto num4 : nums4) {\n int complement = -(num3 + num4);\n if (complement - lowerBound >= 0 && complement - lowerBound < sumCounts.size()) {\n result += sumCounts[complement - lowerBound];\n }\n }\n }\n\n return result;\n }\n};",
"memory": "24800"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> mp;\n vector<int> v;\n int ans = 0, canVec = 0, offset = 1e5, m = 0;\n for(int a: nums1) m = max(abs(a), m);\n for(int a: nums2) m = max(abs(a), m);\n if (offset >= 2 * m) {\n canVec = 1;\n offset = 2 * m;\n v = vector(2 * offset + 1, 0);\n }\n for(int a: nums1) \n for(int b: nums2) {\n if (canVec) v[a + b + offset]++;\n else mp[a + b]++;\n }\n for(int a: nums3) for(int b: nums4){\n if (canVec && abs(a + b) <= offset) ans += v[ -a - b + offset];\n else ans += mp[- a - b];\n }\n return ans;\n }\n};",
"memory": "26000"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> mp;\n vector<int> v;\n int ans = 0, canVec = 0, offset = 1e5, m = 0;\n for(int a: nums1) m = max(abs(a), m);\n for(int a: nums2) m = max(abs(a), m);\n if (offset >= 2 * m) {\n canVec = 1;\n offset = 2 * m;\n v = vector(2 * offset + 1, 0);\n }\n for(int a: nums1) \n for(int b: nums2) {\n if (canVec) v[a + b + offset]++;\n else mp[a + b]++;\n }\n for(int a: nums3) for(int b: nums4){\n if (canVec && abs(a + b) <= offset) ans += v[ -a - b + offset];\n else ans += mp[- a - b];\n }\n return ans;\n }\n};",
"memory": "26000"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n unordered_map<int, int> mp;\n vector<int> v;\n int ans = 0, canVec = 0, offset = 2e5, m = 0;\n for(int a: nums1) m = max(abs(a), m);\n for(int a: nums2) m = max(abs(a), m);\n if (offset >= 2 * m) {\n canVec = 1;\n offset = 2 * m;\n v = vector(2 * offset + 1, 0);\n }\n for(int a: nums1) \n for(int b: nums2) {\n if (canVec) v[a + b + offset]++;\n else mp[a + b]++;\n }\n for(int a: nums3) for(int b: nums4){\n if (canVec && abs(a + b) <= offset) ans += v[ -a - b + offset];\n else ans += mp[- a - b];\n }\n return ans;\n }\n};",
"memory": "26100"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n int N = nums1.size(), count = 0;\n // create a map to count the freq of 2sum\n unordered_map<int, int> twoSum;\n for(int i=0; i<N; ++i) {\n for(int j=0; j<N; ++j)\n ++twoSum[nums1[i]+nums2[j]];\n }\n \n for(int i=0; i<N; ++i) {\n for(int j=0; j<N; ++j) {\n if(twoSum.count(-nums3[i]-nums4[j]))\n count += twoSum[-nums3[i]-nums4[j]];\n }\n }\n return count; \n }\n};",
"memory": "28100"
} |
454 | <p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
<ul>
<li><code>0 <= i, j, k, l < n</code></li>
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length</code></li>
<li><code>n == nums2.length</code></li>
<li><code>n == nums3.length</code></li>
<li><code>n == nums4.length</code></li>
<li><code>1 <= n <= 200</code></li>
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {\n std::unordered_map<int, int> map12;\n for (int x1 : nums1) {\n for (int x2 : nums2) {\n map12[x1+x2]++;\n }\n }\n\n int count = 0;\n \n for (int x3 : nums3) {\n for (int x4 : nums4) {\n std::unordered_map<int, int>::iterator match34 = map12.find(-x3 - x4);\n \n if (match34 != map12.end()) {\n count += match34->second;\n }\n }\n }\n\n return count;\n }\n};",
"memory": "28200"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.