Showing posts with label LEETCODE DAILY CODING SOLUTIONS. Show all posts
Showing posts with label LEETCODE DAILY CODING SOLUTIONS. Show all posts

Saturday, June 5, 2021

LEETCODE JUNE 2021 DAILY CODING CHALLENGE

 LEETCODE JUNE 2021 DAILY CODING CHALLENGE


JUNE 1



JUNE 2



JUNE 3



JUNE 4


JUNE 5

Maximum Performance of a Team

LINK

You are given two integers n and k and two integer arrays speed and efficiency both of length n. There are n engineers numbered from 1 to nspeed[i] and efficiency[i] represent the speed and efficiency of the ith engineer respectively.

Choose at most k different engineers out of the n engineers to form a team with the maximum performance.

The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.

Return the maximum performance of this team. Since the answer can be a huge number, return it modulo 109 + 7.

 

const int mod = 1e9 + 7;

    int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {

        vector<pair<int, int>>ord;

        

        for(int i = 0; i < n; i++){

            ord.push_back({efficiency[i], speed[i]});

        }

        sort(ord.rbegin(), ord.rend());

        priority_queue<int>speedPq;

        

        long totalSpeed = 0, res = 0;

        

        for(auto& p : ord){

            int spd = p.second;

            speedPq.push(-spd);

            if(speedPq.size() <= k)

                totalSpeed += spd;

            else{

                totalSpeed += spd + speedPq.top();

                speedPq.pop();

            }

            res = max(res, totalSpeed*p.first);

        }

        return res% mod;

    }

Sunday, May 9, 2021

LEETCODE DAILY CODING CHALLENGES SOLUTIONS

 MAY DAILY CODING CHALLENGE


1 MAY


2 MAY


3 MAY


4 MAY


5 MAY


6 MAY


7 MAY


8 MAY


9 MAY

CONSTRUCT TARGET ARRAY WITH MULTIPLE SUMS

REFER

Given an array of integers target. From a starting array, A consisting of all 1's, you may perform the following procedure :

  • let x be the sum of all elements currently in your array.
  • choose index i, such that 0 <= i < target.size and set the value of A at index i to x.
  • You may repeat this procedure as many times as needed.

Return True if it is possible to construct the target array from A otherwise return False.


bool isPossible(vector<int>& A) {

        long total = 0;

        int n = A.size(), x;

        priority_queue<int>pq(A.begin(), A.end());

        for(int a : A)

            total += a;

        while(true){

            x = pq.top();

            pq.pop();

            total -= x;

            if(x == 1 || total == 1)

                return true;

            if(x < total || total == 0 || x % total == 0)

                return false;

            x = x % total;

            total += x;

            pq.push(x);

        }

    }

10 MAY

LEETCODE JUNE 2021 DAILY CODING CHALLENGE

 LEETCODE JUNE 2021 DAILY CODING CHALLENGE JUNE 1 JUNE 2 JUNE 3 JUNE 4 JUNE 5 Maximum Performance of a Team LINK You are given two integers ...