-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
50 lines (43 loc) · 1.19 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
49
50
using System;
using System.Collections.Generic;
namespace SnapshotArray
{
class Program
{
static void Main(string[] args)
{
var arr = new SnapshotArray(2);
Console.WriteLine(arr.Snap());
Console.WriteLine(arr.Get(1, 0));
Console.WriteLine(arr.Get(0, 0));
arr.Set(1, 8);
Console.WriteLine(arr.Get(1, 0));
arr.Set(0, 20);
Console.WriteLine(arr.Get(0, 0));
arr.Set(0, 7);
}
}
public class SnapshotArray
{
private Dictionary<int, int> changes;
private List<Dictionary<int, int>> snaps;
public SnapshotArray(int length)
{
changes = new Dictionary<int, int>();
snaps = new List<Dictionary<int, int>>();
}
public void Set(int index, int val)
{
changes[index] = val;
}
public int Snap()
{
snaps.Add(new Dictionary<int, int>(changes));
return snaps.Count - 1;
}
public int Get(int index, int snap_id)
{
return snaps[snap_id].ContainsKey(index) ? snaps[snap_id][index] : 0;
}
}
}