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:
vector restoreArray(vector>& nums) {
vector ans;
unordered_map> mp;
int n=nums.size();
for(int i=0;i
class Solution{
public:
string printMinNumberForPattern(string S){
int maxi = 0;
int i = 0;
vector v;
string s;
bool from_I = false;
if(S[0] == 'I')
{
from_I = true;
v.push_back(1);
maxi = 1;
}
while(i < S.size())
{
if(S[i] == 'I')
{
from_I = true;
int c = 0;
for(int j = i+1; j < S.size(); j++)
{
if(S[j] == 'D')
{
c++;
}
else break;
}
v.push_back(c + 1 + maxi);
maxi = c + 1 + maxi;
i++;
}
else
{
if(from_I)
{
int c = 0;
int temp = maxi;
while(S[i] == 'D')
{
temp = temp - 1;
v.push_back(temp);
i++;
}
}
else
{
int c = 0;
for(int j = i; j < S.size(); j++)
{
if(S[j] == 'D')
{
c++;
}
else break;
}
maxi = maxi + c + 1;
int temp = maxi;
for(int j = i; j < i + c; j++)
{
v.push_back(temp);
temp--;
}
v.push_back(1);
i = i + c;
}
}
}
for(auto i : v)
{
s.push_back(i + '0');
}
return s;
}
};
class Solution {
public:
int mod = 1e9+7;
int countHomogenous(string s) {
int ans = 1;
int n = s.size();
int count = 1;
for(int i=1;i
class Solution{
public:
/*Function to count zeros in each column
* N : Number of rows and columns in array
M is the matrix that is globally declared
*/
int columnWithMaxZeros(vector>arr,int N){
int count=0;
int data=-1;
int max=-1;
for(int i=0;i0 && count>data){
data=count;
max=i;
}
count=0;
}
return max;
}
};
class Solution {
public:
bool isReachableAtTime(int sx, int sy, int fx, int fy, int t) {
int d=max(abs(sx-fx), abs(sy-fy));
if (t==1&&d==0)
return 0;
return d<=t;
}
};
class Solution
{
public:
//Function to return list of integers visited in snake pattern in matrix.
vector snakePattern(vector > matrix)
{
vectorans;
int n=matrix.size();
for(int i=0;i
class Solution {
public:
int eliminateMaximum(vector<int>& dist, vector<int>& speed) {
int x = 1;
int n = dist.size();
vector<pair<int,int>>vp;
vector<int> ans;
for(int i = 0;i<n;i++){
ans.push_back(ceil((double)dist[i]/speed[i]));
}
sort(ans.begin(), ans.end());
for(int i = 0;i<n;i++){
cout << ans[i] << " ";
}
cout << endl;
for(int i = 0;i<n-1;i++){
if(ans[i]==ans[i+1]&&x>=ans[i+1]){
return x;
}
x++;
}
return x;
}
};
class Solution
{
public:
//Function to return sum of upper and lower triangles of a matrix.
vector sumTriangles(const vector >& matrix, int n)
{
int a=0,b=0;
for(int i=0;i
class Solution{
public:
vector matrixSum(int n, int m, vector> mat, int q, vector queries[])
{
vector answer;
int hop,sum,i,j;
for(int k = 0; k < q; k++)
{
hop = queries[k][0];
i = queries[k][1];
j = queries[k][2];
sum = 0;
for(int ele = j-hop; ele <= j+hop ; ele++)
{
if(i-hop >=0 && ele >= 0 && ele <= m-1)
{
sum = sum + mat[i-hop][ele];
}
if(i+hop <= n-1 && ele >= 0 && ele <= m-1)
{
sum = sum + mat[i+hop][ele];
}
}
for(int ele = i-hop+1; ele <= i+hop-1 ; ele++)
{
if(j-hop >=0 && ele >= 0 && ele <= n-1)
{
sum = sum + mat[ele][j-hop];
}
if(j+hop <= m-1 && ele >= 0 && ele <= n-1)
{
sum = sum + mat[ele][j+hop];
}
}
answer.push_back(sum);
}
return answer;
}
};
