-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
66 lines (62 loc) · 1.65 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace InvalidTransactions
{
class Program
{
static void Main(string[] args)
{
string[] transactions = { "alice,20,800,mtv", "alice,50,100,mtv", "alice,51,100,frankfurt" };
Console.WriteLine(InvalidTransactions(transactions));
}
class Transaction
{
public int _id;
public string _name;
public int _time;
public int _amount;
public string _city;
public Transaction(int id, string name, int time, int amount, string city)
{
_id = id;
_name = name;
_time = time;
_amount = amount;
_city = city;
}
}
public static IList<string> InvalidTransactions(string[] transactions)
{
var dict = new Dictionary<string, List<Transaction>>();
var res = new HashSet<int>();
for (int i = 0; i < transactions.Length; i++)
{
var parts = transactions[i].Split(",");
if (!dict.ContainsKey(parts[0]))
dict[parts[0]] = new List<Transaction>();
dict[parts[0]].Add(new Transaction(i, parts[0], int.Parse(parts[1]), int.Parse(parts[2]), parts[3]));
}
foreach (var person in dict)
{
person.Value.Sort((a, b) => a._time - b._time);
for (int i = 0; i < person.Value.Count; i++)
{
var transaction = person.Value[i];
for (int j = i - 1; j >= 0; j--)
{
if (transaction._time - person.Value[j]._time <= 60 && transaction._city != person.Value[j]._city)
{
res.Add(transaction._id);
res.Add(person.Value[j]._id);
}
}
if (transaction._amount > 1000)
res.Add(transaction._id);
}
}
return res.Select(x => transactions[x]).ToList();
}
}
}