Leetcode Minimum Path Sum-----java

Source: Internet
Author: User

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.

Note:you can only move either down or right at any point in time.

Or the extension of the previous question, to find the minimum path. Can still be extended on previous versions. In fact, are all 62 variants, understand DP, in fact, it is not difficult.

 Public classSolution { Public intMinpathsum (int[] grid) {        intm =grid.length; intn = grid[0].length; if(m = = 0 | | n = = 0)            return0; intresult = 0; if(M = = 1){             for(inti = 0;i<n;i++) Result+=grid[0][i]; returnresult; }        if(n = = 1){             for(inti = 0;i<m;i++) {result+ = Grid[i][0]; }            returnresult; }        int[] DP =New int[n]; dp[0] = grid[0][0];  for(inti = 1; i<n;i++) {Dp[i]= Dp[i-1]+grid[0][i]; }         for(inti = 1;i<m;i++) {dp[0] = dp[0]+grid[i][0];  for(intj = 1;j<n;j++) {Dp[j]= Dp[j]>dp[j-1]?dp[j-1]+grid[i][j]:d p[j]+Grid[i][j]; }        }        returnDp[n-1]; }}

Leetcode Minimum Path Sum-----java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.