-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathSwapNodes[Algo].cs
More file actions
156 lines (121 loc) · 3.9 KB
/
SwapNodes[Algo].cs
File metadata and controls
156 lines (121 loc) · 3.9 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Swap Nodes [Algo]
// https://www.hackerrank.com/challenges/swap-nodes-algo/problem
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
/*
* Complete the swapNodes function below.
*/
static int[][] swapNodes(int[][] indexes, int[] queries)
{
var queue = new Queue<Node>();
var dic = new Dictionary<int, List<Node>>();
var root = new Node
{
Data = 1,
Level = 1,
};
dic.Add(1, new List<Node> { root });
queue.Enqueue(root);
var counter = 0;
Func<int, Node, Node> get = (int data, Node current) =>
{
var res = data != -1 ? new Node
{
Data = data,
Level = current.Level + 1,
} : null;
if (res != null)
{
if (dic.TryGetValue(current.Level + 1, out var list))
list.Add(res);
else
dic.Add(current.Level + 1, new List<Node> { res });
queue.Enqueue(res);
}
return res;
};
while (queue.Count > 0)
{
var current = queue.Dequeue();
var values = indexes[counter++];
current.Left = get(values[0], current);
current.Right = get(values[1], current);
}
var result = new List<int[]>();
foreach (var query in queries)
result.Add(Swap(dic, query, root));
return result.ToArray();
}
static int[] Swap(Dictionary<int, List<Node>> dic, int query, Node root)
{
var mul = 1;
while (query * mul <= dic.Count)
if (dic.TryGetValue(query * mul++, out var list))
list.ForEach(x => x.Toggled = !x.Toggled);
var result = new List<int>();
result.AddRange(GetForPrint(root.Left));
result.Add(root.Data);
result.AddRange(GetForPrint(root.Right));
return result.ToArray();
}
private static IEnumerable<int> GetForPrint(Node node)
{
if (node == null) yield break;
foreach (var item in GetForPrint(node.Left))
yield return item;
yield return node.Data;
foreach (var item in GetForPrint(node.Right))
yield return item;
}
class Node
{
Node left;
Node right;
public bool Toggled { get; set; }
public int Level { get; set; }
public int Data { get; set; }
public Node Left
{
get => Toggled ? right : left;
set
{
if (Toggled) right = value;
else left = value;
}
}
public Node Right
{
get => Toggled ? left : right;
set
{
if (Toggled) left = value;
else right = value;
}
}
}
static void Main(string[] args)
{
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int n = Convert.ToInt32(Console.ReadLine());
int[][] indexes = new int[n][];
for (int indexesRowItr = 0; indexesRowItr < n; indexesRowItr++)
{
indexes[indexesRowItr] = Array.ConvertAll(Console.ReadLine().Split(' '), indexesTemp => Convert.ToInt32(indexesTemp));
}
int queriesCount = Convert.ToInt32(Console.ReadLine());
int[] queries = new int[queriesCount];
for (int queriesItr = 0; queriesItr < queriesCount; queriesItr++)
{
int queriesItem = Convert.ToInt32(Console.ReadLine());
queries[queriesItr] = queriesItem;
}
int[][] result = swapNodes(indexes, queries);
textWriter.WriteLine(String.Join("\n", result.Select(x => String.Join(" ", x))));
textWriter.Flush();
textWriter.Close();
}
}