-
Notifications
You must be signed in to change notification settings - Fork 5
/
leetcode-1232-check-if-it-is-a-straight-line.swift
149 lines (106 loc) · 3.9 KB
/
leetcode-1232-check-if-it-is-a-straight-line.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
Copyright (c) 2020 David Seek, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
https://leetcode.com/problems/check-if-it-is-a-straight-line/
You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.
Example 1:
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
Output: true
Example 2:
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
Output: false
*/
/**
Big O Annotation
Time complexity O(n) where n is the number of elements in coordinates.
Space complexity O(n) where n is the number of elements in coordinates.
*/
func checkStraightLine(_ coordinates: [[Int]]) -> Bool {
/**
If we have less than 2 points
we can't have a line...
*/
guard coordinates.count > 1 else {
return false
}
/**
If we have exactly 2 points,
we are supposed to return true.
Makes sense, it's always a straight line...
*/
guard coordinates.count > 2 else {
return true
}
/*
The variable that will hold the last
results of the iteration
*/
var lastResult: Double?
// Let's get the Xs of the first 2 points
let element1x = coordinates[0][0]
let element2x = coordinates[1][0]
// And the Ys of the first 2 points
let element1y = coordinates[0][1]
let element2y = coordinates[1][1]
// Iterate through the rest
for index in 2..<coordinates.count {
// Get X and Y of that current point
let element = coordinates[index]
/**
Get the absolute value of the current
and our 2 original vectors
*/
let absoluteValue = getAbsoluteValue(
x1: element1x,
x2: element2x,
y1: element1y,
y2: element2y,
coordinate3: element
)
/**
Check if we have the same value as before,
if not we know it's not a straight line
and we can return false
*/
if let lastResult_ = lastResult, lastResult_ != absoluteValue {
return false
} else {
lastResult = absoluteValue
}
}
return true
}
/**
Returns the absolute value of each element in a vector.
We need to break up the calculation as below
as otherwise the compiler might spit out build warnings
for too complex calculation.
https://developer.apple.com/documentation/simd/fabs?language=objc
https://stackoverflow.com/questions/4083807/collinear-points
*/
private func getAbsoluteValue(x1: Int, x2: Int, y1: Int, y2: Int, coordinate3: [Int]) -> Double {
let coordinate3x = coordinate3[0]
let coordinate3y = coordinate3[1]
let e2xe1x = x2 - x1
let e3ye1y = coordinate3y - y1
let e3xe1x = coordinate3x - x1
let e2ye1y = y2 - y1
let firstTerm = e2xe1x * e3ye1y
let secondTerm = e3xe1x * e2ye1y
let result = firstTerm - secondTerm
return fabs(Double(result))
}