1. """
  2. Your are given an array of positive integers nums.
  3. Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k.
  4. Example 1:
  5. Input: nums = [10, 5, 2, 6], k = 100
  6. Output: 8
  7. Explanation: The 8 subarrays that have product less than 100 are: [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6].
  8. Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
  9. """
  10. """
  11. 正确做法,双指针加滑动窗口
  12. 很是巧妙
  13. pro为乘积,pro不断乘以nums[j]得到子数组乘积,满足条件小于k时
  14. !!!
  15. res +=(j-i+1),res是计数器。
  16. 滑动窗口内元素间的相互组合的数量
  17. """
  18. class Solution1:
  19. def numSubarrayProductLessThanK(self, nums, k):
  20. res, i = 0, 0
  21. pro = 1 #乘积
  22. if k <= 1: # 对于案例nums=[1, 2, 3] k=0 和 nums=[1, 1, 1] k=1
  23. return 0
  24. for j in range(len(nums)):
  25. pro = pro * nums[j]
  26. while pro >= k:
  27. pro = pro // nums[i]
  28. i += 1
  29. res += j - i + 1 #!!!这个很关键
  30. return res
  31.  
  32. """
  33. 我的解法超时,
  34. 对于输入nums = [1, 1, 1, 1......,1] k=5
  35. """
  36. class Solution2:
  37. def numSubarrayProductLessThanK(self, nums, k):
  38. res = 0
  39. for i in range(len(nums)):
  40. pro = 1
  41. j = i
  42. while j < len(nums):
  43. if nums[j] * pro < k:
  44. res += 1
  45. pro = nums[j] * pro
  46. j += 1
  47. else:
  48. break
  49. return res

leetcode713 Subarray Product Less Than K的更多相关文章

  1. Subarray Product Less Than K LT713

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

  2. [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 ...

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

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

  4. 713. Subarray Product Less Than K

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

  5. LeetCode Subarray Product Less Than K

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

  6. Subarray Product Less Than K

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

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

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

  8. [leetcode] 713. Subarray Product Less Than K

    题目 Given an array of integers nums and an integer k, return the number of contiguous subarrays where ...

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

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

随机推荐

  1. Java IO流详解(三)——字节流InputStream和OutPutStream

    我们都知道在计算机中,无论是文本.图片.音频还是视频,所有的文件都是以二进制(字节)形式存在的,IO流中针对字节的输入输出提供了一系列的流,统称为字节流.字节流是程序中最常用的流.在JDK中,提供了两 ...

  2. dp-简单迷宫捡金币

    链接:https://ac.nowcoder.com/acm/challenge/terminal 吃货LP参加了珠海美食节,每见一家摊位都会大吃一顿,但是如果不加收敛,接下来的日子就只能吃土了,所以 ...

  3. 一份非常值得一看的Java面试题

    包含的模块 本文分为十九个模块,分别是: Java 基础.容器.多线程.反射.对象拷贝.Java Web .异常.网络.设计模式.Spring/Spring MVC.Spring Boot/Sprin ...

  4. Servlet返回的数据js解析问题

    Servlet返回的数据js解析问题 方式1:Json 接收函数:ajax.responseText后面没括号 其实在之前所说的ajax中还遗留了一些问题就是,Servlet返回给js的数据是如何被j ...

  5. 解题报告:luogu P1433 吃奶酪

    题目链接:P1433 吃奶酪 我感觉可以改成:[模板]TSP问题(商旅问题) 了. 爆搜\(T\)一个点,考虑状压\(dp\)(还是爆搜). 我们用\(dp[i][j]\)表示现在是\(i\)状态,站 ...

  6. 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 ...

  7. 133、Java获取main主函数参数

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  8. 测试人员如何使用Git

    测试人员如何使用Git? 首先Git的安装,这里不多做阐述,直接去Git官方网站下载后并傻瓜式安装即可. 如何判定已安装好Git呢?  ------------- 随便打开一个目录,鼠标右键点击可看到 ...

  9. change事件和input事件的区别

    input事件: input事件在输入框输入的时候回实时响应并触发 change事件: change事件在input失去焦点才会考虑触发,它的缺点是无法实时响应.与blur事件有着相似的功能,但与bl ...

  10. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:强制元素隐藏

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...