銆怢eetCode銆戝姩鎬佽鍒掞紙涓嬬瘒鍏?9棰橈級

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆怢eetCode銆戝姩鎬佽鍒掞紙涓嬬瘒鍏?9棰橈級相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/UNC' title='UNC'>UNC   amp   repeat   coin   day   ide   push   nbsp   澶嶄範   

銆?00銆?Non-negative Integers without Consecutive Ones

銆?29銆?K Inverse Pairs Array

銆?38銆?Shopping Offers

銆?39銆?Decode Ways II

銆?46銆?Maximum Length of Pair Chain

銆?47銆?Palindromic Substrings

銆?50銆?2 Keys Keyboard

銆?51銆?4 Keys Keyboard

銆?56銆?Coin Path

銆?64銆?Strange Printer

銆?73銆?Number of Longest Increasing Subsequence 

銆?88銆?Knight Probability in Chessboard

銆?89銆?Maximum Sum of 3 Non-Overlapping Subarrays

銆?91銆?Stickers to Spell Word

銆?98銆?Partition to K Equal Sum Subsets 

 

銆?12銆?Minimum ASCII Delete Sum for Two Strings (2018骞?2鏈?1鏃ワ紝绠楁硶缇?

缁欎簡涓や釜瀛楃涓?s1 鍜?s2锛岃浠?s1 鍜?s2 涓垹闄や竴浜涘瓧绗︿娇寰楄繖涓や釜瀛楃涓茬浉绛夈€傛眰鏈€灏忕殑鍒犻櫎瀛楃鐨?ACSII 鐨勫拰銆?/span>

棰樿В锛氬瓧绗︿覆鐨勭紪杈戣窛绂荤殑鍙樼銆俤p[i][j] 琛ㄧず浠?s1[0..i-1] 鍒?s2[0..j-1] 杩欎袱涓瓧绗︿覆鐩哥瓑鎵€姹傜殑鏈€灏忓拰銆傝浆绉绘柟绋嬩负锛?if (s1[i-1] == s2[j-1]) {dp[i][j] = dp[i-1][j-1]}, else {dp[i][j] = min(dp[i-1][j] + (int)s1[i-1], dp[i][j-1] + (int)s2[j-1])}

鎶€鏈垎浜浘鐗? id=
 1 class Solution {
 2 public:
 3     int minimumDeleteSum(string s1, string s2) {
 4         const int n1 = s1.size(), n2 = s2.size();
 5         vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, INT_MAX));
 6         dp[0][0] = 0;
 7         for (int i = 1; i <= n1; ++i) {
 8             dp[i][0] = dp[i-1][0] + (int)s1[i-1];
 9         }
10         for (int j = 1; j <= n2; ++j) {
11             dp[0][j] = dp[0][j-1] + (int)s2[j-1];
12         }        
13         for (int i = 1; i <= n1; ++i) {
14             for (int j = 1; j <= n2; ++j) {
15                 if (s1[i-1] == s2[j-1]) {
16                     //dp[i][j] = min(dp[i][j], min(dp[i-1][j] + (int)s1[i-1], dp[i][j-1] + (int)s2[j-1]));
17                     dp[i][j] = min(dp[i][j], dp[i-1][j-1]);
18                 } else {
19                     dp[i][j] = min(dp[i][j], min(dp[i-1][j] + (int)s1[i-1], dp[i][j-1] + (int)s2[j-1]));
20                 }
21             }
22         }
23         return dp[n1][n2];
24     }
25 };
View Code

 

銆?14銆?Best Time to Buy and Sell Stock with Transaction Fee 锛堢畻娉曠兢 2018骞?0鏈?2鏃ラ鐩級

杩樻槸鑲$エ涔板崠鐨勭浉鍏抽棶棰橈紝缁欎簡涓€涓偂绁ㄤ环鏍肩殑鏁扮粍锛宲rices[i] 浠h〃绗?i 澶╃殑鑲$エ浠锋牸锛屾瘡娆″崠鍑鸿浜ゆ墜缁垂锛岄棶鏈€鍚庣殑max profit 鏄灏戙€?/span>

