Snippets Collections
class Solution {
public:
    using ll = long long;
    int countBits(ll x){
        int cnt = 0;
        while(x > 0){
            cnt += (x&1);
            x>>=1;
        }
        return cnt;
    }
    int makeTheIntegerZero(int num1, int num2) {
        if(num1 < num2)
            return -1;
        ll x = num1;
        for(int i = 0; i <= 100; i++){
            ll val = x - (ll)i*num2;
            int cnt_ones = countBits(val);
            if(cnt_ones <= i && i <= val)
                return i;
        }
        return -1;
    }
};
int numberOfGoodSubarraySplits(vector<int>& nums) {
    long long ans = 1, m = 1000000007, count  = 0;
    int i = 0;
    while(i < nums.size() && nums[i] == 0) ++i;
    if(i >= nums.size() ) return 0;
    while(i < nums.size()){
        if(nums[i] == 1){  ans = (ans * (count +1 ))%m;  count = 0; }
        else count++;
        i++;
    }
    return ans;
class Solution {
  public:
    map<int,int> deno{
        {5,0},
        {10,0},
        {20,0}
    };
    bool canCreateDeno(int x){
       for(auto i = deno.rbegin(); i != deno.rend();i++){
           if(i->second > 0 ){
               int times = (x / i->first);
               times = min(times, i->second);
               x -= times * i->first;
               i->second -= times;
           }
       } 
       return x == 0;
    }
    bool lemonadeChange(int N, vector<int> &bills) {
        // code here
        for(int i = 0 ; i < N; i++){
            int x = bills[i] - 5;
            if(x == 0){
                deno[bills[i]]++;
            }
            else{
                bool f = canCreateDeno(x);
                if(!f)
                    return false;
                deno[bills[i]]++;
            }
        }
        return true;
    }
};
class Solution
{
    public:
    //Function to find the maximum number of meetings that can
    //be performed in a meeting room.
    int maxMeetings(int start[], int end[], int n)
    {
        // Your code here
        using pi = pair<int,int> ;
        vector<pi> vp;
        for(int i = 0; i < n; i++)
            vp.push_back({start[i] , end[i]});
        sort(vp.begin(), vp.end(),[](auto a, auto b){
           return a.second < b.second; 
        });
        int ans = 0, r = INT_MIN;
        for(int  i =0 ; i < n ; i++){
            if(vp[i].first > r){
                ans++;
                r = vp[i].second;
            }
        }
        return ans;
    }
};
star

Sun Jun 25 2023 04:54:02 GMT+0000 (Coordinated Universal Time) https://leetcode.com/contest/weekly-contest-351/problems/minimum-operations-to-make-the-integer-zero/

#c++ #greedy
star

Sun Jun 25 2023 04:23:41 GMT+0000 (Coordinated Universal Time) https://leetcode.com/problems/ways-to-split-array-into-good-subarrays/discuss/

#contest #greedy #not_solved #looked_like_dp
star

Thu Jun 22 2023 05:31:24 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/lemonade-change/1

#c++ #lemonade_change #greedy #maps
star

Thu Jun 08 2023 07:52:02 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/n-meetings-in-one-room-1587115620/1

#n_meetings #greedy #gfg #c++

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension