Question

In: Computer Science

Write the following function taking in an integer vector (vector &prices) consisting of all prices, in chronological order, for an hypothetical instrument.

/** * FUNCTION SIGNATURE: * PURPOSE: * PARAMETER: * RETURN VALUE: * Write the following function taking in an integer vector (vector &prices) consisting of all prices, in chronological order, for an hypothetical instrument. Your function recommends the maximum profit an investor can make by placing AT MOST one buy and one sell order in the time slice represented by your input vector. Remember BUY LOW, SELL HIGH

Solutions

Expert Solution

#include

using namespace std;

int maxProfit(int price[], int start, int end)

{

    // If the stocks can't be bought

    if (end <= start)

        return 0;

    // Initialise the profit

    int profit = 0;

    // The day at which the stock

    // must be bought

    for (int i = start; i < end; i++) {

        // The day at which the

        // stock must be sold

        for (int j = i + 1; j <= end; j++) {

            // If byuing the stock at ith day and

            // selling it at jth day is profitable

            if (price[j] > price[i]) {

                // Update the current profit

                int curr_profit = price[j] - price[i]

                                  + maxProfit(price, start, i - 1)

                                  + maxProfit(price, j + 1, end);

                // Update the maximum profit so far

                profit = max(profit, curr_profit);

            }

        }

    }

    return profit;

}

// Driver code

int main()

{

    int price[] = { 100, 180, 260, 310,

                    40, 535, 695 };

    int n = sizeof(price) / sizeof(price[0]);

    cout << maxProfit(price, 0, n - 1);

    return 0;

}


Related Solutions

Write a function that takes a numeric or integer vector and adds up only the numbers...
Write a function that takes a numeric or integer vector and adds up only the numbers whose integer parts are even. Modify your answer to question above to include an option that allows you to choose whether to sum numbers whose integer parts are even or are odd. Your function should have as a default that it gives the same output as the function in question 4. In other words, if the user doesn’t specify whether to sum evens or...
Write a function that will accept one integer matrix E and one column vector F by...
Write a function that will accept one integer matrix E and one column vector F by reference parameters, and one integer I as a value parameter. The function will return an integer v, which is the i-th coefficient of row vector denoted by E*F (multiplication of E and F). For example, if V = E*F, the function will return v, which is equal to V[i]. Explain the time complexity of this function inside of the function code as a comment....
Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing...
Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing the smallest value contained in the vector, an integer representing the largest value contained in the vector, and a double that represents the average of the values in the vector. The compileStats function will not “return” a value, it will set the Pass By Reference parameters to the correct values (smallest, largest, and average). Use the following main function in driver1b.cpp as a starting...
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
PWrite a VHDL function that accepts a vector of arbitrary length and integer that specifies the...
PWrite a VHDL function that accepts a vector of arbitrary length and integer that specifies the number of bits the input vector to be rotated to the left and returns another vector. For instance functions accepts two inputs: 0101011 and 3 and it returns 1011010.
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
R programming language A) Create a function that accepts two arguments, an integer and a vector...
R programming language A) Create a function that accepts two arguments, an integer and a vector of integers. It returns the count of the number of occurrences of the integer in the input vector. 1]Input: num_count <-function ??? 2]Input: num_count(2,c(1,1,2,2,3,3)) 2] Output: 2 3] Input: num_count(1,c(1,1,2,2,3,1,4,5,5,2,2,1,3)) 3] Output : 4 B) Create a function that accepts 3 integer values and returns their sum. However, if an integer value is evenly divisible by 3, then it does not count towards the...
Write a function that will accept a list of numbers and an integer (n). The function...
Write a function that will accept a list of numbers and an integer (n). The function should return a list containing every nth item from the input list, always starting with the first item in the list. The original list should not be modified. For example, if the function is passed the list [8, 3, 19, 26, 32, 12, 3, 7, 21, 16] and the integer 3, it will return the list [8, 26, 3, 16] If the function is...
Write a function convert_date that takes an integer as a parameter and returns three integer values...
Write a function convert_date that takes an integer as a parameter and returns three integer values representing the input converted into days, month and year (see the function docstring). Write a program named t03.py that tests the function by asking the user to enter a number and displaying the output day, month and year. Save the function in a PyDev library module named functions.py A sample run for t03.py: Enter a date in the format MMDDYYYY: 05272017 The output will...
Hi i have this for a practical tomorrow (C++): a) Write a function vector merge(vector a,...
Hi i have this for a practical tomorrow (C++): a) Write a function vector merge(vector a, vector b) that merges two vectors, alternating elements from both vectors. If one vector is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer vector. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 Then merge returns the vector 1 9 4 7 9 4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT