LeetCode, GeeksForGeeks Problem of the day solution
Kanalga Telegram’da o‘tish
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
Ko'proq ko'rsatish1 250
Obunachilar
+224 soatlar
+147 kunlar
+2930 kunlar
Postlar arxiv
class Solution {
public:
int numberOfMatches(int n) {
return n-1;
}
};
class Solution {
public:
int getMinDiff(int arr[], int n, int k) {
sort(arr,arr+n);
int result = arr[n - 1] - arr[0];
for (int i = 1; i < n; i++) {
if(arr[i]-k<0)continue;
int currentMax = max(arr[i - 1] + k, arr[n - 1] - k);
int currentMin = min(arr[0] + k, arr[i] - k);
result = min(result, currentMax - currentMin);
}
return result;
}
};
class Solution {
public:
string largestGoodInteger(string num) {
const int n = num.size();
string ans ="";
for(int i = 1 ; i
class Solution{
public:
string sum(string s1,string s2)
{
int i=s1.length()-1,j=s2.length()-1;
vectorans;
int x,y,res,rem,carry;
char ch;
while(i>=0 and j>=0)
{
x=s1[i]-'0';
y=s2[j]-'0';
res=x+y+carry;
rem=res%10;
carry=res/10;
ans.push_back(rem+'0');
i--;
j--;
}
while(i>=0)
{
x=s1[i]-'0';
res=x+carry;
rem=res%10;
carry=res/10;
ans.push_back(rem+'0');
i--;
}
while(j>=0)
{
y=s2[j]-'0';
res=y+carry;
rem=res%10;
carry=res/10;
ans.push_back(rem+'0');
j--;
}
while(carry)
{
int rem=carry%10;
ans.push_back(rem+'0');
carry/=10;
}
string t;
int p;
for( p=ans.size()-1;p>=0;p--)
{
if(ans[i]!='0')
break;
}
while(p>=0)
{
t+=ans[p];
p--;;
}
return t;
}
bool solve(string s,int pos,int l1,int l2)
{
string s1=s.substr(pos,l1);
string s2=s.substr(pos+l1,l2);
string s3=sum(s1,s2);
int n=s3.length();
if(n>s.length()-pos-l1-l2)
return 0;
if(s.substr(pos+l1+l2,n)==s3)
{
if(pos+l1+l2+n==s.length())
return 1;
return solve(s,pos+l1,l2,n);
}
return 0;
}
int isSumString(string s)
{
int n=s.length();
for(int i=1;i
class Solution {
public:
int minTimeToVisitAllPoints(vector>& points) {
int sum = 0;
if(points.size()==1)
return 0;
for( int i = 1; i < points.size() ; i++ )
{
sum+=(std::max( abs((points[i][0])-(points[i-1][0])), abs((points[i][1])-(points[i-1][1])) ));
}
return sum;
}
};
class Solution
{
public:
map mp;
void insert_nodes(Node* root){
if(!root) return;
mp[root->data]=true;
if(root->left) insert_nodes(root->left);
if(root->right) insert_nodes(root->right);
}
int count = 0;
void count_helper(Node* root,int x){
if(!root) return;
if(mp[x-root->data]) count++;
if(root->left) count_helper(root->left,x);
if(root->right) count_helper(root->right,x);
}
int countPairs(Node* root1, Node* root2, int x)
{
insert_nodes(root1);
count_helper(root2,x);
return count;
}
};
class Solution {
public:
int countCharacters(vector& words, string chars) {
vectorch(26, 0);
for(char chs : chars){
ch[chs - 'a'] += 1;
}
int flag;
int ans = 0;
for(string word : words){
if(word.length() > chars.length()) continue;
vectortemp(26, 0);
flag = 1;
for(char chs : word){
temp[chs - 'a']+=1;
}
for(char chs : word){
if(ch[chs - 'a'] < temp[chs - 'a']){
flag = 0;
break;
}
}
if(flag) ans+=word.length();
}
return ans;
}
};
class Solution{
public:
int isRepresentingBST(int arr[], int N)
{
for(int i=1;i
class Solution {
public:
bool arrayStringsAreEqual(vector& w1, vector& w2) {
// support variables
int i2 = 0, j2 = 0, lmt2 = w2.size();
for (string chunk1: w1) {
for (char c: chunk1) {
if (i2 == lmt2 || c != w2[i2][j2++]) return false;
// updating w2 pointers
if (j2 == w2[i2].size()) {
i2++, j2 = 0;
}
}
}
// we return if we are commpletely done parsing w2 too
return i2 == lmt2;
}
};
class Solution{
public:
void getLeafs(Node* root, vector &leafs) {
if(!root) return;
if(!root->left && !root->right)
leafs.push_back(root->data);
getLeafs(root->left, leafs);
getLeafs(root->right, leafs);
}
//Check for the DeadEnd node(node+1, node-1);
bool isPresent(Node* root, int data) {
if(!root) return false;
if(data < root->data) {
isPresent(root->left, data);
}else if(data > root->data) {
isPresent(root->right, data);
}else {
return true;
}
}
bool isDeadEnd(Node *root)
{
vector leafs;
getLeafs(root, leafs);
for(int leaf: leafs) {
if(leaf == 1) return true;
if(isPresent(root, leaf - 1) &&
isPresent(root, leaf + 1)) return true;
}
return false;
}
};
