GeeksForGeeks - POTD | GFG POTD Answer
قناة بسيطة
1 218
المشتركون
لا توجد بيانات24 ساعات
-97 أيام
-5730 أيام
أرشيف المشاركات
16th January : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
class Solution{
public:
int solve(int i,int j,int n,int m,vector>&dp)
{
if(j==0)
return 1;
if(i>m)
return 0;
if(dp[i][j]!=-1)
return dp[i][j];
int take=solve(i*2,j-1,n,m,dp);
int not_take=solve(i+1,j,n,m,dp);
return dp[i][j]=take+not_take;
}
int numberSequence(int m, int n){
vector>dp(m+1,vector(n+1,-1));
return solve(1,n,n,m,dp);
}
};
15th January : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
class Solution{
public:
int dp[1002][1002];
int func(int n, int t, vector<int> &cost, int i){
if(i==n){
return 0;
}
if(t<=0) return 0;
if(dp[i][t]!=-1) return dp[i][t];
int take = 0, notTake = 0;
if(t>=cost[i]){
take = 1+ func(n, t - cost[i]/10.0, cost, i+1);
}
notTake = func(n, t, cost, i+1);
return dp[i][t] = max(take, notTake);
}
int max_courses(int n, int total, vector<int> &cost)
{
//Code Here
memset(dp, -1, sizeof(dp));
return func(n, total, cost, 0);
}
};
14th January : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
class Solution
{
public:
vector repeatedRows(vector> &arr, int m, int n){
set>st;
vectorans;
int temp = 0;
for (int i=0;i
13th January : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
class Solution
{
public:
Node* insertionSort(struct Node* head_ref)
{
Node* head = new Node(-1);
Node* curr = head_ref;
while(curr)
{
Node* temp = head;
while(temp->next && curr->data > temp->next->data)
{
temp = temp->next;
}
Node* temp_curr = curr;
curr = curr->next;
temp_curr->next = temp->next;
temp->next = temp_curr;
}
return head->next;
}
};
12th January : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
#include <queue>
#include <stack>
class Solution
{
public:
// Function to reverse first k elements of a queue.
std::queue<int> modifyQueue(std::queue<int> q, int k) {
if (k <= 0 || k > q.size()) {
// Invalid input for k
return q;
}
std::stack<int> s;
// Push the first k elements into the stack
for (int i = 0; i < k; i++) {
s.push(q.front());
q.pop();
}
// Pop elements from the stack and enqueue them back to the queue
while (!s.empty()) {
q.push(s.top());
s.pop();
}
// Enqueue the remaining elements in the original order
for (int i = 0; i < q.size() - k; i++) {
q.push(q.front());
q.pop();
}
return q;
}
};
11th January : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
class Solution {
public:
string removeKdigits(string S, int K) {
stack<char>st;
for(int i = 0; i<S.size(); i++)
{
char c = S[i];
while(!st.empty() && K>0 && st.top()>c)
{
st.pop();
K--;
}
st.push(S[i]);
}
while(K--)
{
st.pop();
}
if(st.size()==0)
{
return "0";
}
string ans = "";
while(!st.empty())
{
ans.push_back(st.top());
st.pop();
}
reverse(ans.begin(), ans.end());
int i = 0;
while(ans[i]=='0')
{
i++;
}
if(i==ans.size())
{
return "0";
}
return ans.substr(i);
}
};
10th January : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
class Solution{
public:
int longSubarrWthSumDivByK(int arr[], int n, int k)
{
map m;
m[0]=-1;
int sum=0;
int rem;
int largest=0;
for(int i=0;isecond)>largest) largest=i-itr->second;
}
else m[rem]=i;
}
return largest;
}
};
9th January : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
class Solution
{
public:
vector search(string pat, string txt)
{
vector ans;
int i=0;
int j=0;
while(i
8th January : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
class Solution
{
public:
struct Node* reverse(struct Node* head) {
struct Node* curr = head;
struct Node* prev = NULL;
struct Node* next = NULL;
while(curr != NULL) {
next = curr -> next;
curr -> next = prev;
prev = curr;
curr = next;
}
return prev;
}
struct Node* merge(Node* l1, Node* l2) {
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1;
Node* temp = NULL;
if(l1 -> data < l2 -> data) {
temp = l1;
temp -> next = merge(l1 -> next, l2);
}
else {
temp = l2;
temp -> next = merge(l1, l2 -> next);
}
return temp;
}
struct Node * mergeResult(Node *node1,Node *node2)
{
node1 = merge(node1, node2);
node1 = reverse(node1);
return node1;
}
};
7th January : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
class Solution {
public:
bool solve(int arr[], int N, int K, int mid)
{
int sum = 0;
for(int i = 0; i < N; i++)
{
if(arr[i] > mid)
return false;
sum += arr[i];
if(sum > mid)
{
K--;
sum = arr[i];
}
}
if(K >= 1)
return true;
return false;
}
int splitArray(int arr[] ,int N, int K) {
int sum = 0;
for(int i = 0; i < N; i++)
sum += arr[i];
int low = 0, high = sum;
int ans = sum;
while(low <= high)
{
int mid = (low + high) / 2;
if(solve(arr, N, K, mid))
{
ans = mid;
high = mid - 1;
}
else
low = mid + 1;
}
return ans;
}
};
