2018-09-01 23:02:46

问题求解:

问题求解:

最开始的时候,一眼看过去就是一条 dp 嘛,保存每个数字结尾的长度和,最后求和就好,至于长度如何求,本题中需要用滑动窗口来维护。

很好的题目,将滑动窗口算法和动态规划巧妙的结合了起来。

    public int numSubarrayProductLessThanK(int[] nums, int k) {
if (k <= 1) return 0;
int res = 0;
int begin = 0;
int cur = 1;
for (int end = 0; end < nums.length; end++) {
cur *= nums[end];
while (cur >= k) {
cur /= nums[begin];
begin++;
}
res += end - begin + 1;
}
return res;
}

  

动态规划-子数组乘积小于k的总个数 Subarray Product Less Than K的更多相关文章

  1. [LeetCode] Subarray Product Less Than K 子数组乘积小于K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  2. Subarray Product Less Than K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  3. leetcode713 Subarray Product Less Than K

    """ Your are given an array of positive integers nums. Count and print the number of ...

  4. Subarray Product Less Than K LT713

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  5. [Swift]LeetCode713. 乘积小于K的子数组 | Subarray Product Less Than K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  6. [Swift]LeetCode918. 环形子数组的最大和 | Maximum Sum Circular Subarray

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  7. LeetCode Subarray Product Less Than K 题解 双指针+单调性

    题意 给定一个正整数数组和K,数有多少个连续子数组满足: 数组中所有的元素的积小于K. 思路 依旧是双指针的思路 我们首先固定右指针r. 现在子数组的最右边的元素是nums[r]. 我们让这个子数组尽 ...

  8. 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...

  9. LeetCode Subarray Product Less Than K

    原题链接在这里:https://leetcode.com/problems/subarray-product-less-than-k/description/ 题目: Your are given a ...

随机推荐

  1. Android之数据存储之SharedPreferences

    SharedPreferences是以键值对形式存储数据,主要用于记录系统的设置,如飞行模式是否开启,声音大小的值等.//SharedPreferences方式保存到xml文件SharedPrefer ...

  2. html5-Input类型

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>html5-Input类型& ...

  3. Office 2016 永久激活

    启示:office突然过期,QWQ,卖电脑的真坑爹,找了好多办法,总结2个不花钱的办法啦. 1>只有30天试用期 Office 2016预览版序列号:NKGG6-WBPCC-HXWMY-6DQG ...

  4. centos7 install fastdfs nginx

    https://github.com/judasn/Linux-Tutorial/blob/master/markdown-file/FastDFS-Nginx-Lua-GraphicsMagick. ...

  5. JavaScript中数组中的方法:push()、pop()、shift()、unshift()、slice()、splice()、reverse()、join()、split()、concat()、indexOf()、forEach()、map()、

      1.创建数组的几种方法 //a).通过new来创建数组,new可以省略 var arr=new Array(); var arr=Array(); //b). .通过new来创建数组,并且赋值 v ...

  6. topcoder srm 681 div1

    problem1 link 二分答案.然后判断.将所有的机器按照$a_{i}$排序,$a_{i}$相同的按照$b_{i}$排序.用一个优先队列维护这些机器.这样对于第$i$个部分,拿出队列开始的机器来 ...

  7. Hakase and Nano 【思维博弈】

    Hakase and Nano 时间限制: 1 Sec  内存限制: 128 MB 提交: 400  解决: 104 [提交] [状态] [命题人:admin] 题目描述 Hakase and Nan ...

  8. Centos7.5安装VirtualBox-5.2

    1.查看自己的内核版本 [root@localhost /]# rpm -qa |grep kernel kernel-tools-libs-3.10.0-862.6.3.el7.x86_64 ker ...

  9. 大臣的旅费|2013年蓝桥杯A组题解析第十题-fishers

    标题:大臣的旅费 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市 ...

  10. Shiro源码分析

    1.入口类:AbstractAuthenticator 用户输入的登录信息经过其authenticate方法: public final AuthenticationInfo authenticate ...