Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 7 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Sep 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

假设n的二进制长度是maxLen,那么对于任何1<=L<maxLen的二进制回文数自然都符合"小于n"的条件。这些二进制回文数与它的"前半段"有一一对应的关系。也就是说,我们遍历所有的"前半段",就能构造出所有长度为L的二进制回文数。举个例子,如果L=8,它一半的长度h=4,那么我们遍历1000\~1111,将它们各自翻转拼接,就对应了所有长度为8的二进制回文数。注意,如果L=7,它一半的长度h也是4,我们同样遍历1000~1111,将它们各自翻转拼接(但是有一个中轴位),它们依然能对应所有长度为7的二进制回文数。

接下来考虑L=maxLen,且"小于等于n"的二进制回文数。比较简单的处理就是用二分搜值。计算`h=(L+1)/2`,然后"前半段"有下限`mn=1<<(h-1)`,上限是`mx=(1<<h)-1`,我们二分搜值找到最大的、小于等于n的数m,那么`m-mn+1`就是答案。但是注意,存在并没有解的可能。比如h=1时,就有`mn>mx`的情况。
接下来考虑L=maxLen,且"小于等于n"的二进制回文数。比较简单的处理就是用二分搜值。计算`h=(L+1)/2`,然后"前半段"有下限`mn=1<<(h-1)`,上限是`mx=(1<<h)-1`,我们二分搜值找到最大的、小于等于n的数m,那么`m-mn+1`就是答案。但是注意,存在并没有解的可能。比如h=1时,就有`mn>mx`的情况,所以需要对收敛的解做二次验证

Original file line number Diff line number Diff line change
@@ -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 ;)
1 change: 1 addition & 0 deletions Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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+)
Expand Down
28 changes: 28 additions & 0 deletions Stack/3676.Count-Bowl-Subarrays/3676.Count-Bowl-Subarrays.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
long long bowlSubarrays(vector<int>& nums) {
int n = nums.size();
vector<int>nextGreater(n, -1);
vector<int>prevGreater(n, -1);
vector<int>st;
for (int i=0; i<n; i++) {
while (!st.empty() && nums[st.back()]<nums[i]) st.pop_back();
if (!st.empty()) prevGreater[i] = st.back();
st.push_back(i);
}
st.clear();

for (int i=n-1; i>=0; i--) {
while (!st.empty() && nums[st.back()]<nums[i]) st.pop_back();
if (!st.empty()) nextGreater[i] = st.back();
st.push_back(i);
}

long long ret = 0;
for (int i=0; i<n; i++) {
if (prevGreater[i]!=-1 && i-prevGreater[i]>=2) ret++;
if (nextGreater[i]!=-1 && nextGreater[i]-i>=2) ret++;
}
return ret;
}
};
7 changes: 7 additions & 0 deletions Stack/3676.Count-Bowl-Subarrays/Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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 によって変換されたページ (->オリジナル) /