-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
48 lines (43 loc) · 1.14 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Reflection.Metadata;
namespace ProductOfTheLastKNumbers
{
class Program
{
static void Main(string[] args)
{
var product = new ProductOfNumbers();
product.Add(3);
product.Add(0);
product.Add(2);
product.Add(5);
product.Add(4);
Console.WriteLine(product.GetProduct(2));
Console.WriteLine(product.GetProduct(3));
Console.WriteLine(product.GetProduct(4));
product.Add(8);
Console.WriteLine(product.GetProduct(2));
}
}
public class ProductOfNumbers
{
private List<int> prefix;
public ProductOfNumbers()
{
prefix = new List<int> {1};
}
public void Add(int num)
{
if (num == 0)
prefix = new List<int> {1};
else
prefix.Add(num * prefix[^1]);
}
public int GetProduct(int k)
{
if (k >= prefix.Count) return 0;
return prefix[^1] / prefix[^(k + 1)];
}
}
}