Skip to content

Instantly share code, notes, and snippets.

View bhaveshmunot1's full-sized avatar

Bhavesh Munot bhaveshmunot1

View GitHub Profile
@bhaveshmunot1
bhaveshmunot1 / RecursiveFibonacciAnalysis.cpp
Created June 4, 2020 10:01
Time complexity of Recursively Finding Nth Fibonacci Number.
int Fibonacci(int n) { // T(n)
if (n <= 1) { // 1
return n; // 1
}
int prev = Fibonacci(n-1); // T(n-1)
int prevprev = Fibonacci(n-2); // T(n-2)
return prev + prevprev; // 1
}
@bhaveshmunot1
bhaveshmunot1 / RecursiveBinarySeachAnalysis.cpp
Last active June 21, 2020 22:21
Find time complexity of binary search.
int binary_search(vector<int> a, int low, int high, int key) { // T(n)
if (low > high) { // 1
return -1; // 1
}
int mid = (low+high)/2; // 1
if (a[mid] == key) { // 1
return mid; // 1
}
if (a[mid] < key) { // 1
return binary_search(a, mid+1, high, key); // T(n/2)
@bhaveshmunot1
bhaveshmunot1 / PseudocodeMergeSort.cpp
Created June 4, 2020 09:55
General Merge Sort Framework
... merge( .... ) { // O(n)
.....
}
vector<int> mergeSort(vector<int> nums) { // T(n)
.... // 1
mergeSort(leftHalf); // T(n/2)
mergeSort(rightHalf); // T(n/2)
sorted = merge(leftHalft, rightHalf); // O(n)
return sorted; // O(n)
@bhaveshmunot1
bhaveshmunot1 / TimeComplexityExample1.cpp
Created June 4, 2020 09:46
Examples to Understand Time Complexity.
int foo(vector<int> &nums) {
int maximum = 0; // 1
for (int i=0; i<n; i++) { // n times
maximum = max(maximum, nums[i]); // 1
}
int minimum = 0; // 1
for (int i=0; i<n; i++) { // n times
minimum = min(minimum, nums[i]); // 1
}
@bhaveshmunot1
bhaveshmunot1 / TimeComplexityExample2.cpp
Last active June 4, 2020 09:38
Examples to Understand Time Complexity.
int sum(vector<int> nums) {
int ans = 0; // 1
for (int i=0; i<nums.size(); i++) { // n times
ans += nums[i]; // 1
}
return ans; // 1
}
void efficient(vector<int> nums) {
int addition = sum(nums); // O(n). Note: Single line does not
@bhaveshmunot1
bhaveshmunot1 / Leetcode_0072.cpp
Created June 4, 2020 03:49
Leetcode #72: Edit Distance
class Solution {
public:
int minDistance(string word1, string word2) {
int n = word1.size();
int m = word2.size();
vector<vector<int>> dp(n+1, vector<int>(m+1));
for (int i=0; i<=m; i++) {
dp[0][i] = i;
}
for (int i=0; i<=n; i++) {
@bhaveshmunot1
bhaveshmunot1 / Leetcode_1143.cpp
Created June 4, 2020 03:49
Leetcode #1143: Longest Common Subsequence
class Solution {
public:
int longestCommonSubsequence(string string1, string string2) {
int n = string1.size();
int m = string2.size();
vector<vector<int>> dp(n, vector<int>(m));
dp[0][0] = string1[0] == string2[0];
for (int i=1; i<n; i++) {
dp[i][0] = string1[i] == string2[0] ? 1 : dp[i-1][0];
}
@bhaveshmunot1
bhaveshmunot1 / Leetcode_0300.cpp
Created June 4, 2020 03:48
Leetcode #300: Longest Increasing Subsequence
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
if (n==0) {
return 0;
}
vector<int> dp(n);
dp[0] = 1;
for (int i=1; i<n; i++) { // Find the length of the longest increasing
@bhaveshmunot1
bhaveshmunot1 / Leetcode_0053.cpp
Created June 4, 2020 03:47
Leetcode #53: Maximum Subarray
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size();
if (n==0) {
return 0;
}
vector<int> dp(n);
dp[0] = nums[0];
for (int i=1; i<n; i++) {
@bhaveshmunot1
bhaveshmunot1 / Leetcode_0096.cpp
Created June 4, 2020 03:45
Leetcode #96: Unique Binary Search Trees
class Solution {
public:
int numTrees(int n) {
vector<int> dp(n+1);
dp[0] = 1;
dp[1] = 1;
for (int i=2; i<=n; i++) {
int sum = 0;
for (int k=1; k<=i; k++) {
sum += dp[k-1]*dp[i-k];