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: | |
vector<vector<int>> mergeArrays(vector<vector<int>>& nums1, vector<vector<int>>& nums2) { | |
vector<vector<int>> res; | |
int p1=0; | |
int p2=0; | |
int m=nums1.size(); | |
int n=nums2.size(); | |
while(p1<m and p2<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
class Solution { | |
public: | |
vector<int> applyOperations(vector<int>& nums) { | |
//Apply operations and maintain non-zero operations on the left side | |
int n=nums.size(); | |
int non_zero_idx=0; | |
for(int i=0;i<n;++i){ | |
if(i<n-1 and nums[i]==nums[i+1]){ | |
nums[i]*=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
class Solution { | |
public: | |
bool checkPowersOfThree(int n) { | |
//All the digits of base-3 number must be either 0 or 1 | |
while(n){ | |
if(n%3==2) | |
return false; | |
n/=3; | |
} | |
return true; |
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
/* | |
//Approach-1: Greedy Simulation...TC: O(N^2 logM)...SC: O(N) | |
class Solution { | |
public: | |
int lenLongestFibSubseq(vector<int>& arr) { | |
int n=arr.size(); | |
unordered_set<int> values(arr.begin(),arr.end()); | |
int longest=0; | |
for(int i=0;i<n-1;++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 { | |
string findLCS(string str1, string str2) { | |
int len1 = str1.size(); | |
int len2 = str2.size(); | |
vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1, 0)); | |
for (int i = 1; i <= len1; ++i) { | |
for (int j = 1; j <= len2; ++j) { | |
if (str1[i-1] == str2[j-1]) { | |
dp[i][j] = dp[i-1][j-1] + 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
//Solution-1: Using Kadane's Algorithm | |
class Solution { | |
int maxSumSubarrayKadanes(vector<int>& nums){ | |
int max_sum = INT_MIN; | |
int curr_sum = 0; | |
for(int ele: nums){ | |
curr_sum += ele; | |
max_sum = max(max_sum,curr_sum); | |
if(curr_sum<0) | |
curr_sum = 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 { | |
bool findBobPath(vector<vector<int>>& adj,int bob,int parent,vector<int>& curr_path,vector<int>& bob_path){ | |
if(bob==0){ | |
bob_path = curr_path; | |
return true; | |
} | |
//Traverse all nbrs | |
curr_path.push_back(bob); | |
for(int nbr: adj[bob]){ | |
if(nbr!=parent and findBobPath(adj,nbr,bob,curr_path,bob_path)) |
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
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | |
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | |
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | |
* }; |
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 { | |
string res; | |
bool buildNumber(unordered_set<string>& numbers,string& curr,int& n){ | |
if(curr.size() == n){ | |
if(numbers.count(curr)==0){ | |
res = curr; | |
return true; | |
} | |
return false; | |
} |
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
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode() : val(0), left(nullptr), right(nullptr) {} | |
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | |
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | |
* }; |
NewerOlder