This file contains hidden or 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 Node. | |
class Node { | |
public: | |
int val; | |
vector<Node*> children; | |
Node() {} | |
Node(int _val) { |
This file contains hidden or 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 Node. | |
class Node { | |
public: | |
int val; | |
vector<Node*> children; | |
Node() {} | |
Node(int _val) { |
This file contains hidden or 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
TreeNode* searchBST(TreeNode* root, int val) { | |
while(root!=NULL) | |
{ | |
if(root->val==val) | |
return root; | |
else if(root->val < val) | |
root=root->right; | |
else | |
root=root->left; |
This file contains hidden or 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> diStringMatch(string S) { | |
int n=S.size(); | |
vector<int> ans(n+1); | |
int inc=0,dec=n; | |
for(int i=0;i<n;i++) | |
{ | |
if(S[i]=='I') | |
ans[i]=inc++; |
This file contains hidden or 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 repeatedNTimes(vector<int>& A) { | |
int n=A.size(); | |
for (int i = 0; i+2 < n ; i++) | |
{ | |
if (A[i] == A[i + 1] || A[i] == A[i + 2]) | |
return A[i]; | |
} | |
return A[n - 1]; |
This file contains hidden or 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> replaceElements(vector<int>& arr) { | |
int n=arr.size(); | |
int cur=arr[n-1]; | |
arr[n-1]=-1; | |
for(int i=n-2;i >= 0;i--) | |
{ | |
int temp=arr[i]; | |
arr[i] = cur; |
This file contains hidden or 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 countBalls(int lowLimit, int highLimit) { | |
vector<int> boxes(46); | |
int ans=0; | |
for(int i=lowLimit;i<=highLimit;i++) | |
{ | |
int n=i,sum=0; | |
while(n>0) | |
{ |
This file contains hidden or 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 hidden or 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> sortArrayByParity(vector<int>& A) { | |
int i=0; | |
int j=A.size()-1; | |
while(i<j){ | |
if(A[i]%2==0){ | |
i++; | |
}else if(A[i]%2==1 && A[j]%2==0){ |
This file contains hidden or 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> finalPrices(vector<int>& prices) { | |
// next smaller or equal number. | |
int n=prices.size(); | |
stack<int> st; | |
for(int i=0;i<n;i++) | |
{ | |
if(st.empty() or prices[st.top()] < prices[i]) | |
st.push(i); |