Untitled
unknown
c_cpp
a year ago
939 B
92
Indexable
#include<stdio.h> #include<string.h> #include<cmath> // For rounding up // Read only region start int stock(int input1[]){ // Read only region end int n = 10; // Given that stock prices are for the next 10 business days if(n == 0) return 0; int minPrice = input1[0]; int maxProfit = 0; for(int i = 1; i < n; i++) { // Update maxProfit if selling today results in a better profit maxProfit = std::max(maxProfit, input1[i] - minPrice); // Update minPrice if today's price is lower than what we've seen minPrice = std::min(minPrice, input1[i]); } // Convert profit to investment and round up double result = ceil(1000.0 * maxProfit / minPrice); return static_cast<int>(result); } int main() { int prices[] = {110, 180, 260, 310, 40, 535, 695, 765, 830, 905}; printf("Maximum profit is: %d\n", stock(prices)); return 0; }
Editor is loading...