Product of the Last K Numbers Solutions in C++
Number 1352
Difficulty Medium
Acceptance 43.7%
Link LeetCode
Other languages —
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/product-of-the-last-k-numbers//// Author : liuyubobobo/// Time : 2019-02-15#include <iostream>#include <vector>using namespace std;/// Brute Force/// Time Complexity: add: O(1)/// getProduct: O(k)/// Space Complexity: O(1)class ProductOfNumbers {private:vector<int> v;public:ProductOfNumbers() {}void add(int num) {v.push_back(num);}int getProduct(int k) {int res = 1;for(int i = v.size() - 1; i >= (int)v.size() - k; i --)res *= v[i];return res;}};int main() {ProductOfNumbers p;p.add(1);cout << p.getProduct(1) << endl;return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/product-of-the-last-k-numbers//// Author : liuyubobobo/// Time : 2019-02-17#include <iostream>#include <vector>using namespace std;/// Prefix Product/// Time Complexity: add: O(1)/// getProduct: O(1)/// Space Complexity: O(n)class ProductOfNumbers {private:vector<int> pre;public:ProductOfNumbers() {}void add(int num) {if(!num) pre = {1};else pre.push_back(pre.back() * num);}int getProduct(int k) {return k + 1 <= pre.size() ? pre.back() / pre[pre.size() - k - 1] : 0;}};int main() {ProductOfNumbers p;p.add(1);cout << p.getProduct(1) << endl;return 0;}