馃憠馃憖馃挰浠婃棩缁冧範锛堜竴锛?spanclass="mq-7">鎵撳鍔垗II锛圠eetCode-213锛夈€?/span>   &n"/>

绠楁硶-鍔ㄦ€佽鍒掞紙涓夛級

Posted Coding鐨勫摂鍝斿彣鍙?/a>

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绠楁硶-鍔ㄦ€佽鍒掞紙涓夛級相关的知识,希望对你有一定的参考价值。

        浠婂ぉ缁х画鎴戜滑鐨勫姩鎬佽鍒掍笓棰橈紝闆嗕腑缁冧範涓€浜涙湁鍏冲姩鎬佽鍒掔殑棰橈紝鍔犳繁鍗拌薄銆?br class="mq-2">

馃憠馃憖馃挰浠婃棩缁冧範锛堜竴锛?span class="mq-7">鎵撳鍔垗II锛圠eetCode-213锛夈€?/span>

        鍘诲伔涓€鏉$幆褰㈣鐨勬埧灞嬶紝姣忎釜灞嬮噷閮芥湁鏁伴噺涓嶇瓑鐨勯挶锛岀敤鏁扮粍nums鏉ヨ〃绀烘墍鏈夊眿閲岀殑閲戦銆?/span>

        鈿狅笍娉ㄦ剰锛屽鏋滃伔鐩搁偦涓や釜灞嬪氨浼氳Е鍙戞姤璀︼紝涓?span class="mq-12">绗竴瀹跺拰鏈€鍚庝竴瀹跺眿瀛愭槸杩炲湪涓€璧风殑鐜舰锛岃绠楀湪涓嶈Е鍙戞姤璀︾殑鎯呭喌涓嬶紝浣犱竴鏅氭渶澶氳兘鍋峰埌澶氬皯锛岃繑鍥炴渶澶у€笺€傚锛?/p>

杈撳叆锛歔1,3,3]杈撳嚭锛?--------------杈撳叆锛歔2,7,9,3,1]杈撳嚭锛?1=2+9

馃檱鎬濊矾锛?/span>

  渚濇棫鏄姩鎬佽鍒掞紝杩樻槸鎴戜滑涔嬪墠寮鸿皟鐨勶細

  1. 瀹氫箟瀛愰棶棰橈紝

  2. 鎵惧嚭鐘舵€佽浆绉伙紝鍗冲瓙闂鐨勯€掓帹鍏崇郴

  3. 纭畾銆愬~琛ㄣ€戠殑璁$畻椤哄簭

  4. 绌洪棿浼樺寲锛岃繖鍓嶆湡鍙互鍏堟斁涓€鏀撅紝绛夌啛缁冧簡鍦ㄦ潵鑰冭檻銆?/span>

        姝ら鐨勯毦鐐规槸鐜舰锛屼篃灏辨槸璇达紝濡傛灉鍋风涓€瀹跺氨涓嶈兘鍋锋渶鍚庝竴瀹讹紝鍋锋渶鍚庝竴瀹跺氨涓嶈兘鍋风涓€瀹讹紝鏈変簡杩欎釜缁撹锛屽啀搴旂敤鍔ㄦ€佽鍒掍篃灏辩畝鍗曚簡锛屾垜浠彧闇€瑕佸垎寮€涓ょ鎯呭喌锛?/span>

  1. 鍋风涓€瀹讹紝涓嶅伔鏈€鍚庝竴瀹讹紝鍦ㄥ墿涓嬬殑0~N-1涓眿閲屽伔锛?/span>

  2. 涓嶅伔绗竴瀹讹紝鍋锋渶鍚庝竴瀹讹紝鍦ㄥ墿涓嬬殑1~N涓眿閲屽伔銆?/span>

浠g爜锛?br>

