File tree Expand file tree Collapse file tree 2 files changed +0
-27
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +0
-27
lines changed Original file line number Diff line number Diff line change 22
33public class _713 {
44 public static class Solution1 {
5- /**
6- * O(n^2) solution, accepted initially, then Leetcode added one test case to fail it.
7- */
8- public int numSubarrayProductLessThanK (int [] nums , int k ) {
9- int result = 0 ;
10- for (int i = 0 ; i < nums .length ; i ++) {
11- int product = nums [i ];
12- if (product < k ) {
13- result ++;
14- for (int j = i + 1 ; j < nums .length ; j ++) {
15- product *= nums [j ];
16- if (product < k ) {
17- result ++;
18- } else {
19- break ;
20- }
21- }
22- }
23- }
24- return result ;
25- }
26- }
27-
28- public static class Solution2 {
295 public int numSubarrayProductLessThanK (int [] nums , int k ) {
306 if (k < 2 ) {
317 return 0 ;
Original file line number Diff line number Diff line change 88
99public class _713Test {
1010 private static _713 .Solution1 solution1 ;
11- private static _713 .Solution2 solution2 ;
1211 private static int [] nums ;
1312 private static int k ;
1413
1514 @ BeforeClass
1615 public static void setup () {
17- solution2 = new _713 .Solution2 ();
1816 solution1 = new _713 .Solution1 ();
1917 }
2018
2119 @ Test
2220 public void test1 () {
2321 nums = new int []{1 , 2 , 3 };
2422 k = 0 ;
25- assertEquals (0 , solution2 .numSubarrayProductLessThanK (nums , k ));
2623 assertEquals (0 , solution1 .numSubarrayProductLessThanK (nums , k ));
2724 }
2825
You can’t perform that action at this time.
0 commit comments