1. 程式人生 > >LeetCode—Minimum Path Sum 二維陣列最小路徑,動態規劃

LeetCode—Minimum Path Sum 二維陣列最小路徑,動態規劃

感覺這是一系列的動態規劃的演算法,正好也將動態規劃的演算法進行一個總結:

演算法一:

帶權重的最小路徑的問題

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

首先每一個路徑的上一個路徑都是來自於其上方和左方

現將最上面的路徑進行求和,最左邊的路徑進行求和(這裡沒有直接求和的原因是因為用一個一維陣列res[n]記錄路徑,這裡比較巧妙得節約了空間,所以將最左邊的求和演算法放到了迴圈裡面  res[i][j] = min(res[i-1][j],res[i][j-1])+val[i][j]  )

class Solution {
public:
    int minPathSum(vector<vector<int> > &grid) {
        if(grid.empty() || grid[0].empty())
        {
            return 0;
        }
        int m = grid.size();
        int n = grid[0].size();
        int *res = new int[n];
        res[0] = grid[0][0];
        for(int i = 1;i < n; i++)
        {
            res[i] = res[i-1]+grid[0][i];
        }
        for(int i = 1; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if (0 == j)
                {
                    res[j] += grid[i][0];
                }
                else
                {
                    res[j] = min(res[j-1],res[j])+grid[i][j];
                }
                
            }
        }
        int result = res[n-1];
        delete []res;
        return result;
    }
};

和這種動態規劃類似的演算法還有:

Unique Paths


https://leetcode.com/problems/unique-paths/

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?


Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

class Solution {
public:
    int uniquePaths(int m, int n) {
        if(m == 0 || n == 0)
        {
            return 0;
        }
        int res[100][100];
        for(int i = 0; i < n; i++)
        {
            res[0][i] = 1; //<因為只有一種方法
        }
        for(int i = 0; i < m; i++)
        {
            res[i][0] = 1;
        }
        for(int i = 1; i < m; i++)
        {
            for(int j = 1; j < n; j++)
            {
                res[i][j] = res[i-1][j]+res[i][j-1];
            }
        }
        return res[m-1][n-1];
    }
};

如果只是利用一維空間:

class Solution {
public:
    int uniquePaths(int m, int n) {
        if(m <= 0 || n <= 0
        {
            return 0;
        }
        int res[100] = {0};
        res[0] = 1;
        for(int i = 0; i < m; i++)
        {
            for(int j = 1; j < n; j++)
            {
                res[j] += res[j-1];
            }
        }
        return res[n-1];
    }
};

Unique Paths II

這裡涉及到一些障礙物的情況,如果值為1表示這裡有障礙物,不能跨越

[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]

The total number of unique paths is 2.

Note:m and n will be at most 100.
class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
        if(obstacleGrid.empty() || obstacleGrid[0].empty())
        {
            return 0;
        }
        int res[100] = {0};//<還是採用一維陣列
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        res[0] = (obstacleGrid[0][0] != 1);
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                <span style="color:#ff0000;">if(j == 0)
                {
                    if(obstacleGrid[i][j] == 1)  //<這裡需要稍微注意一下如果當前有障礙表示為0,否則可以延續之前的狀態
                    {
                        res[j] = 0;
                    }
                }</span>
                else
                {
                    <span style="color:#ff0000;">if(obstacleGrid[i][j] == 1)
                    {
                        res[j] = 0;
                    }
                    else
                    {
                        res[j] += res[j-1];
                    }</span>
                }
            }
        }
        return res[n-1];
    }
};




相關推薦

LeetCodeMinimum Path Sum 陣列路徑動態規劃

感覺這是一系列的動態規劃的演算法,正好也將動態規劃的演算法進行一個總結: 演算法一: 帶權重的最小路徑的問題 Given a m x n grid filled with non-negative numbers, find a path from top left to

Leetcode 124 Binary Tree Maximum Path Sum 叉樹路徑

原題連結 題目描述 Given a binary tree, find the maximum path sum. 給出一棵二叉樹,計算其最大路徑和。 The path may start and end at any node in the t

回溯法計算陣列路徑

提供的二維數字矩陣地圖,從左上角出發,每次可以向下或向右走,直到到達右下角,途中經過的路徑上的數字加起來,得到的數應該是一個最大的數1.輸出路徑及累計值2.提供二維陣列的輸入(文字檔案匯入或JS檔案匯入)3.輸出每次搜尋花的時間,比如:輸入二維陣列 輸出結果 和 搜尋用的時間4.最大二維陣列為:200x200

數字三角形路徑和—動態規劃

