Simple java solution


  • 0
    J

    The idea is exactly the same as this one: https://leetcode.com/discuss/51721/simple-java-dp-solution

    public class Solution {
        public int minCostII(int[][] costs) {
            int n = costs.length, m = (n == 0) ? 0 : costs[0].length;
            if (n == 0) return 0;
            for (int i = 1; i < n; i++) {
                for (int k = 0; k < m; k++) {
                    costs[i][k] += min(costs[i - 1], k);
                }
            }
            return min(costs[n - 1], -1);
        }
        
        int min(int[] A, int avoid) {
            int min = Integer.MAX_VALUE;
            for (int i = 0; i < A.length; i++) {
                if (i != avoid) {
                    min = Math.min(min, A[i]);
                }
            }
            return min;
        }
    }

Log in to reply
 

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