LeetCode, GeeksForGeeks Problem of the day solution
Open in Telegram
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
Show more1 250
Subscribers
+224 hours
+147 days
+2930 days
Posts Archive
class Solution {
public:
vector> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
int m = rows;
int n = cols;
vector> ans;
vector> vis(m+n, vector(m+n, 0));
int x = rStart;
int y = cStart;
int k = 1;
while(ans.size()=0 && j>=0 && x=0 && y>=0 && i=y-k; j--){
if(x>=0 && j>=0 && x=x-k; i--){
if(i>=0 && y>=0 && i
class Solution {
public:
int f(Node* root){
if(root==NULL){
return 0;
}
if(root->left==NULL && root->right==NULL){
return root->data;
}
int val=f(root->left)+f(root->right);
if(val==root->data){
return 2*val;
}
return -1;
}
bool isSumTree(Node* root) {
if(root==NULL){
return true;
}
return f(root)==-1?false:true;
}
};
class Solution {
public:
string numberToWords(int n) {
long long int limit = 1000000000000, curr , t = 0;
if(n==0)
return "Zero";
string multiplier[]={"","Trillion","Billion","Million","Thousand"};
string first20[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
string tens[] = {"","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
if(n<20){
return first20[n];
}
string ans = "";
for(long long int i = n;i>0;i%=limit, limit /= 1000){
curr = i/limit;
while(curr == 0){
i %= limit;
limit /= 1000;
curr = i/limit;
++t;
}
if(curr>99){
ans += (first20[curr/100] + " Hundred ");
}
curr = curr%100;
if(curr>0 && curr<20){
ans += (first20[curr] + " ");
}
else if(curr%10==0 && curr!=0){
ans += (tens[curr/10-1] + " ");
}
else if(curr>20 && curr<100){
ans += (tens[curr/10-1] + " " + first20[curr%10] + " ");
}
if(t<4){
ans += (multiplier[++t] + " ");
}
}
int l = ans.length();
return ans.substr(0,l-1);
}
};
class Solution {
public:
int kthElement(int k, vector& arr1, vector& arr2) {
size_t m=arr1.size();
size_t n=arr2.size();
int i=0,j=0;
int c=0;
while (i
class Solution {
public:
int minimumPushes(string word) {
int c[123] = { 0 };
for (unsigned int i = 0, len = word.length(); i < len; ++i)
++c[word[i]];
bool sorted = false;
do
{
sorted = true;
for (unsigned short i = 97; i < 122; ++i)
if (c[i] < c[i + 1])
{
int temp = c[i];
c[i] = c[i + 1];
c[i + 1] = temp;
sorted = false;
}
} while (!sorted);
int steps = 0, count = 0;
for (unsigned short i = 97; i < 123; ++i)
if (c[i] != 0)
{
steps += c[i] * (count / 8 + 1);
++count;
}
return steps;
}
};
class Solution {
public:
int isValid(string str) {
int dot = 0;
string temp = "";
for(int i = 0 ; i < str.length() ; i++){
if(str[i] == '.'){
dot++;
if(temp.empty()) return false;
else if(temp.size() > 1 && temp[0] == '0') return false;
int num = stoi(temp);
if(num < 0 || num > 256) return false;
temp = "";
}
else temp += str[i];
}
if(temp.empty()) return false;
else if(temp.size() > 1 && temp[0] == '0') return false;
int num = stoi(temp);
if(num < 0 || num > 256) return false;
return dot == 3 ? true : false;
}
};
class Solution {
public:
string kthDistinct(vector& arr, int k) {
unordered_map mp;
string ans="";
for(int i=0;i
class Solution {
public:
vector bottomView(Node *root) {
vector res;
if(root==NULL){
return res;
}
map mp;
queue> q;
q.push({root, 0});
while(!q.empty()){
auto temp = q.front();
q.pop();
Node* node = temp.first;
int vLine = temp.second;
mp[vLine] = node->data;
if(node->left){
q.push({node->left, vLine-1});
}
if(node->right){
q.push({node->right, vLine+1});
}
}
for(auto i:mp){
res.push_back(i.second);
}
return res;
}
};
class Solution {
public:
int rangeSum(vector& nums, int n, int left, int right) {
vector sub;
long long MOD = 1e9 + 7;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = i; j < n; j++) {
sum += nums[j];
sub.push_back(sum);
}
}
sort(sub.begin(), sub.end());
long long totalSum = 0;
for (int i = left - 1; i < right; i++) {
totalSum = (totalSum + sub[i]) % MOD;
}
return totalSum;
}
};
class Solution {
public:
static bool compare(pair&a,pair&b){
if(a.second==b.second)
return a.first>vec;
for(int i=0;ivec[prev].second){
prev=i;
ans++;
}
}
return ans;
}
};
