From 34446fb0a900197b7030747c44459b395ea29a3b Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Mon, 30 Oct 2023 12:16:48 -0700 Subject: [PATCH] day 29: 22-generate-parentheses, 739-daily-temperatures --- 22-generate-parentheses/driver.cpp | 12 ++++++++ 22-generate-parentheses/soln.cpp | 24 ++++++++++++++++ 22-generate-parentheses/soln.hpp | 8 ++++++ 739-daily-temperatures/driver.cpp | 10 +++++++ 739-daily-temperatures/soln.cpp | 44 ++++++++++++++++++++++++++++++ 739-daily-temperatures/soln.hpp | 8 ++++++ 6 files changed, 106 insertions(+) create mode 100644 22-generate-parentheses/driver.cpp create mode 100644 22-generate-parentheses/soln.cpp create mode 100644 22-generate-parentheses/soln.hpp create mode 100644 739-daily-temperatures/driver.cpp create mode 100644 739-daily-temperatures/soln.cpp create mode 100644 739-daily-temperatures/soln.hpp diff --git a/22-generate-parentheses/driver.cpp b/22-generate-parentheses/driver.cpp new file mode 100644 index 0000000..d7eb970 --- /dev/null +++ b/22-generate-parentheses/driver.cpp @@ -0,0 +1,12 @@ +#include "soln.hpp" + +int main() +{ + int n = 4; + Solution soln; + vector result = soln.generateParenthesis(n); + for (string str : result) + cout << str << "\t"; + cout << endl; + return 0; +} \ No newline at end of file diff --git a/22-generate-parentheses/soln.cpp b/22-generate-parentheses/soln.cpp new file mode 100644 index 0000000..0785732 --- /dev/null +++ b/22-generate-parentheses/soln.cpp @@ -0,0 +1,24 @@ +#include "soln.hpp" + +void Solution::recurseParenthesis(vector &result, string str, int left, int right) +{ + // base case + if (left == 0 & right == 0) + { + result.push_back(str); + return; + } + // opening parenthesis can be added, so add + if (left > 0) + recurseParenthesis(result, str + "(", left - 1, right); + // closing parenthesis can be added, so add + if (right > left) + recurseParenthesis(result, str + ")", left, right - 1); +} + +vector Solution::generateParenthesis(int n) +{ + vector result; + recurseParenthesis(result, "", n, n); + return result; +} \ No newline at end of file diff --git a/22-generate-parentheses/soln.hpp b/22-generate-parentheses/soln.hpp new file mode 100644 index 0000000..62de54b --- /dev/null +++ b/22-generate-parentheses/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + void recurseParenthesis(vector& result, string str, int left, int right); + vector generateParenthesis(int n); +}; \ No newline at end of file diff --git a/739-daily-temperatures/driver.cpp b/739-daily-temperatures/driver.cpp new file mode 100644 index 0000000..65bb3e2 --- /dev/null +++ b/739-daily-temperatures/driver.cpp @@ -0,0 +1,10 @@ +#include "soln.hpp" + +int main() +{ + vector temps{73, 74, 75, 71, 69, 72, 76, 73}; + vector answer{1, 1, 4, 2, 1, 1, 0, 0}; + Solution soln; + cout << "Found next warmer days correctly? " << soln.test(temps, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/739-daily-temperatures/soln.cpp b/739-daily-temperatures/soln.cpp new file mode 100644 index 0000000..9fbe251 --- /dev/null +++ b/739-daily-temperatures/soln.cpp @@ -0,0 +1,44 @@ +#include "soln.hpp" + +vector Solution::dailyTemperatures(vector &temperatures) +{ + // brute force + // vector answer(temperatures.size()); + // for(int i = 0; i < temperatures.size(); i++) { + // bool warmer_day_found = false; + // for(int j = i+1; j < temperatures.size(); j++) { + // if(temperatures[j] > temperatures[i]) { + // warmer_day_found = true; + // answer[i] = j-i; + // break; + // } + // } + // if(!warmer_day_found) + // answer[i] = 0; + // } + // return answer; + + // stack, neetcode + vector answer(temperatures.size()); + stack dec_temps; + for (int i = 0; i < temperatures.size(); i++) + { + while (!dec_temps.empty() && temperatures[i] > temperatures[dec_temps.top()]) + { + answer[dec_temps.top()] = i - dec_temps.top(); + dec_temps.pop(); + } + dec_temps.push(i); + } + while (!dec_temps.empty()) + { + answer[dec_temps.top()] = 0; + dec_temps.pop(); + } + return answer; +} + +bool Solution::test(vector &temperatures, vector &answer) +{ + return dailyTemperatures(temperatures) == answer; +} diff --git a/739-daily-temperatures/soln.hpp b/739-daily-temperatures/soln.hpp new file mode 100644 index 0000000..7907700 --- /dev/null +++ b/739-daily-temperatures/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + vector dailyTemperatures(vector &temperatures); + bool test(vector &temperatures, vector &answer); +}; \ No newline at end of file