forked from emotionless/Google_interview_question_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
problem solution with .gitignore file added
- Loading branch information
Faruk Hossain Milon
committed
Oct 8, 2020
1 parent
43ae386
commit 0475969
Showing
5 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
.o | ||
.exe |
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,32 @@ | ||
class Solution { | ||
public: | ||
int oddEvenJumps(vector<int>& A) { | ||
int n = A.size(); | ||
vector<vector<int>> dp(n, vector<int>(2, 0)); | ||
dp[n-1][0] = dp[n-1][1] = 1; | ||
map<int, int> container; | ||
int ans = 1; | ||
container[A[n-1]] = n-1; | ||
for(int i = n-2; i >= 0; i--) { | ||
auto it = container.lower_bound(A[i]+1); | ||
// cout << "For: " << i << endl; | ||
if (it == container.begin()) { | ||
dp[i][1] = 0; | ||
} else { | ||
it--; | ||
// cout << it->second << endl; | ||
dp[i][1] = dp[it->second][0]; | ||
} | ||
it = container.upper_bound(A[i] - 1); | ||
if (it == container.end()) { | ||
dp[i][0] = 0; | ||
} else { | ||
// cout << "U " <<it->second << endl; | ||
dp[i][0] = dp[it->second][1]; | ||
} | ||
container[A[i]] = i; | ||
ans += dp[i][0]; | ||
} | ||
return ans; | ||
} | ||
}; |
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,45 @@ | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <string> | ||
#include <iostream> | ||
#include <vector> | ||
#include <map> | ||
#include <unordered_map> | ||
|
||
|
||
using namespace std; | ||
|
||
int minNumberOfCharis(const vector<int> &arrival, const vector<int> &leave) { | ||
|
||
map<int, int> numOfPeoples; | ||
int n = arrival.size(); | ||
for(int i = 0; i < n; i++) { | ||
numOfPeoples[ arrival[i] ]++; | ||
numOfPeoples[ leave[i] ]--; | ||
} | ||
int counter = 0; | ||
int mx = 0; | ||
for(auto it : numOfPeoples) { | ||
counter += it.second; | ||
mx = max(mx, counter); | ||
} | ||
return mx; | ||
} | ||
|
||
int main() | ||
{ | ||
vector<int> arr({1, 2, 6, 5, 3}); | ||
vector<int> leave({5, 5, 7, 6, 8}); | ||
|
||
cout << minNumberOfCharis(arr, leave) << endl; | ||
return 0; | ||
} | ||
|
||
/* | ||
Time complexity: | ||
O(nlog(n)) | ||
Space complexity: | ||
O(n) | ||
*/ |
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,40 @@ | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <string> | ||
#include <iostream> | ||
#include <vector> | ||
#include <map> | ||
#include <unordered_map> | ||
|
||
|
||
using namespace std; | ||
|
||
int timeToType(string keyboard, string text) { | ||
|
||
unordered_map<char, int> pos; | ||
for(int i = 0; i < keyboard.size(); i++) { | ||
pos[keyboard[i]] = i; | ||
} | ||
int pre = 0; | ||
int ans = 0; | ||
for(auto ch : text) { | ||
ans += abs(pre - pos[ch]); | ||
pre = pos[ch]; | ||
} | ||
return ans; | ||
} | ||
|
||
int main() | ||
{ | ||
cout << timeToType("abcdefghijklmnopqrstuvwxy", "cba") << endl; | ||
return 0; | ||
} | ||
|
||
/* | ||
Time complexity: | ||
O(n) | ||
Space complexity: | ||
O(1) | ||
*/ |