-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
56 lines (54 loc) · 1.65 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
using System;
namespace PutBoxesIntoTheWarehouseI
{
class Program
{
static void Main(string[] args)
{
int[] boxes = { 4, 3, 4, 1 };
int[] warehouses = { 5, 3, 3, 4, 1 };
Console.WriteLine(MaxBoxesInWarehouse_BruteForce(boxes, warehouses));
}
static int MaxBoxesInWarehouse_BruteForce(int[] boxes, int[] warehouse)
{
Array.Sort(boxes);
int lastRoom = warehouse.Length - 1;
int res = 0;
for (int i = 0; i < boxes.Length; i++)
{
for (int j = 0; j < lastRoom; j++)
{
if (boxes[i] > warehouse[j])
{
lastRoom = j;
break;
}
}
if (lastRoom != warehouse.Length - 1)
lastRoom--;
res += lastRoom >= 0 ? 1 : 0;
if (lastRoom < 0) break;
}
return res;
}
static int MaxBoxesInWarehouse_TwoPointers(int[] boxes, int[] warehouse)
{
var maxHeight = new int[warehouse.Length];
var min = warehouse[0];
for (int i = 0; i < warehouse.Length; i++)
{
min = Math.Min(min, warehouse[i]);
maxHeight[i] = min;
}
Array.Sort(boxes);
int p1 = maxHeight.Length - 1, p2 = 0;
while (p1 >= 0 && p2 < boxes.Length)
{
if (boxes[p2] <= maxHeight[p1])
p2++;
p1--;
}
return p2;
}
}
}