forked from bindubritto/tjip-leetcode-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Submission Week Eight #8
Open
nazmulwanted
wants to merge
7
commits into
master
Choose a base branch
from
week_eight
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1ed9ae3
Problem 5: Count primes. Done.
nazmulwanted 0f883e1
Problem 1: Pow(x, n). Done.
nazmulwanted 260b9f8
Problem 5: Count primes. Updated comment.
nazmulwanted e349511
Problem 2: Sqrt(x). Done.
nazmulwanted 2fdca6c
Problem 8: Guess the word. Done.
nazmulwanted 59212d4
Problem 4: Factorial trailing zeroes. Done.
nazmulwanted 900fed4
Problem 6: Basic Calculator ii. Done.
nazmulwanted File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,32 @@ | ||
// Time complexity: O(N), N = Number of characters in the string. | ||
// Space complexity: O(total count of possible numbers in the string) | ||
|
||
int calculate(string s){ | ||
long long currNum = 0; | ||
long result = 0; | ||
stack<int> stk; | ||
char sign = '+'; | ||
int s_length = s.size(); | ||
for(int i = 0; i < s_length; i++){ | ||
if(s[i] >= '0' && s[i] <= '9'){ | ||
currNum = 10 * currNum + s[i] - '0'; | ||
} | ||
if(!(s[i] >= '0' && s[i] <= '9') && (s[i] != ' ') || i == s_length - 1){ | ||
if(sign == '+') stk.push(currNum); | ||
else if(sign == '-') stk.push(-1 * currNum); | ||
else{ | ||
if(sign == '*') currNum = stk.top() * currNum; | ||
else currNum = stk.top() / currNum; | ||
stk.pop(); | ||
stk.push(currNum); | ||
} | ||
currNum = 0; | ||
sign = s[i]; | ||
} | ||
} | ||
while(!stk.empty()){ | ||
result += stk.top(); | ||
stk.pop(); | ||
} | ||
return result; | ||
} |
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,24 @@ | ||
// Time complexity: O(square_root(N) * log(log(N)) , N = Given integer number. | ||
// Space complexity: O(N) | ||
|
||
int sieve(int n){ | ||
vector<bool> comp(n + 5, false); | ||
comp[1] = true; | ||
for(int i = 2; i * i <= n; i++){ | ||
if(comp[i] == false){ | ||
for(int j = i * i; j <= n; j += i){ | ||
comp[j] = true; | ||
} | ||
} | ||
} | ||
int count = 1; | ||
for(int i = 3; i < n; i += 2){ | ||
if(comp[i] == false) count++; | ||
} | ||
return count; | ||
} | ||
|
||
int countPrimes(int n){ | ||
if(n == 0 || n == 1 || n == 2) return 0; | ||
return sieve(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,11 @@ | ||
//Time complexity: O(Count of 5 in the n's factorial's divisors). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. which is, in worst case, |
||
//Space complexity: O(1) | ||
int trailingZeroes(int n){ | ||
int count_5 = 0; | ||
int div = 5; | ||
while(n >= div){ | ||
count_5 += n / div; | ||
div *= 5; | ||
} | ||
return count_5; | ||
} |
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,27 @@ | ||
// Time complexity: O(N), N = Number of strings. | ||
// Space complexity: O(N) | ||
|
||
int calculateScore(string &ref, string &candidate){ | ||
int res = 0; | ||
int ref_length = ref.size(); | ||
for(int i = 0; i < ref_length; i++){ | ||
res += ref[i] == candidate[i]; | ||
} | ||
return res; | ||
} | ||
|
||
void findSecretWord(vector<string> &wordlist, Master &master){ | ||
while(wordlist.size() > 1){ | ||
int rnd_index = rand() % wordlist.size(); | ||
string query_string = wordlist[rnd_index]; | ||
int score = master.guess(query_string); | ||
vector<string> new_word_list; | ||
for(auto &word: wordlist){ | ||
if(calculateScore(word, query_string) == score){ | ||
new_word_list.push_back(); | ||
} | ||
} | ||
wordlist = new_word_list; | ||
} | ||
master.quess(wordlist[0]); | ||
} |
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,20 @@ | ||
// Time complexity: O(log N), N is the power of the base. | ||
// Space complexity: O(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is |
||
|
||
double modularExpo(double x, long long n){ | ||
if(n == 0) return 1.0; | ||
if(n % 2 == 0){ | ||
double res = modularExpo(x, n / 2); | ||
return res * res; | ||
} | ||
return x * modularExpo(x, n - 1); | ||
} | ||
|
||
double myPow(double x, int n){ | ||
if(n == 0) return 1.0; | ||
bool inverse_flag = n < 0; | ||
long long N = n < 0? (-1) * 1LL * n : n; | ||
double result = modularExpo(x, N); | ||
result = inverse_flag ? 1.0 / result : result; | ||
return result; | ||
} |
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,12 @@ | ||
// Time complexity: O(log N) , N = given Number; | ||
// Space complexity: O(1) | ||
|
||
int mySqrt(int x){ | ||
long long left = 0, right = x; | ||
while(left < right){ | ||
long long mid = left + (right - left + 1) / 2; | ||
if(mid * mid > x) right = mid - 1; | ||
else left = mid; | ||
} | ||
return right; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's
O(N * log(log(N)))
. NotO(sqrt(N) * log(log(N)))
.