Old Top Coder riddle: Making a number by inserting +

后端 未结 6 1677
借酒劲吻你
借酒劲吻你 2021-02-06 05:41

I am thinking about this topcoder problem.

Given a string of digits, find the minimum number of additions required for the string to equal some target n

6条回答
  •  离开以前
    2021-02-06 05:53

    dynamic programming :

    public class QuickSums {
    
    public static int req(int n, int[] digits, int sum) {
    
        if (n == 0) {
            if (sum == 0)
                return 0;
            else
                return -1;
        } else if (n == 1) {
            if (sum == digits[0]) {
                return 0;
            } else {
                return -1;
            }
        }
    
        int deg = 1;
        int red = 0;
    
        int opt = 100000;
        int split = -1;
    
        for (int i=0; i

提交回复
热议问题