Stock Liao information

— Basic knowledge of stocks|Introduction to basics of stocks|Stock learning|Basic knowledge of stocks
Mobile access:m.liaochihuo.com

[Java] The sword refers to the offer(63) the maximum profit of the stock

Release Time:2021-07-20 Topic:Stock trading priority Reading:8 Navigation:Stock Liao information > Education > [Java] The sword refers to the offer(63) the maximum profit of the stock phone-reading

This article refers to the book "Sword Finger Offer", the code uses Java language.

Title

Assuming that the price of a certain stock is stored in an array in chronological order, what is the possible profit from trading the stock? For example, the price of a stock at certain time nodes is {9, 11, 8, 5, 7, 12, 16, 14}. If we can buy when the price is 5 and sell when the price is 16, we can reap the largest profit 11.

Idea

Iterate through each number and save the smallest number before. The largest difference between the two is the maximum profit.

It is worth noting that the code I wrote at the beginning is that it cannot lose money by default (that is, you can not buy and sell, and the profit cannot be negative), so it is relatively simple; but if you can lose money, the maximum profit means Is the smallest loss, so it should be noted that the smallest number cannot be the last one. In the following code, you can pay attention to the difference between the two cases. Examples that can be considered are {16, 11, 7, 4, 2, 1 }

Test case

1. Functional test (array increment/decrement/disorder)

p>

2. Special test (null, empty array)

3. Boundary value test (array only two numbers)

Java code

//Title: Assuming that the price of a certain stock is stored in an array in chronological order, may I ask you to buy and sell the stock

//What is the possible profit from the ticket? For example, the price of a stock at certain time nodes is {9, 11, 8, 5,

//7, 12, 16, 14}. If we can buy when the price is 5 and sell when the price is 16, we can

//receive the largest profit 11.

public class MaximalProfit {

public static int MaxDiff(int[] arr) {

if(arr==null || arr.lengthmaxDiff)

maxDiff=arr[ i ]-min;

}

//You cannot lose money by default, the code is simple, and the complex code above pays attention to details

//int maxDiff=0;

//for(int i=1;i

//if(arr[ i ]

//min =arr[ i ];

//else if(arr[ i ]-min>maxDiff)

//maxDiff=arr[ i ]-min;

//}

return maxDiff;

}

//Simple and quick test

public static void main(String[ ] args) {

int[] arr1=null;

System.out.println(MaxDiff(arr1)==-1);

int[ ] arr2={ };

System.out.println(MaxDiff(arr2)==-1);

int[] arr3={ 16, 16, 16, 16, 16 };

System.out.println(MaxDiff(arr3)==0);

int[] arr4={ 1, 2, 4, 7, 11, 16 };

System.out.println(MaxDiff(arr4)==15);

int[] arr5={ 16, 11, 7, 4, 2, 1 };

System.out.println(MaxDiff(arr5)==-1);

int[] arr6 ={ 9, 11, 5, 7, 16, 1, 4, 2 };

System.out.println(MaxDiff(arr6)==11);

int[ ] arr7={ 2,4};

System.out.println(MaxDiff(arr7)==2);

int[] arr8={ 4,2};

System.out.println(MaxDiff(arr8)==-2);

}

}

true

true

true

true

true

true

true

true

MaximalProfit

Gains

1. The time complexity of the brute force method is O(n^2), which is definitely wrong. We traverse from beginning to end to determine the law. It can be found that just find the previous minimum value.

Article Url:https://www.liaochihuo.com/info/614502.html

Label group:[stock] [profit

Hot topic

Education recommend

Education Popular