LeetCode, GeeksForGeeks Problem of the day solution
Відкрити в Telegram
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
Показати більше1 250
Підписники
+224 години
+147 днів
+2930 день
Архів дописів
class Solution {
public:
bool isIsomorphic(string s, string t) {
char mapS[128] = {0};
char mapT[128] = {0};
for(int i=0;i
class Solution
{
public:
void inorder(Node* root,int &ans,int &k)
{
if(root==NULL)return;
inorder(root->left,ans,k);
ans=min(ans,abs(k-root->data));
k=root->data;
inorder(root->right,ans,k);
}
int absolute_diff(Node *root)
{
//Your code here
int ans=1e9,k=-1e9;
inorder(root,ans,k);
return ans;
}
};
class Solution {
public:
int lengthOfLastWord(string s) {
string word= "";
int j;
for(int i=s.length()-1;i>=0;i--){
if(s[i] != ' '){
j = i;
break;
}
}
for(int i=j;i>=0;i--){
if(s[i]==' '){
break;
}
else{
word = word + s[i];
}
}
return word.length();
}
};
class Solution {
public:
vector<int> arr;
int ans=0;
void inorder(Node* root) {
if(!root) return;
inorder(root->left);
arr.push_back(root->data);
inorder(root->right);
}
void mergeSort(int i,int j) {
if(i>=j)return;
int m=i+(j-i)/2;
mergeSort(i,m);
mergeSort(m+1,j);
int k=m+1, start=i;
while(i<k and k<=j) {
if(arr[i]<=arr[k])i++;
else {
ans+=m-i+1;
k++;
}
}
sort(arr.begin() + start, arr.begin() + j + 1);
}
int pairsViolatingBST(int n, Node *root) {
inorder(root);
mergeSort(0,n-1);
return ans;
}
};
class Solution {
public:
long long countSubarrays(vector& nums, int minK, int maxK) {
long long ans=0;
int start=0, minStart, maxStart;
bool minf=false, maxf=false;
for(int i=0; imaxK){
minf=false;
maxf=false;
start= i+1;
}
if(num==minK){
minf=true;
minStart=i;
}
if(num==maxK){
maxf=true;
maxStart= i;
}
if(maxf && minf){
ans += (min(minStart,maxStart)-start+1);
}
}
return ans;
}
};
class Solution {
public:
int findMaxForN(Node* root, int n) {
int num=-1;
while(root!=NULL){
if(root->key<=n){
num=root->key;
root=root->right;
}
else{
root=root->left;
}
}
return num;
}
};
class Solution {
public:
int subarraysWithKDistinct(vector& nums, int k) {
return countK(nums, k) - countK(nums, k - 1);
}
private:
int countK(vector& nums, int k) {
unordered_map counter;
int left = 0;
int distinct_count = 0;
int result = 0;
for (int right = 0; right < nums.size(); ++right) {
if (counter[nums[right]] == 0) {
distinct_count++;
}
counter[nums[right]]++;
while (distinct_count > k) {
counter[nums[left]]--;
if (counter[nums[left]] == 0) {
distinct_count--;
}
left++;
}
result += right - left + 1;
}
return result;
}
};
class Solution {
public:
int minValue(Node* root) {
if(root==NULL)
return -1;
while(root->left!=NULL)
root=root->left;
return root->data;
}
};
class Solution {
public:
long long countSubarrays(vector& nums, int k) {
int maxi=0;
for(int i=0;i
class Solution {
public:
bool isEularCircuitExist(int v, vectoradj[]){
for(int src=0; src
