-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
38 lines (37 loc) · 1.12 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
using System;
using System.Collections.Generic;
namespace CreateTargetArrayInTheGivenOrder
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 4, 2, 4, 3, 2 };
int[] index = { 0, 0, 1, 3, 1 };
Console.WriteLine(CreateTargetArray(nums, index));
}
static int[] CreateTargetArray(int[] nums, int[] index)
{
var record = new HashSet<int>();
for (int i = 0; i < index.Length; i++)
{
if (!record.Contains(index[i]))
record.Add(index[i]);
else
{
for (int j = 0; j < i; j++)
{
if (index[j] >= index[i])
index[j]++;
if (!record.Contains(index[j]))
record.Add(index[j]);
}
}
}
var res = new int[nums.Length];
for (int i = 0; i < index.Length; i++)
res[index[i]] = nums[i];
return res;
}
}
}