id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n\n // long long ans=0;\n \n stack<int> st;\n int n=nums.size();\n\n vector<int> nextg(n);\n\n for(int i=n-1;i>=0;i--)\n {\n while(!st.empty() and nums[st.top()]<nums[i])\n {\n st.pop();\n }\n\n nextg[i]=st.empty() ? -1: st.top();\n st.push(i);\n }\n\n for(auto i : nextg)\n {\n cout<<i<<\" \";\n }\n\n cout<<endl;\n\n vector<int> ans(n,0);\n for(int i=n-1;i>=0;i--)\n {\n int ind=nextg[i];\n if(ind==-1 || nums[i]!=nums[ind]) continue;\n\n ans[i]+=ans[ind]+1;\n\n }\n\n long long ans2=0;\n for(auto i : ans) ans2+=i;\n\n return ans2+n;\n\n\n\n \n \n }\n};", "memory": "117537" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> getNextGorEArray(vector<int>& v) {\n vector<int> res(v.size(),-1);\n stack<int> s;\n for(int i=v.size()-1;i>=0;i--) {\n while(!s.empty() && v[s.top()] < v[i]) {\n s.pop();\n }\n if(s.empty()) res[i]=-1;\n else res[i]=s.top();\n s.push(i);\n }\n return res;\n }\n int process(vector<int> &nums, vector<bool> &processed, vector<int> &v, int i) {\n if(processed[i] || v[i]==-1 || nums[v[i]]>nums[i]) return 0;\n processed[i]=true;\n return 1 + process(nums, processed, v, v[i]);\n }\n long long numberOfSubarrays(vector<int>& nums) {\n vector<int> v = getNextGorEArray(nums);\n vector<int> dp(nums.size(),1);\n long long ans = 1;\n for(int i=nums.size()-2;i>=0;i--) {\n int idx = v[i];\n if (idx == -1 || nums[idx] > nums[i]) dp[i] = 1;\n else dp[i] = dp[idx] + 1;\n ans += dp[i];\n }\n return ans;\n }\n};", "memory": "119462" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n=nums.size();\n vector<int>v(n,0);\n vector<int>v2(n,0);\n map<int,int>h;\n stack<int>s;\n long long ans=0;\n for(int i=n-1;i>=0;i--){\n int a=nums[i];\n while(!s.empty()&&nums[s.top()]<nums[i]){\n s.pop();\n }\n if(!s.empty()&&nums[s.top()]==nums[i]){\n v[i]+=v[s.top()];\n }\n v[i]++;\n ans+=v[i];\n s.push(i);\n }\n \n return ans;\n }\n};", "memory": "119462" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n stack<pair<int, int>> stack;\n long long result = 0;\n for (int n : nums) {\n while (!stack.empty() && stack.top().first < n)\n stack.pop();\n if (stack.empty() || stack.top().first != n)\n stack.emplace(n, 0);\n result += ++stack.top().second;\n }\n return result;\n }\n};", "memory": "121387" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "// 14:31\n\nclass Solution {\nprivate:\n struct Item\n {\n int val;\n int cnt;\n };\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int N = (int)nums.size();\n long long res = N;\n stack<Item> st;\n for (int i = 0; i < N; )\n {\n if (st.empty())\n {\n st.push(Item{nums[i], 1});\n ++i;\n }\n else if (st.top().val < nums[i])\n st.pop();\n else if (st.top().val == nums[i])\n {\n res += st.top().cnt;\n ++st.top().cnt;\n ++i;\n }\n else\n {\n st.push(Item{nums[i], 1});\n ++i;\n }\n }\n return res;\n }\n};", "memory": "121387" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long pow1(int x){\n long long val=(long long )x;\n val=(val*val+val)/2;\n return val;\n }\n long long numberOfSubarrays(vector<int>& k) {\n int n=k.size();\n long long ans=0;\n stack<int>s;\n int a,b;\n for(int i=0;i<n;i++){\n\n a=0;\n b=-1;\n while(s.size()>0&&s.top()<k[i]){\n if(b!=s.top())ans+=pow1(a),a=1,b=s.top();\n else a++;\n s.pop();\n }\n ans+=pow1(a);\n s.push(k[i]);\n }a=0;b=-1;\n while(s.size()>0){\n if(b!=s.top())ans+=pow1(a),a=1,b=s.top();\n else a++;\n s.pop();\n }ans+=pow1(a);\n return ans;\n \n }\n};", "memory": "123312" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "// Idea: monotonic stack。\n// we can use monotonic stack to discover the non-trivial special arrays that\n// each has 2+ elements.\n// trivial subarrays are ones with just one element.\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n vector<int> stk; // monotonic stack\n long long res = 0;\n for (int e : nums) {\n while (!stk.empty() && e > stk.back()) {\n stk.pop_back();\n }\n // count the array(s) ends with e\n // stack: 9 8 6 6 6, e = 6\n if (!stk.empty() && stk.back() == e) {\n auto iter = upper_bound(stk.rbegin(), stk.rend(), e);\n res += iter - stk.rbegin();\n }\n stk.push_back(e);\n }\n\n return res + nums.size();\n }\n};", "memory": "123312" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& arr) {\n int n = arr.size();\n\n vector<int> same_num(n, 1);\n\n //find prev greater elem\n stack<int> s;\n for(int i=0;i<n;i++){\n while(!s.empty() && arr[s.top()] <= arr[i]){\n if(arr[s.top()] == arr[i]){\n same_num[i] += same_num[s.top()];\n }\n\n s.pop();\n }\n\n s.push(i);\n }\n\n long long ans = 0;\n\n for(int i=0;i<n;i++){\n ans += same_num[i];\n }\n\n return ans;\n }\n};", "memory": "125237" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& arr) {\n int n = arr.size();\n\n vector<int> same_num(n, 1);\n\n //find prev greater elem\n stack<int> s;\n for(int i=0;i<n;i++){\n while(!s.empty() && arr[s.top()] <= arr[i]){\n if(arr[s.top()] == arr[i]){\n same_num[i] += same_num[s.top()];\n }\n\n s.pop();\n }\n\n s.push(i);\n }\n\n long long ans = 0;\n\n for(int i=0;i<n;i++){\n ans += same_num[i];\n }\n\n return ans;\n }\n};", "memory": "125237" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "#define ll long long \nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n ll n=nums.size();\n vector<int>a(n,1);\n // for(int i=0;i<n;i++){\n // for(int j=i;j<n;j++){\n // if(nums[i]==nums[j]){\n // a[i]++;\n // }\n // else if(nums[i]<nums[j]) break;\n // }\n // }\n stack<int>st;\n int i=n-1;\n while(i>=0){\n while(!st.empty() && nums[st.top()]<nums[i]) st.pop();\n if(!st.empty() && nums[st.top()]==nums[i]){\n a[i]=a[st.top()]+1;\n }\n st.push(i);\n i--;\n }\n long long count=0;\n\n for(int i=0;i<n;i++){\n count+=a[i];\n }\n return count;\n }\n};", "memory": "127162" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& arr) {\n int n = arr.size();\n long long cnt = 0;\n stack <pair<int, int>> st;\n\n if (n == 1) return 1;\n\n int j = 1;\n st.push({arr[0], 1});\n\n while (j < n){\n\n while (j < n && (st.empty() || arr[j] < st.top().first)){\n st.push({arr[j++], 1});\n }\n if (j == n) break;\n while (!st.empty() && arr[j] > st.top().first){\n st.pop();\n }\n\n if (st.empty() || arr[j] == st.top().first){\n \n int a = 0;\n\n // a = st.top().second;\n if (!st.empty()) {\n a = st.top().second;\n }\n\n cnt += (long long) a;\n // while (a--){\n st.push({arr[j], a+1}); \n // }\n // st.push({arr[j]});\n j++;\n }\n\n }\n return cnt + (long long)n;\n }\n};", "memory": "127162" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& arr) {\n int n = arr.size();\n\n vector<int> prev_greater(n, -1);\n vector<int> same_num(n, 1);\n\n //find prev greater elem\n stack<int> s;\n for(int i=0;i<n;i++){\n while(!s.empty() && arr[s.top()] <= arr[i]){\n if(arr[s.top()] == arr[i]){\n same_num[i] += same_num[s.top()];\n }\n\n s.pop();\n }\n\n if(!s.empty()){\n prev_greater[i] = s.top();\n }\n\n s.push(i);\n }\n\n long long ans = 0;\n\n for(int i=0;i<n;i++){\n ans += same_num[i];\n }\n\n return ans;\n }\n};", "memory": "129087" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size();\n vector<int> nextGreater(n, -1);\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 if(!st.empty() && nums[st.top()] == nums[i]) {\n nextGreater[i] = st.top();\n st.pop();\n }\n st.push(i);\n }\n long long ans = 0;\n vector<int> dp(n, 1);\n for(int i = 0; i < n; i++) {\n if(nextGreater[i] != -1 && nums[nextGreater[i]] == nums[i]) {\n dp[i] += dp[nextGreater[i]];\n }\n ans += dp[i];\n }\n return ans;\n }\n};", "memory": "129087" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "using ll=long long;\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int l=0,r=0;\n int n=nums.size();\n stack<int> st;\n unordered_map<int,int> mp;\n ll ans=0;\n while(r<n){\n ans++;\n while(st.size() && nums[r]>st.top()){\n mp[st.top()]=0;\n st.pop();\n } \n st.push(nums[r]);\n ans+=mp[st.top()];\n mp[nums[r]]++;\n r++;\n }\n return ans;\n }\n};", "memory": "131012" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size();\n vector<int> stk{};\n auto ans = 0ll;\n unordered_map<int, int> cnt{};\n for(int i = 0 ; i < n; ++i) {\n while(!stk.empty() && stk.back() < nums[i]) {\n cnt[stk.back()] = 0;\n stk.pop_back();\n }\n stk.push_back(nums[i]);\n ans += ++cnt[stk.back()];\n }\n return ans;\n }\n};", "memory": "131012" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& a) {\n long long n, ans;\n n = ans = a.size();\n stack<int> st;\n st.push(a.back());\n map<long long, long long> mp;\n mp[a.back()]++;\n for (int i = n - 2; i >= 0; i--) {\n if (st.empty()) {\n st.push(a[i]);\n mp[a[i]]++;\n continue;\n }\n\n while (st.top() < a[i]) {\n mp[st.top()]--;\n st.pop();\n if (st.empty()) break;\n } \n\n if (st.empty()) {\n st.push(a[i]);\n mp[a[i]]++;\n continue;\n }\n\n // cout << i << ' ' << mp[a[i]] << '\\n';\n ans += mp[a[i]];\n st.push(a[i]);\n mp[a[i]]++;\n }\n return ans;\n }\n};", "memory": "132937" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n=nums.size();\n vector <long long> v(n, 0);\n int ind=-1;\n vector <long long> score(n,1);\n for(int i=0;i<nums.size();i++)\n {\n while(ind!=-1 and nums[v[ind]]<nums[i])\n {\n ind--;\n }\n if(ind!=-1 and nums[v[ind]]==nums[i])\n {\n score[i]+=score[v[ind]];\n v[ind]=i;\n }\n ind++;\n v[ind]=i;\n }\n long long ans=0;\n for(int i=0;i<n;i++)\n {\n ans+=score[i];\n }\n return ans;\n }\n};", "memory": "132937" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> nge(vector<int> &nums)\n {\n stack<int> st;\n int n = nums.size();\n vector<int> ans(nums.size(), -1);\n for(int i = nums.size()-1; i >= 0; i--)\n {\n if(st.empty()) \n {\n st.push(i);\n ans[i] = n;\n } \n else\n {\n while(!st.empty() and nums[i] > nums[st.top()])\n st.pop();\n if(st.empty())\n {\n ans[i] = n;\n }\n else\n ans[i] = st.top();\n \n st.push(i);\n }\n }\n return ans;\n }\n long long int Comb2(int &n)\n {\n long long int ans = (long long int) n * ((long long int)(n+1))/2;\n return ans;\n }\n long long numberOfSubarrays(vector<int>& nums) {\n \n unordered_map<int, int> cnt;\n vector<int> nger = nge(nums);\n int n = nums.size();\n long long int ans = n; //n solo subarrays\n \n for(int i = 0; i < n; i++)\n {\n // cout<<nums[i]<<\" \"<<nums[nger[i]]<<endl;\n if(nger[i] != -1 and nger[i] != n and nums[nger[i]] == nums[i])\n {\n cnt[nums[i]]++;\n }\n else\n {\n ans += Comb2(cnt[nums[i]]);\n cnt[nums[i]] = 0;\n }\n }\n \n for(auto i: cnt)\n ans += Comb2(i.second);\n \n return ans;\n \n }\n};", "memory": "134862" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n stack<long long> s;\n long long ans=nums.size();\n unordered_map<long long ,long long>m;\n for(int i=0;i<nums.size();i++){\n int ele=nums[i];\n int cnt=0;\n while(!s.empty() and s.top()<=ele){\n if(s.top()==ele){\n m[s.top()]++;\n }\n else{\n m[s.top()]=0;\n }\n s.pop();\n }\n ans+=m[ele];\n s.push(ele);\n }\n return ans;\n }\n};", "memory": "134862" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "#define ll long long \nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& v) {\n stack<ll> s;\n map<ll,ll> m;\n s.push(v[0]);\n m[v[0]]++;\n ll ans=v.size();\n v.push_back(2000000000);\n for(int i=1;i<v.size();i++){\n if(v[i]>s.top()){\n while(!s.empty() && v[i]>s.top()){\n if(m[s.top()]>0) ans+=(m[s.top()]*(m[s.top()]-1))/2;\n m[s.top()]=0;\n s.pop();\n }\n }\n s.push(v[i]);\n m[v[i]]++;\n }\n return ans;\n }\n};", "memory": "136787" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\n\n vector<pair<int,int>> t;\n long helper(vector<int>& nums, int begin, int end, int t_index) \n {\n if (begin+1==end) return 1;\n if (t_index>=nums.size()) return 0;\n while (t[t_index].second < begin || t[t_index].second >=end) t_index++;\n //if (t_index>=nums.size()) return 0;\n\n int m = -t[t_index].first;\n long count = 0;\n int last = begin;\n long r = 0;\n while (t_index<nums.size() && t[t_index].first==-m)\n {\n int i = t[t_index].second;\n if (i>=end) break;\n t_index++;\n if (last<i) r += helper(nums, last, i, t_index);\n last=i+1;\n count++;\n }\n\n if (last<end) r += helper(nums, last, end, t_index);\n r += count*(count+1)/2;\n return r;\n }\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size();\n t.resize(n);\n for (int i=0; i<n; i++) t[i] = {-nums[i], i};\n sort(t.begin(), t.end());\n\n return helper(nums, 0, n, 0);\n }\n};", "memory": "136787" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "#define ll long long \nconst int M = 1e9 + 7; \n \nclass Solution { \npublic: \n vector<ll> dp; \n\n ll dfs(int u, vector<bool>& vis, vector<int>& nums) { \n vis[u] = 1; \n ll res = 0; \n if(dp[u] != nums.size() && nums[u] == nums[dp[u]]) { \n res = 1 + dfs(dp[u], vis, nums); \n } \n return res; \n } \n \n void finddp(vector<int>& nums) { \n ll n = nums.size(); \n dp.resize(n, n); \n stack<ll> st; \n for(int i= n- 1;i>=0;i--){\n while(!st.empty() && nums[i]>nums[st.top()]){\n st.pop();\n }\n if(!st.empty()){\n dp[i] = st.top();\n }\n st.push(i);\n }\n \n } \n \n \n \n long long numberOfSubarrays(vector<int>& nums) { \n finddp(nums); \n ll n = nums.size(), res = n; \n vector<bool> vis(n, 0); \n for (int i = 0; i < n; i++) \n { \n if(!vis[i]) { \n ll copy = dfs(i, vis, nums); \n res += (copy * (copy + 1)) / 2; \n } \n } \n for(auto it:dp){\n cout<<it<<\" \";\n }\n return res; \n } \n};", "memory": "138712" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\n\n vector<pair<int,int>> t;\n long helper(vector<int>& nums, int begin, int end, int t_index) \n {\n if (begin+1==end) return 1;\n if (t_index>=nums.size()) return 0;\n while (t[t_index].second < begin || t[t_index].second >=end) t_index++;\n if (t_index>=nums.size()) return 0;\n\n int m = -t[t_index].first;\n long count = 0;\n int last = begin;\n long r = 0;\n while (t_index<nums.size() && t[t_index].first==-m)\n {\n int i = t[t_index].second;\n if (i>=end) break;\n t_index++;\n if (last<i) r += helper(nums, last, i, t_index);\n last=i+1;\n count++;\n }\n\n if (last<end) r += helper(nums, last, end, t_index);\n r += count*(count+1)/2;\n return r;\n }\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size();\n t.resize(n);\n for (int i=0; i<n; i++) t[i] = {-nums[i], i};\n sort(t.begin(), t.end());\n\n return helper(nums, 0, n, 0);\n }\n};", "memory": "138712" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size();\n unordered_map<int, int> mp;\n stack<int> st;\n long long ans = 0;\n for (int i = 0; i < n; i++) {\n ans += mp[nums[i]]++ + 1;\n while (st.size() && nums[st.top()] < nums[i]) {\n mp[nums[st.top()]] = 0;\n st.pop();\n }\n if (st.size() && nums[st.top()] == nums[i])\n continue;\n st.push(i);\n }\n return ans;\n }\n};", "memory": "140637" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n long long ans=0;\n stack<int>st;\n map<int,int>mp;\n for(int x : nums){\n while(!st.empty() and st.top()<x){\n mp[st.top()]--;\n st.pop();\n }\n mp[x]++;\n st.push(x);\n ans +=mp[x];\n // cout<<ans<<endl;\n }\n return ans;\n }\n};", "memory": "146412" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n // stack\n long long ans=1;\n vector<vector<int>>st;\n st.push_back({nums[0],1});\n for(int i=1;i<nums.size();i++){\n while(st.size()>0 && st.back()[0]<nums[i]){\n st.pop_back();\n } \n if(st.size() ==0 || st.back()[0]!=nums[i]){\n st.push_back({nums[i],0});\n }\n ans+= ++st.back()[1];\n }\n return ans;\n \n }\n};", "memory": "146412" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size();\n vector<vector<int>> stack;\n long long res =0;\n for(auto& a:nums){\n while(!stack.empty()&&stack.back()[0]<a) stack.pop_back();\n if(stack.empty()||stack.back()[0]!=a) stack.push_back({a,0});\n res += ++stack.back()[1];\n }\n return res; \n }\n};", "memory": "148337" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "using ll=long long;\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n stack<int>st;ll ans=0;\n // ll ct=0;\n unordered_map<int,ll>mp;\n for(int i=0;i<nums.size();i++){\n if(st.empty()){\n st.push(nums[i]);\n mp[nums[i]]++;\n }\n else{\n if(nums[i]>st.top()){\n while(!st.empty() && st.top()<nums[i]){\n mp[st.top()]--;\n st.pop();\n }\n \n \n }\n if(!st.empty() && nums[i]==st.top()){\n ans+=mp[nums[i]];\n \n }\n \n st.push(nums[i]);\n mp[nums[i]]++;\n }\n \n }\n \n // return x;\n return (long long)(ans+nums.size());\n }\n};", "memory": "148337" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "#define ll long long \nconst int M = 1e9 + 7; \n \nclass Solution { \npublic: \n vector<ll> dp; \n\n ll dfs(int u, vector<bool>& vis, vector<int>& nums) { \n vis[u] = 1; \n ll res = 0; \n if(dp[u] != nums.size() && nums[u] == nums[dp[u]]) { \n res = 1 + dfs(dp[u], vis, nums); \n } \n return res; \n } \n \n void finddp(vector<int>& nums) { \n ll n = nums.size(); \n dp.resize(n, n); \n stack<ll> st; \n for(int i= n- 1;i>=0;i--){\n while(!st.empty() && nums[i]>nums[st.top()]){\n st.pop();\n }\n if(!st.empty()){\n dp[i] = st.top();\n }\n st.push(i);\n }\n \n } \n \n \n \n long long numberOfSubarrays(vector<int>& nums) { \n finddp(nums); \n ll n = nums.size(), res = n; \n vector<bool> vis(n, 0); \n for (int i = 0; i < n; i++) \n { \n if(!vis[i]) { \n ll copy = dfs(i, vis, nums); \n res += (copy * (copy + 1)) / 2; \n } \n } \n for(auto it:dp){\n cout<<it<<\" \";\n }\n return res; \n } \n};", "memory": "150262" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "#define ll long long \nconst int M = 1e9 + 7; \n \nclass Solution { \npublic: \n vector<ll> dp; \n\n ll dfs(int u, vector<bool>& vis, vector<int>& nums) { \n vis[u] = 1; \n ll res = 0; \n if(dp[u] != nums.size() && nums[u] == nums[dp[u]]) { \n res = 1 + dfs(dp[u], vis, nums); \n } \n return res; \n } \n \n void finddp(vector<int>& nums) { \n ll n = nums.size(); \n dp.resize(n, n); \n stack<ll> st; \n for(int i= n- 1;i>=0;i--){\n while(!st.empty() && nums[i]>nums[st.top()]){\n st.pop();\n }\n if(!st.empty()){\n dp[i] = st.top();\n }\n st.push(i);\n }\n \n } \n \n \n \n long long numberOfSubarrays(vector<int>& nums) { \n finddp(nums); \n ll n = nums.size(), res = n; \n vector<bool> vis(n, 0); \n for (int i = 0; i < n; i++) \n { \n if(!vis[i]) { \n ll copy = dfs(i, vis, nums); \n res += (copy * (copy + 1)) / 2; \n } \n } \n // for(auto it:dp){\n // cout<<it<<\" \";\n // }\n return res; \n } \n};", "memory": "152187" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "using ll = long long;\n\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n ll res = 0;\n map<int, ll> cnt, lst;\n stack<array<ll, 2>> st;\n\n for (int i = 0; i < nums.size(); i++) {\n ll x = nums[i];\n while (st.size() && x >= st.top()[0]) {\n st.pop();\n }\n\n int lb = (st.size() ? st.top()[1] : -1);\n if (lb > lst[x]) {\n cnt[x] = 0;\n }\n\n res += ++cnt[x];\n lst[x] = i;\n st.push({x, i});\n }\n\n return res;\n }\n};", "memory": "152187" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n long long res = 0;\n stack<vector<int>> s;\n for (auto c: nums) {\n while (s.size() && s.top()[0] < c) s.pop();\n if (s.size() == 0 || s.top()[0] > c) s.push({c, 0});\n res += ++s.top()[1];\n }\n return res;\n }\n};", "memory": "154112" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n long long res = 0;\n stack<vector<int>>s;\n for (auto c: nums) {\n while (s.size() && s.top()[0] < c) s.pop();\n if (s.size() == 0 || s.top()[0] > c) s.push({c, 0});\n res += ++s.top()[1];\n }\n return res;\n }\n};", "memory": "156037" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n long long res = 0;\n stack<vector<int>>s;\n for (auto c: nums) {\n while (!s.empty() && s.top()[0] < c) s.pop();\n if (s.size() == 0 || s.top()[0] > c) s.push({c, 0});\n res += ++s.top()[1];\n }\n return res;\n }\n};", "memory": "156037" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n // long long count = 0;\n // for(int i=0; i<nums.size(); i++){\n // for(int j=i; j<nums.size(); j++){\n // if(nums[i] == nums[j]){\n // int maxi = 0;\n // for(int k=i; k<=j; k++){\n // maxi = max(maxi, nums[k]);\n // }\n // if(maxi == nums[i])\n // count++;\n // } \n // }\n // }\n // return count;\n int n = nums.size();\n map<int, int> mpp;\n long long count = 0;\n for(int i=0; i<n; i++){\n int t = nums[i];\n if(mpp.find(t) != mpp.end())\n count += mpp[t];\n for(auto it: mpp){\n if(it.first < t)\n mpp.erase(it.first);\n else \n break;\n }\n mpp[t]++;\n }\n count += n;\n return count;\n }\n};", "memory": "157962" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "using ll=long long;\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n ll ans=0;\n map<ll,ll>mp;\n for(int i=0;i<nums.size();i++)\n {\n mp[nums[i]]++;\n auto it =mp.begin();\n\n while(it->first!=nums[i])\n {\n auto it1=it;\n it++;\n mp.erase(it1->first);\n \n }\n ans+=mp[nums[i]];\n }\n return ans;\n }\n};", "memory": "157962" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n long long pow1(int x){\n long long val=(long long )x;\n val=(val*val+val)/2;\n return val;\n }\n long long numberOfSubarrays(vector<int>& k) {\n int n=k.size();\n long long ans=0;\n stack<int>s;\n unordered_map<int,int>f;\n for(int i=0;i<n;i++){\n while(s.size()>0&&s.top()<k[i]){\n f[s.top()]++;\n s.pop();\n }\n for(auto &it:f){\n ans+=pow1(it.second);\n }\n f.clear();\n s.push(k[i]);\n }\n while(s.size())f[s.top()]++,s.pop();\n for(auto &it:f){\n ans+=pow1(it.second);\n }return ans;\n \n }\n};", "memory": "159887" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n class stree\n {\n public:\n vector<int> t;\n stree (int n)\n {\n t.resize(4*n);\n }\n void build(int idx, int tl, int tr, vector<int>&nums)\n {\n if(tr==tl)\n {\n t[idx]=nums[tl]; return;\n }\n else\n {\n int mid=(tr+tl)/2;\n build(2*idx,tl,mid,nums);\n build(2*idx+1,mid+1,tr,nums);\n t[idx]=max(t[2*idx],t[2*idx+1]);\n }\n }\n int query(int idx, int tl, int tr, int l, int r, vector<int>&nums)\n {\n if(r<tl || l>tr) return INT_MIN;\n if(l<=tl && r>=tr) return t[idx];\n else\n {\n int mid=(tl+tr)/2;\n return max(query(2*idx,tl,mid,l,r,nums),query(2*idx+1,mid+1,tr,l,r,nums));\n } \n }\n\n };\n long long numberOfSubarrays(vector<int>& nums) {\n int n=nums.size();\n long long ans=n;\n stree seg(n);\n seg.build(1,0,n-1,nums);\n map<int,int> freq,lpos;\n for(int i=0;i<n;i++)\n {\n freq[nums[i]]++;\n if(freq[nums[i]]>=2)\n {\n if(seg.query(1,0,n-1,lpos[nums[i]],i,nums)==nums[i]) ans+=freq[nums[i]]-1;\n else freq[nums[i]]=1;\n }\n lpos[nums[i]]=i;\n }\n return ans;\n }\n};", "memory": "159887" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "using ll = long long;\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n /*\n for i in [0,n-1]:\n for j in [i:n-1]:\n if nums[i]!=nums[j] : continue;\n if nums[j]>nums[i] : break;\n else ans++;\n */\n ll ans=0,n=nums.size();\n unordered_map<int,vector<int>> mp;\n for(int i=0;i<n;i++)mp[nums[i]].push_back(i);\n stack<int> s;//nearest greater to the right\n vector<int> ngr(n);\n for(int i=n-1;i>=0;i--){\n while(!s.empty() && nums[s.top()]<=nums[i])s.pop();\n if(s.empty())ngr[i]=n;\n else ngr[i]=s.top();\n s.push(i);\n }\n\n // for(int i=0;i<n;i++){\n // cout<<ngr[i]<<\" \";\n // }\n\n for(int i=0;i<n;i++){\n /*\n [i,j-1] j is the nearest greater element index\n number of nums[i] in [i,j-1]\n */\n int j = ngr[i];\n int start = lower_bound(mp[nums[i]].begin(),mp[nums[i]].end(),i)-mp[nums[i]].begin();\n int end = upper_bound(mp[nums[i]].begin(),mp[nums[i]].end(),j-1)-mp[nums[i]].begin();\n end--;\n // cout<<end-start+1<<endl;\n ans+=end-start+1;\n }\n return ans;\n }\n};", "memory": "161812" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\nint bs(vector<int>&arr,int i){\n int start=0,end=arr.size()-1;\n while(start<=end){\n int mid=start+(end-start)/2;\n if(arr[mid]==i)return mid;\n else if(arr[mid]<i)start=mid+1;\n else end=mid-1;\n }\n return -1;\n}\nint bss(vector<int>&arr,int index,int i){\n int start=0,end=arr.size()-1,ans=-1;\n while(start<=end){\n int mid=start+(end-start)/2;\n if(arr[mid]>index)end=mid-1;\n else if(arr[mid]<=i)start=mid+1;\n else{\n ans=mid;\n start=mid+1;\n }\n }\n return ans;\n}\n long long numberOfSubarrays(vector<int>& nums) {\n long long ans=0;\n vector<int> arr(nums.size(),-1);\n stack<int> st;\n for(int i=nums.size()-1;i>=0;i-=1){\n while(!st.empty() and nums[st.top()]<=nums[i])st.pop();\n if(!st.empty())arr[i]=st.top();\n st.push(i);\n }\n unordered_map<int,vector<int>> mp;\n for(int i=0;i<nums.size();i+=1){\n mp[nums[i]].push_back(i);\n }\n for(int i=0;i<nums.size();i+=1){\n int index=arr[i];\n if(index==-1)index=arr.size();\n int curr_index=bs(mp[nums[i]],i);\n int next_index=bss(mp[nums[i]],index,i);\n if(next_index==-1)next_index=curr_index;\n ans=ans+(next_index-curr_index+1);\n }\n return ans;\n }\n};", "memory": "161812" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\n vector<int> prev_greater(vector<int> arr) {\n int n = arr.size();\n \n vector<int> PGE(n, -1);\n stack<int> stk;\n \n for (int i = n-1; i >= 0; i--) {\n while (!stk.empty() && arr[stk.top()] < arr[i]) {\n PGE[stk.top()] = i;\n stk.pop();\n } \n stk.push(i);\n }\n return PGE;\n }\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size();\n long long ans = 0;\n\n vector<long long> dp(n);\n //map to check the previous index of nums[i]\n map<int, int> prevIdx;\n\n // find the previous greater element index to know the starting point of your subarray\n vector<int> prevGreaterIdx = prev_greater(nums);\n\n for(int i=0; i<n; i++){\n //if element appear first time\n if(prevIdx.find(nums[i])==prevIdx.end()){\n dp[i] = 1; //subarray [i,...,i]\n }\n else{\n //If there is no greater element between the j and i where nums[j] = nums[i] \n if(prevGreaterIdx[i] < prevIdx[nums[i]]){\n dp[i] = dp[prevIdx[nums[i]]] + 1; //subarray [j,...,i]\n }\n else{\n dp[i] = 1; //subarray [i,...,i]\n }\n }\n ans += dp[i];\n prevIdx[nums[i]] = i;\n }\n return ans;\n }\n};", "memory": "163737" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n void solve2(vector<int>& nums,vector<int> &prevg) {\n int n=nums.size();\n stack<int> st;\n st.push(0);\n prevg[0]=-1;\n for(int i=0;i<n;i++){\n while(!st.empty() && nums[st.top()]<=nums[i]){\n st.pop();\n }\n if(st.empty()){\n prevg[i]=-1;\n }\n else{\n prevg[i]=st.top();\n }\n st.push(i);\n }\n return;\n }\n long long numberOfSubarrays(vector<int>& nums) {\n int n=nums.size();\n vector<int> prevg(n);\n solve2(nums,prevg);\n long long int ans=0;\n unordered_map<int,vector<int>> mp;\n for(int i=0;i<n;i++){\n mp[nums[i]].push_back(i);\n int s=0;\n int e=mp[nums[i]].size()-1;\n int m=s+(e-s)/2;\n long long int index=-1;\n while(s<=e){\n if(mp[nums[i]][m]>prevg[i]){\n index=m;\n e=m-1;\n }\n else{\n s=m+1;\n }\n m=s+(e-s)/2;\n }\n ans+=mp[nums[i]].size()-index;\n }\n return ans;\n }\n};", "memory": "163737" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\n unordered_map<int, vector<int>> indicesMap;\n vector<int> nextGreater;\npublic:\n int query(int ele, int L, int R) {\n int leftIndex = lower_bound(indicesMap[ele].begin(), indicesMap[ele].end(), L) - indicesMap[ele].begin();\n int rightIndex = lower_bound(indicesMap[ele].begin(), indicesMap[ele].end(), R) - indicesMap[ele].begin();\n \n if(rightIndex == indicesMap[ele].size())\n return rightIndex - leftIndex;\n if(indicesMap[ele][rightIndex] > R)\n return rightIndex - leftIndex;\n else\n return rightIndex - leftIndex + 1;\n }\n void computeNextGreater(vector<int>& nums) {\n int n = nums.size();\n nextGreater.resize(n, n);\n stack<int> stk;\n \n for(int i = 0 ; i < n ; i++) {\n while(!stk.empty() && nums[stk.top()] < nums[i]) {\n nextGreater[stk.top()] = i;\n stk.pop();\n }\n stk.push(i);\n } \n }\n long long numberOfSubarrays(vector<int>& nums) {\n computeNextGreater(nums);\n \n int n = nums.size();\n for(int i = 0 ; i < n ; i++)\n indicesMap[nums[i]].push_back(i); \n \n long long count = 0;\n for(int i = 0 ; i < n ; i++) { \n int L = i;\n int R = nextGreater[i] - 1; \n\n count += query(nums[i], L, R); \n }\n \n return count;\n }\n};", "memory": "165662" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#define ll long long\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n ll ans = 0, n = nums.size();\n vector<ll> sub(n, 1), ci(n, -1);\n unordered_map<ll, ll> seen;\n \n stack<int> st;\n for(int i=0; i<n; i++){\n while(!st.empty() and nums[st.top()] <= nums[i]){\n st.pop();\n }\n if(!st.empty()){\n ci[i] = st.top();\n }\n st.push(i);\n }\n \n for(int i=0; i<n; i++){\n if(seen.count(nums[i]) and ci[i] < seen[nums[i]]){\n sub[i] += sub[seen[nums[i]]];\n }\n seen[nums[i]]=i;\n }\n for(auto x : sub) ans += x;\n return ans;\n }\n};", "memory": "165662" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n#define ll long long\n long long numberOfSubarrays(vector<int>& nums) {\n map<ll,vector<ll>>m;\n for(int i=0;i<nums.size();i++){\n m[nums[i]].push_back(i);\n } \n stack<pair<int,int>>s;\n int arr[nums.size()];\n memset(arr,-1,sizeof(arr));\n for(int i=nums.size()-1;i>=0;i--)\n {\n while(s.size())\n {\n if(s.top().first>nums[i])break;\n s.pop();\n }\n\n if(s.size()>0)\n {\n arr[i]=s.top().second;\n }\n s.push({nums[i],i});\n }\n\n long long ans=0;\n\n for(int i=0;i<nums.size();i++)\n {\n int maxindex=nums.size();\n if(arr[i]!=-1)\n {\n maxindex=arr[i];\n }\n auto it=lower_bound(m[nums[i]].begin(),m[nums[i]].end(),maxindex)-m[nums[i]].begin();\n it--;\n auto itr=lower_bound(m[nums[i]].begin(),m[nums[i]].end(),i)-m[nums[i]].begin(); \n ans+=it-itr+1;\n }\n\n return ans;\n\n }\n};", "memory": "167587" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n \n void build(int node,int st,int en, vector<int> &arr,vector<int> &tree)\n{\n if(st==en)\n {\n tree[node]= arr[st]; return;\n }\n int mid = (st+en)/2;\n\n build(2*node+1,st,mid,arr,tree); //left portion\n build(2*node+2,mid+1,en,arr,tree);\n\n tree[node]=max(tree[2*node+1] ,tree[2*node+2]);\n \n}\n\nint findmax(int i ,int j ,int low,int high,int node ,vector<int> &tree)\n{\n if(i<=low && high<= j) return tree[node];\n\n if(j<low || high<i) return 0;\n int mid=(low+high)/2;\n\n return max(findmax(i,j,low,mid,2*node+1,tree) ,findmax(i,j,mid+1,high,2*node+2,tree));\n}\n\n long long numberOfSubarrays(vector<int>& arr) {\n \n int n=arr.size();\n vector<int> tree(4*n,0);\n \n build(0,0,n-1,arr,tree);\n \n //lets calculate preindex of every index --- \n \n vector<int> preind(n,-1);\n unordered_map<int,int> mp;\n \n for(int i=0;i<n;i++)\n {\n if(mp.find(arr[i])!=mp.end())\n {\n preind[i]=mp[arr[i]];\n }\n \n mp[arr[i]]=i;\n }\n \n vector<long long> ans(n,0);\n \n for(int i=0;i<n;i++)\n {\n if(preind[i]!=-1)\n { \n \n int maxi = findmax(preind[i],i,0,n-1,0,tree);\n if(maxi==arr[i])\n {\n ans[i]=1+ans[preind[i]];\n }\n }\n }\n \n return (long long)n+accumulate(ans.begin(),ans.end(),0*1ll);\n }\n};", "memory": "167587" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n vector<int>NGR(vector<int>input,int n){\n\n vector<int> result(n, n); stack<int> s;\n\n for (int i = 0; i < n; i++) { \n while (!s.empty() && input[s.top()] < input[i]) {\n result[s.top()] = i;\n s.pop();\n }\n s.push(i);\n }\n return result;\n }\n long long numberOfSubarrays(vector<int>& nums) {\n int n=nums.size();\n vector<int>ngr=NGR(nums,n);\n \n unordered_map<int,vector<int>>mp;\n\n for(int i=0;i<n;i++){\n mp[nums[i]].push_back(i);\n }\n\n long long ans=0;\n\n for(int i=0;i<n;i++){\n int res=lower_bound(mp[nums[i]].begin(),mp[nums[i]].end(),ngr[i])-mp[nums[i]].begin();\n mp[nums[i]].erase(mp[nums[i]].begin());\n ans+=res;\n\n }\n return ans;\n\n }\n};", "memory": "169512" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n ios::sync_with_stdio(0); cin.tie(0);\n unordered_map<int, vector<int>> m;\n stack<int> s;\n long long ans = 0;\n vector<int> greater(nums.size(), -1), dp(nums.size(), 1);\n for(int i = nums.size()-1; i >= 0; i--){\n while(!s.empty() && nums[i] > nums[s.top()]){\n greater[s.top()] = i;\n s.pop();\n }\n s.push(i);\n }\n for(int i = 0; i < nums.size(); i++){\n if(!m[nums[i]].empty()){\n auto it = upper_bound(begin(m[nums[i]]), end(m[nums[i]]), greater[i]);\n int num = (end(m[nums[i]]) - it);\n dp[i] += num;\n }\n m[nums[i]].push_back(i);\n }\n for(auto i : dp) ans += (long long) i;\n return ans;\n }\n};", "memory": "169512" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n long long ans = nums.size();\n\n map<int, int> m;\n\n for (auto& x : nums) {\n while (m.size() && m.begin()->first < x) {\n m.erase(m.begin());\n }\n\n if (m.begin()->first == x) {\n ans += m.begin()->second;\n }\n \n m[x]++;\n }\n\n return ans;\n }\n};", "memory": "171437" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& a) {\n long long ans=0;\n map<int,int> m;\n int n=a.size();\n for(int i=0; i<n; i++){\n int num=a[i];\n for(auto x:m){\n if(x.first<num){\n long long count=x.second;\n ans+=count*(count+1)/2;\n m.erase(x.first);\n }else break;\n }\n m[a[i]]++;\n }\n for(auto x:m){\n long long count=x.second;\n ans+=count*(count+1)/2;\n m.erase(x.first);\n }\n return ans;\n }\n};", "memory": "171437" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long pow1(int x){\n long long val=(long long )x;\n val=(val*val+val)/2;\n return val;\n }\n long long numberOfSubarrays(vector<int>& k) {\n int n=k.size();\n long long ans=0;\n stack<int>s;\n map<int,int>f;\n for(int i=0;i<n;i++){\n while(s.size()>0&&s.top()<k[i]){\n f[s.top()]++;\n s.pop();\n }\n for(auto &it:f){\n ans+=pow1(it.second);\n }\n f.clear();\n s.push(k[i]);\n }\n while(s.size())f[s.top()]++,s.pop();\n for(auto &it:f){\n ans+=pow1(it.second);\n }return ans;\n \n }\n};", "memory": "173362" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size();\n map<int, vector<int>> mp;\n for(int i=0;i<n;i++){\n mp[nums[i]].push_back(i);\n }\n // next max \n vector<int> nxt(n,-1);\n stack<int> st;\n for(int i = n-1;i>=0;i--){\n while(!st.empty() && nums[st.top()]<=nums[i]){\n st.pop();\n }\n if(st.empty()) nxt[i] = n;\n else nxt[i] = st.top();\n st.push(i);\n }\n long long ans = 0;\n for(auto [a,v]:mp){\n for(int i=0;i<v.size();i++){\n int idx = lower_bound(v.begin()+i,v.end(),nxt[v[i]]) - v.begin(); \n ans += idx - i;\n }\n }\n return ans;\n }\n};", "memory": "175287" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "using LL=long long;\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n=nums.size();\n stack<int> stk;\n vector<int> prevbigger(n,-1);\n unordered_map<int,vector<int>> m;\n for(int i=n-1;i>=0;i--){\n while(!stk.empty() && nums[i]>nums[stk.top()]){\n prevbigger[stk.top()]=i;\n stk.pop();\n }\n stk.push(i);\n //m[nums[i]].push_back(i);\n }\n for(int i=0;i<n;i++)\n m[nums[i]].push_back(i);\n LL ret=0;\n for(auto [num,pos]:m){\n int j=0;\n for(int i=0;i<pos.size();i++){\n //sort(pos.begin(),pos.end());\n //cout<<prevbigger[pos[i]]<<\" \"<<pos[j]<<endl;\n while(j<n && prevbigger[pos[i]]>pos[j]){\n j++;\n }\n //cout<<i<<\" \"<<j<<endl;\n ret+=i-j+1;\n }\n //cout<<num<<\" \"<<ret<<endl;\n }\n return ret;\n }\n};", "memory": "177212" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n unordered_map<int,vector<int>> indices;\n int n = nums.size();\n stack<int> sta_ind;\n long long ans = 0;\n for(int i = 0;i < n;i++) {\n indices[nums[i]].push_back(i);\n while(!sta_ind.empty() && nums[sta_ind.top()] <= nums[i]) {\n sta_ind.pop();\n }\n if(sta_ind.empty()) {\n ans += indices[nums[i]].size();\n }\n else {\n int not_valid = upper_bound(indices[nums[i]].begin(),indices[nums[i]].end(),sta_ind.top()) - indices[nums[i]].begin();\n ans += (indices[nums[i]].size() - not_valid);\n }\n sta_ind.push(i);\n }\n return ans;\n }\n};", "memory": "177212" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long find(int id,vector<int>& v){\n long long n=v.size();\n long long i=0,j=v.size()-1,ans=n;\n while(i<=j){\n int mid=(i+j)/2;\n if(v[mid]>=id){\n ans=mid;\n j=mid-1;\n }else{\n i=mid+1;\n }\n }\n return n-ans;\n }\n long long numberOfSubarrays(vector<int>& nums) {\n int n=nums.size();\n long long cnt=0;\n unordered_map<int,vector<int>> mp;\n stack<int> st;\n for(int i=0;i<n;i++){\n mp[nums[i]].push_back(i);\n while(!st.empty() && nums[st.top()]<=nums[i]){\n st.pop();\n }\n int ngeid=(st.empty())?-1:st.top();\n int t=find(ngeid,mp[nums[i]]);\n cout<<t<<endl;\n cnt+=t;\n st.push(i);\n }\n return cnt;\n }\n};", "memory": "179137" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "using ll = long long;\n\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n ll n = nums.size();\n\n vector<ll> bigger_to_right(n);\n stack<ll> st;\n for (ll i = n - 1; i >= 0; i--) {\n while (!st.empty() && nums[st.top()] <= nums[i]) st.pop();\n\n if (st.empty()) bigger_to_right[i] = n;\n else bigger_to_right[i] = st.top();\n\n st.push(i);\n }\n\n ll ans = 0;\n\n vector<vector<ll>> remove(n, vector<ll>());\n map<ll, ll> active;\n for (ll i = 0; i < n; i++) {\n for (ll rem : remove[i]) --active[rem];\n\n ++active[nums[i]];\n ans += active[nums[i]];\n\n if (bigger_to_right[i] < n) remove[bigger_to_right[i]].push_back(nums[i]);\n }\n\n return ans;\n }\n};", "memory": "179137" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include <string>\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <queue>\n#include <set>\n#include <fstream>\n#include <map>\n#include <stack>\n\nusing namespace std;\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// };\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n stack<pair<int,int>> monotonic_stk;\n unordered_map<int, vector<int>> m;\n long long ret = 0;\n for(int i=0; i<nums.size(); i++) {\n int last_bigger = -1;\n int val = nums[i];\n m[val].push_back(i);\n \n while(!monotonic_stk.empty() && monotonic_stk.top().first <= val) {\n monotonic_stk.pop();\n }\n \n if(!monotonic_stk.empty())\n last_bigger = monotonic_stk.top().second;\n \n monotonic_stk.push({val, i});\n \n if(last_bigger <= 0) {\n ret+=m[val].size();\n } else {\n \n int num = 0;\n for(int j=m[val].size()-1; j>=0; j--) {\n if(m[val][j] > last_bigger) {\n num++;\n } else {\n break;\n }\n }\n ret+=num;\n }\n }\n \n return ret;\n }\n};\n// int main () {\n// Solution sol;\n \n// // cout<<sol.minMoves2(8);\n// return 0;\n// }\n\n", "memory": "181062" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include <string>\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <queue>\n#include <set>\n#include <fstream>\n#include <map>\n#include <stack>\n\nusing namespace std;\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// };\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n stack<pair<int,int>> monotonic_stk;\n unordered_map<int, vector<int>> m;\n long long ret = 0;\n for(int i=0; i<nums.size(); i++) {\n int last_bigger = -1;\n int val = nums[i];\n m[val].push_back(i);\n \n while(!monotonic_stk.empty() && monotonic_stk.top().first <= val) {\n monotonic_stk.pop();\n }\n \n if(!monotonic_stk.empty())\n last_bigger = monotonic_stk.top().second;\n \n monotonic_stk.push({val, i});\n \n if(last_bigger <= 0) {\n ret+=m[val].size();\n } else {\n \n int num = 0;\n int left = 0;\n int right = m[val].size()-1;\n int mid = (left+right)/2;\n \n while(left < right) {\n mid = (left+right)/2;\n if(last_bigger < m[val][mid]) {\n right = mid;\n } else {\n left = mid+1;\n }\n }\n mid = (left+right)/2; \n num = m[val].size() - mid;\n ret+=num;\n }\n }\n \n return ret;\n }\n};\n// int main () {\n// Solution sol;\n \n// // cout<<sol.minMoves2(8);\n// return 0;\n// }\n", "memory": "181062" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int query(int ele, int L, int R, unordered_map<int,vector<int>>& indicesMap) {\n // Get the indices of the element ele from the indicesMap\n auto& indices = indicesMap[ele];\n \n // Find the lower bound for L and R\n int leftIndex = lower_bound(indices.begin(), indices.end(), L) - indices.begin();\n int rightIndex = upper_bound(indices.begin(), indices.end(), R) - indices.begin() - 1;\n \n // If leftIndex goes out of range or rightIndex is out of range\n if(leftIndex >= indices.size() || rightIndex < leftIndex)\n return 0; // No valid subarray found within [L, R]\n \n // Return the number of elements within the range\n return rightIndex - leftIndex + 1;\n }\n \n long long numberOfSubarrays(vector<int>& a) {\n int n = a.size();\n \n // Calculate next greater element for each index\n vector<int> next(n, n);\n stack<int> st;\n for(int i = n - 1; i >= 0; i--) {\n while(!st.empty() && a[st.top()] <= a[i]) {\n st.pop();\n }\n if(!st.empty()) {\n next[i] = st.top();\n }\n st.push(i);\n }\n \n // Build a map to store the indices of each element in a\n unordered_map<int, vector<int>> mp;\n for(int i = 0; i < n; i++) {\n mp[a[i]].push_back(i);\n }\n \n long long count = 0;\n \n // For each index, calculate the number of subarrays where a[i] is the maximum\n for(int i = 0; i < n; i++) {\n int L = i;\n int R = next[i] - 1; // The range in which a[i] is the maximum\n count += query(a[i], L, R, mp);\n }\n \n return count;\n }\n};", "memory": "182987" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#define ll long long\n\nll solve(const vector<int>& temp, int index) {\n ll cnt = 0;\n int n = temp.size();\n int i = 0, j = n - 1;\n\n while (i <= j) {\n int mid = (i + j) / 2;\n if (temp[mid] > index) {\n cnt = n - mid;\n j = mid - 1;\n } else {\n i = mid + 1;\n }\n }\n return cnt;\n}\n\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n ll ans = 0;\n int n = nums.size();\n vector<int> left(n); \n unordered_map<int, vector<int>> mp; \n stack<int> s;\n for (int i = 0; i < n; i++) {\n while (!s.empty() && nums[s.top()] <= nums[i]) {\n s.pop();\n }\n left[i] = s.empty() ? -1 : s.top();\n s.push(i);\n }\n\n for (int i = 0; i < n; i++) {\n int num = nums[i];\n vector<int>& temp = mp[num]; \n ll res = solve(temp, left[i]);\n ans += res + 1; \n mp[num].push_back(i); \n }\n\n return ans;\n }\n};", "memory": "182987" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size();\n vector<int> largest(n,0);\n stack<int> st;\n st.push(-1);\n\n for(int i=0; i<n; i++) {\n int idx = st.top();\n while(idx != -1 && nums[idx]<=nums[i]) {\n st.pop();\n idx = st.top();\n }\n st.push(i);\n largest[i] = idx;\n }\n long long ans=0;\n unordered_map<int, vector<int>> mp;\n for(int i=0; i<n; i++) {\n int ele = nums[i];\n int nearest_greater_idx = largest[i];\n int invalid = lower_bound(mp[ele].begin(), mp[ele].end(),nearest_greater_idx) - mp[ele].begin();\n ans += (mp[ele].size()-invalid)+1;\n mp[ele].push_back(i);\n }\n return ans;\n }\n};", "memory": "184912" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution\n{\npublic:\n vector<int> nextGreaterElement(vector<int> &arr, int n)\n {\n stack<int> s;\n s.push(-1);\n vector<int> ans(n);\n\n for (int i = n - 1; i >= 0; i--)\n {\n int curr = arr[i];\n while (s.top() != -1 && arr[s.top()] <= curr)\n {\n s.pop();\n }\n\n ans[i] = s.top();\n s.push(i);\n }\n\n return ans;\n }\n\n long long numberOfSubarrays(vector<int> &nums)\n {\n int n = nums.size();\n map<int, vector<int>> mp;\n\n for (int i = 0; i < n; i++)\n {\n mp[nums[i]].push_back(i);\n }\n\n long long ans = 0;\n vector<int> nextGreater = nextGreaterElement(nums, n);\n\n for (auto it : mp)\n {\n if (it.second.size() < 2)\n continue;\n\n vector<int> v = it.second;\n for (int i = 0; i < v.size() - 1; i++)\n {\n if (nextGreater[v[i]] == -1)\n {\n ans += v.size() - i - 1;\n continue;\n }\n\n for (int j = i + 1; j < v.size(); j++)\n {\n if (nextGreater[v[i]] == -1 || nextGreater[v[i]] > v[j])\n ans++;\n }\n }\n }\n\n return ans + n;\n }\n};\n", "memory": "184912" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size();\n // next greater element \n vector<int> next_greater(n, -1);\n stack<int> st;\n st.push(-1);\n for(int i = n-1; i >= 0; i--){\n while(st.top() != -1){\n if(nums[st.top()] > nums[i]){\n next_greater[i] = st.top();\n break;\n }\n else{\n st.pop();\n }\n }\n st.push(i);\n }\n\n map<int, vector<int> > mp;\n\n for(int i = 0; i < n; i++) mp[nums[i]].push_back(i);\n\n long long ans = 0;\n\n for(int i = 0; i < n; i++){\n if(next_greater[i] == -1){\n auto it = lower_bound(mp[nums[i]].begin(), mp[nums[i]].end(), i) - mp[nums[i]].begin();\n ans += mp[nums[i]].size() - it;\n continue;\n }\n int it = lower_bound(mp[nums[i]].begin(), mp[nums[i]].end(), next_greater[i]) - mp[nums[i]].begin();\n int it2 = lower_bound(mp[nums[i]].begin(), mp[nums[i]].end(), i) - mp[nums[i]].begin();\n ans += max(0, it-it2);\n }\n\n\n return ans;\n }\n};", "memory": "186837" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size();\n vector<int> left(n, -1);\n\n stack<int> st;\n unordered_map<int, vector<int>> mp;\n for (int i = n - 1; i >= 0; i--) {\n while (!st.empty() && nums[st.top()] < nums[i]) {\n left[st.top()] = i;\n st.pop();\n }\n st.push(i);\n }\n\n for (int i = 0; i < n; i++) mp[nums[i]].push_back(i);\n\n long long ans = 0;\n\n for (int i = 0; i < n; i++) {\n int val = nums[i];\n int L = left[i] + 1;\n int R = i;\n\n \n auto itL = lower_bound(mp[val].begin(), mp[val].end(), L);\n \n auto itR = upper_bound(mp[val].begin(), mp[val].end(), R);\n\n int temp = distance(itL, itR); \n\n ans += (long long)temp;\n }\n\n return ans;\n }\n};\n", "memory": "186837" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\n vector<int> next_greater(vector<int> &nums)\n {\n int n=nums.size();\n vector<int> res(n, n);\n stack<int> s;\n for(int i=0; i<n; i++)\n {\n if(s.empty())\n {\n s.push(i);\n continue;\n }\n while(!s.empty() && nums[i]>nums[s.top()])\n {\n res[s.top()]=i;\n s.pop();\n }\n s.push(i);\n }\n return res;\n }\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n vector<int> res=next_greater(nums);\n map<int, vector<int>> m;\n int n=nums.size();\n for(int i=0; i<n; i++)\n {\n m[nums[i]].push_back(i);\n }\n\n long long ans=0;\n for(int i=0; i<n; i++)\n {\n int next_max_idx=res[i];\n auto it=upper_bound(m[nums[i]].begin(), m[nums[i]].end(), next_max_idx);\n it--;\n auto it2=lower_bound(m[nums[i]].begin(), m[nums[i]].end(), i);\n int n_e=it+1-it2;\n\n // n_e=max(n_e, 1);\n cout<<n_e<<\" \";\n ans+=n_e;\n }\n return ans;\n }\n};", "memory": "188762" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size(), maxi = nums[0];\n vector<long long> v(n);\n unordered_map<int, vector<int>> mp;\n stack<int> st;\n st.push(-1);\n for (int i = 0; i < n; i++) {\n int id = st.top();\n while (id != -1 && nums[id] <= nums[i]) {\n st.pop();\n id = st.top();\n }\n v[i] = st.top();\n st.push(i);\n }\n long long ans = 0;\n for (int i = 0; i < n; i++) {\n ans +=\n mp[nums[i]].end() -\n lower_bound(mp[nums[i]].begin(), mp[nums[i]].end(), v[i] + 1) +\n 1;\n mp[nums[i]].push_back(i);\n }\n return ans;\n }\n};", "memory": "188762" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#include <vector>\n#include <unordered_map>\n#include <stack>\n#include <algorithm>\n\nclass Solution {\npublic:\n long long numberOfSubarrays(std::vector<int>& nums) {\n std::unordered_map<int, std::vector<int>> mpp;\n \n // Fill the map with indices of each element\n for (int i = 0; i < nums.size(); ++i) {\n mpp[nums[i]].push_back(i);\n }\n \n int n = nums.size();\n std::vector<int> pre_gre(n);\n std::stack<std::pair<int, int>> st;\n \n // Compute the previous greater elements for each index\n for (int i = 0; i < n; ++i) {\n while (!st.empty() && st.top().first < nums[n - i - 1]) {\n pre_gre[st.top().second] = n - i - 1;\n st.pop();\n }\n st.push({nums[n - i - 1], n - i - 1});\n }\n \n while (!st.empty()) {\n pre_gre[st.top().second] = -1;\n st.pop();\n }\n\n long long ans = 0;\n for (int i = n - 1; i >= 0; --i) {\n auto& indices = mpp[nums[i]];\n\n if (indices.size() > 1) {\n int prev_gre_ind = pre_gre[i];\n\n // Use binary search to find the first index not less than prev_gre_ind + 1\n auto it = std::lower_bound(indices.begin(), indices.end(), prev_gre_ind + 1);\n int j = it - indices.begin();\n \n // Count the number of valid subarrays\n ans += indices.size() - j;\n\n // Remove the last element from indices\n indices.pop_back();\n } else {\n ans++;\n }\n }\n\n return ans;\n }\n};\n", "memory": "190687" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(std::vector<int>& nums) {\n std::unordered_map<int, std::vector<int>> mpp;\n \n // Fill the map with indices of each element\n for (int i = 0; i < nums.size(); ++i) {\n mpp[nums[i]].push_back(i);\n }\n \n int n = nums.size();\n std::vector<int> pre_gre(n);\n std::stack<std::pair<int, int>> st;\n \n // Compute the previous greater elements for each index\n for (int i = 0; i < n; ++i) {\n while (!st.empty() && st.top().first < nums[n - i - 1]) {\n pre_gre[st.top().second] = n - i - 1;\n st.pop();\n }\n st.push({nums[n - i - 1], n - i - 1});\n }\n \n while (!st.empty()) {\n pre_gre[st.top().second] = -1;\n st.pop();\n }\n\n long long ans = 0;\n for (int i = n - 1; i >= 0; --i) {\n auto& indices = mpp[nums[i]];\n\n if (indices.size() > 1) {\n int prev_gre_ind = pre_gre[i];\n\n // Use binary search to find the first index not less than prev_gre_ind + 1\n auto it = std::lower_bound(indices.begin(), indices.end(), prev_gre_ind + 1);\n int j = it - indices.begin();\n \n // Count the number of valid subarrays\n ans += indices.size() - j;\n\n // Remove the last element from indices\n indices.pop_back();\n } else {\n ans++;\n }\n }\n\n return ans;\n }\n};\n", "memory": "190687" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "struct Node{\n int mx;\n Node(){\n mx=0;\n }\n};\n\nNode t[4*100000];\n\nNode merge(Node a, Node b){\n Node ans;\n ans.mx=max(a.mx, b.mx);\n return ans;\n}\n\nvoid build(int id, int l, int r, vector<int>& nums){\n if(l==r){\n t[id].mx=nums[l];\n return;\n }\n int mid=l+(r-l)/2;\n build(2*id, l, mid, nums);\n build(2*id+1, mid+1, r, nums);\n t[id]=merge(t[2*id], t[2*id+1]);\n}\n\nNode query(int id, int l, int r, int lq, int rq){\n if(l>rq or lq>r)\n return Node();\n if(lq<=l and r<=rq)\n return t[id];\n int mid=l+(r-l)/2;\n return merge(query(2*id, l, mid, lq, rq), query(2*id+1, mid+1, r, lq, rq));\n}\n\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n int n=nums.size();\n build(1, 0, n-1, nums);\n map<int, vector<int>> index;\n for(int i=0;i<n;i++)\n index[nums[i]].push_back(i);\n long long ans=0;\n for(auto& idx: index){\n long long el=idx.first;\n vector<int> tmp=idx.second;\n long long cur=tmp[0];\n long long cnt=1;\n for(int i=1;i<tmp.size();i++){\n Node node=query(1, 0, n-1, cur, tmp[i]);\n if(node.mx==el)\n cnt++;\n else{\n ans+=(cnt*(cnt+1))/2;\n cnt=1;\n cur=tmp[i];\n }\n }\n ans+=(cnt*(cnt+1))/2;\n }\n return ans;\n }\n};", "memory": "192612" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long find(int in,int range,int val, map<int,vector<int>>&cnt){\n return upper_bound(cnt[val].begin(),cnt[val].end(),in+range-1)-cnt[val].begin()-1;\n }\n long long numberOfSubarrays(vector<int>& nums) {\n int n=nums.size();\n\n vector<long long>left(n),right(n);\n\n stack<int>s;\n\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();\n }\n else{\n right[i]=n;\n }\n s.push(i);\n }\n \n unordered_map<int,vector<int>>cnt;\n for(int i=0;i<n;i++){\n cnt[nums[i]].push_back(i);\n }\n\n long long ans=0;\n for(int i=0;i<n;i++){\n int val1=upper_bound(cnt[nums[i]].begin(),cnt[nums[i]].end(),right[i]-1)-cnt[nums[i]].begin();\n int val2=lower_bound(cnt[nums[i]].begin(),cnt[nums[i]].end(),i)-cnt[nums[i]].begin();\n // cout<<cnt[nums[i]][val1]<<\" \"<<val2<<\" \";\n int len=val1-val2;\n cout<<len<<\" \"<<endl;\n if(len>0)\n ans+=len;\n // ans+=(len*(len+1))/2;\n }\n return ans;\n\n }\n};", "memory": "194537" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "struct node{\n int mx = -1e9;\n};\nclass Solution {\npublic:\n static const int N = 4*(1e5+1);\n node t[N];\n int a[N];\n node merge(node a,node b){\n node p;\n p.mx = max(a.mx,b.mx);\n return p;\n }\n void build(int idx,int l,int r){\n if(l > r)return;\n if(l == r){\n t[idx].mx = a[l];\n return; \n }\n int mid = (l+r)/2;\n build(2*idx,l,mid);\n build(2*idx+1,mid+1,r);\n t[idx] = merge(t[2*idx],t[2*idx+1]);\n }\n node query(int l, int r,int idx, int lr, int rl){\n if(lr > r || l > rl){\n node p;return p;\n }\n if(lr <= l && r <= rl){\n return t[idx];\n }\n int mid = (l+r)/2;\n return merge(query(l,mid,2*idx,lr,rl),query(mid+1,r,2*idx+1,lr,rl));\n }\n long long numberOfSubarrays(vector<int>& nums) {\n int n =nums.size();\n map<int,vector<int>> mp;\n for(int i = 0; i < n; i++){\n a[i] = nums[i];\n mp[a[i]].push_back(i);\n }\n build(1,0,n-1);\n long long ans = n;\n for(auto v:mp){\n int c = 0;\n for(int i = 1; i < v.second.size(); i++){\n if(v.first == query(0,n-1,1,v.second[i-1],v.second[i]).mx){\n c++;\n ans += (c);\n }\n else{c = 0;}\n }\n }\n return ans;\n }\n};", "memory": "196462" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int> &nums){\n int n = nums.size(); \n vector<int> nge(n, n); \n\n stack<int> st; \n\n for (int i = n - 1; i >= 0; i--){\n while (!st.empty() && nums[st.top()] <= nums[i]) st.pop(); \n\n if (!st.empty()) nge[i] = st.top(); \n st.push(i); \n }\n\n return nge; \n }\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size(); \n unordered_map<int, vector<int>> m; \n\n for (int i = 0; i < n; i++){\n m[nums[i]].push_back(i);\n }\n\n vector<int> nge = nextGreaterElement(nums);\n\n long long ans = 0; \n\n for (auto p : m){\n for (int i = 0; i < p.second.size(); i++){\n ans += upper_bound(p.second.begin() + i, p.second.end(), nge[p.second[i]]) - p.second.begin() - i;\n }\n }\n\n return ans;\n }\n};", "memory": "196462" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long find(int in,int range,int val, map<int,vector<int>>&cnt){\n return upper_bound(cnt[val].begin(),cnt[val].end(),in+range-1)-cnt[val].begin()-1;\n }\n long long numberOfSubarrays(vector<int>& nums) {\n int n=nums.size();\n\n vector<long long>left(n),right(n);\n\n stack<int>s;\n\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();\n }\n else{\n right[i]=n;\n }\n s.push(i);\n }\n \n map<int,vector<int>>cnt;\n for(int i=0;i<n;i++){\n cnt[nums[i]].push_back(i);\n }\n\n long long ans=0;\n for(int i=0;i<n;i++){\n int val1=upper_bound(cnt[nums[i]].begin(),cnt[nums[i]].end(),right[i]-1)-cnt[nums[i]].begin();\n int val2=lower_bound(cnt[nums[i]].begin(),cnt[nums[i]].end(),i)-cnt[nums[i]].begin();\n // cout<<cnt[nums[i]][val1]<<\" \"<<val2<<\" \";\n int len=val1-val2;\n cout<<len<<\" \"<<endl;\n if(len>0)\n ans+=len;\n // ans+=(len*(len+1))/2;\n }\n return ans;\n\n }\n};", "memory": "198387" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> nextGreaterElement(vector<int> &nums){\n int n = nums.size(); \n vector<int> nge(n, n); \n\n stack<int> st; \n\n for (int i = n - 1; i >= 0; i--){\n while (!st.empty() && nums[st.top()] <= nums[i]) st.pop(); \n\n if (!st.empty()) nge[i] = st.top(); \n st.push(i); \n }\n\n return nge; \n }\n long long numberOfSubarrays(vector<int>& nums) {\n int n = nums.size(); \n unordered_map<int, vector<int>> m; \n\n for (int i = 0; i < n; i++){\n m[nums[i]].push_back(i);\n }\n\n vector<int> nge = nextGreaterElement(nums);\n\n long long ans = 0; \n\n for (auto p : m){\n for (int i = 0; i < p.second.size(); i++){\n ans += upper_bound(p.second.begin(), p.second.end(), nge[p.second[i]]) - p.second.begin() - i;\n }\n }\n\n return ans;\n }\n};", "memory": "198387" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n unordered_map<int, vector<int>> vec;\n int n = nums.size();\n for(int i=0;i<n;i++)\n vec[nums[i]].push_back(i);\n vector<int> nextgreater(n,INT_MAX);\n vector<pair<int,int>> st;\n for(int i=0;i<n;i++)\n {\n while(st.size() && st.back().first < nums[i])\n {\n nextgreater[st.back().second] = i;\n st.pop_back();\n }\n st.push_back({nums[i],i});\n }\n long long ans = 0;\n for(auto [a,b] : vec)\n {\n long long currtotal = 0;\n int next = nextgreater[b[0]];\n for(int i=0;i<b.size();i++)\n {\n if(next < b[i])\n {\n ans += (currtotal * (currtotal + 1)) / 2;\n next = nextgreater[b[i]];\n currtotal = 1;\n }\n else currtotal++;\n }\n if(next > b.back()) ans += (currtotal * (currtotal + 1)) / 2;\n }\n return ans;\n }\n};", "memory": "200312" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n int getcount(vector<int> v, int pos)\n {\n if(pos >= v.back()) return v.size();\n int ans = 0,l=0,r=v.size()-1,mid,val;\n while(l<=r)\n {\n mid = (l+r)/2;\n val = v[mid];\n if(pos < val) \n r = mid-1;\n else\n {\n ans = mid;\n l=mid+1;\n }\n }\n return ans+1;\n }\n long long numberOfSubarrays(vector<int>& nums) {\n unordered_map<int, vector<int>> vec;\n int n = nums.size();\n for(int i=0;i<n;i++)\n vec[nums[i]].push_back(i);\n vector<int> nextgreater(n,INT_MAX);\n vector<pair<int,int>> st;\n for(int i=0;i<n;i++)\n {\n while(st.size() && st.back().first < nums[i])\n {\n nextgreater[st.back().second] = i;\n st.pop_back();\n }\n st.push_back({nums[i],i});\n }\n long long ans = 0;\n for(auto [a,b] : vec)\n {\n long long currtotal = 0;\n int next = nextgreater[b[0]];\n for(int i=0;i<b.size();i++)\n {\n \n if(next < b[i])\n {\n ans += (currtotal * (currtotal + 1)) / 2;\n // cout<<\"Adding\"<<endl;\n next = nextgreater[b[i]];\n currtotal = 1;\n }\n else currtotal++;\n // cout<<a<<' '<<i<<' '<<b[i]<<' '<<currtotal<<' '<<next<<endl;\n }\n if(next > b.back())\n {\n // cout<<next<<' '<<b.back()<<' '<<currtotal<<endl;\n ans += (currtotal * (currtotal + 1)) / 2;\n }\n }\n return ans;\n }\n};", "memory": "202237" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\n typedef long long ll;\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n unordered_map<int,vector<int>>mp;\n for(int i=0;i<nums.size();i++) mp[nums[i]].push_back(i);\n int n=nums.size();\n vector<int>next(n,n);\n stack<int>st;\n for(int i=0;i<n;i++){\n while(st.size() and nums[st.top()]<nums[i]) next[st.top()]=i,st.pop();\n st.push(i);\n } \n ll ans=0;\n unordered_map<int,int>cnt;\n for(int i=0;i<n;i++){\n int r=next[i];\n r=lower_bound(mp[nums[i]].begin(),mp[nums[i]].end(),r)-mp[nums[i]].begin()-cnt[nums[i]];\n cnt[nums[i]]++;\n ans+=r;\n }\n return ans;\n }\n};", "memory": "204162" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "typedef int ll;\n\nint N;\nvector<ll> segtree;\n\nvoid pull(int t) {\n segtree[t] = max(segtree[2*t], segtree[2*t+1]);\n}\n\nvoid point_set(int idx, ll val, int L = 1, int R = N, int t = 1) {\n if (L == R) segtree[t] = val;\n else {\n int M = (L + R) / 2;\n if (idx <= M) point_set(idx, val, L, M, 2*t);\n else point_set(idx, val, M+1, R, 2*t+1);\n pull(t);\n }\n}\n\nll range_add(int left, int right, int L = 1, int R = N, int t = 1) {\n if (left <= L && R <= right) return segtree[t];\n else {\n int M = (L + R) / 2;\n ll ret = 0;\n if (left <= M) ret = max(ret,range_add(left, right, L, M, 2*t));\n if (right > M) ret = max(ret,range_add(left, right, M+1, R, 2*t+1));\n return ret;\n }\n}\n\nvoid build(vector<ll>& arr, int L = 1, int R = N, int t = 1) {\n if (L == R) segtree[t] = arr[L-1];\n else {\n int M = (L + R) / 2;\n build(arr, L, M, 2*t);\n build(arr, M+1, R, 2*t+1);\n pull(t);\n }\n}\n\n\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n N = nums.size();\n segtree = vector<ll>(4*N);\n build(nums);\n map<int,vector<int>> ocs;\n for (int i = 0; i < N; i++) {\n if (ocs.find(nums[i]) == ocs.end())\n ocs[nums[i]] = vector<int>();\n ocs[nums[i]].push_back(i+1);\n }\n long long ret = 0;\n for (auto p : ocs) {\n int t = p.first;\n int ct = 1;\n int pt = ocs[t][0];\n for (int i : ocs[t]) {\n if (i != pt)\n if (range_add(pt, i) > t) ct = 1;\n else ++ct;\n ret += ct;\n pt = i;\n }\n }\n return ret;\n }\n};", "memory": "206087" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n long long numberOfSubarrays(vector<int>& nums) {\n long long ans = 0;\n map<int, vector<int>> mp;\n for(int i=0; i<nums.size(); i++){\n if(mp.find(nums[i])==mp.end()){\n ans++;\n mp[nums[i]] = {i};\n \n auto itr = mp.lower_bound(nums[i]);\n mp.erase(mp.begin(), itr);\n }\n else{\n ans += mp[nums[i]].size()+1;\n mp[nums[i]].push_back(i);\n \n auto itr = mp.lower_bound(nums[i]);\n mp.erase(mp.begin(), itr);\n }\n }\n return ans;\n }\n};", "memory": "208012" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& v) {\n multiset<int> s;\n long long ans=0;\n map<int,int> m;\n for(int i=0; i<v.size(); i++){\n while(s.size() && *s.begin()<v[i]){\n m[*(s.begin())]--;\n s.erase(s.begin());\n }\n s.insert(v[i]);\n m[v[i]]++;\n ans+=m[v[i]];\n }\n return ans;\n }\n};", "memory": "209937" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "using ll = long long;\nusing pll = pair<ll, ll>;\n\nclass Solution {\npublic:\n void compress(vector<int>& arr) {\n ll n = arr.size();\n\n vector<pll> sorted(n);\n for (ll i = 0; i < n; i++) sorted[i] = {arr[i], i};\n sort(sorted.begin(), sorted.end());\n\n for (ll i = 0; i < n; i++)\n arr[sorted[i].second] = (i == 0) ? 0 : (arr[sorted[i - 1].second] + (sorted[i].first == sorted[i - 1].first));\n }\n\n long long numberOfSubarrays(vector<int>& nums) {\n ll n = nums.size();\n\n vector<ll> bigger_to_right(n);\n stack<ll> st;\n for (ll i = n - 1; i >= 0; i--) {\n while (!st.empty() && nums[st.top()] <= nums[i]) st.pop();\n\n if (st.empty()) bigger_to_right[i] = n;\n else bigger_to_right[i] = st.top();\n\n st.push(i);\n }\n\n ll ans = 0;\n\n vector<map<ll, ll>> remove(n);\n map<ll, ll> active;\n for (ll i = 0; i < n; i++) {\n for (const auto& rem : remove[i]) active[rem.first] -= rem.second;\n\n ++active[nums[i]];\n ans += active[nums[i]];\n\n if (bigger_to_right[i] < n) remove[bigger_to_right[i]][nums[i]]++;\n }\n\n return ans;\n }\n};", "memory": "211862" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "using ll = long long;\n\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n ll n = nums.size();\n\n vector<ll> bigger_to_right(n);\n stack<ll> st;\n for (ll i = n - 1; i >= 0; i--) {\n while (!st.empty() && nums[st.top()] <= nums[i]) st.pop();\n\n if (st.empty()) bigger_to_right[i] = n;\n else bigger_to_right[i] = st.top();\n\n st.push(i);\n }\n\n ll ans = 0;\n\n vector<map<ll, ll>> remove(n);\n map<ll, ll> active;\n for (ll i = 0; i < n; i++) {\n for (const auto& rem : remove[i]) active[rem.first] -= rem.second;\n\n ++active[nums[i]];\n ans += active[nums[i]];\n\n if (bigger_to_right[i] < n) remove[bigger_to_right[i]][nums[i]]++;\n }\n\n return ans;\n }\n};", "memory": "213787" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "using ll = long long;\nusing pll = pair<ll, ll>;\n\nclass Solution {\npublic:\n void compress(vector<int>& arr) {\n ll n = arr.size();\n\n vector<pll> sorted(n);\n for (ll i = 0; i < n; i++) sorted[i] = {arr[i], i};\n sort(sorted.begin(), sorted.end());\n\n for (ll i = 0; i < n; i++)\n arr[sorted[i].second] = (i == 0) ? 0 : (arr[sorted[i - 1].second] + (sorted[i].first == sorted[i - 1].first));\n }\n\n long long numberOfSubarrays(vector<int>& nums) {\n ll n = nums.size();\n\n vector<ll> bigger_to_right(n);\n stack<ll> st;\n for (ll i = n - 1; i >= 0; i--) {\n while (!st.empty() && nums[st.top()] <= nums[i]) st.pop();\n\n if (st.empty()) bigger_to_right[i] = n;\n else bigger_to_right[i] = st.top();\n\n st.push(i);\n }\n\n ll ans = 0;\n\n vector<map<ll, ll>> remove(n);\n map<ll, ll> active;\n for (ll i = 0; i < n; i++) {\n for (const auto& rem : remove[i]) active[rem.first] -= rem.second;\n\n ++active[nums[i]];\n ans += active[nums[i]];\n\n if (bigger_to_right[i] < n) remove[bigger_to_right[i]][nums[i]]++;\n }\n\n return ans;\n }\n};", "memory": "213787" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n map<int,vector<int>> mp;\n int n=nums.size(); \n for(int i=0;i<n;i++){\n mp[nums[i]].push_back(i);\n }\n\n vector<int> prevGr(n,-1);\n stack<int> st;\n for(int i = n-1;i>=0;i--){\n while(!st.empty() and nums[st.top()]<nums[i]){\n int idx = st.top();\n prevGr[idx] = i;\n st.pop();\n }\n st.push(i);\n }\n long long res = 0;\n for(auto it : mp){\n vector<int> v = it.second;\n int size = v.size();\n\n for(int i = 0 ; i < size ;i++){\n int prev = prevGr[v[i]];\n int low = 0;\n int high = i;\n int ans = 0;\n while(low<=high){\n int mid = (low+high)/2;\n if(v[mid] > prev ){\n high = mid-1;\n ans = mid;\n }\n else{\n low = mid+1;\n }\n }\n\n res += i - ans + 1;\n\n }\n }\n\n return res;\n }\n};", "memory": "215712" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "using ll = long long;\nusing pll = pair<ll, ll>;\n\nclass Solution {\npublic:\n void compress(vector<int>& arr) {\n ll n = arr.size();\n\n vector<pll> sorted(n);\n for (ll i = 0; i < n; i++) sorted[i] = {arr[i], i};\n sort(sorted.begin(), sorted.end());\n\n for (ll i = 0; i < n; i++)\n arr[sorted[i].second] = (i == 0) ? 0 : (arr[sorted[i - 1].second] + (sorted[i].first != sorted[i - 1].first));\n }\n\n long long numberOfSubarrays(vector<int>& nums) {\n ll n = nums.size();\n\n compress(nums);\n\n vector<ll> bigger_to_right(n);\n stack<ll> st;\n for (ll i = n - 1; i >= 0; i--) {\n while (!st.empty() && nums[st.top()] <= nums[i]) st.pop();\n\n if (st.empty()) bigger_to_right[i] = n;\n else bigger_to_right[i] = st.top();\n\n st.push(i);\n }\n\n ll ans = 0;\n\n vector<map<ll, ll>> remove(n);\n map<ll, ll> active;\n for (ll i = 0; i < n; i++) {\n for (const auto& rem : remove[i]) active[rem.first] -= rem.second;\n\n ++active[nums[i]];\n ans += active[nums[i]];\n\n if (bigger_to_right[i] < n) remove[bigger_to_right[i]][nums[i]]++;\n }\n\n return ans;\n }\n};", "memory": "223412" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n\nusing namespace std;\nusing namespace __gnu_pbds;\n\n// Define ordered_set using PBDS\n#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>\n\nclass Solution {\npublic:\n long long numberOfSubarrays(vector<int>& nums) {\n vector<int> left(nums.size(), 0);\n stack<int> st;\n st.push(-1);\n for (int i = 0; i < nums.size(); i++) {\n int id = st.top();\n while (id != -1 && nums[id] <= nums[i]) {\n st.pop();\n id = st.top();\n }\n st.push(i);\n left[i] = id;\n }\n\n long long ans = 0;\n unordered_map<int, ordered_set> mp;\n for (int i = 0; i < nums.size(); i++) {\n int nearestleft = left[i];\n long long cnt = mp[nums[i]].size() - mp[nums[i]].order_of_key(nearestleft);\n ans += cnt + 1;\n mp[nums[i]].insert(i);\n }\n return ans;\n }\n};\n", "memory": "225337" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long Calc(vector<array<int,2>> &vseg,vector<int> &vi)\n {\n long long ans=0;\n int n=vi.size(),i=0;\n vector<array<int,2>> nseg;\n for(auto [le,ri]:vseg)\n {\n int cnt=0;\n for(;i<n && vi[i]<=ri;++i)\n {\n cnt+=1;\n if(le<=vi[i]-1) nseg.push_back({le,vi[i]-1});\n le=vi[i]+1;\n }\n if(le<=ri) nseg.push_back({le,ri});\n ans+=cnt*(cnt-1LL)/2;\n }\n swap(vseg,nseg);\n return ans;\n }\n long long numberOfSubarrays(vector<int>& nums) {\n int n=nums.size();\n map<int,vector<int>> mvi;\n for(int i=0;i<n;++i)\n {\n mvi[nums[i]].push_back(i);\n }\n vector<array<int,2>> vseg(1,{0,n-1});\n long long ans=n;\n for(auto it=mvi.rbegin();it!=mvi.rend();++it)\n {\n ans+=Calc(vseg,it->second);\n }\n return ans;\n }\n};", "memory": "227262" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "typedef long long ll;\n\nclass SegmentTree\n{\n\nprivate:\n\n vector<ll> seg;\n\npublic:\n\n SegmentTree(ll n)\n {\n seg.resize(4*n+1);\n }\n\n void build(ll ind,ll lo,ll hi,vector<ll> &a)\n {\n if(lo==hi)\n {\n seg[ind]=a[lo];\n return;\n }\n ll mid=lo+(hi-lo)/2;\n build(2*ind+1,lo,mid,a);\n build(2*ind+2,mid+1,hi,a);\n seg[ind]=max(seg[2*ind+1],seg[2*ind+2]);\n }\n\n ll query(ll ind,ll lo,ll hi,ll l,ll r)\n {\n if(l>hi||lo>r)\n {\n return -1e18;\n }\n if(l<=lo&&hi<=r)\n {\n return seg[ind];\n }\n ll mid=lo+(hi-lo)/2;\n return max(query(2*ind+1,lo,mid,l,r),query(2*ind+2,mid+1,hi,l,r));\n }\n\n};\n\ninline ll calc(ll n)\n{\n return (n*(n+1))/2;\n}\n\n\nclass Solution {\npublic:\nll numberOfSubarrays(vector<int> &nums) \n{\n ll n=(ll)nums.size(),ans=0;\n vector<ll> a;\n map<ll,vector<ll>> indices;\n for(auto &v:nums)\n {\n a.push_back((ll)v);\n }\n for(ll i=0;i<n;++i)\n {\n indices[a[i]].push_back(i);\n }\n SegmentTree st(n);\n st.build(0,0,n-1,a);\n for(auto &val:indices)\n {\n ll cnt=1,num=val.first,cur_idx=val.second[0];\n vector<ll> tmp=val.second;\n for(ll i=1;i<tmp.size();++i)\n {\n if(st.query(0,0,n-1,cur_idx,tmp[i])==num)\n {\n cnt++;\n }\n else\n {\n ans+=calc(cnt);\n cnt=1;\n cur_idx=tmp[i];\n }\n }\n ans+=calc(cnt);\n }\n return ans;\n}\n};", "memory": "229187" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n #define ll long long\n #define vi vector<int> \n #define pii pair<int , int>\n #define dbg(x) cout << #x << \" : \" << x << endl\n long long numberOfSubarrays(vector<int>& a) {\n ll ans = 0;\n\n // nice one\n\n // iterate over the numbers in a reverse order\n // iterate through its indices, then group indices which \n // do not have any bigger number in between... add to final ans, done!\n // use a set to keep track of the indices of the bigger elements\n\n int n = a.size();\n\n map<int , vi> mpp; \n for(int i=0;i<n;i++){\n mpp[a[i]].push_back(i);\n }\n\n set<int> s; // idx that are greater than num\n for(auto it = mpp.rbegin() ; it != mpp.rend() ; it++){\n int num = ((*it).first);\n vi idx = ((*it).second);\n\n if(s.size() == 0){\n int add = (idx.size() * (idx.size() + 1))/2;\n // dbg(add);\n ans += (idx.size() * (idx.size() + 1))/2;\n for(int i=0;i<idx.size();i++){\n s.insert(idx[i]);\n assert(a[idx[i]] == num);\n }\n continue;\n }\n\n int add = 0;\n for(int i=0;i<idx.size();i++){\n auto iter = s.lower_bound(idx[i]);\n assert(a[idx[i]] == num);\n\n if(iter == s.end()){\n // means idx[i] .. idx[idx.size()-1] are all in one group\n int add = ((idx.size()-i) * (idx.size()-i+1)) / 2;\n // dbg(add);\n ans += add;\n break;\n }\n \n int cap = (*iter);\n\n assert(idx[i] < cap);\n\n // dbg(cap);\n\n int j = i;\n while(1){\n if(j < idx.size() && idx[j] < cap){\n j++;\n }\n else{\n break;\n }\n }\n j--;\n add += ((j-i+1) * (j-i+2))/2;\n i = j;\n }\n\n // dbg(add);\n ans += add;\n\n for(int i=0;i<idx.size();i++){\n s.insert(idx[i]);\n }\n }\n return ans;\n }\n};", "memory": "231112" }
3,382
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>Return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>, where the <strong>first</strong> and the <strong>last</strong> elements of the subarray are <em>equal</em> to the <strong>largest</strong> element in the subarray.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<strong><u>1</u></strong>,4,3,3,2]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</li> <li>subarray <code>[1,<u><strong>4</strong></u>,3,3,2]</code>, with its largest element 4. The first element is 4 and the last element is also 4.</li> <li>subarray <code>[1,4,<u><strong>3</strong></u>,3,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,<u><strong>3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[1,4,3,3,<u><strong>2</strong></u>]</code>, with its largest element 2. The first element is 2 and the last element is also 2.</li> <li>subarray <code>[1,4,<u><strong>3,3</strong></u>,2]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 6 subarrays which have the first and the last elements equal to the largest element of the subarray:</p> <ul> <li>subarray <code>[<u><strong>3</strong></u>,3,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<strong><u>3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,3,<u><strong>3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<strong><u>3,3</u></strong>,3]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[3,<u><strong>3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> <li>subarray <code>[<u><strong>3,3,3</strong></u>]</code>, with its largest element 3. The first element is 3 and the last element is also 3.</li> </ul> <p>Hence, we return 6.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>There is a single subarray of <code>nums</code> which is <code>[<strong><u>1</u></strong>]</code>, with its largest element 1. The first element is 1 and the last element is also 1.</p> <p>Hence, we return 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n #define ll long long\n #define vi vector<int> \n #define pii pair<int , int>\n #define dbg(x) cout << #x << \" : \" << x << endl\n long long numberOfSubarrays(vector<int>& a) {\n ll ans = 0;\n\n // nice one\n int n = a.size();\n\n map<int , vi> mpp; \n for(int i=0;i<n;i++){\n mpp[a[i]].push_back(i);\n }\n\n set<int> s; // idx that are greater than num\n for(auto it = mpp.rbegin() ; it != mpp.rend() ; it++){\n int num = ((*it).first);\n vi idx = ((*it).second);\n\n // dbg(num);\n\n // cout << \"idx : \";\n // for(int x : idx){\n // cout << x << \" \";\n // }\n // cout << endl;\n\n if(s.size() == 0){\n int add = (idx.size() * (idx.size() + 1))/2;\n // dbg(add);\n ans += (idx.size() * (idx.size() + 1))/2;\n for(int i=0;i<idx.size();i++){\n s.insert(idx[i]);\n assert(a[idx[i]] == num);\n }\n continue;\n }\n\n int add = 0;\n for(int i=0;i<idx.size();i++){\n auto iter = s.lower_bound(idx[i]);\n assert(a[idx[i]] == num);\n\n if(iter == s.end()){\n // means idx[i] .. idx[idx.size()-1] are all in one group\n int add = ((idx.size()-i) * (idx.size()-i+1)) / 2;\n // dbg(add);\n ans += add;\n break;\n }\n \n int cap = (*iter);\n\n assert(idx[i] < cap);\n\n // dbg(cap);\n\n int j = i;\n while(1){\n if(j < idx.size() && idx[j] < cap){\n j++;\n }\n else{\n break;\n }\n }\n j--;\n add += ((j-i+1) * (j-i+2))/2;\n i = j;\n }\n\n // dbg(add);\n ans += add;\n\n for(int i=0;i<idx.size();i++){\n s.insert(idx[i]);\n }\n }\n return ans;\n }\n};", "memory": "233037" }
3,361
<p>You are given a string <code>s</code> representing a 12-hour format time where some of the digits (possibly none) are replaced with a <code>&quot;?&quot;</code>.</p> <p>12-hour times are formatted as <code>&quot;HH:MM&quot;</code>, where <code>HH</code> is between <code>00</code> and <code>11</code>, and <code>MM</code> is between <code>00</code> and <code>59</code>. The earliest 12-hour time is <code>00:00</code>, and the latest is <code>11:59</code>.</p> <p>You have to replace <strong>all</strong> the <code>&quot;?&quot;</code> characters in <code>s</code> with digits such that the time we obtain by the resulting string is a <strong>valid</strong> 12-hour format time and is the <strong>latest</strong> possible.</p> <p>Return <em>the resulting string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1?:?4&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11:54&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;11:54&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0?:5?&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;09:59&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;09:59&quot;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>s.length == 5</code></li> <li><code>s[2]</code> is equal to the character <code>&quot;:&quot;</code>.</li> <li>All characters except <code>s[2]</code> are digits or <code>&quot;?&quot;</code> characters.</li> <li>The input is generated such that there is <strong>at least</strong> one time between <code>&quot;00:00&quot;</code> and <code>&quot;11:59&quot;</code> that you can obtain after replacing the <code>&quot;?&quot;</code> characters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string findLatestTime(string s) {\n for(int i=0;i<s.size();i++){\n if(s[i]=='?'){\n if(i==0 && (s[i+1]-48<2 || s[i+1]-48==15))\n s[i]='1';\n else if(i==0)\n s[i]='0';\n else if(i==1 && s[i-1]-48==0)\n s[i]='9';\n else if(i==1 )\n s[i]='1';\n else if(i==3)\n s[i]='5';\n else if(i==4)\n s[i]='9';\n\n }\n }\n return s;\n }\n};", "memory": "7500" }
3,361
<p>You are given a string <code>s</code> representing a 12-hour format time where some of the digits (possibly none) are replaced with a <code>&quot;?&quot;</code>.</p> <p>12-hour times are formatted as <code>&quot;HH:MM&quot;</code>, where <code>HH</code> is between <code>00</code> and <code>11</code>, and <code>MM</code> is between <code>00</code> and <code>59</code>. The earliest 12-hour time is <code>00:00</code>, and the latest is <code>11:59</code>.</p> <p>You have to replace <strong>all</strong> the <code>&quot;?&quot;</code> characters in <code>s</code> with digits such that the time we obtain by the resulting string is a <strong>valid</strong> 12-hour format time and is the <strong>latest</strong> possible.</p> <p>Return <em>the resulting string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1?:?4&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11:54&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;11:54&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0?:5?&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;09:59&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;09:59&quot;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>s.length == 5</code></li> <li><code>s[2]</code> is equal to the character <code>&quot;:&quot;</code>.</li> <li>All characters except <code>s[2]</code> are digits or <code>&quot;?&quot;</code> characters.</li> <li>The input is generated such that there is <strong>at least</strong> one time between <code>&quot;00:00&quot;</code> and <code>&quot;11:59&quot;</code> that you can obtain after replacing the <code>&quot;?&quot;</code> characters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string findLatestTime(string time) {\n string result = \"\";\n char leadHour = time[0];\n char subHour = time[1];\n\n char leadMinute = time[3];\n char subMinute = time[4];\n\n if (leadHour == '?') {\n if (subHour == '0' || subHour == '1' || subHour == '?') {\n result += '1';\n } else {\n result += '0';\n }\n } else {\n result += leadHour;\n }\n\n if (subHour == '?') {\n if (leadHour == '?' || leadHour == '1') {\n result += '1';\n } else {\n result += '9';\n }\n } else {\n result += subHour;\n }\n\n result += ':';\n\n if (leadMinute == '?') {\n result += '5';\n } else {\n result += leadMinute;\n }\n\n if (subMinute == '?') {\n result += '9';\n } else {\n result += subMinute;\n }\n\n return result;\n }\n};\n", "memory": "7600" }
3,361
<p>You are given a string <code>s</code> representing a 12-hour format time where some of the digits (possibly none) are replaced with a <code>&quot;?&quot;</code>.</p> <p>12-hour times are formatted as <code>&quot;HH:MM&quot;</code>, where <code>HH</code> is between <code>00</code> and <code>11</code>, and <code>MM</code> is between <code>00</code> and <code>59</code>. The earliest 12-hour time is <code>00:00</code>, and the latest is <code>11:59</code>.</p> <p>You have to replace <strong>all</strong> the <code>&quot;?&quot;</code> characters in <code>s</code> with digits such that the time we obtain by the resulting string is a <strong>valid</strong> 12-hour format time and is the <strong>latest</strong> possible.</p> <p>Return <em>the resulting string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1?:?4&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11:54&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;11:54&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0?:5?&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;09:59&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;09:59&quot;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>s.length == 5</code></li> <li><code>s[2]</code> is equal to the character <code>&quot;:&quot;</code>.</li> <li>All characters except <code>s[2]</code> are digits or <code>&quot;?&quot;</code> characters.</li> <li>The input is generated such that there is <strong>at least</strong> one time between <code>&quot;00:00&quot;</code> and <code>&quot;11:59&quot;</code> that you can obtain after replacing the <code>&quot;?&quot;</code> characters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string findLatestTime(string s) {\n if(s[0]=='?')\n {\n if(s[1]=='?') s[0]='1';\n else if(s[1]-'0'>1) s[0]='0';\n else s[0]='1';\n }\n if(s[1]=='?')\n {\n if(s[0]=='0') s[1]='9';\n else s[1]='1';\n }\n if(s[3]=='?')\n {\n s[3]='5';\n }\n if(s[4]=='?')\n {\n s[4]='9';\n }\n return s;\n }\n};", "memory": "7700" }
3,361
<p>You are given a string <code>s</code> representing a 12-hour format time where some of the digits (possibly none) are replaced with a <code>&quot;?&quot;</code>.</p> <p>12-hour times are formatted as <code>&quot;HH:MM&quot;</code>, where <code>HH</code> is between <code>00</code> and <code>11</code>, and <code>MM</code> is between <code>00</code> and <code>59</code>. The earliest 12-hour time is <code>00:00</code>, and the latest is <code>11:59</code>.</p> <p>You have to replace <strong>all</strong> the <code>&quot;?&quot;</code> characters in <code>s</code> with digits such that the time we obtain by the resulting string is a <strong>valid</strong> 12-hour format time and is the <strong>latest</strong> possible.</p> <p>Return <em>the resulting string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1?:?4&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11:54&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;11:54&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0?:5?&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;09:59&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;09:59&quot;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>s.length == 5</code></li> <li><code>s[2]</code> is equal to the character <code>&quot;:&quot;</code>.</li> <li>All characters except <code>s[2]</code> are digits or <code>&quot;?&quot;</code> characters.</li> <li>The input is generated such that there is <strong>at least</strong> one time between <code>&quot;00:00&quot;</code> and <code>&quot;11:59&quot;</code> that you can obtain after replacing the <code>&quot;?&quot;</code> characters.</li> </ul>
0
{ "code": "\nclass Solution {\npublic:\n\tstring findLatestTime(string& s) {\n\t\tfor (int i = 0; i < s.length(); i++) {\n\t\t\tif (s[i] == '?') {\n\t\t\t\tif (i == 0) {\n\t\t\t\t\tif (s[1] == '?' || s[1] - '0' < 2) s[0] = '1';\n\t\t\t\t\telse s[0] = '0';\n\t\t\t\t}\n\t\t\t\telse if (i == 1) s[0] == '1' ? s[1] = '1' : s[1] = '9';\n\t\t\t\telse if (i == 3) s[3] = '5';\n\t\t\t\telse if (i == 4) s[4] = '9';\n\t\t\t}\n\t\t}\n\t\treturn s;\n\t}\n};\n", "memory": "7800" }
3,361
<p>You are given a string <code>s</code> representing a 12-hour format time where some of the digits (possibly none) are replaced with a <code>&quot;?&quot;</code>.</p> <p>12-hour times are formatted as <code>&quot;HH:MM&quot;</code>, where <code>HH</code> is between <code>00</code> and <code>11</code>, and <code>MM</code> is between <code>00</code> and <code>59</code>. The earliest 12-hour time is <code>00:00</code>, and the latest is <code>11:59</code>.</p> <p>You have to replace <strong>all</strong> the <code>&quot;?&quot;</code> characters in <code>s</code> with digits such that the time we obtain by the resulting string is a <strong>valid</strong> 12-hour format time and is the <strong>latest</strong> possible.</p> <p>Return <em>the resulting string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1?:?4&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11:54&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;11:54&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0?:5?&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;09:59&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;09:59&quot;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>s.length == 5</code></li> <li><code>s[2]</code> is equal to the character <code>&quot;:&quot;</code>.</li> <li>All characters except <code>s[2]</code> are digits or <code>&quot;?&quot;</code> characters.</li> <li>The input is generated such that there is <strong>at least</strong> one time between <code>&quot;00:00&quot;</code> and <code>&quot;11:59&quot;</code> that you can obtain after replacing the <code>&quot;?&quot;</code> characters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string findLatestTime(string time) {\n // Fix hour\n if (time[0] == '?') {\n if (time[1] == '?' || time[1] <= '1') {\n time[0] = '1'; // If second digit is '?' or between '0' and '1', max is 11:xx\n } else {\n time[0] = '0'; // If second digit is '2' or greater, max is 09:xx\n }\n }\n\n if (time[1] == '?') {\n if (time[0] == '1') {\n time[1] = '1'; // max is 11:xx\n } else {\n time[1] = '9'; // max is 09:xx\n }\n }\n\n // Fix minute\n if (time[3] == '?') {\n time[3] = '5'; // max is xx:59\n }\n \n if (time[4] == '?') {\n time[4] = '9'; // max is xx:59\n }\n\n return time;\n }\n};\n", "memory": "7900" }
3,361
<p>You are given a string <code>s</code> representing a 12-hour format time where some of the digits (possibly none) are replaced with a <code>&quot;?&quot;</code>.</p> <p>12-hour times are formatted as <code>&quot;HH:MM&quot;</code>, where <code>HH</code> is between <code>00</code> and <code>11</code>, and <code>MM</code> is between <code>00</code> and <code>59</code>. The earliest 12-hour time is <code>00:00</code>, and the latest is <code>11:59</code>.</p> <p>You have to replace <strong>all</strong> the <code>&quot;?&quot;</code> characters in <code>s</code> with digits such that the time we obtain by the resulting string is a <strong>valid</strong> 12-hour format time and is the <strong>latest</strong> possible.</p> <p>Return <em>the resulting string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1?:?4&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11:54&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;11:54&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0?:5?&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;09:59&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;09:59&quot;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>s.length == 5</code></li> <li><code>s[2]</code> is equal to the character <code>&quot;:&quot;</code>.</li> <li>All characters except <code>s[2]</code> are digits or <code>&quot;?&quot;</code> characters.</li> <li>The input is generated such that there is <strong>at least</strong> one time between <code>&quot;00:00&quot;</code> and <code>&quot;11:59&quot;</code> that you can obtain after replacing the <code>&quot;?&quot;</code> characters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string findLatestTime(string s) {\n if (s[0] == '?') \n {\n if (s[1] != '0' && s[1] != '1' && s[1] != '?')\n s[0] = '0';\n else \n s[0] = '1';\n }\n if (s[1] == '?') \n {\n if (s[0] == '1')\n s[1] = '1';\n else\n s[1] = '9';\n }\n if (s[3] == '?')\n s[3] = '5';\n if (s[4] == '?')\n s[4] = '9';\n return s;\n }\n};", "memory": "7900" }
3,361
<p>You are given a string <code>s</code> representing a 12-hour format time where some of the digits (possibly none) are replaced with a <code>&quot;?&quot;</code>.</p> <p>12-hour times are formatted as <code>&quot;HH:MM&quot;</code>, where <code>HH</code> is between <code>00</code> and <code>11</code>, and <code>MM</code> is between <code>00</code> and <code>59</code>. The earliest 12-hour time is <code>00:00</code>, and the latest is <code>11:59</code>.</p> <p>You have to replace <strong>all</strong> the <code>&quot;?&quot;</code> characters in <code>s</code> with digits such that the time we obtain by the resulting string is a <strong>valid</strong> 12-hour format time and is the <strong>latest</strong> possible.</p> <p>Return <em>the resulting string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1?:?4&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11:54&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;11:54&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0?:5?&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;09:59&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;09:59&quot;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>s.length == 5</code></li> <li><code>s[2]</code> is equal to the character <code>&quot;:&quot;</code>.</li> <li>All characters except <code>s[2]</code> are digits or <code>&quot;?&quot;</code> characters.</li> <li>The input is generated such that there is <strong>at least</strong> one time between <code>&quot;00:00&quot;</code> and <code>&quot;11:59&quot;</code> that you can obtain after replacing the <code>&quot;?&quot;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string findLatestTime(string s) {\n string ans;\n if(s[0] == '?') {\n s[0] = (s[1] == '?' || s[1] < '2') ? '1' : '0';\n }\n if(s[1] == '?') {\n s[1] = (s[0] == '1') ? '1' : '9';\n }\n if(s[3] == '?') {\n s[3] = '5';\n }\n if(s[4] == '?') {\n s[4] = '9';\n }\n return s;\n }\n};\n// Hint 1\n// Try using a brute force approach.\n// Hint 2\n// Iterate over all possible times that can be generated from the string and find the latest one.", "memory": "8000" }
3,361
<p>You are given a string <code>s</code> representing a 12-hour format time where some of the digits (possibly none) are replaced with a <code>&quot;?&quot;</code>.</p> <p>12-hour times are formatted as <code>&quot;HH:MM&quot;</code>, where <code>HH</code> is between <code>00</code> and <code>11</code>, and <code>MM</code> is between <code>00</code> and <code>59</code>. The earliest 12-hour time is <code>00:00</code>, and the latest is <code>11:59</code>.</p> <p>You have to replace <strong>all</strong> the <code>&quot;?&quot;</code> characters in <code>s</code> with digits such that the time we obtain by the resulting string is a <strong>valid</strong> 12-hour format time and is the <strong>latest</strong> possible.</p> <p>Return <em>the resulting string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1?:?4&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11:54&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;11:54&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0?:5?&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;09:59&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;09:59&quot;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>s.length == 5</code></li> <li><code>s[2]</code> is equal to the character <code>&quot;:&quot;</code>.</li> <li>All characters except <code>s[2]</code> are digits or <code>&quot;?&quot;</code> characters.</li> <li>The input is generated such that there is <strong>at least</strong> one time between <code>&quot;00:00&quot;</code> and <code>&quot;11:59&quot;</code> that you can obtain after replacing the <code>&quot;?&quot;</code> characters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string findLatestTime(string s) {\n if (s[0] == '?' && (s[1] == '1' || s[1] == '0' || s[1] == '?')) {\n s[0] = '1';\n }\n else if (s[0] == '?') {\n s[0] = '0';\n }\n\n if (s[1] == '?' && s[0] == '0') {\n s[1] = '9';\n }\n else if (s[1] == '?') {\n s[1] = '1';\n }\n\n if (s[3] == '?') {\n s[3] = '5';\n }\n if (s[4] == '?') {\n s[4] = '9';\n }\n\n return s;\n }\n};", "memory": "8000" }
3,361
<p>You are given a string <code>s</code> representing a 12-hour format time where some of the digits (possibly none) are replaced with a <code>&quot;?&quot;</code>.</p> <p>12-hour times are formatted as <code>&quot;HH:MM&quot;</code>, where <code>HH</code> is between <code>00</code> and <code>11</code>, and <code>MM</code> is between <code>00</code> and <code>59</code>. The earliest 12-hour time is <code>00:00</code>, and the latest is <code>11:59</code>.</p> <p>You have to replace <strong>all</strong> the <code>&quot;?&quot;</code> characters in <code>s</code> with digits such that the time we obtain by the resulting string is a <strong>valid</strong> 12-hour format time and is the <strong>latest</strong> possible.</p> <p>Return <em>the resulting string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1?:?4&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;11:54&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;11:54&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0?:5?&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;09:59&quot;</span></p> <p><strong>Explanation:</strong> The latest 12-hour format time we can achieve by replacing <code>&quot;?&quot;</code> characters is <code>&quot;09:59&quot;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>s.length == 5</code></li> <li><code>s[2]</code> is equal to the character <code>&quot;:&quot;</code>.</li> <li>All characters except <code>s[2]</code> are digits or <code>&quot;?&quot;</code> characters.</li> <li>The input is generated such that there is <strong>at least</strong> one time between <code>&quot;00:00&quot;</code> and <code>&quot;11:59&quot;</code> that you can obtain after replacing the <code>&quot;?&quot;</code> characters.</li> </ul>
3
{ "code": "\n\n #include <string>\nusing namespace std;\n\nclass Solution {\npublic:\n string findLatestTime(string timeString) {\n int a=8678,b=8678,c=846;\n // If hours tens and ones places are both '?', set them to '11'\n if (timeString[0] == '?' && timeString[1] == '?') {\n timeString = \"11\" + timeString.substr(2);\n }\n // If hour tens place is '?', determine based on hour ones place\n else if (timeString[0] == '?') {\n if (timeString[1] == '0' || timeString[1] == '1') {\n timeString[0] = '1';\n } else {\n timeString[0] = '0';\n }\n }\n // If hour ones place is '?', determine based on hour tens place\n else if (timeString[1] == '?') {\n if (timeString[0] == '0') {\n timeString[1] = '9';\n } else {\n timeString[1] = '1';\n }\n }\n // If minutes tens place is '?', set it to '5'\n if (timeString[3] == '?') {\n timeString[3] = '5';\n }\n // If minutes ones place is '?', set it to '9'\n if (timeString[4] == '?') {\n timeString[4] = '9';\n }\n return timeString;\n }\n};\n\n \n ", "memory": "8100" }
3,373
<p>You are given an integer array <code>nums</code>.</p> <p>Return an integer that is the <strong>maximum</strong> distance between the <strong>indices</strong> of two (not necessarily different) prime numbers in <code>nums</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,5,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> <code>nums[1]</code>, <code>nums[3]</code>, and <code>nums[4]</code> are prime. So the answer is <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,2,8]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> <code>nums[2]</code> is prime. Because there is just one prime number, the answer is <code>|2 - 2| = 0</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> <li>The input is generated such that the number of prime numbers in the <code>nums</code> is at least one.</li> </ul>
0
{ "code": "class Solution {\npublic:\n \n bool isPrime(int n){\n if (n <= 1)\n return false;\n for (int i = 2; i <= n / 2; i++)\n if (n % i == 0)\n return false;\n\n return true;\n }\n int maximumPrimeDifference(vector<int>& nums) {\n int front = 0 ;\n int n = nums.size();\n int back = n-1 ;\n while(true){\n if(front == n) break ;\n if(isPrime(nums[front])){\n break ;\n }\n front++;\n }\n while(true){\n if(back < 0){\n break ;\n }\n if(isPrime(nums[back])){\n break ;\n }\n back--;\n }\n return back-front;\n }\n};", "memory": "107400" }