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]. 我们让这个子数组尽 ...
随机推荐
- Java IO流详解(三)——字节流InputStream和OutPutStream
我们都知道在计算机中,无论是文本.图片.音频还是视频,所有的文件都是以二进制(字节)形式存在的,IO流中针对字节的输入输出提供了一系列的流,统称为字节流.字节流是程序中最常用的流.在JDK中,提供了两 ...
- dp-简单迷宫捡金币
链接:https://ac.nowcoder.com/acm/challenge/terminal 吃货LP参加了珠海美食节,每见一家摊位都会大吃一顿,但是如果不加收敛,接下来的日子就只能吃土了,所以 ...
- 一份非常值得一看的Java面试题
包含的模块 本文分为十九个模块,分别是: Java 基础.容器.多线程.反射.对象拷贝.Java Web .异常.网络.设计模式.Spring/Spring MVC.Spring Boot/Sprin ...
- Servlet返回的数据js解析问题
Servlet返回的数据js解析问题 方式1:Json 接收函数:ajax.responseText后面没括号 其实在之前所说的ajax中还遗留了一些问题就是,Servlet返回给js的数据是如何被j ...
- 解题报告:luogu P1433 吃奶酪
题目链接:P1433 吃奶酪 我感觉可以改成:[模板]TSP问题(商旅问题) 了. 爆搜\(T\)一个点,考虑状压\(dp\)(还是爆搜). 我们用\(dp[i][j]\)表示现在是\(i\)状态,站 ...
- A letter for NW RDMA configuration
Dear : If you have to use EMC NW NDMA to backup oracle database and want to see what happen when bac ...
- 133、Java获取main主函数参数
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 测试人员如何使用Git
测试人员如何使用Git? 首先Git的安装,这里不多做阐述,直接去Git官方网站下载后并傻瓜式安装即可. 如何判定已安装好Git呢? ------------- 随便打开一个目录,鼠标右键点击可看到 ...
- change事件和input事件的区别
input事件: input事件在输入框输入的时候回实时响应并触发 change事件: change事件在input失去焦点才会考虑触发,它的缺点是无法实时响应.与blur事件有着相似的功能,但与bl ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:强制元素隐藏
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...