# 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

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

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

• ### I want to enter an investment bank, what should I do?

The host is only a junior, but there are still many opportunities. When I was undergraduate, I didnt even know what investment banking was. Howeve...

• ### What is the experience of working in the investment banking department of a domestic securities firm?

In this experience post, a friend of Uni sauce studied at Xi’an Jiaotong University with a bachelor’s degree, and transferred his master’s degree to ...

• ### I have bought so many VIPs, but it is the only one who can buy 3 tickets!

When did you do so many VIPs?It is always when the payment is automatically deducted at the beginning of the month, we realize that the number of mem...

• ### 1.2 trillion profit and tax a year! China's most profitable company is here, 300 million people pay the bill

Wandering through the minefield, we finally ushered in the most profitable company in China, none of which officially announced its 2018 results Acc...

• ### Created 500 billion transactions a year! This private equity has another major event, and the China Securities Regulatory Commission has taken action!

China Fund News reporter Gao Wen Last year, he was fined 1.6 million yuan for manipulating ETF funds, and now he is trading illegally CABR received ...

• ### New Year's Speech by the Four Major Securities Newspapers: A-shares have entered a period of fierce battle, and it will be better year after year

Hot column Funds flow to thousands of shares, thousands of reviews individual stock diagnosis latest rating simulation trading client Download Sina...

• ### Four major securities newspapers collectively talk about A shares: funds remain optimistic about the medium and long-term market

Abstract Four major securities newspapers collectively discuss A shares Securities Daily Five major problems caused the value of A stock market to eva...

• ### Highlights of the four major newspapers: the central bank vowed to stabilize the exchange rate

NetEase Finance News, May 24th The essence of the headlines of the four major securities newspapers on May 24th. China Securities Journal Liu Gu...