Giter VIP home page Giter VIP logo

gfg_potd's Introduction

GFG Problem Of The Day

Date - 24 December, 2023

In a stock market, there is a product with its infinite stocks. The stock prices are given for N days, where price[i] denotes the price of the stock on the ith day.

  • There is a rule that a customer can buy at most i stock on the ith day.
  • If the customer has an amount of k amount of money initially. The task is to find out the maximum number of stocks a customer can buy.

Approach Used :

- Goal is to buy maximum number of stocks, so we buy stocks from the cheapest to more expensive, hence sorting is required but we have to keep the index in mind.

- First form a list stock with (price, index). For this question, the index is also the maximum number of this stock we can buy at this price.

- Sort the list by price.

- We keep buying stock from the beginning of the list until we run out of money.

Example : N = 3, k = 45, arr = [10, 7, 19]

Output : 4

Explanation:

  • First create a list of stock with (price, index): [(10,1), (7,2), (19,3)].
  • Then sort the list, it becomes : [(7,2), (10,1), (19,3)]
  • Now, keep buying the stock until we ran out of money, here with k = 45, we can buy: 72 + 101 + 19*1 = 43.
  • Hence, total 4 stocks we can buy with k = 45;

Time and Auxiliary Space Complexity

  • Time Complexity : O(NLogN), because of sorting the vector array
  • Auxiliary Space Complexity : O(N), vector array size

Code (C++)

class Solution {
public:
    int buyMaximumProducts(int n, int k, int price[]) {
        vector<pair<int, int>> v;
        for (int i = 0; i < n; ++i)
            v.push_back({price[i], i + 1});

        sort(v.begin(), v.end());

        int count = 0;

        for (auto i : v) {
            int maxBuy = min(k / i.first, i.second);
            count += maxBuy;
            k -= i.first * maxBuy;
        }
        return count;
    }
};

Contribution and Support

If you have a better solution or any queries / discussions , please visit our discussion section.

If you find this solution helpful, consider supporting us by giving a โญ star to the SHRbharat/gfg_potd repository.

Total number of repository visitors

Total number of repository stars

gfg_potd's People

Contributors

dineshk3012 avatar shrbharat avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

dineshk3012

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.