美文网首页
动态规划

动态规划

作者: juexin | 来源:发表于2017-04-19 14:58 被阅读0次

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]表示一个长为i的字符串word1变为长为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个字母,对应的是word1[i-1],第j个字母,对应的是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]表示一个长为i的字符串word1变为长为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个字母,对应的是word1[i-1],第j个字母,对应的是word2[j-1]
                f[i][j] = f[i-1][j];//删除一个字母
              else
                f[i][j] = f[i-1][j-1] + f[i-1][j];//都增加一个字母,或删除s的一个字母
          }
        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;
    }
};

相关文章

网友评论

      本文标题:动态规划

      本文链接:https://www.haomeiwen.com/subject/lumczttx.html