Lintcode 29,30,31,32

  • Post author:
  • Post category:其他




109 · 数字三角形

public class Solution {
    public int minimumTotal(int[][] triangle) {
        int n = triangle.length;
        if (n == 0) return -1;
        
        for (int i = n - 2; i >= 0; i --) {
            for (int j = 0; j <= i; j ++) {
                triangle[i][j] += Math.min(triangle[i + 1][j], triangle[i + 1][j + 1]);
            }
        }
        return triangle[0][0];
    }
}
public class Solution {
    public int minimumTotal(int[][] triangle) {
        int n = triangle.length;
        if (n == 0) return -1;
        
        //因为最后一行最后一列需要和右边的邻居比,所以这里声明数组,长度要加1
        int [] res = new int[n + 1];        

        for (int i = n - 1; i >= 0; i --) {
            for (int j = 0; j <= i; j ++) {
                res[j] = Math.min(res[j], res[j + 1]) + triangle[i][j];
            }
        }
        return res[0];
    }
}



关于29章17页:栈的容量是有限的,递归次数越多,消耗的栈空间越多,栈空间用完了就会溢出



114 · 不同的路径

public class Solution {
    public int uniquePaths(int m, int n) {
        int f[][] = new int[m + 1][n + 1];
        f[0][1] = 1;

        for (int i = 1; i <= m; i ++) {
            for (int j = 1; j <= n; j ++) {
                f[i][j] = f[i - 1][j] + f[i][j - 1];
            }
        }
        return f[m][n];
    }
}
public class Solution {
    public int uniquePaths(int m, int n) {
        int f[] = new int[n + 1];
        f[1] = 1;

        for (int i = 1; i <= m; i ++) {
            for (int j = 1; j <= n; j ++) {
                f[j] += f[j - 1];
            }
        }
        return f[n];
    }
}



115 · 不同的路径 II

public class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int n = obstacleGrid.length;
        int m = obstacleGrid[0].length;

        int f[] = new int [m];
        f[0] = 1;
        
        for (int i = 0; i < n; i ++) {
            if (obstacleGrid[i][0] == 1) f[0] = 0;      //有障碍方案数为1
            for (int j = 1; j < m; j ++) {
                if (obstacleGrid[i][j] == 1) f[j] = 0;
                else f[j] += f[j - 1];
            }
        }

        return f[m - 1];
    }
}



679 · 不同的路径 III

public class Solution {
    public int uniqueWeightedPaths(int[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        if (n == 0 || m == 0) return 0;        

        Set<Integer>[][] f = new HashSet[n][m];

        for (int i = 0; i < n; i ++) {
            for (int j = 0; j < m; j ++) {
                f[i][j] = new HashSet();
                if (i == 0 && j == 0) {
                    f[i][j].add(grid[i][j]);
                }
                else {
                    if (i != 0) {
                        for (int num : f[i - 1][j]) {
                            f[i][j].add(num + grid[i][j]);
                        }
                    }
                    if (j != 0) {
                        for (int num : f[i][j - 1]) {
                            f[i][j].add(num + grid[i][j]);
                        }
                    }
                }
            }
        }

        int ans = 0;
        for (int num : f[n - 1][m - 1]) ans += num;

        return ans;
    }
}



630 · 骑士的最短路径II

public class Solution {
     public int shortestPath2(boolean[][] grid) {
		int n = grid.length;
		int m = grid[0].length;
		if (n == 0 || m == 0) return -1;

		int[] dx = {-2, -1, 1, 2};
		int[] dy = {-1, -2, -2, -1};	

		int f[][] = new int[n][m];
		f[0][0] = 1;						//起点

	   	for (int j = 0; j < m; j ++) {		//从左到右
			for (int i = 0; i < n; i ++) {
				if (grid[i][j]) continue;	//有障碍物

			   	for (int k = 0; k < 4; k ++) {
					int x = i + dx[k];
					int y = j + dy[k];					
				   	if (x < 0 || y < 0 || x >= n || f[x][y] == 0) continue;
					
					if (f[i][j] == 0) f[i][j] = f[x][y] + 1;
					else f[i][j] = Math.min(f[i][j], f[x][y] + 1);
			   	}
		   	}
	   	}

	   	return f[n - 1][m - 1] - 1;
    }
}



116 · 跳跃游戏

public class Solution {
    public boolean canJump(int[] A) {
        int n = A.length;
        if (n == 0) return true;

        int maxx = 0;          //能够到达的最远处
        for (int i = 0; i < n; i ++) {
            if (i <= maxx) {
                maxx = Math.max(maxx, i + A[i]);
            }
        }
        return maxx >= n - 1;
    }
}



92 · 背包问题

