Skip to content
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

Added potd 11_10_2024 #25

Merged
merged 1 commit into from
Oct 10, 2024
Merged
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
26 changes: 26 additions & 0 deletions october_2024/potd_11_10_2024.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
int maxWidthRamp(vector<int>& nums) {
int n = nums.size();
stack<int> s;

// Step 1: Build a decreasing stack of indices
for (int i = 0; i < n; ++i) {
if (s.empty() || nums[s.top()] > nums[i]) {
s.push(i);
}
}

int maxWidth = 0;

// Step 2: Traverse from the end and find maximum width ramp
for (int j = n - 1; j >= 0; --j) {
while (!s.empty() && nums[s.top()] <= nums[j]) {
maxWidth = max(maxWidth, j - s.top());
s.pop();
}
}

return maxWidth;
}
};
Loading