LeetCode Problem | LeetCode Problems For Beginners | LeetCode Problems & Solutions | Improve Problem Solving Skills | LeetCode Problems Java | LeetCode Solutions in C++
Hello Programmers/Coders, Today we are going to share solutions to the Programming problems of LeetCode Solutions in C++, Java, & Python. At Each Problem with Successful submission with all Test Cases Passed, you will get a score or marks and LeetCode Coins. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for the Max Points on a Line in C++, Java & Python-LeetCode problem. We are providing the correct and tested solutions to coding problems present on LeetCode. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
About LeetCode
LeetCode is one of the most well-known online judge platforms to help you enhance your skills, expand your knowledge and prepare for technical interviews.
LeetCode is for software engineers who are looking to practice technical questions and advance their skills. Mastering the questions in each level on LeetCode is a good way to prepare for technical interviews and keep your skills sharp. They also have a repository of solutions with the reasoning behind each step.
LeetCode has over 1,900 questions for you to practice, covering many different programming concepts. Every coding problem has a classification of either Easy, Medium, or Hard.
LeetCode problems focus on algorithms and data structures. Here is some topic you can find problems on LeetCode:
- Mathematics/Basic Logical Based Questions
- Arrays
- Strings
- Hash Table
- Dynamic Programming
- Stack & Queue
- Trees & Graphs
- Greedy Algorithms
- Breadth-First Search
- Depth-First Search
- Sorting & Searching
- BST (Binary Search Tree)
- Database
- Linked List
- Recursion, etc.
Leetcode has a huge number of test cases and questions from interviews too like Google, Amazon, Microsoft, Facebook, Adobe, Oracle, Linkedin, Goldman Sachs, etc. LeetCode helps you in getting a job in Top MNCs. To crack FAANG Companies, LeetCode problems can help you in building your logic.
Link for the Problem – Max Points on a Line– LeetCode Problem
Max Points on a Line– LeetCode Problem
Problem:
Given an array of points
where points[i] = [xi, yi]
represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.
Example 1:
![Max Points on a Line LeetCode Programming Solutions | LeetCode Problem Solutions in C++, Java, & Python [💯Correct] 2 plane1](https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg)
Input: points = [[1,1],[2,2],[3,3]] Output: 3
Example 2:
![Max Points on a Line LeetCode Programming Solutions | LeetCode Problem Solutions in C++, Java, & Python [💯Correct] 3 plane2](https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg)
Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] Output: 4
Constraints:
1 <= points.length <= 300
points[i].length == 2
-104 <= xi, yi <= 104
- All the
points
are unique.
Max Points on a Line– LeetCode Solutions
Max Points on a Line Solution in C++:
class Solution { public: int maxPoints(vector<vector<int>>& points) { int ans = 0; for (int i = 0; i < points.size(); ++i) { unordered_map<pair<int, int>, int, pairHash> slopeCount; const vector<int> p1{points[i]}; int samePoints = 1; int maxPoints = 0; // maximum number of points with the same slope for (int j = i + 1; j < points.size(); ++j) { const vector<int> p2{points[j]}; if (p1 == p2) ++samePoints; else maxPoints = max(maxPoints, ++slopeCount[getSlope(p1, p2)]); } ans = max(ans, samePoints + maxPoints); } return ans; } private: pair<int, int> getSlope(const vector<int>& p1, const vector<int>& p2) { const int dx = p2[0] - p1[0]; const int dy = p2[1] - p1[1]; if (dx == 0) return {0, p1[0]}; if (dy == 0) return {p1[1], 0}; const int d = __gcd(dx, dy); return {dx / d, dy / d}; } struct pairHash { size_t operator()(const pair<int, int>& p) const { return p.first ^ p.second; } }; };
Max Points on a Line Solution in Java:
class Solution { public int maxPoints(int[][] points) { int ans = 0; for (int i = 0; i < points.length; ++i) { Map<Pair<Integer, Integer>, Integer> slopeCount = new HashMap<>(); int[] p1 = points[i]; int samePoints = 1; int maxPoints = 0; // maximum number of points with the same slope for (int j = i + 1; j < points.length; ++j) { int[] p2 = points[j]; if (p1[0] == p2[0] && p1[1] == p2[1]) ++samePoints; else { Pair<Integer, Integer> slope = getSlope(p1, p2); slopeCount.merge(slope, 1, Integer::sum); maxPoints = Math.max(maxPoints, slopeCount.get(slope)); } } ans = Math.max(ans, samePoints + maxPoints); } return ans; } private Pair<Integer, Integer> getSlope(int[] p1, int[] p2) { final int dx = p2[0] - p1[0]; final int dy = p2[1] - p1[1]; if (dx == 0) return new Pair<>(0, p1[0]); if (dy == 0) return new Pair<>(p1[1], 0); final int d = gcd(dx, dy); return new Pair<>(dx / d, dy / d); } private int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } }
Max Points on a Line Solution in Python:
class Solution: def maxPoints(self, points: List[List[int]]) -> int: ans = 0 def gcd(a: int, b: int) -> int: return a if b == 0 else gcd(b, a % b) def getSlope(p1: List[int], p2: List[int]) -> Tuple[int, int]: dx = p2[0] - p1[0] dy = p2[1] - p1[1] if dx == 0: return (0, p1[0]) if dy == 0: return (p1[1], 0) d = gcd(dx, dy) return (dx // d, dy // d) for i, p1 in enumerate(points): slopeCount = defaultdict(int) samePoints = 1 maxPoints = 0 for j in range(i + 1, len(points)): p2 = points[j] if p1 == p2: samePoints += 1 else: slope = getSlope(p1, p2) slopeCount[slope] += 1 maxPoints = max(maxPoints, slopeCount[slope]) ans = max(ans, samePoints + maxPoints) return ans