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

[1,2,3]

0

这个情况是可能导致j < i,所以要加上j= Math.max(i, j)

还有

[1,1,1,8,1,1,1,1,1,1,1,1,1]

5

除以8可能导致prod = 0,仅仅当i < j的时候才能做除法

class Solution {
    public int numSubarrayProductLessThanK(int[] nums, int k) {
        int prod = 1;
        int res = 0;
        for(int i = 0, j = 0; i < nums.length; i++) {
            j = Math.max(i, j);
            while(j < nums.length && prod * nums[j] < k) {
                prod *= nums[j++];
            }
            res += j - i;
            if(i < j) prod /= nums[i];
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/rookielet/p/10620886.html

时间: 2024-10-10 17:16:23

LeetCode 713. Subarray Product Less Than K的相关文章

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

[Swift]LeetCode713. 乘积小于K的子数组 | 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

LeetCode 1099. Two Sum Less Than K

原题链接在这里:https://leetcode.com/problems/two-sum-less-than-k/ 题目: Given an array A of integers and integer K, return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If no i, jexist satisfying this equation, return -1. Example

Leetcode - 628 Maximum Product of Three Numbers

Leetcode - 628 Maximum Product of Three Numbers 628. Maximum Product of Three Numbers Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4]

LeetCode——Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6. 原题链接: https://oj.leetcode.com/p

LeetCode: Maximum Subarray [052]

[题目] Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6. [题意] 给定一个数组,找出和最大的子数组,返回这个

[leetcode]Maximum Subarray @ Python

原题地址:https://oj.leetcode.com/problems/maximum-subarray/ 题意: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4],the contiguous subarray [4,?1,2,

【leetcode刷题笔记】Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解:最开始用了最naive的方法,每次在k个链表头中找出最小的元素,插入到新链表中.结果果断TLE了. 分析一下,如果这样做,每取出一个节点,要遍历k个链表一次,假设k个链表一共有n个节点,那么就需要O(nk)的时间复杂度. 参考网上的代码,找到了用最小堆的方法.维护一个大小为k的最小堆,存放当前k

(Java) LeetCode 25. Reverse Nodes in k-Group —— k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in