-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
41 lines (40 loc) · 1.28 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
using System;
using System.Collections.Generic;
namespace CreateSortedArraythroughInstructions
{
class Program
{
static void Main(string[] args)
{
int[] instructions = {1, 3, 3, 3, 2, 4, 2, 1, 2};
Console.WriteLine(CreateSortedArray(instructions));
}
static int CreateSortedArray(int[] instructions)
{
long res = 0;
var nums = new List<int>();
foreach (var instruction in instructions)
{
int li = 0, hi = nums.Count - 1;
while (li <= hi)
{
int mid = li + (hi - li) / 2;
if (nums[mid] >= instruction) hi = mid - 1;
else li = mid + 1;
}
int less = li;
li = 0; hi = nums.Count - 1;
while (li <= hi)
{
int mid = li + (hi - li) / 2;
if (nums[mid] <= instruction) li = mid + 1;
else hi = mid - 1;
}
int greater = nums.Count - li;
res += Math.Min(less, greater);
nums.Insert(li, instruction);
}
return (int) (res % (1_000_000_000 + 7));
}
}
}