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:
int maxSubarrayLength(vector& nums, int k) {
int i = 0, j = 0;
map mp;
int res = 0;
while(j < nums.size()){
mp[nums[j]]++;
while(mp[nums[j]] > k && i < j){
mp[nums[i]]--;
i++;
}
res = max(res, j - i + 1);
j++;
}
return res;
}
};
class Solution {
public:
int findCity(int n, int m, vector<vector<int>>& edges, int dt) {
// Your code here
vector<pair<int,int>>adj[n];
for(int i=0;i<edges.size();i++)
{
adj[edges[i][0]].push_back({edges[i][1],edges[i][2]});
adj[edges[i][1]].push_back({edges[i][0],edges[i][2]});
}
vector<pair<int,int>>v;
int maxi = INT_MAX;
int result = -1;
for(int i=0;i<n;i++)
{
priority_queue<pair<int,int> , vector<pair<int,int>>, greater<pair<int,int>>>q;
vector<int>visited(n,1e9);
visited[i]=0;
q.push({i,0});
int count=0;
while(!q.empty())
{
pair<int,int>ll=q.top();
int h=ll.first;
int k=ll.second;
q.pop();
for(auto it:adj[h])
{
if( k+it.second<visited[it.first])
{
visited[it.first]=k+it.second;
q.push({it.first,k+it.second});
}
}
}
for(int j = 0; j < visited.size(); j++){
if(visited[j] <= dt){
count++;
}
}
if(count <= maxi){
maxi = count;
result = i;
}
}
return result;
}
};
class Solution {
public:
int numSubarrayProductLessThanK(vector& nums, int k) {
if(k <= 1)
return 0;
int n = nums.size();
int count = 0;
int left = 0;
int right = 0;
int prod = 1;
while(right < n){
prod *= nums[right];
while(prod >= k) {
prod /= nums[left];
left++;
}
count += (right-left)+1;
right++;
}
return count;
}
};
class Solution
{
public:
int findShortestPath(vector> &mat)
{
int r = mat.size(), c = mat[0].size();
int dir[5] = {-1, 0, 1, 0, -1};
for(int i=0; i= 0 and x < r and y >= 0 and y < c and mat[x][y] == 1)
mat[x][y] = 2;
}
}
}
}
vector> visited(r, vector (c, false));
queue> Q;
for(int i=0; i= 0 and x < r and y >= 0 and y < c and mat[x][y] == 1 and !visited[x][y])
{
Q.push({x,y});
visited[x][y] = true;
}
}
}
level++;
}
return -1;
}
};
class Solution {
public:
int firstMissingPositive(vector& A) {
int n = A.size();
for(int i = 0;i < n;i++)
{
while(A[i] > 0 and A[i] <= n and A[A[i] - 1] != A[i])
{
swap(A[i] , A[A[i] - 1]);
}
}
for(int i = 0;i < n;i++)
{
if(A[i] != i + 1)
{
return i + 1;
}
}
return n + 1;
}
};
class Solution {
public:
bool valid(string &n,int k,int num1,int num2){
for(int i=k;i
class Solution {
public:
vector findDuplicates(vector& nums) {
vector res;
map mp;
for(int i=0;i
class Solution{
public:
void find(vector<string>& s, string temp, int n,int count1, int index)
{
if(count1<(temp.size()+1)/2) return;
if(index==n){
s.push_back(temp);
return;
}
find(s, temp+'1', n, count1+1, index+1);
find(s, temp+'0', n, count1, index+1);
}
vector<string> NBitBinary(int n)
{
// Your code goes here
vector<string> s;
find(s,"",n,0,0);
return s;
}
};
class Solution{
public:
stack<int> insertAtBottom(stack<int> st,int x){
vector<int> temp;
while(!st.empty()){
temp.push_back(st.top());
st.pop();
}
st.push(x);
for(int i=temp.size()-1; i>=0; i--){
st.push(temp[i]);
}
return st;
}
};
