-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path713.java
53 lines (47 loc) · 1.64 KB
/
713.java
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
44
45
46
47
48
49
50
51
52
53
__________________________________________________________________________________________________
sample 7 ms submission
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
if(k<=1) return 0;
int prod = 1, left = 0, cnt = 0;
for(int right = 0; right < nums.length; right++){
prod *= nums[right];
while(prod>=k){
prod /= nums[left++];
}
cnt += right - left + 1;
}
return cnt;
}
}
__________________________________________________________________________________________________
sample 52776 kb submission
class Solution {
public int numSubarrayProductLessThanK(int[] nums, int k) {
List<Integer> prevs = new LinkedList<>();
int subCount = 0;
for(int num : nums){
List<Integer> currs = new LinkedList<>();
if(num < k){
if(num==1){
prevs.add(0,1);
currs=prevs;
}else{
currs.add(num);
int prod = 0;
while(!prevs.isEmpty() && prod<k){
int prev = prevs.remove(0);
prod = prev*num;
if(prod < k){
currs.add(prod);
}
}
}
}
subCount += currs.size();
prevs = currs;
}
return subCount;
}
}
__________________________________________________________________________________________________