classSolution: defsumSubarrayMins(self, arr: List[int]) -> int: mod = 10 ** 9 + 7 n = len(arr) left, right = [-1] * n, [n] * n st = [] i = 0 for i, x inenumerate(arr): while st and arr[st[-1]] <= x: right[st.pop()] = i if st: left[i] = st[-1] st.append(x) res = sum(a * (i - l) * (r - i) for i, (l, r) inenumerate(zip(left, right))) return res
classSolution: defsecondGreaterElement(self, nums: List[int]) -> List[int]: n = len(nums) st1 = [] st2 = [] ans = [-1] * n for i, x inenumerate(nums): while st2 and nums[st2[-1]] < x: ans[st2.pop()] = x j = len(st1) - 1 while j >= 0and nums[st1[j]] < x: j -= 1 st2 += st1[j + 1:] del st1[j + 1:] st1.append(i) return ans
classSolution { public: vector<int> mostCompetitive(vector<int>& nums, int k) { int n = nums.size(); int last = n - k; vector<int>st; for (int i = 0; i < n; i++) { while (!st.empty() && last > 0 && st.back() > nums[i]) { st.pop(); last--; } } while(last--) st.pop(); return st; } };
classSolution { public: longlongcountSubarrays(vector<int>& nums, longlong k) { int n = nums.size(); longlong ans = 0L; int l = 0; longlong sum = 0L; for (int r = 0; r < n; r++) { sum += nums[r]; while (sum * (r - l + 1) >= k) // zhu'yi'shi { sum -= nums[l++]; } ans += r - l + 1; } return ans;