My 12ms c++ solution,beats 100%


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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.