-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path99.4.swift
59 lines (44 loc) · 1.45 KB
/
99.4.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
56
57
58
59
public func solution(inout A : [Point2D]) -> Int {
// write your code in Swift 2.2 (Linux)
func isClockwise(a: Point2D, b: Point2D, c: Point2D) -> Int {
let result = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)
if result > 0 {
return 1
} else if result < 0 {
return -1
} else {
return 0
}
}
var lowestY = A[0].y
var lowestYIndex: [Int] = []
for i in 0..<A.count {
if A[i].y < lowestY {
lowestY = A[i].y
lowestYIndex = [i]
} else if A[i].y == lowestY {
lowestYIndex.append(i)
} else {
continue
}
}
var startPoint = lowestYIndex[0]
var lowestYArray = Array(count: A.count, repeatedValue: false)
for i in lowestYIndex {
lowestYArray[i] = true
}
while lowestYArray[startPoint] == true {
startPoint = (startPoint + 1) % A.count
}
startPoint = (startPoint - 1 + A.count) % A.count
let rotatedA = Array(A[startPoint..<A.count] + A[0..<startPoint])
let direction = isClockwise(rotatedA[rotatedA.count - 1], b: rotatedA[0], c: rotatedA[1])
let extendedA = rotatedA + Array(rotatedA[0..<2])
for i in 0..<A.count {
let temp = isClockwise(extendedA[i], b: extendedA[i + 1], c: extendedA[i + 2])
if temp * direction < 0 {
return (i + 1 + startPoint) % A.count
}
}
return -1
}