Problem

There is now an order with demand for n items, and the demand for the i-th item is order[i]. The factory has m production modes. Each production mode is shaped like [p[1],p[2],...p[n]], that is, produce p[1] first items, p[2] second items... You can use multiple production modes. Please tell me how many items do not meet the demand at least in the case of not exceeding the demand of any kind of items?

Example

Given order=[2,3,1], pattern=[[2,2,0],[0,1,1],[1,1,0]] , return 0.

Explanation:
Use [0,1,1] once, [1,1,0] twice, remaining [0,0,0].
Given order=[2,3,1], pattern=[[2,2,0]] , return 2.

Explanation:
Use [2,2,0] once, remaining [0,1,1].

Solution

public class Solution {
    private int minCount;
    /**
     * @param order: The order
     * @param pattern: The pattern
     * @return: Return the number of items do not meet the demand at least
     */
    public int getMinRemaining(int[] order, int[][] pattern) {
        for (int count: order) {
            minCount += count;
        }
        
        if (order == null || order.length == 0) {
            return 0;
        }
        
        if (pattern == null || pattern.length == 0) {
            return minCount;
        }

        int[] record = new int[order.length];
        DFS(order, pattern, record, 0);
        return minCount;
    }

    private void DFS(int[] order, int[][] pattern, int[] record, int curIndex) {
        if (!isValid(order, record) || curIndex == pattern.length) {
            return;
        }
        // path_1: 
        DFS(order, pattern, record, curIndex + 1);
        
        int max = getMaxUsage(order, pattern[curIndex]);
        if (max < 0) {
            return;
        }
        for (int i = 0; i < max; i++) {
            for (int j = 0; j < order.length; j++) {
                record[j] += pattern[curIndex][j];
            }
            // path_2
            DFS(order, pattern, record, curIndex + 1);
        }
        
        for (int j = 0; j < order.length; j++) {
            record[j] -= (max * pattern[curIndex][j]);
        }
    }

    // get the max times the pattern can be used, if any item exceeds the limit, return -1
    private int getMaxUsage(int[] order, int[] pattern) {
        int max = -1;
        for (int i = 0; i < order.length; i++) {
            if (order[i] < pattern[i]) {
                return -1;
            } else if (pattern[i] > 0) {
                int cur = order[i] / pattern[i];
                if (cur < max || max < 0) {
                    max = cur;
                }
            } else {
                continue;
            }
        }
        return max;
    }

    //check if the record is valid, update minCount if true
    private boolean isValid(int[] order, int[] record) {
        int curCount = 0;
        for (int i = 0; i < order.length; i++) {
            int diff = order[i] - record[i];
            if (diff < 0) {
                return false;
            }
            curCount += diff;
        }
        minCount = Math.min(minCount, curCount);
        return true;
    }
}

linspiration
161 声望53 粉丝