Stock Maximize
Problem Statement :
Your algorithms have become so good at predicting the market that you now know what the share price of Wooden Orange Toothpicks Inc. (WOT) will be for the next number of days. Each day, you can either buy one share of WOT, sell any number of shares of WOT that you own, or not make any transaction at all. What is the maximum profit you can obtain with an optimum trading strategy? Example prices = [1,2] Buy one share day one, and sell it day two for a profit of 1. Return 1. prices = [2,1] No profit can be made so you do not buy or sell stock those days. Return 0. Function Description Complete the stockmax function in the editor below. stockmax has the following parameter(s): prices: an array of integers that represent predicted daily stock prices Returns int: the maximum profit achievable Input Format The first line contains the number of test cases t. Each of the next t pairs of lines contain: - The first line contains an integer n, the number of predicted prices for WOT. - The next line contains n space-separated integers prices[i], each a predicted stock price for day i. Constraints 1 <= t <= 10 1 <= n <= 5000 1 <= prices[i] <= 100000 Output Format Output t lines, each containing the maximum profit which can be obtained for the corresponding test case.
Solution :
Solution in C :
In C++ :
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long int n,a[50005],i,idx,b[50005],sum=0;
scanf("%lld",&n);
for(i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
idx=n;
b[n]=n;
for(i=n-1;i>0;i--)
{
if(a[idx]<a[i])
idx=i;
b[i]=idx;
}
for(i=1;i<=n;i++)
{
if((a[b[i]]-a[i])>=0)
sum+=(a[b[i]]-a[i]);
}
cout<<sum<<endl;
}
}
In Java :
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int T = input.nextInt();
for (int t=0; t<T; t++) {
int N = input.nextInt();
int[] A = new int[N];
for (int n=0; n<N; n++) {
A[n] = input.nextInt();
}
boolean[] buy = new boolean[N];
int max = A[N-1];
for (int n=N-2; n>=0; n--) {
int value = A[n];
if (value > max) {
max = value;
} else {
buy[n] = true;
}
}
long money = 0;
long stock = 0;
for (int n=0; n<N; n++) {
if (buy[n]) {
money -= A[n];
stock++;
} else {
money += stock*A[n];
stock = 0;
}
}
System.out.println(money);
}
}
}
In C :
#include<stdio.h>
int maxarray(int *a,int start,int end)
{
int i,pos,big;
big=a[start];
pos=start;
for(i=start;i<=end;i++)
{
if(a[i]>big)
{
pos=i;
big=a[i];
}
}
return pos;
}
int main()
{
int t,n,i;
scanf("%d",&t);
while(t--)
{
long long pro=0;
int pos=0,start=0;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
while(pos!=(n-1))
{
pos=maxarray(a,start,n-1);
for(i=start;i<pos;i++)
{
pro=pro+(a[pos]-a[i]);
}
start=pos+1;
}
printf("%lld\n",pro);
}
return 0;
}
In Python3 :
test=int(input())
for i in range(test):
n=int(input())
a=list(map(int,input().split()))
c=0
i=len(a)-1
while(i>=0):
d=a[i]
l=i
p=0
while(a[i]<=d and i>=0):
p+=a[i]
i-=1
c+=(l-i)*a[l]-p
continue
print (c)
View More Similar Problems
Balanced Forest
Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a
View Solution →Jenny's Subtrees
Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .
View Solution →Tree Coordinates
We consider metric space to be a pair, , where is a set and such that the following conditions hold: where is the distance between points and . Let's define the product of two metric spaces, , to be such that: , where , . So, it follows logically that is also a metric space. We then define squared metric space, , to be the product of a metric space multiplied with itself: . For
View Solution →Array Pairs
Consider an array of n integers, A = [ a1, a2, . . . . an] . Find and print the total number of (i , j) pairs such that ai * aj <= max(ai, ai+1, . . . aj) where i < j. Input Format The first line contains an integer, n , denoting the number of elements in the array. The second line consists of n space-separated integers describing the respective values of a1, a2 , . . . an .
View Solution →Self Balancing Tree
An AVL tree (Georgy Adelson-Velsky and Landis' tree, named after the inventors) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. We define balance factor for each node as : balanceFactor = height(left subtree) - height(righ
View Solution →Array and simple queries
Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty
View Solution →