Leedcode Max Points on a Line
来源:互联网 发布:中日网络战 编辑:程序博客网 时间:2024/06/07 19:13
1.Description
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
input parameter: vector<Point> &points
output parameter: int
2.Analysis
A. How to define a line on a 2D plane?
y = kx + b y -y0 = k (x - x0)
Note: These equations can't describe line without slope.
B. we are supplied with list of points.
with points how to get k and b?
A line is unique with two different points on its given.
when slope exists, k = (y1 - y0)/(x1 - x0)
when slope not exists, k = INT_MAX
So, we can scan every points, using equation y -y0 = k (x - x0) to represent one line.
use hash table map to store the line numbers setting k as key and points on line as value.
C. duplicate
Every line need add the duplicate num
3.Code
if (points.size() <= 2) { return points.size(); } std::map<double, int> lines; std::map<double, int>::iterator it; double k = 0; //for slope int i = 0; //for loop int j = 0; // for loop int max_points = 0; int size = points.size(); int x0 = 0; int y0 = 0; int x1 = 0; int y1 = 0; int duplicate = 0; for (i = 0; i < size; ++i) { x0 = points[i].x; y0 = points[i].y; //set variable default lines.clear(); duplicate = 0; for (j = 0; j < size; ++j) { x1 = points[j].x; y1 = points[j].y; // duplicate if (x1 == x0 && y1 == y0) { duplicate++; } else { // test whether the k exists or not k = x1 == x0 ? INT_MAX: (1.0*(y1 - y0))/(x1 - x0); // line[k] set default 0 lines[k]++; } } // aimed at reduce the computation in compare procedure max_points -= duplicate; for (it = lines.begin(); it != lines.end(); ++it) { max_points = max_points >= (it->second) ? max_points:(it->second); } max_points += duplicate; //if the lines is empty very important!!! if (lines.empty()) { max_points = duplicate; } } return max_points;
4.Enhancement
We need a*n^2 computation using the above algorithm. The computation procedure is shown in below table.
p1p2p3p4p1D k12 k13 k14p2k12Dk23k24p3k13k23Dk34p4k14k24k34DIt is obviously that there exists some duplicated computation.
e.g. the max_point line has point p2 , p3 ,p4 with slope k
then k23 = k24 = k34
we can remove the duplicate computation, then the computation will be reduced more then half.
duplicate = 1; for (j = i - 1; j >= 0; --j) { x1 = points[j].x; y1 = points[j].y; // duplicate if (x1 == x0 && y1 == y0) { duplicate++; } else { // test whether the k exists or not k = x1 == x0 ? INT_MAX: (1.0*(y1 - y0))/(x1 - x0); // line[k] set default 0 lines[k]++; } }
NOTE:
1. For one problem, analyse is very important?
how to define
extreme situation
--> test scenarios
2.coding
A proper framework and data struct can benefits lot.
- Leedcode Max Points on a Line
- LeedCode:Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points On a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- max points on a line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Struts2+Hibernate+Spring框架搭建(三)
- 自定义listview 实现仿qq聊天列表左滑删除效果
- PROC---动态SQL
- 使用StoryBoard的时候加入用户引导页面
- Linux信号列表
- Leedcode Max Points on a Line
- 底层设置
- C++中的虚函数(virtual function)
- 精确表达浮点数 -- 编程之美
- JSP运行原理及运行过程
- 图片的切换动画 CATransition
- 系统分析师/软件架构师(26/3397)
- apache openoffice安装手册
- 可以从CSS框架中借鉴到什么