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:
string removeDuplicateLetters(string s) {
int n=s.size();
string result;
vectorlastindex(26);
vectortaken(26,false);
for(int i=0;i0 and result.back()>ch and lastindex[result.back()-'a']>i){
taken[result.back()-'a']=false;
result.pop_back();
}
result.push_back(ch);
taken[idx]=true;
}
return result;
}
};
class Solution{
public:
// arr[] : int input array of integers
// k : the quadruple sum required
vector > fourSum(vector &arr, int k) {
int n=arr.size();
vector >res;
sort(arr.begin(),arr.end());
for(int i=0;i0 and arr[i]==arr[i-1])
continue;
for(int j=i+1;ji+1 and arr[j]==arr[j-1])
continue;
int l=j+1;
int r=n-1;
while(r>l)
{
if(arr[i]+arr[j]+arr[l]+arr[r]==k)
{
res.push_back({arr[i],arr[j],arr[l],arr[r]});
l++;r--;
while(lk)
r--;
else
l++;
}
}
}
return res;
}
};
class Solution {
public:
char findTheDifference(string s, string t) {
int ans=t[t.length()-1];
for(int i=0;i
class Solution {
public:
vector<int> maxCombinations(int N, int K, vector<int> &A, vector<int> &B) {
// code here
sort(A.begin(),A.end());
sort(B.begin(),B.end());
vector<int>ans;
priority_queue<pair<int,int>>pq;
for(int i=0;i<N;i++)
{
int sum = A[i]+B[N-1];
pq.push({sum,N-1});
}
while(!pq.empty() and K--)
{
int sum = pq.top().first;
int idx = pq.top().second;
pq.pop();
ans.push_back(sum);
if(idx-1>=0)
pq.push({sum-B[idx]+B[idx-1], idx-1});
}
return ans;
}
};
class Solution {
public:
double champagneTower(int poured, int query_row, int query_glass) {
if (poured==0) return 0;
vector<double> row(1, poured);
for(int i=0; i<query_row; i++){
vector<double> next(i+2, 0);
for(int j=0; j<=i; j++){
if (row[j]<=1) continue;
double excess=(row[j]-1)/2.0;
next[j]+= excess;
next[j+1]+= excess;
}
row=next;
}
return min(1.0, row[query_glass]);
}
};
class Solution{
public:
vector duplicates(int arr[], int n) {
vectorv;
int flag = 0;
sort(arr,arr+n);
for(int i=0; i 0) return v;
return {-1};
}
};
class Solution {
public:
static bool compare(string &s1,string &s2){
return s1.size() < s2.size();
}
bool isPredecessor(string s1, string s2){
if(s1.length() + 1 == s2.length()){
int count = 0;
int k = 0;
char c = s1[0];
for(int i = 0; i1){
return false;
}
}
}
return true;
}
return false;
}
int longestStrChain(vector& words) {
sort(words.begin(), words.end(), compare);
int start = 0;
int end = 1;
int maxCount = 1;
map m;
for(int i = 0;i
class Solution{
public:
// Function to find equilibrium point in the array.
// a: input array
// n: size of array
int equilibriumPoint(long long a[], int n) {
long long sum=0;
long long crsum=0;
for(int i=0;i
class Solution {
public:
bool isSubsequence(string s, string t) {
int i=0;
int j=0;
int n1=s.size();
int n2=t.size();
while(i
class Solution
{
public:
vector<int> find(int arr[], int n , int x )
{
int s = 0;
int e=n-1;
//vector<int> ans;
int first = -1;
while(s<=e){
int mid = s + (e-s)/2;
if(arr[mid]==x){
first = mid;
e = mid - 1;
}
else if(arr[mid]>x){
e = mid -1;
}
else{
s = mid +1;
}
}
s = 0;
e = n-1;
int last = -1;
while(s<=e){
int mid = s + (e-s)/2;
if(arr[mid]==x){
last = mid;
s = mid + 1;
}
else if(arr[mid]>x){
e = mid -1;
}
else{
s = mid + 1;
}
}
return {first, last};
}
};
