Website Summary Files Commits Branches @commit @diff @stats
path: root / Practicum / pract06 / solutions / Task 01.cpp
diff options
context:
space:
mode:
Commit4928246bfb99672c7f874a7b66cc3ef8cb02a3b2 (patch)
Author
 Daniel Ghenov <47058577+Daniel-Ghenov@users.noreply.github.com>
2024-11-11 23:59:13 +0200
Committer
 GitHub <noreply@github.com>
2024-11-11 23:59:13 +0200
Files353d7575af2fe658939be110348d401c11f74423 /Practicum/pract06/solutions/Task 01.cpp
Data-structures-and-algorithms-4928246bfb99672c7f874a7b66cc3ef8cb02a3b2.tar
Data-structures-and-algorithms-4928246bfb99672c7f874a7b66cc3ef8cb02a3b2.tar.gz
Data-structures-and-algorithms-4928246bfb99672c7f874a7b66cc3ef8cb02a3b2.zip
Parent33366f5c7a84093a3b794410bcfe5479536894b4 (diff)
Update Task 01.cpp
1 files changed, 19 insertions, 21 deletions
-rw-r--r--Practicum/pract06/solutions/Task 01.cpp40
diff --git a/Practicum/pract06/solutions/Task 01.cpp b/Practicum/pract06/solutions/Task 01.cpp
index 738b769..9ddd5e8 100644
--- a/Practicum/pract06/solutions/Task 01.cpp
+++ b/Practicum/pract06/solutions/Task 01.cpp
@@ -1,35 +1,33 @@
-#include <string>
+#include <vector>
#include <stack>
class Solution
{
public:
- bool isValid(std::string s)
+ std::vector<int> nextGreaterElements(std::vector<int> &nums)
{
- std::stack<char> open;
-
- for (char ch : s)
+ std::stack<std::pair<int, int>> stack;
+ std::vector<int> ans(nums.size(), -1);
+ for (int i = 0; i < nums.size(); ++i)
{
- if (ch == '(' || ch == '{' || ch == '[')
+ while (!stack.empty() && stack.top().first < nums[i])
{
- open.push(ch);
+ ans[stack.top().second] = nums[i];
+ stack.pop();
}
- else
- {
- if (open.empty())
- return false;
- if ((open.top() == '(' && ch == ')') ||
- (open.top() == '{' && ch == '}') ||
- (open.top() == '[' && ch == ']'))
- {
- open.pop();
- continue;
- }
- return false;
+ stack.emplace(nums[i], i);
+ }
+
+ for (int i = 0; i < nums.size(); ++i)
+ {
+ while (!stack.empty() && stack.top().first < nums[i])
+ {
+ ans[stack.top().second] = nums[i];
+ stack.pop();
}
}
- return open.empty();
+ return ans;
}
-}; \ No newline at end of file
+};