-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMed_713_SubArrayLessK.kt
More file actions
43 lines (35 loc) · 1.08 KB
/
Med_713_SubArrayLessK.kt
File metadata and controls
43 lines (35 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.boycoder.problems.array
import com.boycoder.utils.asserts
/**
* @Author: zhutao
* @datetime: 2021/6/21
* @desc: https://leetcode-cn.com/problems/subarray-product-less-than-k/
* Brutte force
* Two pointer
*/
object Med_713_SubArrayLessK {
fun numSubarrayProductLessThanK(nums: IntArray, k: Int): Int {
return count(nums, k)
}
private fun count(nums: IntArray, k: Int): Int {
if (nums.size == 0) return 0
var left = 0
var product = 1
var count = 0
for (right in 0 until nums.size) {
product = product * nums[right]
while (product >= k && left <= right) {
product = product / nums[left]
left++
}
// find the min left index that [left, right] product less than k
// and in this range, there is (right - left + 1) count
count = count + right - left + 1
}
return count
}
}
fun main() {
val res = Med_713_SubArrayLessK.numSubarrayProductLessThanK(intArrayOf(10,5,2,6), 100)
asserts(res, 8)
}