Тёмный

Leetcode 149 - Maximum Points on a Line - Python 

NeetCodeIO
Подписаться 177 тыс.
Просмотров 19 тыс.
50% 1

Опубликовано:

 

10 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 76   
@gourabsingha1
@gourabsingha1 Год назад
Glad that you created this channel for uploading more solution videos. Some solutions which should be on youtube just aren't there. And some of those which are available are hard to understand. Thats why I always look out for your video solution first. Thanks for everything. I hope you're able to manage.
@NeetCodeIO
@NeetCodeIO Год назад
Hey everyone, welcome back and lets write some more neetcode today! :) Subscribe to this second channel for more!
@atreides4911
@atreides4911 Год назад
Why a second channel? Whats the purpose of the first channel if all new leetcode vids will be here? Just wondering, I love your content and I subbed to this one too
@bumbchick
@bumbchick Год назад
What is the purpose of this second channel?
@codeaperture
@codeaperture Год назад
@@bumbchick everyone wondering 🤭
@qwertyasdf6301
@qwertyasdf6301 Год назад
Saw this solution posted in the discussions by another user. Parallel lines have same slope which would make this fail. Surprised that this works. Probably works because a new dictionary is created every loop. 🤔
@orellavie6233
@orellavie6233 Год назад
Indeed. Need to make a line equation and save that on the cache (slope, intercept). Maybe the test cases are awful.
@NeetCodeIO
@NeetCodeIO Год назад
Yes, the slope is calculated using the exact same p1. So it's impossible for there to be a parallel line, because we reset the hashmap every loop.
@vishnuc2682
@vishnuc2682 Год назад
I believe there could also be issues wrt floating point precision if the slope value is high enough and the real difference in slopes between two different lines is relatively small enough. This could lead to false collisions where slightly different slopes get the same floating point value and we miscount the coincident points. For this reason it might be better to use a tuple (numerator: int, denominator: int) as hashmap key and make sure to reduce these as far as possible using gcd.
@orellavie6233
@orellavie6233 Год назад
@@vishnuc2682 another simple fix, use polar coordinates like in hough transform. Fix that problem completely
@Isaac-eh6uu
@Isaac-eh6uu Год назад
@@NeetCodeIO oh that makes sense it was driving me nuts 😭
@scresat
@scresat Год назад
Hey neetcode, I don't know if you've heard this before, but your voice is very soothing. You can be a great audiobook narrator.
@rakesha9176
@rakesha9176 19 дней назад
"Longest line that could be made with each point" - This simple idea you gave is pretty much enough to solve this problem
@FrankZChen
@FrankZChen Год назад
Great solution! I overcomplicated the problem by doing a defaultdict(set).
@Ashadow700
@Ashadow700 Год назад
Is this an accepted solution by Leetcode? What about the y-offset? Shouldn't that make this solution not work? The formula that describes a straight line is _y = kx + m_ , where _k_ is the slope and _m_ is the offset at x = 0. It is possible to have two different lines with the same slope, but different offset. For example, the line through the points [0, 0] and [1, 2] (y = 2k) will have the exact same slope as a line through [1, 0] and [1, 3] (y=2k+1), but there is still no way to draw a line through all 4 points, since they have different offsets. Am I missing something here? Or is this just a fairly poorly test by LeetCode?
@saikrishna-mn7nd
@saikrishna-mn7nd Год назад
You are right, both slope and y-intercept are required to uniquely determine a line. I think we have to keep something like "slope-yintercept" format in the hashmap to get it right in other platforms where there are stronger testcases.
@Ruin3.14
@Ruin3.14 Год назад
I've though about using a integral
@two697
@two697 Год назад
​@saikrishna-mn7nd all the points we're considering in one iteration pass through p1 so, if the gradient is the same, they will lie on the same line
@Manatoro
@Manatoro 5 месяцев назад
code is incomplete, here is a solution: var maxPoints = function(points) { const propIdxMap = new Map() for(let i = 0; i { const indiSet = new Set(n) max = Math.max(indiSet.size, max) } ) return max };
@CST1992
@CST1992 4 месяца назад
Like @two697 said, the loop with j considers p1 as a common point. So p1 point is common and slope is the same means the points lie on the same line that passes through p1. Also, don't forget that we are looping through all the points so we'll also get the case where the pair of p2 and p3 is considered, excluding p1. It's already established that p1 and p2 are on the same line, so p2 and p3 have to be as well if the slope is the same.
@YunChiehChiu
@YunChiehChiu Год назад
Hey Neetcode, welcome back!!! This is the daily challenge! So glad to see you again!
@mucle6
@mucle6 Год назад
To account for the slope, use the x intercept in the count map. The x intercept is where y = 0, so 0 = slope*x + b, or -slope*x = b. Then using the slope and intercept you'll get the actual answer
@woodylucas
@woodylucas Год назад
When we obtain slope (rise / run) as a key, it looks like we are storing a decimal/floating number. When we store a floating number as a hash table key wouldn't we run into a floating number precision? Aren't computers really bad at handling floating number arithmetics? We might have two slopes that round to the same number. Like two slopes that a very close in decimal will round to the same slope; that necessarily isn't the same line with the same points.
@uniquename2386
@uniquename2386 Год назад
Glad you're back! I actually was thinking about if you could solve it and you did it, thx.
@AbdulAziz-fg2cy
@AbdulAziz-fg2cy Год назад
if you are doing this on cpp don't forget float arithmetics
@Ruin3.14
@Ruin3.14 Год назад
Im from a math background before i began coding and this is my first hard i tried. This wasnt too bad! One issue, y =mx PLUS B. The slops can be the same and not be on the same line, they could be adjacent. Did LC mess that up?
@vamsikrishna6362
@vamsikrishna6362 Год назад
I am thinking the same I don't know how the code passed all the tests
@two697
@two697 Год назад
All of the lines were considering will pass through p1 so, if the gradient is the same, they will lie on the same line
@axay3.0
@axay3.0 Год назад
is this complete solution? yes i know it passes testcases sir but line has two parameter slope and constant as by high school maths, y=mx+c . so two lines with same slope is parallel but need not to be same straight line. we haven't considered c.
@addiegupta
@addiegupta 26 дней назад
we are forming pairs by keeping 1 point as fixed. so if we get same slope for 2 pairs, note that 1 point is the same in both pairs. notice on line 10 we are creating a new hash map every time for the fixed point. hence there will never be a case where slope is same but y intercept is not
@yiyunkim6062
@yiyunkim6062 Год назад
what if the points have same slope but different line like 2x = y and 2x + 1 = y ..?
@aakashpawan1542
@aakashpawan1542 Год назад
Hii Neetcode, Congrats for your new channel. I have an enhancement request for you. It would be great if we have a text box column near the code column of the problems page in the site. It will help us to write some hints or notes for that problem for future reference. Hope you read this comment.
@def__init
@def__init Год назад
Would save so much time vs adding a new row every time in my excel sheet. Just a notes text box is all we need!!
@yousif2493
@yousif2493 9 месяцев назад
Thank you for your great explanation, but you should update your solution because it will fail in the new test cases, as two different lines may have the same slope with two different y_intercept
@shinemithril
@shinemithril Год назад
Already subscribed, thanks for making great content bro
@atharvapatil4247
@atharvapatil4247 Год назад
Good luck with this channel, Already subscribed ; )
@krateskim4169
@krateskim4169 Год назад
Great explanation
@vaibhavgupta2130
@vaibhavgupta2130 Год назад
was waiting for your explanation
@mallikarjun413
@mallikarjun413 Год назад
Hi Neetcode, please upload sudoku autosolver problem
@shreshthkaushik-hu8bz
@shreshthkaushik-hu8bz Месяц назад
I don't know how your line 13 p2[0] == p1[0] worked out. Because if your p2[0] = -2 and your p1[0] = 2 then adding them produces 0 ( Error, can't divide by 0 ). This is what I did to fix that # Create a variable to store the longest line we could get longest_straight_line = 1 # Go through each pair in the list and find the slope for point_pair_1 in range(len(points)): # With the current point we are at, try to find the number of same slopes we can get same_slope_points = {} for point_pair_2 in range(point_pair_1 + 1, len(points)): y1 = points[point_pair_1][1] y2 = points[point_pair_2][1] x1 = points[point_pair_1][0] x2 = points[point_pair_2][0] # If adding the two x axis don't result in 0. That means they are on same "x" axis. (2,1)(3,1)(4,1) if (x2 - x1 != 0): slope = (y2 - y1) / (x2 - x1) # Add this slope in the same_slope_points same_slope_points[slope] = same_slope_points.get(slope, 0) + 1 # If on same x axis else: slope = "same" same_slope_points[slope] = same_slope_points.get(slope, 0) + 1 longest_straight_line = max(longest_straight_line, same_slope_points[slope] + 1) return longest_straight_line
@azharuddinkhan1865
@azharuddinkhan1865 Год назад
Subscribed
@AbhishekKhareOriginal
@AbhishekKhareOriginal Год назад
Thank god you are back ❤️ I am third 🥉
@nipunshah1373
@nipunshah1373 Год назад
After so many days !
@NeetCode
@NeetCode Год назад
First :)
@oxyht
@oxyht Год назад
You have good sense of humour. :D
@babalawo99
@babalawo99 Год назад
😂
@azgharkhan4498
@azgharkhan4498 Год назад
@neetcodeIO Parallel lines also have the same slope. no?
@shubhampatel7870
@shubhampatel7870 6 дней назад
Since we are fixing start point hence if the slope is the same then the line is the same...
@Android-17
@Android-17 Год назад
Yes!
@ernest987987
@ernest987987 11 месяцев назад
What s n? len(n) will throw an error no?
@jessanraj9086
@jessanraj9086 Год назад
Awesome awesome
@mennatullahabdelrahman3840
@mennatullahabdelrahman3840 Год назад
I can't understand how the count dictionary is created for every single element of the points array, that might be the max slope not in the beginning?
@tanujarawat6972
@tanujarawat6972 Год назад
what about the pararellel and horizontal line?
@thelastbit8154
@thelastbit8154 Год назад
This is today's 'LeetCode problem' 😈
@drac4195
@drac4195 Год назад
Amazing!! NeetCode is my inspiration he makes every hard problem easy
@gunahawk6893
@gunahawk6893 Год назад
import math from collections import defaultdict class Solution: def solve(self, A, B): n = len(A) output = 0 if n output: output = temp return output
@yuxiangwang963
@yuxiangwang963 Год назад
I don't think that is correct since slope can be a decimal number. How would you make a decimal number to be an index? (line 17)
@CS_n00b
@CS_n00b Год назад
I dont see why getting the slop for each pair of points is n^3, I thought it would be n^2
@rakshitmehta4959
@rakshitmehta4959 Год назад
Hello
@interestingspace2931
@interestingspace2931 Год назад
Nicce😇
@akalizaakaliza7049
@akalizaakaliza7049 Год назад
why do we need to increment count[slope]+1 line 18, why this is wrong : res = max(res, count[slope]), for me count has been already incremented at line 17
@NeetCodeIO
@NeetCodeIO Год назад
Its basically an off-by-one calculation.
@gunahawk6893
@gunahawk6893 Год назад
@@NeetCodeIO because to include current point
@harshitsingh2118
@harshitsingh2118 Год назад
Hi... Already subscribed to this one as well. Waiting for the same awesome content here.
@heatchecknyc2142
@heatchecknyc2142 Год назад
Can anyone explain why this works in python 3 but fails in python
@mrunankmistry7255
@mrunankmistry7255 Год назад
Probably because of the precision that is stored in the slope
@gunahawk6893
@gunahawk6893 Год назад
hi can someone in comments please tell why count[slope] + 1 ?
@neelpatel4330
@neelpatel4330 Год назад
because slope count all possible points(j: i+1 to n) from one point(i: 0 to n) so we also include that point
@maxkinli
@maxkinli Год назад
I study algorithms already 3 years.. and still can’t solve the medium problems
@valo-fun50
@valo-fun50 Год назад
Same
@midasredblade236
@midasredblade236 Год назад
prolly the most disliked problem in leetcode
@akankshasharma7498
@akankshasharma7498 Год назад
Its wasn't really a hard problem
@moulee007
@moulee007 Год назад
I find you everywhere..discord channels yt etc
@akankshasharma7498
@akankshasharma7498 Год назад
@@moulee007 not on twitter? 🤣🤣🤣🤣
@pkmkb
@pkmkb Год назад
second:)
@Arzuzaka
@Arzuzaka Год назад
subscribed
Далее
Number of Good Paths - Leetcode 2421 - Python
25:13
Просмотров 12 тыс.
Maximum Performance of a Team - Leetcode 1383 - Python
14:53
BeastMasters Hawk just had enough #ti13
00:30
Просмотров 203 тыс.
iPhone 16 - презентация Apple 2024
01:00
Просмотров 55 тыс.
Modern Python logging
21:32
Просмотров 183 тыс.
The Untold Story of VS Code
12:42
Просмотров 10 тыс.
LeetCode was HARD until I Learned these 15 Patterns
13:00
3 Types of Algorithms Every Programmer Needs to Know
13:12
This Algorithm is 1,606,240% FASTER
13:31
Просмотров 814 тыс.
Ugly Number II - Leetcode 264 - Python
15:17
Просмотров 13 тыс.
5 Useful F-String Tricks In Python
10:02
Просмотров 303 тыс.
BeastMasters Hawk just had enough #ti13
00:30
Просмотров 203 тыс.