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

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 SOLUTIONS

 VALID PARENTHESIS

REFER

Given a string s containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

bool isValid(string s) {

        stack<char>st;

        for(int i = 0; i < s.length(); i++){

            switch(s[i]){

                case '(':

                case '{':

                case '[':

                    st.push(s[i]);

                    break;

                case ')':

                    if(st.empty() || st.top() != '(')

                        return false;

                    else 

                        st.pop();

                        break;

                case '}':

                    if(st.empty() || st.top() != '{')

                        return false;

                    else 

                        st.pop();

                        break;

                case ']':

                    if(st.empty() || st.top() != '[')

                        return false;

                    else 

                        st.pop();

                        break;      

            }

        }

        return st.empty();

    }


BEST TIME TO BUY AND SELL STOCKS - 1

REFER

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.


int maxProfit(vector<int>& prices) {

        int minSoFar = prices[0];

        int maxProfit = 0;

        for(int i = 0; i < prices.size(); i++){

            //minSoFar = minSoFar < prices[i] ? minSoFar : prices[i];

            minSoFar = min(minSoFar, prices[i]);

            int profit = prices[i] - minSoFar;

            maxProfit = max(profit, maxProfit);

            //if(profit > maxProfit)

                // maxProfit = profit;

        }

        return maxProfit;

    }


BEST TIME TO BUY AND SELL STOCKS - 2

REFER

You are given an array prices where prices[i] is the price of a given stock on the ith day.

Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

int maxProfit(vector<int>& prices) {

        int totProfit = 0;

        for(int i = 1; i < prices.size(); i++){

            if(prices[i] > prices[i-1])

                totProfit += prices[i] - prices[i-1];

        }

        return totProfit;

    }


AVERAGE OF LEVELS IN BINARY TREE

REFER

Given the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5 of the actual answer will be accepted.

vector<double> averageOfLevels(TreeNode* root) {

        vector<double>res;

        if(root == NULL)

            return res;

        queue<TreeNode*>q;

        q.push(root);

        long long sum = 0;

        int NodeCount = 0, n = 0;

        while(!q.empty()){

            NodeCount = q.size();

            n = NodeCount;

            sum = 0;

            while(NodeCount > 0){

                TreeNode* tmp = q.front();

                q.pop();

                sum += tmp->val;

                if(tmp->left != NULL)

                    q.push(tmp->left);

                if(tmp->right != NULL)

                    q.push(tmp->right);

                NodeCount--;

            }

            double avg = (double)sum/n;

            res.push_back(avg);

        }

        return res;

    }


BALANCED BINARY TREE

REFER

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as:

a binary tree in which the left and right subtrees of every node differ in height by no more than 1.

int height(TreeNode* node){

        if(node == NULL)

            return 0;

        int lh = height(node->left);

        int rh = height(node->right);

        return 1 + max(lh, rh);

    }

    bool isBalanced(TreeNode* root) {

        if(root == NULL)

            return true;

        if(abs(height(root->left) - height(root->right)) <= 1)

            return isBalanced(root->left) && isBalanced(root->right);

        return false;

    }

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 ...