Leetcode 713 Subarray Product Less Than K (子数组乘积大于K的个数) (双指针)

目录

  • 问题描述
  • 例子
  • 方法

Leetcode 713

问题描述

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.

方法

** Solution Java **
** 7ms, beats 98.53% **
** 49.5MB, beats 100.00% **
class Solution {
    public int numSubarrayProductLessThanK(int[] nums, int k) {
        if (k == 0)
            return 0;
        int product = 1, res = 0, n = nums.length;
        for (int left = 0, right = 0; right < n; ++right){
            product *= nums[right];
            while (left <= right && product >= k)
                product /= nums[left++];
            res += right - left + 1;
        }
        return res;
    }
}
** Solution Python3 **
** 1060ms, beats 36.99% **
** 16.7MB, beats 14.29% **
class Solution(object):
    def numSubarrayProductLessThanK(self, nums, k):
        if (k == 0) :
            return 0
        product, res, left = 1, 0, 0
        for right in range(len(nums)) :
            product *= nums[right]
            while (left <= right and k <= product) :
                product /= nums[left]
                left += 1
            res += right - left + 1
        return res

原文地址:https://www.cnblogs.com/willwuss/p/12590981.html

时间: 2024-07-31 05:35:06

Leetcode 713 Subarray Product Less Than K (子数组乘积大于K的个数) (双指针)的相关文章

LeetCode 713. Subarray Product Less Than K

Problem Description: 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. 题解: 很快想到了two pointer的解法,但是一直被corner case困住. 两个典型的case [

二分查找,查指定值、小于k的最大值,大于k的最大值

我们经常会用到二分查找 二分查找应该很多人都会写了,今天要写一个用二分查找找到小于k的最大值的时候看了很久不懂他设计的思路,后来想通了,记录一下. 所以这篇主要是讲 用二分查找找到小于k的最大值和大于k的最大值. 二分查找查找指定值 这个挺简单的,直接上代码吧 //获取值是k的位置,找不到则返回-1 public static int getK(int[] a, int k){ if(a.length == 0){ return -1; } int l = 0; int r = a.length

[LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead. Example: Input: s = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: the subarr

LeetCode之Maximum Product Subarray

1.(原文)问题描述 Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 2.翻译 找最大的数组中的子数组乘积 例如,给予的数组是[2

最大和子数组/最大和子序列

最大和子数组是数组中和最大的子数组,又名最大和子序列.子数组是数组中连续的n个元素,比如a2,a3,a4就是一个长度为3的子数组.顾名思义求最大和子数组就是要求取和最大的子数组. n个元素的数组包含n个长度为1的子数组:{a0},{a1},…{an-1}: n个元素的数组包含n-1个长度为2的子数组:{a0,a1},{a1,a2},{an-2,an-1}: ……………………………………………………………………………………………… n个元素的数组包含1个长度为n的子数组:{a0,a1,…,an-1

子数组和最大

1.设计思路 直接将每个数组的和计算出来然后比较大小 2.代码 #include<iostream>#include<ctime>using namespace std; void main(){     int a[15];     int f,i,j,k,m,n;     int sum,max=-100;     cout<<"请输入数值|X|的范围(0-?):";     cin>>f;     srand((unsigned)

整数子数组求最大和添加验证

实验要求: 要求程序必须能处理1000个元素 每个元素是int32类型的,出现子数组之和大于整形表示的最大范围会出现什么情况 设计思路: 首先进行数组的溢出处理,从小向大开始验证,直到程序处理时间较长. 然后进行程序所支持的最大整数测试,因为结果或过程中所出现的最大数是无法预测的,所以只需确定最大的随机数范围. 测试结果: 100个数据: 200个数据: 500个数据: 1000个数据: 99999999—999999999 999999999—9999999999 package 子数组更改;

编程之美 2.14求数组的子数组之和的最大值

对于一个有N个元素的数组,a[0]~a[n-1],求子数组最大值. 如:数组A[] = [−2, 1, −3, 4, −1, 2, 1, −5, 4],则连续的子序列[4,−1,2,1]有最大的和6. 方法一:暴力 循环遍历,输出所有,判断最大的和 1 #include"iostream" 2 #define MAX 1001 3 using namespace std; 4 5 int main(){ 6 int n, a[MAX], sum , maxsum ; 7 8 cin &

子数组返回一个整数数组中最大的和

本次实验的要求: 1要求程序必须能处理1000个程序: 2每个元素是int32类型的,出现子数组之和大于整型表示的最大范围会是什么情况: 3输入一个整形数组,数组里有正数也有负数. 4数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. 5求所有子数组的和的最大值.要求时间复杂度为O(o). 设计思路: 将数组的大小定义为1000,每个元素定义为int32类型,取数值时对数成2的32次方,这样数值可以越界. 程序代码 #include <iostream>  #include<