-
Notifications
You must be signed in to change notification settings - Fork 2
/
Contents.swift
55 lines (35 loc) · 1.3 KB
/
Contents.swift
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
import Foundation
public func solution(_ A : inout [Int]) -> Int {
// write your code in Swift 4.2.1 (Linux)
let positiveValues = A.filter({ $0 > 0 })
if positiveValues.count == 0 {
return 1
}
let uniqueValues = Array(Set(positiveValues))
let sortedValues = uniqueValues.sorted(by: { $0 < $1 })
for i in 0..<sortedValues.count {
if sortedValues[i] != (i + 1) {
return (i + 1)
}
}
return sortedValues[sortedValues.count - 1] + 1
}
var A1 = [1, 3, 6, 4, 1, 2] // -> 5
var A2 = [1, 2, 3] // -> 4
var A3 = [-1, -3] // -> 1
print(solution(&A1))
print(solution(&A2))
print(solution(&A3))
// MARK: - Prompt
/*
This is a demo task.
Write a function:
public func solution(_ A : inout [Int]) -> Int
that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.
For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.
Given A = [1, 2, 3], the function should return 4.
Given A = [−1, −3], the function should return 1.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..100,000];
each element of array A is an integer within the range [−1,000,000..1,000,000].
*/