-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1232_Check_If_It_Is_a_Straight_line.cpp
More file actions
98 lines (90 loc) · 2.6 KB
/
1232_Check_If_It_Is_a_Straight_line.cpp
File metadata and controls
98 lines (90 loc) · 2.6 KB
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
/*******************************************************************************
* 1232_Check_If_It_Is_a_Straight_line.cpp
* Billy.Ljm
* 05 June 2023
*
* =======
* Problem
* =======
* 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.
*
* ===========
* My Approach
* ===========
* We calculate the slope `(y2-y1)/(x2-x1)` b/w a common point `coordinates[0]`
* and every other point. If the lines formed b/w pairs share the same slope and
* same point, the lines will be collinear.
*
* This has a time complexity of O(n) and space complexity of O(1), where n is
* the nuber of points.
******************************************************************************/
#include <iostream>
#include <vector>
/**
* Solution
*/
class Solution {
private:
/**
* Gets the slope b/w two points
*
* @param pt1 xy-coordinates of 1st point
* @param pt2 xy-coordinates of 2nd point
*
* @return normalised vector b/w pt1 and pt2.
*/
float getSlope(std::vector<int> pt1, std::vector<int> pt2) {
if (pt2[0] == pt1[0]) return FLT_MAX;
else return 1.0 * (pt2[1] - pt1[1]) / (pt2[0] - pt1[0]);
}
public:
/**
* Checks if a list of xy coordinates lie on a straight line
*
* @param coordinates list of [x,y] coordinates
*
* @return true if all coorindates lie on the same line, false otherwise
*/
bool checkStraightLine(std::vector<std::vector<int>>& coordinates) {
// get slope b/w 0 and 1
float dir = getSlope(coordinates[0], coordinates[1]);
// check slope b/w 0 and i
for (int i = 2; i < coordinates.size(); i++) {
if (getSlope(coordinates[0], coordinates[i]) != dir) return false;
}
// all direction same
return true;
}
};
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (int i = 0; i < v.size(); i++) {
os << v[i] << ",";
}
os << "\b]";
return os;
}
/**
* Test cases
*/
int main(void) {
Solution sol;
std::vector<std::vector<int>> coordinates;
// test case 1
coordinates = { {1,2}, {2,3}, {3,4}, {4,5}, {5,6}, {6,7} };
std::cout << "checkStraightLine(" << coordinates << ") = ";
std::cout << std::boolalpha << sol.checkStraightLine(coordinates) << std::endl;
// test case 2
coordinates = { {1,1}, {2,2}, {3,4}, {4,5}, {5,6}, {7,7} };
std::cout << "checkStraightLine(" << coordinates << ") = ";
std::cout << std::boolalpha << sol.checkStraightLine(coordinates) << std::endl;
return 0;
}