Check If It Is a Straight Line - Array - Easy - LeetCode
💻 coding

Check If It Is a Straight Line - Array - Easy - LeetCode

2 min read 473 words
2 min read
ShareWhatsAppPost on X
  • 1The problem requires checking if a set of points in the XY plane forms a straight line.
  • 2The solution involves calculating the slope and intercept of the line formed by the first two points.
  • 3The algorithm runs in O(n) time complexity and uses O(1) space complexity.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The problem requires checking if a set of points in the XY plane forms a straight line."

Check If It Is a Straight Line - Array - Easy - LeetCode

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

Constraints:

2 <= coordinates.length <= 1000

coordinates[i].length == 2

-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4

coordinates contains no duplicate point.

Solution:

using System;
using System.Collections.Generic;
using System.Text;

namespace LeetCode.AskGif.Easy.Array
{
 public class CheckStraightLineSoln
 {
 public bool CheckStraightLine(int[][] coordinates)
 {
 double m = 0;
 double c = 0;
 
 if(coordinates.Length == 0)
 {
 return false;
 }

 if(coordinates.Length == 1 || coordinates.Length == 2)
 {
 return true;
 }
 
 //Equation of Line y = mx + c
 //Let's find value for m and c;
 //y1 = mx1 + c
 //y2 = mx2 + c
 //y2 - y1 = m(x2 - x1);

 var x1 = coordinates[0][0];
 var x2 = coordinates[1][0];
 var y1 = coordinates[0][1];
 var y2 = coordinates[1][1];

 if((x2-x1) == 0) 
 {
 //check if all x is same;
 for (int i = 2; i < coordinates.Length; i++)
 {
 if(x1 != coordinates[i][0])
 {
 return false;
 };
 }
 return true;
 }

 m = (double)(y2 - y1) / (x2 - x1);
 c = y1 - m * x1;

 for (int i = 2; i < coordinates.Length; i++)
 {
 if(coordinates[i][1] != m * coordinates[i][0] + c)
 {
 return false;
 }
 }

 return true;
 }
 }
}

Time Complexity: O(n)

Space Complexity: O(1)

Unit Tests:

using LeetCode.AskGif.Easy.Array;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingUnitTest.Easy.Array
{
 [TestClass]
 public class CheckStraightLineSolnTests
 {
 [TestMethod]
 public void CheckStraightLineSoln_First()
 {
 var coordinates = new int[,] {
 { 1, 2 },
 { 2, 3 },
 { 3, 4 },
 { 4, 5 },
 { 5, 6 },
 { 6, 7 }
 }; 
 var output = true;

 var res = new CheckStraightLineSoln().CheckStraightLine(ArrayMapper(coordinates));

 Assert.AreEqual(output, res);
 }

 [TestMethod]
 public void CheckStraightLineSoln_Second()
 {
 var coordinates = new int[,] {
 { 1, 1 },
 { 2, 2 },
 { 3, 4 },
 { 4, 5 },
 { 5, 6 },
 { 7, 7 }
 };
 var output = false;

 var res = new CheckStraightLineSoln().CheckStraightLine(ArrayMapper(coordinates));

 Assert.AreEqual(output, res);
 }

 [TestMethod]
 public void CheckStraightLineSoln_Third()
 {
 var coordinates = new int[,] {
 { 0, 0 },
 { 0, 1 },
 { 0, -1 }
 };
 var output = true;

 var res = new CheckStraightLineSoln().CheckStraightLine(ArrayMapper(coordinates));

 Assert.AreEqual(output, res);
 }

 [TestMethod]
 public void CheckStraightLineSoln_Fourth()
 {
 var coordinates = new int[,] {
 { 2, 1 },
 { 4, 2 },
 { 6, 3 }
 };
 var output = true;

 var res = new CheckStraightLineSoln().CheckStraightLine(ArrayMapper(coordinates));

 Assert.AreEqual(output, res);
 }

 private int[][] ArrayMapper(int[,] matrix)
 {
 var arr = new int[matrix.GetLength(0)][];
 for (int i = 0; i < matrix.GetLength(0); i++)
 {
 arr[i] = new int[matrix.GetLength(1)];
 for (int j = 0; j < matrix.GetLength(1); j++)
 {
 arr[i][j] = matrix[i, j];
 }
 }

 return arr;
 }
 }
}

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 12 June 2020 · 2 min read · 473 words

Part of AskGif Blog · coding

You might also like