public int rob(int[] nums) { int len=nums.length; if(len<1){ return 0; } if(len<2){ return nums[0]; }    // 娉ㄦ剰杩欓噷浼犲€兼槸鏁扮粍涓嬭〃鑰屼笉鏄暱搴︺€?/span> return Math.max(myrob(nums,0,len-2),myrob(nums,1,len-1));}private int myrob(int[] nums ,int start,int end){ int prev=nums[start]; if(start >= end){ return prev; } int curr=Math.max(nums[start+1],prev); for(int i=start+2;i<=end;i++){ int temp=curr; curr=Math.max(curr,prev+nums[i]); prev=temp; } return curr;}

澶嶆潅搴﹀垎鏋?/span>

  • 鏃堕棿澶嶆潅搴︼細O(N)锛屼袱娆¢亶鍘唍ums鐨勭嚎鎬ф椂闂淬€?/span>

  • 绌洪棿澶嶆潅搴︼細O(1)锛屾垜浠敤鍒癱urr鍜宲rev锛屼娇鐢ㄥ父鏁板ぇ灏忕殑绌洪棿銆?/span>


馃憠馃憖馃挰浠婃棩缁冧範锛堜簩锛?span class="mq-57">鎵撳鍔垗III锛圠eetCode-337锛夈€?/span>

        鍏堣鏄庝竴涓嬶紝杩欓亾棰樹笉鏄姩鎬佽鍒掔殑瑙f硶锛屼笉杩囨棦鐒跺仛鍒颁簡鎵撳鍔垗锛屽氨鎶婅繖涓郴鍒楃殑閮藉仛瀹屽緱浜嗐€?br>

        姝ら涓紝灏忓伔鍘诲伔涓€涓爲褰㈢粨鏋勭殑琛楅亾锛屼粛鏃ф槸鐩搁偦涓や釜灞嬩笉鑳藉伔锛岃绠楄兘鍋峰埌鐨勬渶澶ч噾棰濄€傚锛?/p>

杈撳叆: [3,2,3,null,3,null,1] 3 /  2 3    3 1杈撳嚭: 7 瑙i噴: 灏忓伔涓€鏅氳兘澶熺洍鍙栫殑鏈€楂橀噾棰?= 3 + 3 + 1 = 7.

杈撳叆: [3,4,5,1,3,null,1]  3 / 4 5 / 1 3 1
杈撳嚭: 9瑙i噴: 灏忓伔涓€鏅氳兘澶熺洍鍙栫殑鏈€楂橀噾棰?nbsp;= 4 + 5 = 9.

馃檱鎬濊矾锛?/span>

        姝ら涓紝鎴垮眿鐨勭粨鏋勫彉涓烘爲褰㈢粨鏋勶紝涓旂浉閭绘埧灞嬩笉鑳藉伔锛屼篃灏辨槸璇存爲涓殑鐖跺瓙鑺傜偣涓嶈兘鍚屾椂鍋凤紝鎵€浠ユ棰樼殑鎬濊矾鍒嗘瀽涓猴細

  1. 鍋风埗鑺傜偣锛屼袱涓浉閭荤殑鎴垮眿锛堝瓙鑺傜偣锛夊氨涓嶈兘鍋凤紝杩欏氨鐩稿綋浜庢垜浠鎵惧嚭瀛欏瓙鑺傜偣涓兘鍋峰埌鐨勬渶澶ч噾棰濆姞鐖惰妭鐐圭殑閲戦銆?/p>

  2. 涓嶅伔鐖惰妭鐐癸紝姝ゆ椂瑕佹壘鍑轰袱涓瓙鑺傜偣鑳藉伔鍒扮殑鏈€澶ч噾棰濄€?br>

馃檵瑙f硶涓€锛?/span>鏆村姏閫掑綊銆?/span>

public int rob(TreeNode root) { if(root==null){ return 0; } int res = root.val; if(root.left != null){ res+=rob_1(root.left.left)+rob_1(root.left.right); } if(root.right != null){ res+=rob_1(root.right.left)+rob_1(root.right.right); } return Math.max(res,rob_1(root.left)+rob_1(root.right));}

馃檵瑙f硶浜岋細鏆村姏閫掑綊+璁板繂鍖?/strong>銆?/span>

        鍦ㄨВ娉曚竴涓紝鎴戜滑姣忎竴娆$殑閫掑綊閮借绠椾簡褰撳墠鑺傜偣銆佸瓙鑺傜偣鍜屽瓩瀛愯妭鐐癸紝鎵€浠ヨ繖鍎挎垜浠紩鍏ヨ蹇嗗寲锛岃В鍐抽噸澶嶈绠楃殑闂銆?br>

public int rob(TreeNode root) {    HashMap<TreeNodeIntegermap = new HashMap<>(); return rob_2_helper(root,map);}private int rob_2_helper(TreeNode root, Map<TreeNode,Integer> map){ if(root==null){ return 0; } if(map.containsKey(root)){ return map.get(root); } int res = root.val; if(root.left != null){ res+=rob_2_helper(root.left.left,map)+rob_2_helper(root.left.right,map); } if(root.right != null){ res+=rob_2_helper(root.right.left,map)+rob_2_helper(root.right.right,map);    } int money = Math.max(res,rob_2_helper(root.left,map)+rob_2_helper(root.right,map)); map.put(root,money); return money;}

馃檵瑙f硶涓夛細

馃檱鎬濊矾锛?/span>

        涓婇潰涓ょ瑙f硶姣忔閫掑綊涓紝閮介渶瑕佽绠楀瓩瀛愯妭鐐癸紝铏界劧鍔犱簡璁板繂鍖栦絾浠嶆棫鑰楁椂銆傛瑙f硶鐨勬€濊矾鍙兘澶翠竴鍥炵湅鐨勮瘽锛屾湁鐐规嚨锛屼絾鏄‘瀹炴槸涓緢濂藉緢妫掔殑瑙f瀽鎬濊矾銆?/p>

        姝よВ娉曚腑锛屾垜浠紩鍏ヤ竴涓暱搴︿负2鐨勬暟缁剅es锛岀敤浜庤褰曞綋鍓嶈妭鐐规槸鍚﹀幓鍋凤紝res[0]琛ㄧず涓嶅伔鑷繁锛宺es[1]琛ㄧず鍋疯嚜宸憋紱鐩歌繛鐨勪袱涓妭鐐逛笉鑳藉悓鏃跺幓鍋枫€備换鎰忎竴涓妭鐐硅兘鍋峰埌閽卞彲浠ヨ〃绀轰负锛?br>

  1. 褰撳墠鑺傜偣閫夋嫨涓嶅伔锛屽綋鍓嶈妭鐐硅兘鍋峰埌鐨勬渶澶ч噾棰?宸﹀瀛愯兘鍋峰埌鐨勬渶澶ч噾棰?鍙冲瀛愯兘鍋峰埌鐨勬渶澶ч噾棰濄€傚叕寮忚〃绀轰负锛?br>res[0] = max(left[0]+left[1]) + max(right[0],right[1])

  2. 褰撳墠鑺傜偣閫夋嫨鍋凤紝褰撳墠鑺傜偣鑳藉伔鍒扮殑鏈€澶ч噾棰?宸﹀瀛愪笉鍋疯嚜宸辨椂鑳藉伔鍒扮殑鏈€澶ч噾棰?鍙冲瀛愪笉鍋疯嚜宸辨椂鑳藉伔鍒扮殑鏈€澶ч噾棰濄€傚叕寮忚〃绀轰负锛?br>res[1] = left[0]+right[1]+root.val 

浠g爜锛?span class="mq-85">

public int rob(TreeNode root) { int[] res=helper(root); return Math.max(res[0],res[1]);}private int[] helper(TreeNode node){ int[] res = new int[2]; if(node == null){ return res; } int[] left = helper(node.left); int[] right=helper(node.right); res[0] = Math.max(left[0],left[1]) + Math.max(right[0],right[1]); res[1] = left[0]+right[0]+node.val; return res;}


绠楁硶-鍔ㄦ€佽鍒掞紙涓夛級涓嶇Н璺锛屾棤浠ヨ嚦鍗冮噷銆?/strong>

鏂囩珷鏈夊府鍔╃殑璇濓紝鐐逛釜杞彂銆佸湪鐪嬪憲绠楁硶-鍔ㄦ€佽鍒掞紙涓夛級銆?/strong>

璋㈣阿鏀寔鍝?(*^__^*)

END


绠楁硶-鍔ㄦ€佽鍒掞紙涓夛級馃憞

以上是关于绠楁硶-鍔ㄦ€佽鍒掞紙涓夛級的主要内容,如果未能解决你的问题,请参考以下文章

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

绠楁硶-鍔ㄦ€佽鍒掞紙鍥涳級

绠楁硶鍔ㄦ€佽鍒掞紙浜旓級-鑳屽寘闂2

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

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

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