From 33a38e1938fb0238fd6bee03d7cdd540c60c0151 Mon Sep 17 00:00:00 2001 From: Afroz Chakure Date: Sun, 1 Oct 2023 13:54:50 +0530 Subject: [PATCH 1/4] add longest cycle In graph changes --- .../Algorithms/Hard/LongestCycleInAGraph.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 LeetCode/Algorithms/Hard/LongestCycleInAGraph.cpp diff --git a/LeetCode/Algorithms/Hard/LongestCycleInAGraph.cpp b/LeetCode/Algorithms/Hard/LongestCycleInAGraph.cpp new file mode 100644 index 0000000..504a081 --- /dev/null +++ b/LeetCode/Algorithms/Hard/LongestCycleInAGraph.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int longestCycle(vector& edges) { + int ans = -1; + + vector> memo(edges.size(), {-1, -1}); + + // Distance, Cycle Identifier (Source Identifier) + // cycleTracker.push_back(vector{-1, -1}); + + for(int i=0; i Date: Sun, 1 Oct 2023 14:22:54 +0530 Subject: [PATCH 2/4] add changes --- LeetCode/Algorithms/.vscode/settings.json | 4 +- .../Algorithms/Easy/.vscode/settings.json | 5 +++ LeetCode/Algorithms/Hard/RestoreTheArray.cpp | 45 +++++++++++++++++++ .../ShortestPathWithAlternatingColors.cpp | 0 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 LeetCode/Algorithms/Easy/.vscode/settings.json create mode 100644 LeetCode/Algorithms/Hard/RestoreTheArray.cpp create mode 100644 Leetcode/Algorithms/Medium/ShortestPathWithAlternatingColors.cpp diff --git a/LeetCode/Algorithms/.vscode/settings.json b/LeetCode/Algorithms/.vscode/settings.json index 48d27d6..5d99845 100644 --- a/LeetCode/Algorithms/.vscode/settings.json +++ b/LeetCode/Algorithms/.vscode/settings.json @@ -13,6 +13,8 @@ "initializer_list": "cpp", "regex": "cpp", "valarray": "cpp", - "ostream": "cpp" + "ostream": "cpp", + "xstring": "cpp", + "xlocale": "cpp" } } \ No newline at end of file diff --git a/LeetCode/Algorithms/Easy/.vscode/settings.json b/LeetCode/Algorithms/Easy/.vscode/settings.json new file mode 100644 index 0000000..fe0c1eb --- /dev/null +++ b/LeetCode/Algorithms/Easy/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "xlocale": "cpp" + } +} \ No newline at end of file diff --git a/LeetCode/Algorithms/Hard/RestoreTheArray.cpp b/LeetCode/Algorithms/Hard/RestoreTheArray.cpp new file mode 100644 index 0000000..112df13 --- /dev/null +++ b/LeetCode/Algorithms/Hard/RestoreTheArray.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + int numberOfArrays(string s, int k) { + vector dp(s.size(), -1); + + } +}; + + +arr = [1,3,4,2], + +mat = + 0 1 +0 [[1,4], +1 [2,3]] + +mpr + +1 0 +4 0 +2 1 +3 1 + +mpc +1 0 +4 1 +2 0 +3 1 + +mpr[1] = 0 +mprc[mpr[1]] = 1 +mpcc[mpc[1]] = 1 + +mpr[3] = 1 +mpc[3] = 1 + +mprc[mpr[1]] = 2 +mpcc[mpc[1]] = 2 + +mpr[4] = 1 +mpc[4] = 1 + +mprc[mpr[1]] = 3 +mpcc[mpc[1]] = 3; + diff --git a/Leetcode/Algorithms/Medium/ShortestPathWithAlternatingColors.cpp b/Leetcode/Algorithms/Medium/ShortestPathWithAlternatingColors.cpp new file mode 100644 index 0000000..e69de29 From 13e8269b2238dcd882a62f3787b0d1c70724dc80 Mon Sep 17 00:00:00 2001 From: Afroz Chakure Date: Sun, 1 Oct 2023 21:51:42 +0530 Subject: [PATCH 3/4] Create ReverseWordsInAString3.cpp --- .../Easy/ReverseWordsInAString3.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 LeetCode/Algorithms/Easy/ReverseWordsInAString3.cpp diff --git a/LeetCode/Algorithms/Easy/ReverseWordsInAString3.cpp b/LeetCode/Algorithms/Easy/ReverseWordsInAString3.cpp new file mode 100644 index 0000000..a76f239 --- /dev/null +++ b/LeetCode/Algorithms/Easy/ReverseWordsInAString3.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + string reverseWords(string s) { + stringstream ss(s); + + string t; + string result = ""; + + while(getline(ss, t, ' ')) { + reverse(t.begin(), t.end()); + result += t + ' '; + } + return result.substr(0, result.size() - 1); + } + +}; + +// Time Complexity - O(N) +// Space Complexity - O(1) \ No newline at end of file From e2ab97444ac943b3a105d4b238dbac10ef7ed300 Mon Sep 17 00:00:00 2001 From: Afroz Chakure Date: Sun, 29 Oct 2023 23:55:22 +0530 Subject: [PATCH 4/4] add completed solutions --- LeetCode/Algorithms/Hard/PaintingTheWalls.cpp | 25 +++++++++++++++++++ LeetCode/Algorithms/Hard/PoorPigs.cpp | 25 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 LeetCode/Algorithms/Hard/PaintingTheWalls.cpp create mode 100644 LeetCode/Algorithms/Hard/PoorPigs.cpp diff --git a/LeetCode/Algorithms/Hard/PaintingTheWalls.cpp b/LeetCode/Algorithms/Hard/PaintingTheWalls.cpp new file mode 100644 index 0000000..5b24060 --- /dev/null +++ b/LeetCode/Algorithms/Hard/PaintingTheWalls.cpp @@ -0,0 +1,25 @@ +// Memoization +// Time Complexity - O(N^2) +// Space Complexity - O(N^2) + +class Solution { +public: + int paintWalls(vector& cost, vector& time) { + + return dfs(cost, time, cost.size(), 0); + } + + int dfs(vector &cost, vector &time, int remain, int i) { + if(remain <= 0) { + return 0; + } + + if(i == cost.size()) { + return INT_MAX; + } + + int pain = dfs(cost, time, remain - 1 - time[i], i + 1); + int skip = dfs(cost, time, remain, i+1); + return min(pain, skip); + } +}; diff --git a/LeetCode/Algorithms/Hard/PoorPigs.cpp b/LeetCode/Algorithms/Hard/PoorPigs.cpp new file mode 100644 index 0000000..23766bd --- /dev/null +++ b/LeetCode/Algorithms/Hard/PoorPigs.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int poorPigs(int buckets, int minutesToDie, int minutesToTest) { + if(buckets-- == 1) { + return 0; + } + + // 60 / 15 + 1 = 5 + int base = minutesToTest / minutesToDie + 1; + + // buckets = 5 + // buckets = 25, take 5 + // buckets = buckets>25 and buckets < 125 + int res = 0; + while(buckets > 0) { + buckets = buckets / base; + res += 1; + } + return res; + } +}; + +// Time Complexity - O(log(1000)) ~ O(1) +// Space Complexity - O(1) +// Refer to solution from Suchit Dedeja for it \ No newline at end of file