動(dòng)態(tài)規(guī)劃

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 of all numbers along its path.

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

class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        vector<vector<int>> f(m,vector<int>(n,0));
        
        f[0][0] = grid[0][0];
        for(int i=1;i<m;i++)
          f[i][0] = f[i-1][0] + grid[i][0];
        for(int j=1;j<n;j++)
          f[0][j] = f[0][j-1] + grid[0][j];
          
        for(int i=1;i<m;i++)
          for(int j=1;j<n;j++)
          {
              f[i][j] = min(f[i-1][j],f[i][j-1]) + grid[i][j];
          }
        return f[m-1][n-1];
    }
};

62. 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) {
        vector<vector<int>> f(100,vector<int>(100,0));
        
        f[0][0] = 1;
        for(int i=1;i<m;i++)
          f[i][0] = f[i-1][0];
        for(int j=1;j<n;j++)
          f[0][j] = f[0][j-1];
        
        for(int i=1;i<m;i++)
          for(int j=1;j<n;j++)
          {
              f[i][j] = f[i][j-1] + f[i-1][j];
          }
        return f[m-1][n-1];
    }
};

63. Unique Paths II

Follow up for "Unique Paths":

Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1 and 0 respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
  [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) {
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        
        vector<vector<int>> f(m,vector<int>(n,0));
        if(obstacleGrid[0][0]==0)
          f[0][0] = 1;
        else
          f[0][0] = 0;
        for(int i=1;i<m;i++)
        {
            if(obstacleGrid[i][0]==0)
              f[i][0] = f[i-1][0];
            else
              f[i][0] = 0;
        }
        for(int j=1;j<n;j++)
        {
            if(obstacleGrid[0][j]==0)
              f[0][j] = f[0][j-1];
            else
              f[0][j] = 0;
        }
        
        for(int i=1;i<m;i++)
          for(int j=1;j<n;j++)
          {
              if(obstacleGrid[i][j]==0)
                f[i][j] = f[i-1][j] + f[i][j-1];
              else
                f[i][j] = 0;
          }
        return f[m-1][n-1];  
    }
};

72. Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character
b) Delete a character
c) Replace a character

class Solution {
public:
    int minDistance(string word1, string word2) {
        int m = word1.size();
        int n = word2.size();
        if(m<=0)
          return n;
        if(n<=0)
          return m;
        
        vector<vector<int>> f(m+1,vector<int>(n+1,0));
        //f[i][j]表示一個(gè)長(zhǎng)為i的字符串word1變?yōu)殚L(zhǎng)為j的字符串word2的最短距離
        f[0][0] = 0;//都沒有字母
        for(int i=1;i<=m;i++)
        {
            f[i][0] = i;
        }
        for(int j=1;j<=n;j++)
        {
            f[0][j] = j;
        }
        for(int i=1;i<=m;i++)
          for(int j=1;j<=n;j++)
          {
              if(word1[i-1]==word2[j-1]) //f中的第i個(gè)字母,對(duì)應(yīng)的是word1[i-1],第j個(gè)字母,對(duì)應(yīng)的是word2[j-1]
                f[i][j] = f[i-1][j-1];
              else
                f[i][j] = min(f[i-1][j-1],min(f[i-1][j],f[i][j-1])) + 1;
          }
        return f[m][n];
    }
};

115. Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
Here is an example:
S = "rabbbit", T = "rabbit"
Return 3.

class Solution {
public:
    int numDistinct(string s, string t) {
        int m = s.size();
        int n = t.size();
        if(m<n)
          return 0;
        
        vector<vector<int>> f(m+1,vector<int>(n+1,0));
        //f[i][j]表示一個(gè)長(zhǎng)為i的字符串word1變?yōu)殚L(zhǎng)為j的字符串word2的最短距離
        f[0][0] = 1;//都沒有字母
        for(int i=1;i<=m;i++)
        {
            f[i][0] = 1;
        }
        for(int j=1;j<=n;j++)
        {
            f[0][j] = 0;
        }
        for(int i=1;i<=m;i++)
          for(int j=1;j<=n;j++)
          {
              if(s[i-1]!=t[j-1]) //f中的第i個(gè)字母,對(duì)應(yīng)的是word1[i-1],第j個(gè)字母,對(duì)應(yīng)的是word2[j-1]
                f[i][j] = f[i-1][j];//刪除一個(gè)字母
              else
                f[i][j] = f[i-1][j-1] + f[i-1][j];//都增加一個(gè)字母,或刪除s的一個(gè)字母
          }
        return f[m][n];
    }
};

118. Pascal's Triangle

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]
]
class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<int> temp;
        vector<vector<int>> rec(numRows,temp);
        for(int i=0;i<numRows;i++)
          for(int j=0;j<i+1;j++)
              rec[i].push_back(1);
         
        
        for(int i=1;i<numRows;i++)
            for(int j=1;j<i;j++)
              rec[i][j] = rec[i-1][j-1] + rec[i-1][j];
        
        return rec;
    }
};

119. Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,
Return [1,3,3,1].

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> temp;
        vector<vector<int>> rec(rowIndex+1,temp);
        for(int i=0;i<rowIndex+1;i++)
          for(int j=0;j<i+1;j++)
              rec[i].push_back(1);
         
        
        for(int i=1;i<rowIndex+1;i++)
            for(int j=1;j<i;j++)
              rec[i][j] = rec[i-1][j-1] + rec[i-1][j];
        
        return rec[rowIndex];        
    }
};

120. 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 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).

class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        int m = triangle.size();
        int result = INT_MAX;
        vector<vector<int>> f(m,vector<int>(m,0));
        
        f[0][0] = triangle[0][0];
        for(int i=1;i<m;i++)
        {
            f[i][0] = f[i-1][0] + triangle[i][0];
            f[i][i] = f[i-1][i-1] + triangle[i][i];
            cout<<f[i][0]<<"--"<<f[i][i]<<endl;
        }

        for(int i=1;i<m;i++)
          for(int j=1;j<i;j++)  //j只能到i-1
          {
              f[i][j] = min(f[i-1][j-1],f[i-1][j]) + triangle[i][j];
          }
        for(int k=0;k<m;k++)
        {
            if(f[m-1][k]<result)
              result = f[m-1][k];
            cout<<f[m-1][k]<<" ";
        }
        return result;
    }
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 如果跟一個(gè)自己不喜歡,抗拒的人在一起,我想,這樣的生活我才會(huì)后悔吧。 我不知道大家都在急什么,剛走出校園就要被逼婚...
    紀(jì)錄回憶閱讀 238評(píng)論 0 0
  • 11月26日,第53屆臺(tái)灣電影金馬獎(jiǎng)在臺(tái)北國(guó)父紀(jì)念館舉行。周冬雨、馬思純憑借《七月與安生》拿下本屆金馬雙影后獎(jiǎng)。 ...
    奶油拌飯閱讀 694評(píng)論 0 0

友情鏈接更多精彩內(nèi)容