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: | |
set <int> groups[2]; | |
bool dfs(int node, vector <int> graph[], int x){ | |
int aux = 1 - x; | |
if(groups[aux].count(node)) return false; | |
groups[x].insert(node); | |
for(int i = 0; i < graph[node].size(); i++){ | |
int u = graph[node][i]; | |
if(!groups[aux].count(u) && !dfs(u, graph, aux)) 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
class Solution { | |
public: | |
/*bool Areequal(string s){ | |
int z=0,v=0; | |
for(auto x : s){ | |
if(x == '0') | |
z++; | |
else | |
v++; | |
} |
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>> intervalIntersection(vector<vector<int>>& A, vector<vector<int>>& B) { | |
//already sorted intervals | |
//vector<pair<int,int>> u; | |
//vector<pair<int,int>> v; | |
vector<vector<int>> ans; | |
//have to handle empty cases | |
int m = A.size(); | |
//for(int i=0; i<m; 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
Given a string, sort it in decreasing order based on the frequency of characters. | |
Example 1: | |
Input: | |
"tree" | |
Output: | |
"eert" |
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
In a town, there are N people labelled from 1 to N. There is a rumor that one of these people is secretly the town judge. | |
If the town judge exists, then: | |
The town judge trusts nobody. | |
Everybody (except for the town judge) trusts the town judge. | |
There is exactly one person that satisfies properties 1 and 2. | |
You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b. | |
If the town judge exists and can be identified, return the label of the town judge. Otherwise, return -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
class Solution { | |
public: | |
int majorityElement(vector<int>& nums) { | |
/*//Average time complexity is O(n*logn)+1 | |
sort(nums.begin(),nums.end()); | |
int n = nums.size(); | |
//as major element occurs more than [n/2] times,when sorted,compulsorily occurs at mid,as there are n //elements | |
return nums[n/2]; | |
*/ | |
/*unordered_map<int,int> m; |
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
//overcome runtime error by not doing n=n*2 operation causing overflow | |
class Solution { | |
public: | |
int findComplement(int num) { | |
/* | |
5/2 = 2 and 5%2 = 1 | |
2/2 = 1 and 2%2 = 0 | |
1/2 = 0 and 1%2 = 1 | |
*/ | |
/*int 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
//TOOK 48 ms | |
class Solution { | |
public: | |
bool canConstruct(string ransomNote, string magazine) { | |
if(ransomNote.length() == 0 || ransomNote == magazine) | |
return true; | |
if(ransomNote.length() > magazine.length()) | |
return false; | |
unordered_map<char,int> m1; | |
unordered_map<char,int> m2; |
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
// The API isBadVersion is defined for you. | |
// bool isBadVersion(int version); | |
class Solution { | |
public: | |
int firstBadVersion(int n) { | |
//searching for first Bad Version(true) | |
int l = 1; | |
int r = n; | |
int mid; |
NewerOlder