leetcode713 Subarray Product Less Than K
"""
Your are given an array of positive integers nums.
Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k.
Example 1:
Input: nums = [10, 5, 2, 6], k = 100
Output: 8
Explanation: The 8 subarrays that have product less than 100 are: [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6].
Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
"""
"""
正确做法,双指针加滑动窗口
很是巧妙
pro为乘积,pro不断乘以nums[j]得到子数组乘积,满足条件小于k时
!!!
res +=(j-i+1),res是计数器。
滑动窗口内元素间的相互组合的数量
"""
class Solution1:
def numSubarrayProductLessThanK(self, nums, k):
res, i = 0, 0
pro = 1 #乘积
if k <= 1: # 对于案例nums=[1, 2, 3] k=0 和 nums=[1, 1, 1] k=1
return 0
for j in range(len(nums)):
pro = pro * nums[j]
while pro >= k:
pro = pro // nums[i]
i += 1
res += j - i + 1 #!!!这个很关键
return res """
我的解法超时,
对于输入nums = [1, 1, 1, 1......,1] k=5
"""
class Solution2:
def numSubarrayProductLessThanK(self, nums, k):
res = 0
for i in range(len(nums)):
pro = 1
j = i
while j < len(nums):
if nums[j] * pro < k:
res += 1
pro = nums[j] * pro
j += 1
else:
break
return res
leetcode713 Subarray Product Less Than K的更多相关文章
- Subarray Product Less Than K LT713
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- [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 ...
- [LeetCode] Subarray Product Less Than K 子数组乘积小于K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- 713. Subarray Product Less Than K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- LeetCode Subarray Product Less Than K
原题链接在这里:https://leetcode.com/problems/subarray-product-less-than-k/description/ 题目: Your are given a ...
- Subarray Product Less Than K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
- 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...
- [leetcode] 713. Subarray Product Less Than K
题目 Given an array of integers nums and an integer k, return the number of contiguous subarrays where ...
- LeetCode Subarray Product Less Than K 题解 双指针+单调性
题意 给定一个正整数数组和K,数有多少个连续子数组满足: 数组中所有的元素的积小于K. 思路 依旧是双指针的思路 我们首先固定右指针r. 现在子数组的最右边的元素是nums[r]. 我们让这个子数组尽 ...
随机推荐
- 路由器安全-NetFlow
1.NetFlow介绍 提供高层次的诊断,分类和识别网络异常. 使用NetFlow来检查哪些行为改变明显的攻击是非常有效的. 就像Wiretap一样捕获数据包. NetFlow像电话账单.(谁和谁在通 ...
- MVC 拦截器
https://www.cnblogs.com/blosaa/archive/2011/06/02/2067632.html
- javascript入门教程02
JavaScript中的运算符 (1)算术运算符 + :相加 var a=123,b=45; document.write(a+b); - :相减 document.write(a-b); *:相乘 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:设置浮动和偏移
<!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...
- 2019年5月6日A股两百点暴跌行情思考
原因:特朗普推特发布贸易战消息 盘面:跳空低开,单边下跌,上证指数最大跌幅200点,收盘千股跌停 操作:开盘加仓,盘中加仓,尾盘满仓 总结: 特大黑天鹅事件爆发引发大盘暴跌时,后续必将迎来一个反弹机会 ...
- CentOS 7 搭建Cobbler实现自动化安装系统
1.安装软件包 # yum -y install epel-release #安装EPEL源 # yum -y install cobbler dhcp pykickstart 2.启动cob ...
- shell脚本基础及重定向!
重定向: 补充:/dev/null(名叫黑洞)就是把输出的文件混合重定向到黑洞从而不显示在屏幕 yum -y install http &> /dev/null 重定向输入: 重定向输出 ...
- Linux CentOS7 VMware LAMP架构Apache用户认证、域名跳转、Apache访问日志
一.Apache用户认证 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把111.com那个虚拟主机编辑成如下内容 <Virtu ...
- 实现JSP部分内容继承
我们的网站框架搭好以后,只需要主体部分显示不同的数据. 如果每次代码重写都会造成冗余. 今天欣赏别人代码,学到了 maven 核心代码 <dependency> <groupId&g ...
- 2 (mysql实战) 日志系统
前面我们系统了解了一个查询语句的执行流程,并介绍了执行过程中涉及的处理模块.相信你还记得,一条查询语句的执行过程一般是经过连接器.分析器.优化器.执行器等功能模块,最后到达存储引擎. 那么,一条更新语 ...