Skip to content

Commit 7d19930

Browse files
authored
Create Product of the Last K Numbers
1 parent 6c12467 commit 7d19930

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
想法:因為只會出現 0 - 100 的數字,所以我們可以用一個二維矩陣紀錄到目前為止看過每個數字的次數;而最後 k 個數字可以利用
2+
每個數字出現次數減掉每個數字在 k 個數字前的出現次數而得到最後 k 個數字是什麼
3+
接著用快速冪平方法 / pow() 的運算求出最後 k 元素的乘積
4+
5+
// assume there are n add() operations and m getProduct() operations
6+
Time Complexity : O( n + mlogk ) for each add() need O(1) time and getProduct() need O(logk) time
7+
Space Complexity : O(n) for storing n arrays , each one has O(100) = O(1) elements
8+
9+
class ProductOfNumbers {
10+
public:
11+
vector<vector<int>> numbercount ;
12+
ProductOfNumbers() {
13+
vector<int> init(101) ;
14+
numbercount.push_back(init) ;
15+
}
16+
17+
void add(int num) {
18+
vector<int> lastcount = numbercount.back() ;
19+
lastcount[num]++ ;
20+
numbercount.push_back(lastcount) ;
21+
}
22+
23+
int getProduct(int k) {
24+
vector<int> lastelements = numbercount.back() ;
25+
int zeros = lastelements[0] - numbercount[numbercount.size() - 1 -k][0] ;
26+
if (zeros > 0)
27+
return 0 ;
28+
29+
for(int i = 1 ; i < 101 ; i++)
30+
lastelements[i] -= numbercount[ numbercount.size() - 1 - k ][i] ;
31+
32+
int product = 1 ;
33+
for(int i = 1 ; i < 101 ; i++)
34+
product *= pow(i , lastelements[i]) ;
35+
return product ;
36+
}
37+
};
38+
39+
/**
40+
* Your ProductOfNumbers object will be instantiated and called as such:
41+
* ProductOfNumbers* obj = new ProductOfNumbers();
42+
* obj->add(num);
43+
* int param_2 = obj->getProduct(k);
44+
*/
45+
46+
// 法二:由於 0 的出現,只要最後 k 個元素中有包含 0,最後乘積必然是 0;所以實際上我們只需要紀錄末尾不為 0 的數字有哪些即可
47+
如果 k > 末尾不為 0 的數字長度,即可直接輸出 0
48+
接著,為了方便快速運算,我們可以使用 prefix product 紀錄前面已獲得的乘積
49+
那麼最後 k 個元素的乘機就是 prefixproduct[size] - prefixproduct[size- k]
50+
51+
Time Complexity : O(n + m) for each operation need O(1) time
52+
Space Complexity : O(n) for record the non-zero elements
53+
54+
class ProductOfNumbers {
55+
public:
56+
vector<int> prefixproduct ;
57+
int size = 0 ; // size == prefixproduct.size() - 1
58+
ProductOfNumbers() {
59+
prefixproduct.push_back(1) ;
60+
}
61+
62+
void add(int num) {
63+
if (num == 0) {
64+
prefixproduct = {1} ;
65+
size = 0 ;
66+
}
67+
else {
68+
prefixproduct.push_back( prefixproduct.back() * num ) ;
69+
size++ ;
70+
}
71+
}
72+
73+
int getProduct(int k) {
74+
// prefixproduct record last size elements products
75+
if ( k > size)
76+
return 0 ;
77+
else
78+
return prefixproduct[size] / prefixproduct[size - k] ;
79+
80+
}
81+
};
82+
83+
/**
84+
* Your ProductOfNumbers object will be instantiated and called as such:
85+
* ProductOfNumbers* obj = new ProductOfNumbers();
86+
* obj->add(num);
87+
* int param_2 = obj->getProduct(k);
88+
*/
89+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy