If you wish to survive, you need to cultivate a strong mental attitude. If you want to live, you need to develop a strong heart.


An overview of the

Inventory interview often asked what questions of buying and selling stocks, the program brother here sorted out and attached the answer, the end of the article has the program brother in the spring and autumn recruitment process sorted out the interview FAQ PDF, welcome the public number backstage message, free!!

  • The best time to buy and sell stocks
  • The best time to buy and sell stocks
  • The best time to buy and sell stocks
  • Best time to buy and sell stocks [including freeze period]
  • The best time to buy and sell stocks [including fees]
  • Two people take turns placing coins on the table. Who wins?

1 the best time to buy and sell stocks

Given an array, its ith element is the price of a given stock on the ith day. If you only allow one trade (buy and sell a stock once), design an algorithm to calculate the maximum profit you can make. Note: You cannot sell a stock before you buy it. Input: [7,1,5,3,6,4] output: 5 explanation: buy on day 2 (stock price = 1) and sell on day 5 (stock price = 6), maximum profit = 6-1 = 5. Note that the profit cannot be 7-1 = 6, because the selling price needs to be higher than the buying price; Also, you can’t sell stocks before you buy them.

The answer

You just care about the current value and the historical minimum.public static int maxProfit(int[] prices) {
    int result = 0;
    int min = Integer.MAX_VALUE;
    for (int num : prices) {
 min = Math.min(min, num);  result = Math.max(result, num - min);  }  return result; } Copy the code

2 The best time to buy and sell stocks

Core: Buy and sell on the same day, twice,

The answer

 public int maxProfit(int[] prices) {
    int minPrice1 = Integer.MAX_VALUE;         
    int maxProfit1 = 0;                        
    int maxProfitAfterBuy = Integer.MIN_VALUE; 
    int maxProfit2 = 0;                        
 for(int price : prices) { // 1. Minimum purchase price for the first time minPrice1 = Math.min(minPrice1, price);  // 2. Maximum profit from the first sale maxProfit1 = Math.max(maxProfit1, price - minPrice1);  // 3. Residual net profit after the second purchase maxProfitAfterBuy = Math.max(maxProfitAfterBuy, maxProfit1 - price );  // 4. Maximum total profit after the second sale (net profit of step 3 + stock money sold at Step 4) maxProfit2 = Math.max(maxProfit2, price + maxProfitAfterBuy);  }  return maxProfit2; } Copy the code

3 The best time to buy and sell stocks [unlimited]

Design an algorithm to calculate the maximum profit you can make. You can make as many trades (buying and selling a stock multiple times) as possible. Note: You can’t participate in more than one transaction at a time (you must sell your previous shares before buying again). Input: [7,1,5,3,6,4] Output: 7 Explanation: If you buy on day 2 (stock price = 1) and sell on day 3 (stock price = 5), the exchange will make a profit = 5-1 = 4. Then, by buying on day 4 (stock price = 3) and selling on day 5 (stock price = 6), the exchange makes a profit = 6-3 = 3

The answer

/ / many timespublic static int maxProfit(int[] prices) {
    int res = 0;
// Multiple trades, for maximum profit, need to be bought and sold on the upswing of each link    for(int i =0; i <prices.length-1; i++){
 if(prices[i+1] - prices[i] > 0){  res += prices[i+1] - prices[i];  }  }  return res; } Copy the code

4 The best time to buy and sell stocks [including freeze period]

After selling, you cannot buy the stock the next day (i.e. freeze period is 1 day).

The answer

public static int maxProfit(int[] prices) {
    if (prices.length == 0) {
        return 0;
    }
// Define three arrays to represent three states, int n = prices.length; int[] A = new int[n]; // Wait and see the best profit in state A on day Iint[] B = new int[n]; / / stake - pricesint[] C = new int[n]; / / cooling + prices A[0] = 0;  B[0] = C[0] - prices[0];  for (int i = 1; i < n; i++) { // There are two ways to go into A state, a-A and C-a  A[i] = Math.max(A[i - 1], C[i - 1]); // There are two ways to enter the B state, b-B and A-B B[i] = Math.max(B[i - 1], A[i - 1] - prices[i]); // There is only one way to change to C C[i] = B[i - 1] + prices[i];  }  return Math.max(A[n - 1], C[n - 1]); } Copy the code

5 The best time to buy and sell stocks [including fees]

Input: prices = [1, 3, 2, 8, 4, 9], fee = 2 output: 8 total profits: (a), (8-1-2) + (a) (9-4-2) = 8.

The answer

public static int maxProfit(int[] prices, int fee) {
    int len = prices.length;
    if (len < 2) {
        return 0;
    }
// dp[I][j] represents the maximum return from the interval [0, I] to the first day when the state is j//j = 0 indicates no shareholding, j = 1 indicates shareholding int[][] dp = new int[len][2]; dp[0][0] = 0; / / no stakedp[0][1] = -prices[0] - fee; / / stake for (int i = 1; i < len; i++) {  dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] + prices[i]);  dp[i][1] = Math.max(dp[i-1][1], dp[i][0] -prices[i] - fee);  }  return dp[len-1][0]; } Copy the code

6 Two people take turns to put coins on the table, who will win

Two people on the round table take turns to drop the coin, the last person to drop the coin and just occupy the table, ask if there is a way to win?

The “answer” is actually very simple, as long as the first coin, and placed in the center of the table, according to symmetry, can guarantee that the first person will win.


Share via PDF

  • Public number background reply [add group], the program brother will see the first time to share.


This article is formatted using MDNICE