en
Feedback
GEEKS FOR GEEKS SOLUTIONS🫢

GEEKS FOR GEEKS SOLUTIONS🫢

Open in Telegram

🚩 Channel was restricted by Telegram

Show more
1 342
Subscribers
No data24 hours
-27 days
-2930 days
Posts Archive
19 th june || c++

class Solution{ public: vector temp; // arr: input array // n: size of array //Function to rearrange an array so that arr[i] becomes arr[arr[i]] //with O(1) extra space. void arrange(long long arr[], int n) { for(int i = 0; i

18 th june || c++

class Solution { public: int distributeTicket(int totalTickets, int groupSize) { int quotient = totalTickets / groupSize; int ticketIndex; if (quotient % 2 == 1) { ticketIndex = ((quotient / 2) + 1) * groupSize; if (totalTickets % groupSize == 0) return ticketIndex; else return ticketIndex + 1; } else { ticketIndex = (totalTickets + 1) - (quotient / 2) * groupSize; if (totalTickets % groupSize == 0) return ticketIndex; else return ticketIndex - 1; } } };

17 th june || c++

class Solution{ public: void insert(queue &q, int k){ q.push(k); } int findFrequency(queue &q, int k){ queueq2=q; int count=0; while(!q2.empty()){ if(q2.front()==k){ count++; } q2.pop(); } return count; } };

16 th june || c++

public: long long minTime(int n, vector &locations, vector &types) { unordered_map mxPos, mnPos; for(int i=0;i disTypes; for(auto it = mxPos.begin(); it != mxPos.end(); it++) { disTypes.push_back(it->first); } sort(disTypes.begin(), disTypes.end()); int l = disTypes.size(); vector> dp(l,vector(2, 0)); dp[l-1][0] = abs(mnPos[disTypes[l-1]] - mxPos[disTypes[l-1]]) + abs(mxPos[disTypes[l-1]] - 0); dp[l-1][1] = abs(mxPos[disTypes[l-1]] - mnPos[disTypes[l-1]]) + abs(mnPos[disTypes[l-1]] - 0); for(int i=disTypes.size()-2;i>=0;i--) { // dp[i][0] : starting from minimum location of disType[i] dp[i][0] = abs(mnPos[disTypes[i]] - mxPos[disTypes[i]]); long long A = abs(mxPos[disTypes[i]] - mnPos[disTypes[i+1]]) + dp[i+1][0]; long long B = abs(mxPos[disTypes[i]] - mxPos[disTypes[i+1]]) + dp[i+1][1]; dp[i][0] += min(A,B); // dp[i][1] : starting from maximum location of disType[i] dp[i][1] = abs(mxPos[disTypes[i]] - mnPos[disTypes[i]]); A = abs(mnPos[disTypes[i]] - mnPos[disTypes[i+1]]) + dp[i+1][0]; B = abs(mnPos[disTypes[i]] - mxPos[disTypes[i+1]]) + dp[i+1][1]; dp[i][1] += min(A,B); } long long ans1 = abs(0 - mnPos[disTypes[0]]) + dp[0][0]; long long ans2 = abs(0 - mxPos[disTypes[0]]) + dp[0][1]; return min(ans1, ans2); }

15 th june || c++

string longestPalin (string S) { int st=0,end=0,n=S.length(); for(int i=0;i=0 && k=0 && k

14 th june || c++

class Solution { public: long long maxDiamonds(int A[], int N, int K) { long long ans=0; priority_queue<int>pq; for(int i=0;i<N;i++){ pq.push(A[i]); } int i=0; while(i<K){ int temp=pq.top(); pq.pop(); ans+=temp; temp=temp/2; pq.push(temp); i++; } return ans; } };

14 th june || c++

class Solution { static long maxDiamonds(int[] A, int N, int K) { PriorityQueue q = new PriorityQueue<>(Collections.reverseOrder()); for(int i: A){ q.add(i); } long res = 0; while(K>0){ int x = q.poll(); res += x; q.add(x/2); K--; } return res; } }

13 th june || c++

class Solution: def kLargest(self,arr, n, k): arr.sort(reverse=True) return arr[:k]

12 th june || c++

class Solution{ public: int f(int price[], int n,int index, vector &dp) { if(index==n) return 0; if(dp[index]!=-1) return dp[index]; int ans = 0; for(int i=1;i<=n-index;i++) { int cut = price[i-1] + f(price,n,index+i,dp); ans = max(ans,cut); } return dp[index] = ans; } int cutRod(int price[], int n) { vector dp(n,-1); return f(price,n,0,dp); } };