From eaaf7574cdd53f332f6a1bd1ec53966977857150 Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Fri, 1 Feb 2019 19:27:38 +0800 Subject: [PATCH] New Problem Solution "Best Time to Buy and Sell Stock with Transaction Fee" --- README.md | 1 + ...imeToBuyAndSellStockWithTransactionFee.cpp | 201 ++++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100644 algorithms/cpp/bestTimeToBuyAndSellStock/BestTimeToBuyAndSellStockWithTransactionFee.cpp diff --git a/README.md b/README.md index 6fda548..c0d7c2f 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ LeetCode |747|[Largest Number At Least Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/) | [Python](./algorithms/python/LargestNumberAtLeastTwiceOfOthers/dominantIndex.py)|Easy| |746|[Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) | [Python](./algorithms/python/MinCostClimbingStairs/minCostClimbingStairs.py)|Easy| |717|[1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters/) | [Python](./algorithms/python/1-bitAnd2-bitCharacters/isOneBitCharacter.py)|Easy| +|714|[Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee) | [C++](./algorithms/cpp/bestTimeToBuyAndSellStock/BestTimeToBuyAndSellStockWithTransactionFee.cpp)|Medium| |712|[Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/) | [C++](./algorithms/cpp/minimumASCIIDeleteSumForTwoStrings/MinimumAsciiDeleteSumForTwoStrings.cpp)|Medium| |687|[Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path/) | [Python](./algorithms/python/LongestUnivaluePath/longestUnivaluePath.py)|Easy| |684|[Redundant Connection](https://leetcode.com/problems/redundant-connection/) | [Python](./algorithms/python/RedundantConnection/findRedundantConnection.py)|Medium| diff --git a/algorithms/cpp/bestTimeToBuyAndSellStock/BestTimeToBuyAndSellStockWithTransactionFee.cpp b/algorithms/cpp/bestTimeToBuyAndSellStock/BestTimeToBuyAndSellStockWithTransactionFee.cpp new file mode 100644 index 0000000..d176ae5 --- /dev/null +++ b/algorithms/cpp/bestTimeToBuyAndSellStock/BestTimeToBuyAndSellStockWithTransactionFee.cpp @@ -0,0 +1,201 @@ +// Source : https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee +// Author : Hao Chen +// Date : 2019-02-01 + +/***************************************************************************************************** + * + * Your are given an array of integers prices, for which the i-th element is the price of a given + * stock on day i; and a non-negative integer fee representing a transaction fee. + * + * You may complete as many transactions as you like, but you need to pay the transaction fee for each + * transaction. You may not buy more than 1 share of a stock at a time (ie. you must sell the stock + * share before you buy again.) + * + * Return the maximum profit you can make. + * + * Example 1: + * + * Input: prices = [1, 3, 2, 8, 4, 9], fee = 2 + * Output: 8 + * + * Explanation: The maximum profit can be achieved by: + * Buying at prices[0] = 1Selling at prices[3] = 8Buying at prices[4] = 4Selling at prices[5] = 9The + * total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8. + * + * Note: + * 0 < prices.length <= 50000. + * 0 < prices[i] < 50000. + * 0 <= fee < 50000. + ******************************************************************************************************/ + + + +class Solution { +private: + int max(int x, int y) { + return x > y ? x: y; + } + int max(int x, int y, int z) { + return max(x, max(y,z)); + } + +public: + + int maxProfit(vector& prices, int fee) { + return maxProfit_dp03(prices, fee); // 100ms + return maxProfit_dp02(prices, fee); // 100ms + return maxProfit_dp01(prices, fee); // 2700ms + } + + // find the [buy-low, sell-high] prices pairs, + // and remove the unnecessary prices. + void genPricesPairs(vector &prices, vector< pair > &prices_pairs, int fee){ + + int low = -1; + for (int i=0; i prices[i+1] && low >= 0) { + prices_pairs.push_back( make_pair( prices[low], prices[i]) ); + low = -1; // reset the `low` & `high` + } + } + + // edge case + if ( low >= 0 ) { + prices_pairs.push_back( make_pair( prices[low], prices[prices.size()-1] ) ); + } + + } + + int maxProfit_dp01(vector &prices, int &fee) { + + vector< pair > prices_pairs; + genPricesPairs(prices, prices_pairs, fee); + + vector dp(prices_pairs.size()+1, 0); + + for (int i=0; i &prices, int &fee) { + + vector< pair > prices_pairs; + genPricesPairs(prices, prices_pairs, fee); + + if ( prices_pairs.size() < 1 ) return 0; + + + // first - represent the max profit if we buy. + // second - represent the max profit if we sell. + vector< pair > dp(prices_pairs.size() , make_pair(0,0) ); + + //buy profit - if we buy it in day 0, then we got negtive profit. + dp[0].first = - prices_pairs[0].first; + + //sell profit - if we sell it in day 0, then we have the profits + // if the profit is negtive, then won't sell it. + dp[0].second = max(0, prices_pairs[0].second - prices_pairs[0].first - fee); + + for (int i=1; i &prices, int &fee) { + int buy=-prices[0], sell=0; + int pre_buy=0, pre_sell=0; + + for(auto price: prices) { + pre_buy = buy; + buy = max (sell - price, pre_buy); + + pre_sell = sell; + sell = max( pre_buy + price - fee, pre_sell); + } + + return sell; + } +};