-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcp-004-missing-positive-integer.linq
74 lines (59 loc) · 1.82 KB
/
dcp-004-missing-positive-integer.linq
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
<Query Kind="Program" />
// This problem was asked by Stripe.
// Given an array of integers, find the first missing positive integer
// in linear time and constant space. In other words, find the lowest
// positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well.
//
// For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.
// You can modify the input array in-place.
void Main()
{
f2(new[] { 3, 4, -1, 1 }).Dump("2");
f2(new[] { 1, 2, 0 }).Dump("3");
f2(new[] { 5, 3, 2, 1, }).Dump("4");
f2(new[] { 5, 4, 2, 1, }).Dump("3");
f2(new[] { 5, 4, 3, 2, }).Dump("1");
f2(new[] { 4, 3, 2, 1, }).Dump("5");
f2(new[] { -4, -3, -2, -1, }).Dump("1");
}
public int f(int[] a)
{
// problem -- space: O(n)
HashSet<int> set = new HashSet<int>();
// time: O(n)
foreach (var x in a)
set.Add(x); // O(1)
// time: O(n)
for (int i = 1; true; i++)
if (!set.Contains(i)) // O(1)
return i;
}
public int f2(int[] a)
{
// when n is amount of elements, answer is less than or equal (n + 1)
// given we can use an array as memory, let use sign of the number to encode what we want to store
// let say that negative number at index i means that (i + 1) is present
// since we do not care about about not positive numbers, let's replace them with something bigger than n first
int n = a.Length;
// zero pass - get rid of non positive
for (int i = 0; i < n; i++)
if (a[i] <= 0)
a[i] = n + 1;
// first pass - counting
for (int i = 0; i < n; i++)
{
int val = Math.Abs(a[i]);
if (val <= n)
{
// store that "val" is present as a "-" sign (if not yet negative)
if (a[val - 1] > 0)
a[val - 1] *= -1;
}
}
//a.Dump();
// second pass - figure out missing
for (int i = 0; i < n; i++)
if (a[i] > 0)
return i + 1;
return n + 1;
}