码迷,mamicode.com
首页 > 其他好文 > 详细

330. Patching Array

时间:2016-07-10 06:27:13      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

    /*
     * 330. Patching Array
     * 2016-7-9 by Mingyang
     * hehe
     */
    public int minPatches(int[] nums, int n) {
        if (n <= 0)
            return 0;
        nums = nums == null ? new int[0] : nums;
        int current_ind = 0, ret = 0;
        long boundary_val = 1, sum = 0;
        while (boundary_val <= n) {
            if (current_ind < nums.length && nums[current_ind] <= boundary_val) {
                sum += nums[current_ind++];
                boundary_val = sum + 1;
            } else {
                ret++;
                sum += boundary_val;
                boundary_val = sum + 1;
            }
        }
        return ret;
    }

 

330. Patching Array

标签:

原文地址:http://www.cnblogs.com/zmyvszk/p/5657040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!