diff --git a/Binary_Search/3677.Count-Binary-Palindromic-Numbers/Readme.md b/Binary_Search/3677.Count-Binary-Palindromic-Numbers/Readme.md index b6a8b4f5b..1dbc9d5a4 100644 --- a/Binary_Search/3677.Count-Binary-Palindromic-Numbers/Readme.md +++ b/Binary_Search/3677.Count-Binary-Palindromic-Numbers/Readme.md @@ -2,5 +2,5 @@ 假设n的二进制长度是maxLen,那么对于任何1<=lmx`的情况。 +接下来考虑L=maxLen,且"小于等于n"的二进制回文数。比较简单的处理就是用二分搜值。计算`h=(L+1)/2`,然后"前半段"有下限`mn=1<<(h-1)`,上限是`mx=(1<mx`的情况,所以需要对收敛的解做二次验证。 diff --git a/Greedy/2749.Minimum-Operations-to-Make-the-Integer-Zero/2749.Minimun-Operations-to-make-the-integer-Zero-PYTHON b/Greedy/2749.Minimum-Operations-to-Make-the-Integer-Zero/2749.Minimun-Operations-to-make-the-integer-Zero-PYTHON new file mode 100644 index 000000000..820e251bf --- /dev/null +++ b/Greedy/2749.Minimum-Operations-to-Make-the-Integer-Zero/2749.Minimun-Operations-to-make-the-integer-Zero-PYTHON @@ -0,0 +1,17 @@ +class Solution(object): + def makeTheIntegerZero(self,num1,num2): + x = num1 + y = num2 + k = 1 + + while True: + x = x - y + if x < k: + return -1 + + if bin(x).count('1') <= k: + return k + + k = k + 1 + +# I hope this can help anyone whos resolving this huge problem on python ;) diff --git a/Readme.md b/Readme.md index b6c812625..063a42457 100644 --- a/Readme.md +++ b/Readme.md @@ -450,6 +450,7 @@ [2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String](https://github.com/wisdompeak/LeetCode/tree/master/Stack/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String) (H-) [2454.Next-Greater-Element-IV](https://github.com/wisdompeak/LeetCode/tree/master/Stack/2454.Next-Greater-Element-IV) (H-) [3113.Find-the-Number-of-Subarrays-Where-Boundary-Elements-Are-Maximum](https://github.com/wisdompeak/LeetCode/tree/master/Stack/3113.Find-the-Number-of-Subarrays-Where-Boundary-Elements-Are-Maximum) (M) +[3676.Count-Bowl-Subarrays](https://github.com/wisdompeak/LeetCode/tree/master/Stack/3676.Count-Bowl-Subarrays) (M+) * ``monotonic stack: other usages`` [084.Largest-Rectangle-in-Histogram](https://github.com/wisdompeak/LeetCode/tree/master/Stack/084.Largest-Rectangle-in-Histogram) (H) [2334.Subarray-With-Elements-Greater-Than-Varying-Threshold](https://github.com/wisdompeak/LeetCode/tree/master/Stack/2334.Subarray-With-Elements-Greater-Than-Varying-Threshold) (M+) diff --git a/Stack/3676.Count-Bowl-Subarrays/3676.Count-Bowl-Subarrays.cpp b/Stack/3676.Count-Bowl-Subarrays/3676.Count-Bowl-Subarrays.cpp new file mode 100644 index 000000000..50239989f --- /dev/null +++ b/Stack/3676.Count-Bowl-Subarrays/3676.Count-Bowl-Subarrays.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + long long bowlSubarrays(vector& nums) { + int n = nums.size(); + vectornextGreater(n, -1); + vectorprevGreater(n, -1); + vectorst; + for (int i=0; i=0; i--) { + while (!st.empty() && nums[st.back()]=2) ret++; + if (nextGreater[i]!=-1 && nextGreater[i]-i>=2) ret++; + } + return ret; + } +}; diff --git a/Stack/3676.Count-Bowl-Subarrays/Readme.md b/Stack/3676.Count-Bowl-Subarrays/Readme.md new file mode 100644 index 000000000..066f63299 --- /dev/null +++ b/Stack/3676.Count-Bowl-Subarrays/Readme.md @@ -0,0 +1,7 @@ +### 3676.Count-Bowl-Subarrays + +非常有趣的题目。 + +我们很容易想到,对于每个nums[i]考虑其作为一个端点时,另一个端点应该在哪些位置。我们不妨认为nums[i]是左边且较低的端点,那么我们很容易找到对应的next greater element,比如说j,这是下一个可以作为右端点的位置,而这恰恰也是它所对应的唯一的右端点。如果再往右寻找右端点,那么j处在bowl内部就高于了左端点,不符合条件。 + +于是我们就可以得出结论,对于每个nums[i],只有唯一的next greater element可以配对为右边且更高的端点。同理,对于每个nums[i],只有唯一的prev greater element可以配对为左边且更高的端点。这样我们就枚举除了所有的bowl的形状。

AltStyle によって変換されたページ (->オリジナル) /