LeetCode 149 Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

在一个平面上有n个点,求一条直线最多能够经过多少个这些点。

Runtime: 10 ms  beats 99.93% of java submissions

	public int maxPoints(Point[] points) {
		int n = points.length;
		if (n < 2) return n;
		int currentL, maxL = 2, x, y, dx, dy, overlap = 0, upperB = n;
		for (int i = 0; i < upperB; i++) {
			for (int j = i + 1; j < n; j++) {
				currentL = 1;
				x = points[i].y - points[j].y;
				y = points[j].x - points[i].x;
				if (x == 0 && y == 0) overlap++;
				else {
					currentL++;
					for (int k = j + 1; k < n && currentL + n - k > maxL; k++) {
						dx = points[k].x - points[i].x;
						dy = points[k].y - points[i].y;
						if (x * dx + y * dy == 0)
							currentL++;
					}
				}
				maxL = Math.max(currentL + overlap, maxL);
			}
			upperB = n - maxL;
			overlap = 0;
		}
		return maxL;
	}
https://discuss.leetcode.com/topic/3991/my-java-accepted-solution-for-your-reference-only-using-array

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值