-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
83 lines (73 loc) · 2.76 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
namespace NestedListWeightSumII
{
interface NestedInteger
{
bool IsInteger();
// @return the single integer that this NestedInteger holds, if it holds a single integer
// Return null if this NestedInteger holds a nested list
int GetInteger();
// Set this NestedInteger to hold a single integer.
public void SetInteger(int value);
// Set this NestedInteger to hold a nested list and adds a nested integer to it.
public void Add(NestedInteger ni);
// @return the nested list that this NestedInteger holds, if it holds a nested list
// Return null if this NestedInteger holds a single integer
IList<NestedInteger> GetList();
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
static int DepthSumInverse_DFS(IList<NestedInteger> nestedList)
{
int maxHeight = 0, res = 0;
var record = new List<int[]>();
foreach (var nestedInteger in nestedList)
DFS(nestedInteger, 1, record, ref maxHeight);
foreach (var pair in record)
res += pair[0] * (maxHeight - pair[1] + 1);
return res;
}
static void DFS(NestedInteger integer, int height, List<int[]> record, ref int maxHeight)
{
if (integer.IsInteger())
{
maxHeight = Math.Max(maxHeight, height);
record.Add(new[] {integer.GetInteger(), height});
}
foreach (var nestedInteger in integer.GetList())
DFS(nestedInteger, height + 1, record, ref maxHeight);
}
static int DepthSumInverse_BFS(IList<NestedInteger> nestedList)
{
int maxHeight = 0, res = 0, height = 1;
var record = new List<int[]>();
var queue = new Queue<NestedInteger>();
foreach (var nestedInteger in nestedList)
queue.Enqueue(nestedInteger);
while (queue.Count != 0)
{
var count = queue.Count;
for (int i = 0; i < count; i++)
{
var cur = queue.Dequeue();
if (cur.IsInteger())
{
maxHeight = Math.Max(maxHeight, height);
record.Add(new[] { cur.GetInteger(), height });
}
foreach (var next in cur.GetList())
queue.Enqueue(next);
}
height++;
}
foreach (var pair in record)
res += pair[0] * (maxHeight - pair[1] + 1);
return res;
}
}
}