Solution maxProfit is a problem statement of finding the maximum profit that can be achieved by buying and selling stocks in a given array of prices. The prices array is represented as a vector of positive integers and the function returns an integer value representing the maximum profit that can be made.
Example: Let's consider an example of given array of prices [10,20,30,40,50]. To maximize the profit, we buy the stock at a lower price and sell it at a higher price, which means we have to determine the minimum price and maximum price in given array of prices. In this example, we start with 10 as the minimum price and then compare it with the next price. We can then determine the difference between the maximum and minimum value to get the maximum profit. Therefore, the maximum profit that can be made in this example is 40.
Code Example: Here is the sample code in C++ using the STL library:
#include #include #include
using namespace std;
int maxProfit(vector& prices) { int minPrice = INT_MAX; int maxProfit = 0;
Package Library: The C++ Standard Library (STL) is used in our example code to import necessary header files. The header files used are ``, `` and ``.
C++ (Cpp) Solution::maxProfit - 30 examples found. These are the top rated real world C++ (Cpp) examples of Solution::maxProfit from package LeetCode extracted from open source projects. You can rate examples to help us improve the quality of examples.