首页 > 代码库 > split-array-largest-sum(参考了discuss)

split-array-largest-sum(参考了discuss)

 

注意,第一种用法,涉及到一些Java的知识。就是采用Object作为HashMap的key的时候,需要重载这个Class的 equals 和 hashCode 这两个方法。其中equals需要判断一下比较元素的类型,而hashCode 里面可以采用 String.valueOf(val).hashCode() ^ 的方法来处理。

在HashMap里面查找的时候,会调用HashMap里面的元素的equals方法,把待查找的元素作为参数传给这个方法,来进行比较和判断元素是否存在于HashMap中。

// 参考 https://discuss.leetcode.com/topic/61315/java-easy-binary-search-solution-8ms// 开始用类似回溯的方法做,ETL了public class Solution {        public int splitArray(int[] nums, int m) {        int mlen = nums.length - m;        int minM = 0;        int maxM = 0;        int sum = 0;        for (int k=0; k<nums.length; k++) {            sum += nums[k];            if (k > mlen) {                sum -= nums[k-1-mlen];            }            maxM = Math.max(maxM, sum);            minM = Math.max(minM, nums[k]);        }        System.out.printf("min:%d, max %d\n", minM, maxM);        int result = bsearch(nums, m, minM, maxM);        return result;    }    private int bsearch(int[] nums, int m, int low, int high) {        int mid = 0;        while (low < high) {            mid = low + (high-low) / 2;            if (isValid(nums, m, mid)) {                high = mid;            } else {                low = mid + 1;            }        }        return high;    }    private boolean isValid(int[] nums, int m, int cand) {        int split = 1;        int sum = 0;        for (int i=0; i<nums.length; i++) {            sum += nums[i];            if (sum > cand) {                split++;                if (split > m) {                    return false;                }                sum = nums[i];            }        }        return true;    }    /*    class KPair {        public int pos;        public int m;        @Override        public int hashCode() {            int ret = String.valueOf(pos).hashCode() ^ String.valueOf(m).hashCode();            return ret;        }        @Override        public boolean equals(Object obj) {            if (null == obj) {                return false;            }            if (!(obj instanceof KPair)) {                return false;            }            KPair kp = (KPair)obj;            //System.out.printf("kp%d p%d km%d m%d\n", kp.pos, pos, kp.m, m);            return kp.pos == pos && kp.m == m;        }    }    public int splitArray(int[] nums, int m) {        Map mp = new HashMap();        KPair okp = new KPair();        int tmp = 0;        int newval = 0;        KPair kp =  new KPair();        kp.pos = 0;        kp.m = 1;        //System.out.printf("in1 p%d m%d\n", kp.pos, kp.m);        mp.put(kp, nums[0]);        for (int i=1; i<nums.length; i++) {            okp.pos = i-1;            okp.m = 1;            tmp = (int)(mp.get(okp))+nums[i];            KPair kp2 = new KPair();            kp2.pos = i;            kp2.m = 1;            //System.out.printf("in2 p%d m%d\n", kp2.pos, kp2.m);            mp.put(kp2, tmp);            for (int k=0; k<i; k++) {                // tmp is sum of k+1 to i                tmp -= nums[k];                okp.pos = k;                for (int j=2; j<=m && j<=k+2; j++) {                    okp.m = j-1;                    //System.out.printf("for2 p%d m%d\n", okp.pos, okp.m);                    newval = (int)(mp.get(okp));                    if (tmp > newval) {                        newval = tmp;                    }                    KPair kp3 = new KPair();                    kp3.pos = i;                    kp3.m = j;                    if (mp.get(kp3) == null || (int)(mp.get(kp3)) > newval) {                        //System.out.printf("in3 p%d m%d\n", kp3.pos, kp3.m);                        mp.put(kp3, newval);                    }                }            }        }        KPair kpr =  new KPair();        kpr.pos = nums.length-1;        kpr.m = m;        return (int)(mp.get(kpr));    }    */}

 

split-array-largest-sum(参考了discuss)