[Leetcode] dynamic planning (39 questions in the next article)

Source: Internet
Author: User
Tags stock prices

[600] non-negative integers without consecutive ones

(629) K inverse pairs Array

[638] Shopping offers

[639] Decode ways II

[646] Maximum length of pair chain

[2, 647] palindromic substrings

[650] 2 keys keyboard

[651] 4 keys keyboard

[656] Coin path

[2, 664] Strange printer

[673] Number of longest increasing subsequence

[2, 688] Knight probability in chessboard

[689] maximum sum of 3 non-overlapping subarrays

[691] stickers to spell word

[698] partition to k equal sum Subsets

[712] minimum ASCII Delete sum for two strings

[714] best time to buy and keep stock with transaction grouping (subject of the October 22, 2018 algorithm Group)

I also gave an array of stock prices to the questions related to stock trading. Prices [I] indicates the stock price on the day I. a service fee is required for each sale, ask the max profit at the end.

Question: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108892/Java-DP-solution-O (N)-to-O (1)-space

This problem can be solved using a rolling array (the space complexity of the two variables is optimized to O (1), but it is hard to understand after optimization, and this optimization is not a high-end technique. So we will first discuss the nature of this topic DP.

We either hold the stock or do not hold the stock on the day I. We use hold [I] to indicate the max profit of the stock held on day I, and unhold [I] to indicate the max profit of the stock not held on day I.

The transfer equation can be understood as follows: we have two situations on the day I, either we hold the stock or we do not hold the stock.

1. hold [I] = max (hold [I-1], unhold [I-1]-Prices [I]) it means we either hold the stock on the day of the I-1, or do nothing on the day I; or we bought the stock on the day I

2. day I without stock, unhold [I] = max (unhold [I-1], hold [I-1] + Prices [I]-shares) // It means that we do not hold the stock on the day of I-1, the day I do not do anything; we want to sell the stock on the day I

The current time complexity is O (n), and the space complexity is O (1 ). You can use two variables to optimize two arrays.

 1 class Solution { 2 public: 3     //hold[i] represents max profit of holding stock until day i 4     //nothold[i] represents max profit of not holding stock until day i 5     //transaction function: for each day i, we have 2 situations. 6     //1. hold stock in day i (you can either do nothing in day i or buy stock int day i) 7     // hold[i] = max(hold[i-1], nothold[i-1]-price[i]) 8     //2. not hold stock in day i (you can either do nothing in day i or sell stock in day i) 9     // nothold[i] = max(nothold[i-1], hold[i-1] + price[i] - fee)10     11     int maxProfit(vector<int>& prices, int fee) {12         const int n = prices.size();13         if (n <= 1) { return 0; }14         vector<int> hold(n, 0), unhold(n, 0);15         hold[0] = -prices[0], unhold[0] = 0;16         for (int i = 1; i < n; ++i) {17             hold[i] = max(hold[i-1], unhold[i-1] - prices[i]);18             unhold[i] = max(unhold[i-1], hold[i-1] + prices[i] - fee);19         }20         return unhold[n-1];21     }22 };
View code

[718] Maximum length of repeated subarray

[727] minimum window subsequence

[730] Count different palindromic subsequences

[740] Delete and earn

[741] cherry pickup

[746] min cost climbing stairs

750 Number of corner rectangles

[1, 764] largest plus sign

[787] cheapest flights within K stops

[790] Domino and tromino Tiling

[801] minimum swaps to make sequences increasing

[808] soup servings

[1, 813] largest sum of averages

[818] race car

837 new 21 game

[838] Push dominoes

[0, 847] Shortest Path visiting all nodes

[871] minimum number of refueling stops

[873] length of longest Fibonacci subsequence

[877] stone game

[1, 879] profitable schemes

[1, 887] Super Egg Drop

 

[Leetcode] dynamic planning (39 questions in the next article)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.