棰樿В锛歨ttps://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108892/Java-DP-solution-O(n)-to-O(1)-space

杩欓鍙互鐢ㄦ粴鍔ㄦ暟缁勶紙涓や釜鍙橀噺浼樺寲鎴怬(1)鐨勭┖闂村鏉傚害锛夛紝浣嗘槸浼樺寲鍚庣殑涓嶅ソ鐞嗚В锛岃€屼笖杩欑浼樺寲涔熶笉绠椾粈涔堥珮绔妧宸с€傛墍浠ユ垜浠繖閲屽厛璁ㄨ涓嬭繖棰?dp 鐨勬湰璐ㄣ€?/span>

鎴戜滑绗?i 澶╄涔堟寔鏈夎偂绁紝瑕佷箞涓嶆寔鏈夎偂绁ㄣ€?鎴戜滑鐢?hold[i] 琛ㄧず绗?i 澶╂寔鏈夎偂绁ㄧ殑 max profit锛?unhold[i] 琛ㄧず绗?i 澶╀笉鎸佹湁鑲$エ鐨?max profit銆?/span>

閭d箞杞Щ鏂圭▼鍙互杩欎箞鐞嗚В锛?鎴戜滑鍦ㄧ i 澶╂湁涓ょ鎯呭喌锛岃涔堟垜浠寔鏈夎偂绁紝瑕佷箞鎴戜滑涓嶆寔鏈夎偂绁ㄣ€?/span>

1. 绗?i 澶╂寔鏈夎偂绁ㄧ殑鎯呭喌涓嬶紝 hold[i] = max(hold[i-1], unhold[i-1] - prices[i])  //鎰忔€濇槸璇存垜浠涔堢 i-1 澶╁氨鎸佹湁鑲$エ锛?绗?i 澶╁暐涔熶笉骞诧紱 瑕佷箞鎴戜滑鍦ㄧ i 澶╀拱浜嗚偂绁?/span>

2.绗?i 澶╀笉鎸佹湁鑲$エ鐨勬儏鍐典笅锛?unhold[i] = max(unhold[i-1], hold[i-1] + prices[i] - fee) //鎰忔€濇槸璇存垜浠?绗?i-1 澶╁氨涓嶆寔鏈夎偂绁ㄤ簡锛岀 i 澶╁暐涔熶笉骞诧紱 瑕佷滑鎴戜滑鍦ㄧ i 澶╁崠浜嗚偂绁?/span>

鐜板湪鐨勬椂闂村鏉傚害鏄疧(N)锛?绌洪棿澶嶆潅搴︽槸O(1)銆傚彲浠ョ敤涓や釜鍙橀噺浼樺寲涓や釜鏁扮粍銆?/span>

鎶€鏈垎浜浘鐗? id=
 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

銆?18銆?Maximum Length of Repeated Subarray

銆?27銆?Minimum Window Subsequence 

銆?30銆?Count Different Palindromic Subsequences

銆?40銆?Delete and Earn

銆?41銆?Cherry Pickup 锛?019骞?鏈?2鏃ワ紝绠楁硶缇わ級

浠婂ぉ蹇冩儏寰堝樊锛屾妱鐨勭瓟妗?=锛屾湁鐐箄pset

銆?46銆?Min Cost Climbing Stairs

銆?50銆?Number Of Corner Rectangles

銆?64銆?Largest Plus Sign

銆?87銆?Cheapest Flights Within K Stops

銆?90銆?Domino and Tromino Tiling

銆?01銆?Minimum Swaps To Make Sequences Increasing 

 

銆?08銆?Soup Servings 锛?019骞?鏈?鏃ワ紝绠楁硶缇わ紝绗竴娆″仛锛岄渶瑕佸涔狅級

