-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add longest cycle In graph changes * add changes * Create ReverseWordsInAString3.cpp * add completed solutions
- Loading branch information
1 parent
05a9eca
commit c91ae6b
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Memoization | ||
// Time Complexity - O(N^2) | ||
// Space Complexity - O(N^2) | ||
|
||
class Solution { | ||
public: | ||
int paintWalls(vector<int>& cost, vector<int>& time) { | ||
|
||
return dfs(cost, time, cost.size(), 0); | ||
} | ||
|
||
int dfs(vector<int> &cost, vector<int> &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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |