Triangle LeetCode |My solution

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 is 11 (i.e., 2 + 3 + 5 + 1 =
11).

public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        if (triangle == null) {
            return 0;
        }
        int rowNumber = triangle.size() ;
        int [][] minArray = new int [rowNumber][rowNumber];
        minArray[0][0] = triangle.get(0).get(0);

        for (int i = 1; i < rowNumber; i++) {
            for (int j = 0; j <= i ; j++) {
                if (j == 0)
                {
                    minArray[i][j] = minArray[i-1][j] + triangle.get(i).get(j) ;
                } else if (j == i  && j != 0)
                {
                    minArray[i][j] = minArray[i-1][j - 1] + triangle.get(i).get(j);
                } else {
                     minArray[i][j] = Math.min(minArray[i- 1][j] + triangle.get(i).get(j), minArray[i- 1][j - 1] + triangle.get(i).get(j));
                }
            }
        }
        int min = minArray[rowNumber - 1][0];
        for (int i = 0; i < rowNumber; i++) {
            if (min > minArray[rowNumber - 1][i]) {
                min = minArray[rowNumber - 1][i];
            }
        }
        return min;

    }
}
时间: 2024-08-11 10:09:57

Triangle LeetCode |My solution的相关文章

Pascal&#39;s Triangle leetcode java(杨辉三角)

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题解:既然讲到了Pascal‘s Triangle,即杨辉三角.那么就先去Wikipedia上面复习一下杨辉三角吧:”杨辉三角形,又称賈憲三角形.帕斯卡三角形.海亚姆三角形,是二项式係數在的

Pascal&#39;s Triangle leetcode

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] Subscribe to see which companies asked this question 0 0 0[1]0 0[1 1]0 0[1 2 1]0 0[1 3 3 1]0 0[1 4 6

Triangle leetcode java

题目: 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 is 1

leetcode 118. Pascal&#39;s Triangle &amp;&amp; leetcode 119. Pascal&#39;s Triangle II

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]nextline[j] = currentline[j] + currentline[j+1] 1 vector<vector<int> > generate(int numRows) 2

Pascal&#39;s Triangle (leetcode java)

问题描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 问题分析: 第n行的数据是在第n-1行的基础上计算出来的.是一个迭代的过程 解决方法: //生成杨辉三角的前n行 public static List<List<Integer>&

Triangle &lt;leetcode&gt;

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 is 11 (i

Triangle Leetcode Python

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 is 11 (i

LeetCode My Solution: Minimum Depth of Binary Tree

Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

LeetCode :My solution N-Queens

N-Queens Total Accepted: 15603 Total Submissions: 60198My Submissions The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-qu