|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../count-negative-numbers-in-a-sorted-matrix "Count Negative Numbers in a Sorted Matrix") |
| 9 | + |
| 10 | +[Next >](../maximum-number-of-events-that-can-be-attended "Maximum Number of Events That Can Be Attended") |
| 11 | + |
| 12 | +## [1352. Product of the Last K Numbers (Medium)](https://leetcode.com/problems/product-of-the-last-k-numbers "最后 K 个数的乘积") |
| 13 | + |
| 14 | +<p>Implement the class <code>ProductOfNumbers</code> that supports two methods:</p> |
| 15 | + |
| 16 | +<p>1.<code> add(int num)</code></p> |
| 17 | + |
| 18 | +<ul> |
| 19 | + <li>Adds the number <code>num</code> to the back of the current list of numbers.</li> |
| 20 | +</ul> |
| 21 | + |
| 22 | +<p>2.<code> getProduct(int k)</code></p> |
| 23 | + |
| 24 | +<ul> |
| 25 | + <li>Returns the product of the last <code>k</code> numbers in the current list.</li> |
| 26 | + <li>You can assume that always the current list has <strong>at least</strong> <code>k</code> numbers.</li> |
| 27 | +</ul> |
| 28 | + |
| 29 | +<p>At any time, the product of any contiguous sequence of numbers will fit into a single 32-bit integer without overflowing.</p> |
| 30 | + |
| 31 | +<p> </p> |
| 32 | +<p><strong>Example:</strong></p> |
| 33 | + |
| 34 | +<pre> |
| 35 | +<strong>Input</strong> |
| 36 | +["ProductOfNumbers","add","add","add","add","add","getProduct","getProduct","getProduct","add","getProduct"] |
| 37 | +[[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]] |
| 38 | + |
| 39 | +<strong>Output</strong> |
| 40 | +[null,null,null,null,null,null,20,40,0,null,32] |
| 41 | + |
| 42 | +<strong>Explanation</strong> |
| 43 | +ProductOfNumbers productOfNumbers = new ProductOfNumbers(); |
| 44 | +productOfNumbers.add(3); // [3] |
| 45 | +productOfNumbers.add(0); // [3,0] |
| 46 | +productOfNumbers.add(2); // [3,0,2] |
| 47 | +productOfNumbers.add(5); // [3,0,2,5] |
| 48 | +productOfNumbers.add(4); // [3,0,2,5,4] |
| 49 | +productOfNumbers.getProduct(2); // return 20. The product of the last 2 numbers is 5 * 4 = 20 |
| 50 | +productOfNumbers.getProduct(3); // return 40. The product of the last 3 numbers is 2 * 5 * 4 = 40 |
| 51 | +productOfNumbers.getProduct(4); // return 0. The product of the last 4 numbers is 0 * 2 * 5 * 4 = 0 |
| 52 | +productOfNumbers.add(8); // [3,0,2,5,4,8] |
| 53 | +productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers is 4 * 8 = 32 |
| 54 | +</pre> |
| 55 | + |
| 56 | +<p> </p> |
| 57 | +<p><strong>Constraints:</strong></p> |
| 58 | + |
| 59 | +<ul> |
| 60 | + <li>There will be at most <code>40000</code> operations considering both <code>add</code> and <code>getProduct</code>.</li> |
| 61 | + <li><code>0 <= num <= 100</code></li> |
| 62 | + <li><code>1 <= k <= 40000</code></li> |
| 63 | +</ul> |
| 64 | + |
| 65 | +### Related Topics |
| 66 | + [[Design](../../tag/design/README.md)] |
| 67 | + [[Array](../../tag/array/README.md)] |
| 68 | + |
| 69 | +### Hints |
| 70 | +<details> |
| 71 | +<summary>Hint 1</summary> |
| 72 | +Keep all prefix products of numbers in an array, then calculate the product of last K elements in O(1) complexity. |
| 73 | +</details> |
| 74 | + |
| 75 | +<details> |
| 76 | +<summary>Hint 2</summary> |
| 77 | +When a zero number is added, clean the array of prefix products. |
| 78 | +</details> |
0 commit comments