缁欎簡 A锛孊 涓ょ soup锛屼竴寮€濮嬮兘鏈?N ml銆傛湁濡備笅鍥涚閫夋嫨锛屾瘡绉嶉€夋嫨鐨勬鐜囬兘鏄?25%銆?/span>

  1. Serve 100 ml of soup A and 0 ml of soup B
  2. Serve 75 ml of soup A and 25 ml of soup B
  3. Serve 50 ml of soup A and 50 ml of soup B
  4. Serve 25 ml of soup A and 75 ml of soup B

濡傛灉鍦ㄩ厤姣斾腑鏈変竴绉峴oup鐨勫墏閲忎笉澶熻皟涓€浠界殑锛屽氨鐢ㄥ墿涓嬫墍鏈夌殑璋冧竴浠姐€傝繑鍥?soup A 鍏堢┖鐨勬鐜?鍔犱笂 soup A锛?B涓€璧风┖鐨勬鐜囥€?/p>

棰樿В锛氭垜浠敤 25ml 绠椾竴涓?serving锛屽厛璁$畻A锛?B姣忕 soup 鐨剆erving銆傛垜浠敤涓€涓蹇嗗寲鏁扮粍鏉ヨ褰曠畻杩囩殑姒傜巼銆?/p>

f[a][b] = 0.25 * (f[a-4][b] + f[a-3][b-1] + f[a-2][b-2] + f[a-1][b-3])

鎶€鏈垎浜浘鐗? id=
 1 class Solution {
 2 public:
 3     double soupServings(int N) {
 4         const int serving = N / 25 + (N % 25 >= 1 ? 1 : 0);
 5         if (serving >= 500) {
 6             return 1.0;
 7         }
 8         return helper(serving, serving);
 9     }
10     double helper(int a, int b) {
11         if (a == 0 && b == 0) {
12             return 0.5;
13         } else if (a == 0) {
14             return 1.0;
15         } else if (b == 0) {
16             return 0.0;
17         }
18         if (memo.find(a) != memo.end() && memo[a].find(b) != memo[a].end()) {
19             return memo[a][b];
20         }
21         double ret = 0.0;
22         ret = 0.25 * (helper(max(a-4, 0), b) + helper(max(a-3, 0), max(b-1, 0)) + helper(max(a-2, 0), max(b-2, 0)) + helper(max(a-1, 0), max(b-3, 0)));
23         memo[a][b] = ret;
24         return ret;
25     }
26     unordered_map<int, unordered_map<int, double>> memo;
27 };
View Code

 

銆?13銆?Largest Sum of Averages 

銆?18銆?Race Car 

銆?37銆?New 21 Game 

銆?38銆?Push Dominoes

銆?47銆?Shortest Path Visiting All Nodes 

銆?71銆?Minimum Number of Refueling Stops

銆?73銆?Length of Longest Fibonacci Subsequence

銆?77銆?Stone Game

銆?79銆?Profitable Schemes

銆?87銆?Super Egg Drop

 

以上是关于銆怢eetCode銆戝姩鎬佽鍒掞紙涓嬬瘒鍏?9棰橈級的主要内容,如果未能解决你的问题,请参考以下文章

绠楁硶-鍔ㄦ€佽鍒掞紙浜岋級

绠楁硶鍔ㄦ€佽鍒掞紙涓冿級-鑳屽寘闂4

濡備綍鍚戜竴涓洓宀佸皬瀛╄В閲婂姩鎬佽鍒掞紵

銆愩€恏enuacm2016绾ф殤鏈熻缁冦€戝姩鎬佽鍒掍笓棰?D銆慦riting Code

鍔ㄦ€佽鍒掞紙浜岋級涔嬫墦瀹跺姭鑸?/h1>

鍥炴函绠楁硶鍜屽姩鎬佽鍒掞紝鍒板簳璋佹槸璋佺埞锛熸枃鏈€佷功