-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0364-NestedListWeightSumII.cs
132 lines (116 loc) · 4.36 KB
/
0364-NestedListWeightSumII.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
//-----------------------------------------------------------------------------
// Runtime: 68ms
// Memory Usage: 19.7 MB
// Link: https://leetcode.com/submissions/detail/369931182/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* interface NestedInteger {
*
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* 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();
* }
*/
public class _0364_NestedListWeightSumII
{
public int DepthSumInverse(IList<NestedInteger> nestedList)
{
if (nestedList.Count == 0) return 0;
var levels = new Dictionary<int, IList<int>>();
FlatList(nestedList, levels, 1);
if (levels.Count == 0) return 0;
var max = levels.Keys.Max();
var sum = 0;
foreach (var key in levels.Keys)
sum += levels[key].Sum() * (max - key + 1);
return sum;
}
private void FlatList(IList<NestedInteger> nestedList, IDictionary<int, IList<int>> levels, int level)
{
foreach (var ni in nestedList)
{
if (ni.IsInteger())
{
if (levels.ContainsKey(level))
levels[level].Add(ni.GetInteger());
else
levels[level] = new List<int>() { ni.GetInteger() };
}
else
FlatList(ni.GetList(), levels, level + 1);
}
}
public class NestedInteger
{
private int? number;
private IList<NestedInteger> list;
// Constructor initializes an empty nested list.
public NestedInteger()
{
number = null;
list = new List<NestedInteger>();
}
// Constructor initializes a single integer.
public NestedInteger(int value)
{
number = value;
}
// @return true if this NestedInteger holds a single integer, rather than a nested list.
public bool IsInteger()
{
return number.HasValue;
}
// @return the single integer that this NestedInteger holds, if it holds a single integer
// Return null if this NestedInteger holds a nested list
public int GetInteger()
{
return number.Value;
}
// Set this NestedInteger to hold a single integer.
public void SetInteger(int value)
{
number = value;
}
// Set this NestedInteger to hold a nested list and adds a nested integer to it.
public void Add(NestedInteger ni)
{
number = null;
if (list == null)
list = new List<NestedInteger>() { ni };
else
list.Add(ni);
}
// @return the nested list that this NestedInteger holds, if it holds a nested list
// Return null if this NestedInteger holds a single integer
public IList<NestedInteger> GetList()
{
return list;
}
}
}
}