-
Notifications
You must be signed in to change notification settings - Fork 115
/
0332-ReconstructItinerary.cs
136 lines (111 loc) · 3.76 KB
/
0332-ReconstructItinerary.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//-----------------------------------------------------------------------------
// Runtime: 292ms
// Memory Usage: 34.1 MB
// Link: https://leetcode.com/submissions/detail/359552897/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
public class _0332_ReconstructItinerary
{
public IList<string> FindItinerary(IList<IList<string>> tickets)
{
var map = new Dictionary<string, MinPriorityQueue>();
foreach (var ticket in tickets)
{
if (!map.ContainsKey(ticket[0]))
map.Add(ticket[0], new MinPriorityQueue());
map[ticket[0]].Insert(ticket[1]);
}
var result = new List<string>();
Visit("JFK", map, result);
return result;
}
private void Visit(string airport, IDictionary<string, MinPriorityQueue> map, IList<string> route)
{
while (map.ContainsKey(airport) && map[airport].Size() > 0)
{
var target = map[airport].DeleteMin();
Visit(target, map, route);
}
route.Insert(0, airport);
}
public class MinPriorityQueue : PriorityQueue
{
public MinPriorityQueue() : base() { }
public MinPriorityQueue(int initCapacity) : base(initCapacity) { }
protected override void Sink(int k)
{
while (2 * k <= N)
{
int j = 2 * k;
if (j < N && pq[j].CompareTo(pq[j + 1]) > 0) j++;
if (pq[k].CompareTo(pq[j]) <= 0) break;
Swap(k, j);
k = j;
}
}
protected override void Swim(int k)
{
while (k > 1 && pq[k / 2].CompareTo(pq[k]) > 0)
{
Swap(k / 2, k);
k = k / 2;
}
}
public string Min() => First();
public string DeleteMin() => Delete();
}
public abstract class PriorityQueue
{
protected int N;
protected string[] pq;
public PriorityQueue() : this(1) { }
public PriorityQueue(int initCapacity)
{
this.N = 0;
pq = new string[initCapacity + 1];
}
public bool IsEmpty() => N == 0;
public int Size() => N;
public string First()
{
if (IsEmpty()) { throw new ArgumentOutOfRangeException(); }
return pq[1];
}
public void Insert(string x)
{
if (N >= pq.Length - 1)
Resize(pq.Length * 2);
pq[++N] = x;
Swim(N);
}
protected abstract void Swim(int k);
public string Delete()
{
var result = pq[1];
Swap(1, N--);
Sink(1);
pq[N + 1] = string.Empty;
if (N > 0 && N == (pq.Length - 1) / 4)
Resize(pq.Length / 2);
return result;
}
protected abstract void Sink(int k);
private void Resize(int newCapacity)
{
var temp = new string[newCapacity + 1];
for (int i = 1; i <= N; i++)
temp[i] = pq[i];
pq = temp;
}
protected void Swap(int index1, int index2)
{
var temp = pq[index1];
pq[index1] = pq[index2];
pq[index2] = temp;
}
}
}
}