1. 程式人生 > >triangle(三角形)——leetcode

triangle(三角形)——leetcode

題目如下:
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]

The minimum path sum from top to bottom is11(i.e., 2 + 3 + 5 + 1 = 11).
Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

題解如下:

public static int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
    int []d=new int [1];
    d[0]=triangle.get(0).get(0).intValue();
    for(int i=1;i<triangle.size();i++) {
        d=dp(d,triangle,i);
    }
    int min=Integer.MAX_VALUE;
    for(int i=0;i<d.length;i++) {
        if
(min>d[i]) { min=d[i]; } } return min; } public static int [] dp(int []d,ArrayList<ArrayList<Integer>> triangle,int now) { int []p=new int [d.length+1]; p[0]=d[0]+triangle.get(now).get(0).intValue(); p[now]=d[now-1]+triangle.get(now).get(now).intValue(); for
(int j=1;j<now;j++) { int min=d[j-1]; if(d[j]<min) { min=d[j]; } p[j]=min+triangle.get(now).get(j).intValue(); } return p; }

相關推薦

triangle三角形——leetcode

題目如下: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

2.Triangle 三角形

1.等腰直角三角形:   https://www.cnblogs.com/FlyingLiao/p/9869040.html 2.任意三角形: <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g

習題 12.4 寫一個程式,定義抽象基類Shape,由它派生出3個派生類:Circle圓形、Rectangle矩形Triangle三角形,用一個函式printArea分別輸出以上。。。

C++程式設計(第三版) 譚浩強 習題12.4 個人設計 習題 12.4 寫一個程式,定義抽象基類Shape,由它派生出3個派生類:Circle(圓形)、Rectangle(矩形)、Triangle(三

C++第十三週【任務3】定義抽象基類Shape,由它派生出3個派生類,Circle圓形、Rectangle矩形Triangle三角形

/* * 程式的版權和版本宣告部分 * Copyright (c) 2011, 煙臺大學計算機學院學生  * All rights reserved. * 檔名稱:C++第十三週【任務3】                               * 作    者:  

LeetCode | Triangle三角形路徑和

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For examp

leetcode 118. Pascal's Trianglepython3楊輝三角

題目:楊輝三角 題目分析: 楊輝三角,第一個第二組數的值由第一組數的值決定,例如,x[2][1]=x[1][0]+x[1][1] 既:2=1+1 程式設計思路: 1.題目給出輸入為一個

Leetcode」976. Largest Perimeter TriangleC++

mat break triangle size 大於 不能 clas angle largest 分析 好久不刷題真的思維僵化,要考慮到這樣一個結論:如果遞增的三個數\(x_i,x_{i+1},x_{i+2}\)不符合題意,那麽最大的兩邊之差一定大於等於第一條邊,那麽任何比

5_Longest Palindromic SubstringManacher --LeetCode

HP fse for tco sts 子串 ora ces sub 參考:https://www.felix021.com/blog/read.php?2040,https://segmentfault.com/a/1190000002991199 做了修改。   首先用一

swift 字串轉整數atoi- LeetCode

實現 atoi,將字串轉為整數。 在找到第一個非空字元之前,需要移除掉字串中的空格字元。如果第一個非空字元是正號或負號,選取該符號,並將其與後面儘可能多的連續的數字組合起來,這部分字元即為整數的值。如果第一個非空字元是數字,則直接將其與之後連續的數字字元組合起來,形成整數。 字串可

CCPC 2016 長春區域賽 D - Triangle思維

HDU - 5914 題目大意:        有n根棒,長度分別為1,2,3......n,要求從中刪掉一些棒,使得剩下的任意三根不能構成三角形,問最少刪除幾根 題解:     

第八週週六LeetCode

#791 給出字串S和T,把T中的字元按照S中字元出現的順序排序,(S中不會重複,沒有出現的可以隨意位置)。 這題只需要按照S中出現次序的字元一個個從T中找出來,然後放到需要返回的字串裡面,最後再把沒有出現過的字元加在最後就可以了。 程式碼 class Solution: d

每天一道程式設計題 LeetCode-Container With Most Water

LeetCode上的題目太多了,每道都寫bolg實在是沒必要。接下來,小天會主要講解有趣的和重要的題目,這裡的重要是指題目有很大可能出現在面試中。 題目簡介: 這裡對題目作進一步形式化定義,給定一組非負整型數,題目希望找到兩個數字,使得目標函式最小。 舉例說明:輸入一組

C++學習筆記——leetcode記錄

C++學習筆記(一)——leetcode記錄 944. Delete Columns to Make Sorted [Easy] 852. Peak Index in a Mountain Array [Easy] 942. DI String

玩轉演算法面試:LeetCode連結串列類問題

在連結串列中穿針引線 連結串列和陣列都是線性結構,但是連結串列和陣列的不同在於陣列可以隨機的對於資料進行訪問。給出索引。可以以O(1)的時間複雜度迅速訪問到該元素。 連結串列只能從頭指標開始。 next指標指向哪裡? 206. Reverse Linked List

Codeforces Beta Round #6 (Div. 2 Only)A. Triangle暴力

題目連結:http://codeforces.com/contest/6/problem/A 題意:給出四條木棍拿走其中任意一個,有三種情況: 能組成三角形就輸出TRIANGLE。 最大邊等於另外兩邊之和就輸出SEGMENT。 都不行就輸出IMPOSSIBLE。

玩轉演算法面試:LeetCode查詢類問題

查詢問題 兩類查詢問題 查詢有無:元素’a’是否存在?set;集合 查詢對應關係(鍵值對應):元素’a’出現了幾次?map;字典 通常語言的標準庫中都內建set和map 容器類 遮蔽實現細節 瞭解語言中標準庫裡常見容器類的使

玩轉演算法面試:LeetCode陣列類問題

陣列中的問題其實最常見。 排序:選擇排序;插入排序;歸併排序;快速排序 查詢:二分查詢法 資料結構:棧;佇列;堆 …… 如何寫出正確的程式 建立一個基礎的框架,什麼是正確的程式 二分查詢法: - 二分查詢法的思想在1946年提出。 - 第一個沒有bug的二分查詢法在1962

查詢陣列中只出現一次的數——Leetcode系列十四

Single Number   Given an array of integers, every element appears twice except for one.  Find

OpenGL學習筆記一三角形

OpenGL學習筆記之三角形篇 在計算機圖形中三角形就如所有程式語言中的“hello world!”。所以接下來我們將要畫一個三角形。 在上一個篇部落格我已經講過OpenGL的環境配此次就不再詳細說明了(OpenGL的執行環境配置)。 參考資料(非

C++LeetCode#236. Lowest Common Ancestor of a Binary Tree

題目:給定一個棵二叉樹的兩個節點o1/o2,求兩個節點的最近公共祖先(LCA) 難度:Medium 思路: 方法1.記錄路徑:先遍歷二叉樹,分別得到從根節點到o1/o2的兩個路徑儲存在vector中