forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9ec6d92
Create 2749.Minimun-Operations-to-make-the-integer-Zero-PYTHON
JaredMcCarthy 67bc72b
Update Readme.md
wisdompeak 615fbdc
Update 3677.Count-Binary-Palindromic-Numbers.cpp
wisdompeak 0899ed9
Merge pull request #115 from JaredMcCarthy/patch-1
wisdompeak de51403
Create 3676.Count-Bowl-Subarrays.cpp
wisdompeak 99b375c
Update Readme.md
wisdompeak b64842c
Create Readme.md
wisdompeak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
...erations-to-Make-the-Integer-Zero/2749.Minimun-Operations-to-make-the-integer-Zero-PYTHON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ;) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
Stack/3676.Count-Bowl-Subarrays/3676.Count-Bowl-Subarrays.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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的形状。 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.