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
//always STL generally takes extra space than arrays | |
//better use them | |
class Solution { | |
public: | |
int distance(vector<int>& v){ | |
return v[0]*v[0]+v[1]*v[1]; | |
} | |
vector<vector<int>> kClosest(vector<vector<int>>& points, int K) { | |
//for all points in points ,calculate distances and store it in array | |
//sort it and get distance of kth nearest point |
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: | |
//sizeof(int) = 32 bits/4 bytes | |
//what i observed is | |
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ... indices | |
//0 1 [1 2][1 2, 2 3] [1 2 2 3, 2 3 3 4] [1 2 2 3 2 3 3 4, 2 3 3 4 3 4 4 5] | |
vector<int> countBits(int num) { | |
if(num == 0) | |
return {0}; | |
else if(num == 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: | |
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
class Solution { | |
public: | |
int solve(vector<vector<int>>& matrix){ | |
//copy first col and row of matrix,because we need them. | |
//start from i=j=1, | |
//if(S[i][j] == 1) S[i][j] = min(S[i][j-1],S[i-1][j],S[i-1][j-1])+1; | |
//else S[i][j] = 0 | |
//vector<int> a(n,0); | |
//vector<vector<int>> S(m,a); | |
int m = matrix.size(); |
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 { | |
public: | |
bool check_anagrams(vector<int>& a,vector<int>& b){ | |
if(a == b) | |
return true; | |
else | |
return false; | |
} | |
bool solve(string&s ,string& p){ | |
int m = p.size(); |
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 singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode() : val(0), next(nullptr) {} | |
* ListNode(int x) : val(x), next(nullptr) {} | |
* ListNode(int x, ListNode *next) : val(x), next(next) {} | |
* }; | |
*/ |
NewerOlder