亚洲精品不卡AV在线播放|国产性爱无码高清视频|国产成人 ,97人人色,国产免费一区二区三区,日本一区二区在线

內容中心

返回列表
2026年評價高的寧波水電氣自動供料設計/局部水電氣自動供料怎么選真實參考銷售廠家參考
2026-02-14 07:58:13

To solve this problem, we need to determine the maximum number of points that lie on the same straight line given an array of points. The solution involves calculating slopes between points to identify lines and handling edge cases like duplicate points, vertical lines, and floating-point precision issues.

Approach

  1. Edge Cases Handling: If there are fewer than 2 points, return the count of points directly.
  2. Slope Calculation: For each point, compute the slope it forms with every other point. To avoid floating-point precision issues, represent slopes as reduced fractions (numerator and denominator in simplest form with a consistent sign).
  3. Duplicate Points: Count duplicate points (same coordinates) as they contribute to all lines passing through the original point.
  4. Vertical Lines: Represent vertical lines with a special key ('inf') since their slope is undefined.
  5. Max Points on Line: For each point, track the maximum number of points on any line through it by counting slopes and adding duplicate points to this count.

Solution Code

import math
from collections import defaultdict

def max_points(points):
    if len(points) <= 1:
        return len(points)

    max_p = 0

    for i in range(len(points)):
        slope_dict = defaultdict(int)
        duplicates = 1
        current_max = 0
        x1, y1 = points[i]

        for j in range(len(points)):
            if i == j:
                continue
            x2, y2 = points[j]

            # Count duplicate points
            if x1 == x2 and y1 == y2:
                duplicates += 1
                continue

            dy = y2 - y1
            dx = x2 - x1

            # Handle vertical lines (undefined slope)
            if dx == 0:
                key = ('inf',)
            else:
                # Reduce dy/dx to simplest form
                gcd_val = math.gcd(abs(dy), abs(dx))
                reduced_dy = dy // gcd_val
                reduced_dx = dx // gcd_val

                # Ensure consistent sign for the denominator
                if reduced_dx < 0:
                    reduced_dy *= -1
                    reduced_dx *= -1

                key = (reduced_dy, reduced_dx)

            slope_dict[key] += 1
            current_max = max(current_max, slope_dict[key])

        # Total points on the line: duplicates + points with same slope
        total = duplicates + current_max
        max_p = max(max_p, total)

    return max_p

Explanation

  1. Slope Representation: Slopes are represented as tuples of reduced fractions to avoid precision errors. For example, the slope 2/4 becomes (1,2), and -1/2 becomes (-1,2) (consistent sign for denominator).
  2. Duplicate Points: Each duplicate point adds to all lines passing through the original point, so we count them separately and add to the slope count for each line.
  3. Vertical Lines: Handled with a special key ('inf') since their slope is undefined.
  4. Efficiency: The algorithm runs in O(n2) time complexity, where n is the number of points, as each point is compared to all other points once. This is optimal for this problem given the constraints.

This approach effectively handles all edge cases and ensures accurate results using integer arithmetic to avoid floating-point errors. It is robust and efficient for the given problem.



(免責聲明:本文為本網站出于傳播商業(yè)信息之目的進行轉載發(fā)布,不代表本網站的觀點及立場。本文所涉文、圖、音視頻等資料的一切權利和法律責任歸材料提供方所有和承擔。本網站對此資訊文字、圖片等所有信息的真實性不作任何保證或承諾,亦不構成任何購買、投資等建議,據此操作者風險自擔。) 本文為轉載內容,授權事宜請聯(lián)系原著作權人,如有侵權,請聯(lián)系本網進行刪除。

點擊呼叫(詳情介紹)
在線客服

在線留言
您好,很高興為您服務,可以留下您的電話或微信嗎?