Skip to content

Commit 5706746

Browse files
feat(revision): add Day 03 - Stacks & Queues (Valid Parentheses, Next Greater Element) — clean implementations
1 parent e9e568b commit 5706746

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// 01_valid_parentheses.cpp
2+
// Problem: Valid Parentheses
3+
// LeetCode: https://leetcode.com/problems/valid-parentheses/
4+
// Author: DeveloperViraj (curated DSA set)
5+
// Compile: g++ -std=c++17 01_valid_parentheses.cpp -o 01_valid_parentheses
6+
// Run: ./01_valid_parentheses
7+
//
8+
// Description:
9+
// Given a string containing only the characters '(', ')', '{', '}', '[' and ']',
10+
// determine if the input string is valid. An input string is valid if:
11+
// 1) Open brackets are closed by the same type of brackets.
12+
// 2) Open brackets are closed in the correct order.
13+
// The algorithm uses a stack to keep track of opening brackets; upon encountering
14+
// a closing bracket we check whether the top of the stack matches the expected opening.
15+
//
16+
// Use case:
17+
// Syntax validation, small parser checks, expression validation in editors.
18+
//
19+
// Time Complexity: O(n) — each character is pushed/popped at most once.
20+
// Space Complexity: O(n) — stack for open brackets (worst case all opens).
21+
//
22+
// Input format (example):
23+
// A single line containing the bracket string.
24+
// Example:
25+
// ()[]{} => Valid
26+
// (] => Invalid
27+
//
28+
// Output:
29+
// Prints "Valid" if the string is valid, otherwise prints "Invalid".
30+
31+
#include <iostream>
32+
#include <string>
33+
#include <stack>
34+
#include <unordered_map>
35+
36+
using namespace std;
37+
38+
bool isValidParentheses(const string &s) {
39+
stack<char> openStack;
40+
// Map closing -> corresponding opening
41+
unordered_map<char, char> closingToOpening = {
42+
{')', '('},
43+
{'}', '{'},
44+
{']', '['},
45+
};
46+
47+
for (char ch : s) {
48+
// If it's an opening bracket, push to stack
49+
if (ch == '(' || ch == '{' || ch == '[') {
50+
openStack.push(ch);
51+
} else if (ch == ')' || ch == '}' || ch == ']') {
52+
// If stack empty or top doesn't match expected opening, invalid
53+
if (openStack.empty() || openStack.top() != closingToOpening[ch]) {
54+
return false;
55+
}
56+
openStack.pop();
57+
} else {
58+
// If other characters exist, ignore or treat as invalid depending on spec.
59+
// For this problem we assume input contains only parentheses characters.
60+
}
61+
}
62+
63+
// Valid only if no unmatched opens remain
64+
return openStack.empty();
65+
}
66+
67+
int main() {
68+
cout << "🔹 Problem: Valid Parentheses\n";
69+
cout << "Enter a single line containing the string of brackets (e.g. ()[]{}):\n";
70+
71+
string input;
72+
if (!getline(cin, input)) {
73+
cerr << "No input provided.\n";
74+
return 0;
75+
}
76+
77+
bool result = isValidParentheses(input);
78+
if (result) cout << "Valid\n";
79+
else cout << "Invalid\n";
80+
81+
return 0;
82+
}
83+
84+
/*
85+
Example:
86+
Input:
87+
()[]{}
88+
89+
Output:
90+
Valid
91+
*/
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// 02_next_greater_element.cpp
2+
// Problem: Next Greater Element (monotonic stack)
3+
// LeetCode (related): https://leetcode.com/problems/next-greater-element-i/
4+
// Author: DeveloperViraj (curated DSA set)
5+
// Compile: g++ -std=c++17 02_next_greater_element.cpp -o 02_next_greater_element
6+
// Run: ./02_next_greater_element
7+
//
8+
// Description:
9+
// For each element in an array, find the next greater element to its right.
10+
// If no greater element exists, the answer for that position is -1.
11+
// We use a monotonic decreasing stack that stores indices of elements whose
12+
// next greater element hasn't been found yet. As we iterate, when we find
13+
// a greater element, we pop and assign its next greater.
14+
//
15+
// Use case:
16+
// Useful in many pattern problems: stock span variants, skyline queries,
17+
// preprocessing for range queries, etc.
18+
//
19+
// Time Complexity: O(n) — each element is pushed and popped at most once.
20+
// Space Complexity: O(n) — stack + output array.
21+
//
22+
// Input format:
23+
// n
24+
// a0 a1 a2 ... a(n-1)
25+
//
26+
// Output:
27+
// n integers (space-separated) — next greater element for each index, -1 if none.
28+
//
29+
// Example:
30+
// Input:
31+
// 9
32+
// 2 1 2 4 3 5 1 2 0
33+
//
34+
// Output:
35+
// 4 2 4 5 5 -1 2 -1 -1
36+
//
37+
// (Each index replaced by its next greater to the right)
38+
39+
#include <iostream>
40+
#include <vector>
41+
#include <stack>
42+
43+
using namespace std;
44+
45+
vector<int> nextGreaterElements(const vector<int> &values) {
46+
int n = (int)values.size();
47+
vector<int> answer(n, -1);
48+
stack<int> indexStack; // store indices of elements, monotonic decreasing by value
49+
50+
for (int i = 0; i < n; ++i) {
51+
int currentValue = values[i];
52+
// While current value is greater than the value at stack top index,
53+
// we have found the next greater for that index.
54+
while (!indexStack.empty() && values[indexStack.top()] < currentValue) {
55+
int idx = indexStack.top();
56+
indexStack.pop();
57+
answer[idx] = currentValue;
58+
}
59+
// Push current index — its NG not known yet
60+
indexStack.push(i);
61+
}
62+
63+
// Remaining indices have no next greater; their answer remains -1.
64+
return answer;
65+
}
66+
67+
int main() {
68+
cout << "🔹 Problem: Next Greater Element (to the right)\n";
69+
cout << "Input format:\n n\n a0 a1 ... a(n-1)\nEnter n and the array:\n";
70+
71+
int n;
72+
if (!(cin >> n) || n < 0) {
73+
cerr << "Invalid input for n.\n";
74+
return 0;
75+
}
76+
77+
vector<int> arr(n);
78+
for (int i = 0; i < n; ++i) cin >> arr[i];
79+
80+
vector<int> result = nextGreaterElements(arr);
81+
82+
cout << "\nNext greater elements:\n";
83+
for (int i = 0; i < n; ++i) {
84+
cout << result[i];
85+
if (i + 1 < n) cout << ' ';
86+
}
87+
cout << '\n';
88+
89+
return 0;
90+
}
91+
92+
/*
93+
Example:
94+
Input:
95+
6
96+
2 7 3 5 4 6
97+
98+
Output:
99+
7 -1 5 6 6 -1
100+
101+
Explanation:
102+
- Next greater after 2 is 7
103+
- 7 has no next greater -> -1
104+
- After 3 is 5
105+
- After 5 is 6
106+
- After 4 is 6
107+
- 6 has none -> -1
108+
*/

0 commit comments

Comments
 (0)