  • 二维数组
public class Solution {
    public int backPack(int m, int[] A) {
        int n = A.length;

        int f[][] = new int[n + 1][m + 1];  //前i件物品放入容量为j的背包,最多能装f[i][j]
        for (int i = 1; i <= n; i ++) {
            int t = A[i - 1];				//第i件物品的重量是A[i - 1]
            for (int j = 0; j <= m; j ++) {         
                if (t > j) f[i][j] = f[i - 1][j];
                else f[i][j] = Math.max(f[i - 1][j], f[i - 1][j - t] + t);
            }
        }

        return f[n][m];
    }
}
  • 一维数组
public class Solution {
    public int backPack(int m, int[] A) {
    	int n = A.length;
    	
        int f[] = new int[m + 1];		//前i件物品放入容量为j的背包,最多能装f[j]
        for (int i = 1; i <= n; i ++) {
            int t = A[i - 1];			//第i件物品的重量是A[i - 1]
            for (int j = m; j >= t; j --) {      		
                f[j] = Math.max(f[j], f[j - t] + t);
            }
        }

        return f[m];
    }
}
  • 一维 + 或运算
public class Solution {
    public int backPack(int m, int[] A) {
    	int n = A.length;
    	
        int f[] = new int[m + 1];       //如果前i件物品可以装出j的容量,则f[j] = 1        
        f[0] = 1;
        
        for (int i = 1; i <= n; i ++) {
            int t = A[i - 1];           //第i件物品的重量是A[i - 1]
            for (int j = m; j >= t; j --) {  
                f[j] |= f[j - t];
            }
        }

        int ans = 0;
        for (int i = m; i >= 0; i --) {
            if (f[i] == 1) {
                ans = i;
                break;
            }
        }
        return ans;
    }
}



724 · 最小划分

public class Solution {
    public int findMin(int[] nums) {
        int n = nums.length;
        int m = 0;
        for (int i = 0; i < n; i ++) m += nums[i];

        int f[] = new int[m + 1];

        for (int i = 1; i <= n; i ++) {
            int t = 2 * nums[i - 1];
            for (int j = m; j >= t; j --) {
                f[j] = Math.max(f[j], f[j - t] + t);
            }
        }

        return m - f[m];
    }
}



125 · 背包问题(二)

public class Solution {
    public int backPackII(int m, int[] A, int[] V) {
        int n = A.length;

        int f[] = new int[m + 1];
        for (int i = 1; i <= n; i ++) {
            for (int j = m; j >= A[i - 1]; j --) {
                f[j] = Math.max(f[j], f[j - A[i - 1]] + V[i - 1]);
            }
        }

        return f[m];
    }
}



440 · 背包问题 III

32章26页,这是完全背包,并不是多重背包

public class Solution {
    public int backPackIII(int[] A, int[] V, int m) {
       int n = A.length;

        int f[] = new int[m + 1];
        for (int i = 1; i <= n; i ++) {
            for (int j = A[i - 1]; j <= m; j ++) {
                f[j] = Math.max(f[j], f[j - A[i - 1]] + V[i - 1]);
            }
        }

        return f[m];
    }
}



562 · 背包问题 IV

public class Solution {
    public int backPackIV(int[] nums, int target) {
        int n = nums.length;
        int m = target;

        int f[] = new int[m + 1];
        f[0] = 1;

        for (int i = 1; i <= n; i ++) {
            for (int j = nums[i - 1]; j <= m; j ++) {
                f[j] += f[j - nums[i - 1]];
            }
        }

        return f[m];
    }
}



563 · 背包问题 V

public class Solution {
    public int backPackV(int[] nums, int target) {
        int n = nums.length;
        int m = target;

        int f[] = new int[m + 1];
        f[0] = 1;

        for (int i = 1; i <= n; i ++) {
            for (int j = m; j >= nums[i - 1]; j --) {
                f[j] += f[j - nums[i - 1]];
            }
        }

        return f[m];        

    }
}



版权声明:本文为davidliule原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。