📜  查找是否只有两条平行线包含所有坐标点

📅  最后修改于: 2021-10-23 08:43:32             🧑  作者: Mango

给定一个数组,该数组表示坐标平面上一组点的 y 坐标,其中 (i, arr[i]) 表示单个点。找出是否可以绘制一对平行线,其中包括给定的所有坐标点,并且两条线都必须包含一个点。可能打印1 ,不可能打印0

例子:

方法:由点(x1,y1)和(x2,y2)组成的直线的斜率是y2-y2/x2-x1。由于给定的数组由点的坐标组成 (i, arr[i])。因此,(arr[2]-arr[1]) / (2-1) 是由 (1, arr[i]) 和 (2, arr[2]) 构成的线的斜率。只考虑三个点,比如 P0(0, arr[0]), P1(1, arr[1]) 和 P2(2, arr[2]) 因为要求只有两条平行线,这是强制性的这三个点中的两个在同一条线上。所以,三种可能的情况是:

  • P0 和 P1 在同一条线上,因此它们的斜率将是 arr[1]-arr[0]
  • P1 和 P2 在同一条线上,因此它们的斜率将是 arr[2]-arr[1]
  • P0 和 P2 在同一条线上,因此它们的斜率将是 arr[2]-arr[0]/2

取三种情况中的一种,假设 P0 和 P1 位于同一条线上,在这种情况下,让 m=arr[1]-arr[0] 是我们的斜率。对于数组 (i, arr[i]) 中的一般点,直线方程为:

=> (y-y1) = m (x-x1)
=> y-arr[i] = m (x-i)
=> y-mx = arr[i] - mi

现在,由于 y-mx=c 是这里的一般直线方程 c = arr[i] -mi。现在,如果给定数组的解是可能的,那么我们必须正好有两个截距 (c)。
因此,如果上述三种可能中的任何一种存在两个不同的截距,则所需的解决方案是可能的,并打印 1 else 0。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Find if slope is good with only two intercept
bool isSlopeGood(double slope, int arr[], int n)
{
    set setOfLines;
    for (int i = 0; i < n; i++)
        setOfLines.insert(arr[i] - slope * (i));
 
    // if set of lines have only two distinct intercept
    return setOfLines.size() == 2;
}
 
// Function to check if required solution exist
bool checkForParallel(int arr[], int n)
{
    // check the result by processing
    // the slope by starting three points
    bool slope1 = isSlopeGood(arr[1] - arr[0], arr, n);
    bool slope2 = isSlopeGood(arr[2] - arr[1], arr, n);
    bool slope3 = isSlopeGood((arr[2] - arr[0]) / 2, arr, n);
 
    return (slope1 || slope2 || slope3);
}
 
// Driver code
int main()
{
    int arr[] = { 1, 6, 3, 8, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << (int)checkForParallel(arr, n);
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
 
class GfG
{
 
// Find if slope is good
// with only two intercept
static boolean isSlopeGood(double slope,
                        int arr[], int n)
{
    Set setOfLines = new HashSet ();
    for (int i = 0; i < n; i++)
        setOfLines.add(arr[i] - slope * (i));
 
    // if set of lines have only two distinct intercept
    return setOfLines.size() == 2;
}
 
// Function to check if required solution exist
static boolean checkForParallel(int arr[], int n)
{
    // check the result by processing
    // the slope by starting three points
    boolean slope1 = isSlopeGood(arr[1] - arr[0], arr, n);
    boolean slope2 = isSlopeGood(arr[2] - arr[1], arr, n);
    boolean slope3 = isSlopeGood((arr[2] - arr[0]) / 2, arr, n);
 
    return (slope1 == true || slope2 == true || slope3 == true);
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 6, 3, 8, 5 };
    int n = arr.length;
    if(checkForParallel(arr, n) == true)
    System.out.println("1");
    else
    System.out.println("0");
}
}
 
// This code is contributed by Prerna Saini.


Python3
# Python3 implementation of the
# above approach
 
# Find if slope is good with only
# two intercept
def isSlopeGood(slope, arr, n):
 
    setOfLines = dict()
    for i in range(n):
        setOfLines[arr[i] - slope * (i)] = 1
 
    # if set of lines have only
    # two distinct intercept
    return len(setOfLines) == 2
 
# Function to check if required solution exist
def checkForParallel(arr, n):
     
    # check the result by processing
    # the slope by starting three points
    slope1 = isSlopeGood(arr[1] - arr[0], arr, n)
    slope2 = isSlopeGood(arr[2] - arr[1], arr, n)
    slope3 = isSlopeGood((arr[2] - arr[0]) // 2, arr, n)
 
    return (slope1 or slope2 or slope3)
 
# Driver code
arr = [1, 6, 3, 8, 5 ]
n = len(arr)
if checkForParallel(arr, n):
    print(1)
else:
    print(0)
     
# This code is contributed by Mohit Kumar


C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
 
class GfG
{
 
// Find if slope is good
// with only two intercept
static bool isSlopeGood(double slope,
                        int []arr, int n)
{
 
    HashSet setOfLines = new HashSet ();
    for (int i = 0; i < n; i++)
        setOfLines.Add(arr[i] - slope * (i));
 
    // if set of lines have only two distinct intercept
    return setOfLines.Count == 2;
}
 
// Function to check if required solution exist
static bool checkForParallel(int []arr, int n)
{
    // check the result by processing
    // the slope by starting three points
    bool slope1 = isSlopeGood(arr[1] - arr[0], arr, n);
    bool slope2 = isSlopeGood(arr[2] - arr[1], arr, n);
    bool slope3 = isSlopeGood((arr[2] - arr[0]) / 2, arr, n);
 
    return (slope1 == true || slope2 == true || slope3 == true);
}
 
// Driver code
public static void Main()
{
    int []arr = { 1, 6, 3, 8, 5 };
    int n = arr.Length;
    if(checkForParallel(arr, n) == true)
        Console.WriteLine("1");
    else
        Console.WriteLine("0");
}
}
 
// This code is contributed by Ryuga.


PHP


Javascript


输出:
1

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程