DP(鍔ㄦ€佽鍒?缁忓吀璺緞闂 | LeetCode
Posted 鍋氭5澶ф爲
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DP(鍔ㄦ€佽鍒?缁忓吀璺緞闂 | LeetCode相关的知识,希望对你有一定的参考价值。
涓€瀹氳璁ょ湡鐪嬪畬杩欑瘒鏂囩珷鉁?/p>
澶ф爲涓嶆暍淇濊瘉鐪嬪畬浣犲氨鍙帉鎻″姩鎬佽鍒掞紝浣嗘槸锛屼綘涓€瀹氬彲浠?AC 鍔ㄦ€佽鍒掍腑鐨勮矾寰勯棶棰橈紒锛侌煒?/p>
鐢变簬绡囧箙闄愬埗涔熶负浜嗕笉璁╁ぇ瀹朵骇鐢熼槄璇荤柌鍔筹紝980. 涓嶅悓璺緞 III 杩欓亾棰樼洰浼氬崟鐙啓涓€绡囦綔涓鸿矾寰勯棶棰樼殑鏀跺熬绡囥€?/p>
鍔ㄦ€佽鍒掍腑鐨勮矾寰勯棶棰橈紝棰樼洰鏉ヨ嚜浜?LeetCode锛屽瓙鏍囬涓?棰樺彿 鍚嶇О 鐨勬牸寮忋€?/p>
涓€涓満鍣ㄤ汉浣嶄簬涓€涓?m x n 缃戞牸鐨勫乏涓婅 锛堣捣濮嬬偣鍦ㄤ笅鍥句腑鏍囪涓衡€淪tart鈥?锛夈€?/p>
鏈哄櫒浜烘瘡娆″彧鑳藉悜涓嬫垨鑰呭悜鍙崇Щ鍔ㄤ竴姝ャ€傛満鍣ㄤ汉璇曞浘杈惧埌缃戞牸鐨勫彸涓嬭锛堝湪涓嬪浘涓爣璁颁负鈥淔inish鈥濓級銆?/p>
闂€诲叡鏈夊灏戞潯涓嶅悓鐨勮矾寰勶紵 渚嬪锛屼笂鍥炬槸涓€涓? x 3 鐨勭綉鏍笺€傛湁澶氬皯鍙兘鐨勮矾寰勶紵 绀轰緥 1: 杈撳叆: m = 3, n = 2 杈撳嚭: 3 瑙i噴: 浠庡乏涓婅寮€濮嬶紝鎬诲叡鏈?3 鏉¤矾寰勫彲浠ュ埌杈惧彸涓嬭銆?/p>
绀轰緥 2: 杈撳叆: m = 7, n = 3 杈撳嚭: 28 鎻愮ず锛?/p>
鏍规嵁绉诲姩瑙勫垯锛屾垜浠彲浠ョ‘瀹氾紝鍦?[i, j] 浣嶇疆鐨勭偣锛屽彧鑳介€氳繃 [i-1, j] 浠ュ強 [i, j-1] 鍒拌揪 鐢变互涓婂垎鏋愶紝鎴戜滑鍙互鍐欏嚭 瀵逛簬璇ヨВ鍐虫柟妗堬紝鍏舵椂闂村鏉傚害涓庣┖闂村鏉傚害涓猴細 鎴戜滑鐭ラ亾锛岄€氬父瀵逛簬DP棰樼洰鐨勫垵濮嬭В娉曪紝鏄彲浠?span class="mq-86">瀵瑰叾绌洪棿澶嶆潅搴﹁繘琛屼紭鍖?/strong>鐨勶紝姣斿杩欓亾棰樼洰锛屾垜浠垰寮€濮嬬敤浜嗕簩缁存暟缁勬潵瀛樺偍锛屾墍浠ユ秷鑰楃┖闂翠负 O(m * n) 浣嗘垜浠畬鍏ㄥ彲浠ュ帇缂╀负涓€缁存暟缁勫瓨鍌紝涓嬮潰鎴戜滑灏辨潵灏濊瘯瀹炵幇涓€涓嬨€?/p>
涓€涓満鍣ㄤ汉浣嶄簬涓€涓?m x n 缃戞牸鐨勫乏涓婅 锛堣捣濮嬬偣鍦ㄤ笅鍥句腑鏍囪涓衡€淪tart鈥?锛夈€?/p>
鏈哄櫒浜烘瘡娆″彧鑳藉悜涓嬫垨鑰呭悜鍙崇Щ鍔ㄤ竴姝ャ€傛満鍣ㄤ汉璇曞浘杈惧埌缃戞牸鐨勫彸涓嬭锛堝湪涓嬪浘涓爣璁颁负鈥淔inish鈥濓級銆?/p>
鐜板湪鑰冭檻缃戞牸涓湁闅滅鐗?/strong>銆傞偅涔堜粠宸︿笂瑙掑埌鍙充笅瑙掑皢浼氭湁澶氬皯鏉′笉鍚岀殑璺緞锛?/p>
缃戞牸涓殑闅滅鐗╁拰绌轰綅缃垎鍒敤 璇存槑锛?/strong> m 鍜?n 鐨勫€煎潎涓嶈秴杩?100銆?/p>
绀轰緥 1: 杈撳叆: [ [0,0,0], [0,1,0], [0,0,0] ] 杈撳嚭: 2 瑙i噴: 3x3 缃戞牸鐨勬涓棿鏈変竴涓殰纰嶇墿銆備粠宸︿笂瑙掑埌鍙充笅瑙掍竴鍏辨湁 2 鏉′笉鍚岀殑璺緞锛?/p>
瀵逛簬 涓嶅悓璺緞 II 璇ラ鎴戜滑鍙互鍙戠幇锛岀浉瀵逛簬绗竴棰橈紝澶氫簡涓€涓檺鍒舵潯浠?鑰冭檻缃戞牸涓湁闅滅鐗?/strong>锛岄偅杩欎釜鏉′欢瀵逛簬鎴戜滑瑙e喅璇ラ鑰岃█鏈変粈涔堝惈涔夊憿锛?/p>
鎵€浠ユ垜浠杩欎釜鏉′欢杩涜闄愬畾鍚庡氨杞彉涓哄悓绗竴棰樼浉鍚岀殑闂浜嗐€?/p>
鉁?Kill 鎺夎棰樹簡 瀵逛簬璇ヨВ鍐虫柟妗堬紝鍏舵椂闂村鏉傚害涓庣┖闂村鏉傚害鍚岀涓€棰樹紭鍖栧墠鐨勮В娉曠浉鍚屻€?/p>
濂斤紝涓嬭竟鎴戜滑鏉ヨ繘琛屼竴涓嬩紭鍖栵紝杩欎釜dp鐨勪紭鍖栧憿锛屾垜浠氨涓嶅啀鍘诲仛灏?O(m*n) 浼樺寲鍒?O(m) 鐨勭┖闂翠紭鍖栦簡銆傛垜浠彲浠ョ洿鎺ュ皢鍏惰繘琛屽師鍦扮殑dp锛屼紭鍖栧埌 O(1锛夌殑鏃堕棿澶嶆潅搴︺€?/p>
娉ㄦ剰浜嗭紒 馃槑 鏍规嵁棰樼洰涓紶閫掔殑鍙傛暟锛屾垜浠彲浠ュ彂鐜帮紝瀹冩湰韬氨鏄竴涓簩缁存暟缁勶紝鎵€浠ユ垜浠鎯冲湪瀹冭嚜韬繘琛岃鏁帮紝浠庤€岀渷鍘荤┖闂翠笂鐨勬秷鑰椼€?/p>
鍙互鐪嬪埌锛屽湪涓婅竟浠g爜涓紝鍒濆鍖栭鍒楁椂鐨勫垵濮嬫潯浠朵负 鍏跺疄鏄洜涓烘垜浠湪鍒濆鍖栬鐨勬椂鍊欙紝灏? 浣嗘槸鍥犱负鍦ㄥ嚱鏁板紑濮嬫椂锛屾垜浠究杩涜浜嗗垽鏂? 缁忚繃杩欐浼樺寲锛屾渶缁堢┖闂村鏉傚害鍙湁甯搁噺绾у埆锛涘湪鍋氶鏃跺€欏彲浠ョ敤锛屼絾鏄鏋滆鐢ㄥ湪瀹為檯鐜涓笉寤鸿鐩存帴瀵瑰師鏁扮粍杩涜淇敼鍝 馃槈 濡傛灉浣犵湅鍒颁簡杩欙紝鎭枩浣犲張璇诲畬浜嗕竴绡囨枃绔狅紒 鑷虫鏈枃宸茬粡閫艰繎2000瀛椾簡锛屼负浜嗕繚璇佷笉浜х敓闃呰鐤插€︼紝璺緞闂鐨勬渶鍚庝竴涓?boss 980. 涓嶅悓璺緞 III 杩欓亾棰樼洰浼氬崟鐙啓涓€绡囦綔涓篋P璺緞闂鐨勫畬缁撶瘒銆?/p>
鐐逛釜鍦ㄧ湅鍚?馃憞馃憞62 涓嶅悓璺緞
1 <= m, n <= 100
2 * 10 ^ 9
solution
浠g爜Solution
class Solution {
public int uniquePaths(int m, int n) {
// 瀹氫箟dp鏁扮粍锛屾暟缁勪腑鐨勫€间唬琛ㄥ埌杈捐浣嶇疆鐨勮矾寰勬暟鐩?/span>
int[][] dp = new int[m][n];
// 鍒濆鍖栫涓€琛?amp;绗竴鍒楁暟鍊硷紝鍥犱负鍙兘鍚戜笅鍜屽悜鍙筹紝鎵€浠ュ€奸粯璁や负1
for(int i = 0; i < m; i++){
dp[i][0] = 1;
}
for(int j = 0; j < n; j++){
dp[0][j] = 1;
}
// 鏍规嵁绉诲姩瑙勫垯锛屾垜浠彲浠ョ‘瀹氾紝鍦?nbsp;[i, j] 浣嶇疆鐨勭偣锛屽彧鑳介€氳繃 [i-1, j] 浠ュ強 [i, j-1] 鍒拌揪
// 鎵€浠?nbsp;dp[i][j] = dp[i-1][j] + dp[i][j-1]
for(int i = 1; i < m; i++){
for(int j = 1; j < n; j++){
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}
}
Solution路space-optimization
class Solution {
public int uniquePaths(int m, int n) {
int[] result = new int[m];
// 鍒濆鍖栭琛屼俊鎭?/span>
for(int i = 0; i < m; i++) result[i] = 1;
for(int i = 1; i < n; i++){
for(int j = 1; j < m; j++){
// 鏇存柊鏁扮粍锛屽綋鍓嶄綅缃?nbsp;= 鏇存柊鍚庣殑宸︿晶 + 鏇存柊鍓嶇殑涓婁竴浣?/span>
result[j] += result[j-1];
}
}
return result[m-1];
}
}63. 涓嶅悓璺緞 II
1
鍜?0
鏉ヨ〃绀恒€?/p>
Solution
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if(obstacleGrid == null){
return 0;
}
int nr = obstacleGrid.length, nc = obstacleGrid[0].length;
int[][] dp = new int[nr][nc];
// 鍒濆鍖栬竟鐣屾潯浠?/span>
for(int r = 0; r < nr; r++){
// 鑻ヨ杈圭晫涓?锛屽嵆闅滅鐗╋紝鍒欎箣鍚庣殑鐐瑰潎涓嶅彲杈撅紝鐩存帴break
if(obstacleGrid[r][0] == 1) break;
dp[r][0] = 1;
}
for(int c = 0; c < nc; c++){
if(obstacleGrid[0][c] == 1) break;
dp[0][c] = 1;
}
// 寰幆濉厖鍐呴儴鏁版嵁
for(int r = 1; r < nr; r++){
for(int c = 1; c < nc; c++){
if(obstacleGrid[r][c] == 1) {
// 闅滅鐗╋紝涓嶅彲鍒拌揪锛屾墍浠ョ洿鎺ュ~鍐欎负 0
dp[r][c] = 0;
continue;
}
dp[r][c] = dp[r-1][c] + dp[r][c-1];
}
}
return dp[nr-1][nc-1];
}
}Solution路space-optimization
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if(obstacleGrid == null || obstacleGrid[0][0] == 1){
// 鑻ュ乏涓婅涓洪殰纰嶇墿锛岀洿鎺ヨ繑鍥?
return 0;
}
int nr = obstacleGrid.length, nc = obstacleGrid[0].length;
// 鍒濆鍖栬竟鐣屾潯浠?/span>
for(int r = 0; r < nr; r++){
if(obstacleGrid[r][0] == 1){
for(int i = r; i < nr; i++){
// 濡傛灉鍙戠幇涓€涓?锛屽垯灏嗗悗缁叏閮ㄧ疆涓?
obstacleGrid[i][0] = 0;
}
break;
}
obstacleGrid[r][0] = 1;
}
for(int c = 1; c < nc; c++){
// 娉ㄦ剰杩欓噷鐨?c 鍒濆涓?1锛?nbsp;澶у鍙互鑰冭檻涓嬩负浠€涔堬紵
if(obstacleGrid[0][c] == 1) {
for(int j = c; j < nc; j++){
obstacleGrid[0][j] = 0;
}
break;
}
obstacleGrid[0][c] = 1;
}
// 寰幆濉厖鍐呴儴鏁版嵁
for(int r = 1; r < nr; r++){
for(int c = 1; c < nc; c++){
if(obstacleGrid[r][c] == 1) {
obstacleGrid[r][c] = 0;
continue;
}
obstacleGrid[r][c] = obstacleGrid[r-1][c] + obstacleGrid[r][c-1];
}
}
return obstacleGrid[nr-1][nc-1];
}
}int c = 1; c < nc; c++
锛屼负浠€涔堟槸浠?寮€濮嬪憿锛?/p>
obstacleGrid[0][0]
鐨勪綅缃疆涓轰簡 1
锛屾墍浠ュ鏋滄垜浠湪鍒濆鍒楁椂渚濇棫浠?寮€濮嬪氨浼氬皢宸︿笂瑙掔殑 1
鐪嬩綔涓洪殰纰嶇墿锛屼粠鑰屾棤娉曞埌杈俱€?/p>
if(obstacleGrid == null || obstacleGrid[0][0] == 1)
锛屾墍浠ュ乏涓婅鐨?1
蹇呯劧鏄垜浠慨鏀瑰悗鐨勶紝鑰岄潪闅滅鐗┿€?/p>
以上是关于DP(鍔ㄦ€佽鍒?缁忓吀璺緞闂 | LeetCode的主要内容,如果未能解决你的问题,请参考以下文章