Dynamic planning-The total number of sub-arrays multiplied by K Subarray product less Than k

Source: Internet
Author: User

2018-09-01 23:02:46

Problem solving:

Problem solving:

At the beginning, a glance at the past is a DP, save each number end of the length and, finally, the sum is good, as for the length of how to ask, the subject needs to use sliding window to maintain.

After writing the code, it is not necessary to actually apply for a 1000-length array to save each number, and simply sum it up.

    public int Numsubarrayproductlessthank (int[] nums, int k) {        int res = 0;        int begin = 0;        int cur = 1;        for (int end = 0; end < Nums.length; end++) {            cur *= nums[end];            while (Begin <= End && cur >= k) {                cur/= nums[begin];                begin++;            }            if (end < begin) Continue;            Res + = End-begin + 1;        }        return res;    }

Dynamic planning-The total number of sub-arrays multiplied by K Subarray product less Than k

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.