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 #321

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 3 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Jun 10, 2025
Merged
Changes from 1 commit
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
Next Next commit
Create 3578.Count-Partitions-With-Max-Min-Difference-at-Most-K.cpp
  • Loading branch information
wisdompeak authored Jun 10, 2025
commit ff0548e2e6aea23157b46f82348b5f2bc5c1413f
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution {
public:
int countPartitions(vector<int>& nums, int k) {
int n = nums.size();
long M = 1e9+7;
nums.insert(nums.begin(), 0);

vector<long>dp(n+1);
vector<long>presum(n+1);
dp[0] = 1;
presum[0] = 1;

deque<int>dq1;
deque<int>dq2;
int left = 1;
for (int i=1; i<=n; i++) {
int x = nums[i];
while (!dq1.empty() && nums[dq1.back()]<x) {
dq1.pop_back();
}
dq1.push_back(i);
while (!dq2.empty() && nums[dq2.back()]>x) {
dq2.pop_back();
}
dq2.push_back(i);

while (left <= i && nums[dq1.front()] - nums[dq2.front()] > k) {
if (dq1.front()==left) dq1.pop_front();
if (dq2.front()==left) dq2.pop_front();
left++;
}
// Any valid parition that ends at left-1, left, left+1, ..., i-1 is good.

dp[i] = presum[i-1] - (left>=2?presum[left-2]:0);
presum[i] = presum[i-1] + dp[i];

dp[i] = (dp[i] + M) % M;
presum[i] = (presum[i] + M) % M;
}

return dp[n];
}
};

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