div 路徑和 image 動態 節點 spa 直接 .cn 一行 思路:自底向上求解,從倒數第二行開始,本行節點到最後一行的最小路徑和等於該節點的數據加上下面左右兩個數據中最小的一個。不使用額外空間,直接將最小路徑和存儲到原有的數組中。1 int minimumTota

算法52-----矩陣路徑動態規劃

sum data 列表 路徑 二次 解釋 示例 一行 lse 一、題目:矩陣最小路徑 給定一個包含非負整數的 m x n 網格,請找出一條從左上角到右下角的路徑,使得路徑上的數字總和為最小。 說明:每次只能向下或者向右移動一步。 示例: 輸入: [ [1,3,1],

leetcode64. 路徑和---動態規劃

給定一個包含非負整數的 m x n 網格,請找出一條從左上角到右下角的路徑,使得路徑上的數字總和為最小。 說明:每次只能向下或者向右移動一步。 示例: 輸入: [   [1,3,1], [

Leetcode】【DP-陣列】 64. Minimum Path Sum / 路徑和】

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum

陣列大值首尾相連

  題目要求: 返回一個整數陣列中最大子陣列的和。 輸入一個整形陣列,數組裡有正數也有負數。 陣列中連續的一個或多個整陣列成一個子陣列,每個子陣列都有一個和。 如果陣列A[0]……A[j-1]首尾相鄰,允許A[i-1],…… A[n-1],A[0]……A[j-1]之和最大。同時返回最大子陣

返回一個陣列 大子陣列的和

作業要求: 1、輸入一個二維整形陣列,數組裡有正數有負數。 2、二維陣列中連續的一個子矩陣組成一個子陣列。 3、求所有子陣列的和的最大值。   設計思路 1·首先我們先定義要用到的二維陣列和未知數,定義二維陣列的最大行數和列數。 2·輸入一個二維陣列行數和列數,通過if語句判斷 行數和

返回一個陣列大子陣列的和

要求: n輸入一個二維整形陣列,數組裡有正數也有負數。 n二維陣列中連續的一個子矩陣組成一個子陣列,每個子陣列都有一個和。 n求所有子陣列的和的最大值。要求時間複雜度為O(n)。 組員:常嘯帆( 負責程式分析,程式碼程式設計),畢文強( 

陣列大子陣列和的求解

 一.設計思想       首先要用一段程式碼,可以讀入txt檔案裡的二維陣列。需要將txt檔案放入同一目錄。        其次要用一段程式碼得到最大子陣列的和

Leetcode】【DP-陣列】 63. Unique Paths II / 不同路徑2(帶障礙)

給定一個二維陣列,每格為0/1值,1代表無法通過。求從左上到右下的不同路徑數。只能往右/下走。 Input: [   [0,0,0],   [0,1,0],   [0,0,0] ] Output: 2 Explanation: There is one obstacl

Leetcode】【DP-陣列】 62. Unique Paths / 不同路徑

給一個形狀為m x n的矩陣,求從左上角到右下角的不同路徑的個數。行進時只能往右/下移動。 方法一:使用二維陣列儲存每個位置的dp值 稍作畫圖分析即可得到dp式子:dp [i] [j] = dp [i-1] [j] + dp [i] [j-1] (

陣列大連續子陣列

#include "stdafx.h" #include "stdio.h" #include "stdlib.h" int _tmain(int argc, _TCHAR* argv[]) { int x[3][3] = {0}; int sum = 0; int max = 0; int

LeetCode 124. Binary Tree Maximum Path Sum(樹中路徑遞迴)

Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node t

LeetCode 62. Unique Paths--陣列從左上角到右下角的唯一路徑的種數有多少只能向右或向下移動--DP

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or r

Binary Tree Maximum Path Sum 叉樹中任意路徑大和

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tr

[LeetCode]Minimum Path Sum,解題報告

前言 這道題目我今年面試的時候考過,不給出具體的哪家公司了,也是給定矩陣從左上角到右下角的和最小的路徑。 開始我並不知道是確定了起始點和結束點,因此我第一反應是用DFS遍歷矩陣,然後那個面試官說不讓我用遞迴(其實dfs也不一定非用遞迴實現)。想了一下我說用動態規劃,給他寫

LeetCode 74. Search a 2D Matrix--陣列 每行有序且當前行的首元素大於上一行的末位元素判斷是否存在某元素

74. Search a 2D Matrix Medium Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following p

陣列值----利用函式呼叫形式(2種方法)

(1)利用氣泡排序思想求二維陣列中的最大值 /* 利用函式呼叫形式,傳遞二維陣列名進行函式呼叫 設計求二維陣列中最小值、最大值的函式,在主函式中呼叫 利用氣泡排序思想求二維陣列最大值。 演算法思想:先對各行進行一次氣泡排序,使每行的最後一個數為當前行中最大值, 即二維