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
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 | |
} |
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
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) |
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
... 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) |
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
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 | |
} |
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
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 |
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
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++) { |
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
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]; | |
} |
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
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 |
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
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++) { |
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
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]; |