post_href
stringlengths
57
213
python_solutions
stringlengths
71
22.3k
slug
stringlengths
3
77
post_title
stringlengths
1
100
user
stringlengths
3
29
upvotes
int64
-20
1.2k
views
int64
0
60.9k
problem_title
stringlengths
3
77
number
int64
1
2.48k
acceptance
float64
0.14
0.91
difficulty
stringclasses
3 values
__index_level_0__
int64
0
34k
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2028388/Python-Clean-and-Simple!
class Solution: def nearestValidPoint(self, x1, y1, points): minIdx, minDist = -1, inf for i,point in enumerate(points): x2, y2 = point if x1 == x2 or y1 == y2: dist = abs(x1-x2) + abs(y1-y2) if dist < minDist: minIdx = i minDist = min(dist,minDist) return minIdx
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python - Clean and Simple!
domthedeveloper
5
458
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,500
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1990947/Python-Pretty-Brute-Forceish-but-easy-to-understand
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: valid_pair = [] #Initializing an empty array distance_holder = (9999,9999) #creating default values for distance holder def __calculate_manhattan(x,y,x_point,y_point): #helper function to calculate manhattan distance return abs(x-x_point) + abs(y-y_point) for idx,pair in enumerate(points): #iterate through the points and keep index x_point,y_point = pair #unpack pairs into x/y points if x==x_point or y==y_point: #checking if x or y equal points distance = __calculate_manhattan(x,y,x_point,y_point) #get manhattan distance if distance <= distance_holder[1]: #check if distance is less than what's currently in holder if distance == distance_holder[1]: #check if distances are equal to each other distance_holder = (min(distance_holder[0],idx),distance) #if distances are equal only use minimum index else: distance_holder = (idx,distance) #update distance holder valid_pair.append(distance_holder) #this was a remnant of brainstorming ways to solve problem , would need to refactor logic to remove this if not valid_pair: #checks if any elements are in valid pair return -1 else: return distance_holder[0] #returns index ```
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python - Pretty Brute Forceish but easy to understand
djohnson1612
2
74
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,501
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1851667/Python3-memory-usage-less-than-94.85
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: ind = -1 man = -1 for i in points: if i[0] == x or i[1] == y: if man == -1 or (abs(i[0] - x) + abs(i[1] - y)) < man: man = abs(i[0] - x) + abs(i[1] - y) ind = points.index(i) return ind
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python3 memory usage less than 94.85%
alishak1999
2
172
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,502
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2518846/Python-for-beginners-2-solutions
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: #Runtime:1167ms dic={} iterate=0 for i in points: if(i[0]==x or i[1]==y): dic[iterate]=dic.get(iterate,0)+(abs(i[0]-x)+abs(i[1]-y)) iterate+=1 #print(dic) if(len(dic)==0): return -1 for k,v in sorted(dic.items(), key=lambda x:x[1]): return k
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python for beginners 2 solutions
mehtay037
1
117
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,503
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2518846/Python-for-beginners-2-solutions
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: #Runtime:764ms mindist=math.inf ans=-1 for i in range(len(points)): if points[i][0]==x or points[i][1]==y: mandist=abs(points[i][0]-x)+abs(points[i][1]-y) if(mandist<mindist): ans=i mindist=mandist return ans
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python for beginners 2 solutions
mehtay037
1
117
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,504
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2490542/Python-For-loop-%2B-Dictionary-solution-explained.
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: i = 0 valid_dict = {} for ele in points: if ele[0] == x or ele[1] == y: valid_dict[i] = abs(x - ele[0]) + abs(y - ele[1]) i = i + 1 if valid_dict: return min(valid_dict, key=valid_dict.get) else: return -1
find-nearest-point-that-has-the-same-x-or-y-coordinate
[Python] For loop + Dictionary solution, explained.
duynl
1
41
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,505
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2172911/Python-Simple-Python-Solution-Using-Manhattan-Distance-Formula
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: minmum_index = 1000000000 minmum_distance = 100000000 for i in range(len(points)): ai , bi = points[i] if ai == x or bi == y: Manhattan_Distance = abs(ai - x) + abs(bi - y) if Manhattan_Distance < minmum_distance: minmum_distance = Manhattan_Distance minmum_index = i if minmum_index != 1000000000: return minmum_index return -1
find-nearest-point-that-has-the-same-x-or-y-coordinate
[ Python ] ✅✅ Simple Python Solution Using Manhattan Distance Formula 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
1
134
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,506
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2047748/python3-List-Comprehension
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: valid_point = [ (i, abs(x - point[0]) + abs(y - point[1])) for i, point in enumerate(points) if any((x == point[0], y == point[1])) ] if not valid_point: return -1 elif len(valid_point) > 1: return sorted(valid_point, key=lambda x: (x[1], x[0]))[0][0] else: return valid_point[0][0]
find-nearest-point-that-has-the-same-x-or-y-coordinate
python3 List Comprehension
kedeman
1
71
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,507
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1521069/Clean-and-Fast-(99.48)-using-Dict
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: validDists = {} for i, point in enumerate(points): if point[0] == x or point[1] == y: validDists[i] = abs(point[0] - x) + abs(point[1] - y) return min(validDists, key=validDists.get, default=-1)
find-nearest-point-that-has-the-same-x-or-y-coordinate
Clean and Fast (99.48) using Dict
Sima24
1
280
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,508
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2847164/python-solution-easy-to-understand-O(n)-O(1)
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: minn=float('inf') index=-1 for i , v in enumerate(points): if v[0]==x or v[1]==y: man_dis=abs(x - v[0]) + abs(y - v[1]) if(man_dis<minn) : minn=man_dis index=i return index
find-nearest-point-that-has-the-same-x-or-y-coordinate
python solution easy to understand O(n) , O(1)
sintin1310
0
1
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,509
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2845522/Python-719-ms-beat-97.38
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: coord = [898989] * len(points) for i in range(len(points)): flag = False if points[i][0] == x or points[i][1] == y: coord[i] = (abs(x - points[i][0]) + abs(y - points[i][1])) if min(coord) != 898989: return coord.index(min(coord)) return -1
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python 719 ms beat 97.38%
Ki4EH
0
1
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,510
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2828349/Simple-Python-or-One-Loop-or-Hash-map-or-beats-92
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: distances = {} for idx, point in enumerate(points): if point[0] == x or point[1] == y: distances[idx] = abs(x-point[0]) + abs(y-point[1]) if distances: return(min(distances,key=distances.get)) return(-1)
find-nearest-point-that-has-the-same-x-or-y-coordinate
Simple Python | One Loop | Hash map | beats 92%
briancottle
0
4
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,511
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2824112/Simple-python-solution
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: min_dist = float(inf) dic = defaultdict(list) for i in range(len(points)): if x == points[i][0] or y == points[i][1]: dist = abs(x-points[i][0])+abs(y-points[i][1]) dic[dist].append(i) min_dist = min(min_dist,dist) return dic[min_dist][0] if min_dist != float(inf) else -1
find-nearest-point-that-has-the-same-x-or-y-coordinate
Simple python solution
aruj900
0
4
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,512
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2791142/Python3-1-line-shenanigans
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: return min([(i, abs(u-x)+abs(v-y)) for i, (u,v) in enumerate(points) if u == x or v == y], key = lambda i:i[1], default = [-1])[0]
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python3 1-line shenanigans
TJessop
0
9
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,513
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2775092/explained-or-python3
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: # list of valid points valids = [] # closest point (default is what question considers biggest number which can be # assigned for x or y) and 0 is just a random number so we can have a tuple with 2 items valid = (10000, 0) le = len(points) for i in range(le): if points[i][0] == x: # append to valids diffrenece between none equal points and index valids.append((abs(y - points[i][1]), i)) elif points[i][1] == y: valids.append((abs(x - points[i][0]), i)) # if no valids then no valid point if not valids: return -1 # assign tuple containing the nearest point to our 'valid' variable for i in valids: if i[0] < valid[0]: valid = i # return secound item of the valid tuple which is index of nearest point return valid[1]
find-nearest-point-that-has-the-same-x-or-y-coordinate
explained | python3
Pedram-Naghib
0
7
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,514
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2748158/Python3-Easy-Solution-O(n)-time
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: min_dist = inf # keep two vars with the min distance and min_index = -1 #the index of the min for i , (x1, y1) in enumerate(points): # find valid distance if x1 == x or y1 == y: manhat_dist = abs(x1 - x) + abs(y1 - y) # only upadte min_dist if greater than this way we dont # have to worry about a same value at a later iteration if min_dist > manhat_dist: min_index = i min_dist = min(manhat_dist, min_dist) return min_index
find-nearest-point-that-has-the-same-x-or-y-coordinate
[Python3] Easy Solution O(n) time
avgpersonlargetoes
0
16
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,515
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2697586/Python-O(n)-solution-or-Less-memory-than-97.37
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: dist = math.inf index = -1 for i in range(len(points)): if points[i][0] == x or points[i][1] == y: manhattanDistance = abs(points[i][0] - x) + abs(points[i][1] - y) if dist > manhattanDistance: dist = manhattanDistance index = i return index
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python O(n) solution | Less memory than 97.37%
anandanshul001
0
6
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,516
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2682576/My-Python3-Solutions-or-Easy
class Solution: def nearestValidPoint(self, x1: int, y1: int, points: List[List[int]]) -> int: d = float(inf) # Smallest manhattan distance p = -1 # position, default -1 in case no point is valid. for i, j in enumerate(points): x2, y2 = j[0], j[1] if x1 == x2 or y1 == y2: # if valid md = abs(x1 - x2) + abs(y1 - y2) if md < d: d = md p = i return p
find-nearest-point-that-has-the-same-x-or-y-coordinate
My Python3 Solutions | Easy
AnzheYuan1217
0
20
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,517
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2682576/My-Python3-Solutions-or-Easy
class Solution: def nearestValidPoint(self, x1: int, y1: int, points: List[List[int]]) -> int: valid = {} for i, j in enumerate(points): x2, y2 = j[0], j[1] if x1 == x2 or y1 == y2: # if valid md = abs(x1 - x2) + abs(y1 - y2) valid[i] = md if len(valid) == 0: return -1 else: return min(valid, key=valid.get)
find-nearest-point-that-has-the-same-x-or-y-coordinate
My Python3 Solutions | Easy
AnzheYuan1217
0
20
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,518
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2681620/Easy-Python-O(n)-solution.
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: temp=float(inf) ans=-1 result=float(inf) for index,i in enumerate(points): if x==i[0] or y==i[1]: temp=min(temp,(abs(i[0]-x)+abs(i[1]-y))) if temp<result: result=temp ans=index return ans
find-nearest-point-that-has-the-same-x-or-y-coordinate
Easy Python O(n) solution.
guneet100
0
28
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,519
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2664346/Python-or-Simple-O(n)-solution
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: ans, dist = -1, 1e18 for i in range(len(points)): x1, y1 = points[i] if x == x1 or y == y1: d = abs(x - x1) + abs(y - y1) if d < dist: ans = i dist = d return ans
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python | Simple O(n) solution
LordVader1
0
25
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,520
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2633814/Python-Solution
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: mini=1e9 ans=-1 for i in range(len(points)): if points[i][0]==x or points[i][1]==y: dist=abs(points[i][0]-x) + abs(points[i][1]-y) if mini>dist: mini=dist ans=i return ans
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python Solution
Siddharth_singh
0
31
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,521
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2519796/Simple-solution-or-PYTHON-or-Faster-than-94.63-or-O(n)
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: s=999999999999 ans=0 for i,p in enumerate(points): a=p[0] b=p[1] if x == a or y == b: d=abs(x-a)+abs(y-b) if d<s: s=d ans=i if s==999999999999: return -1 return ans
find-nearest-point-that-has-the-same-x-or-y-coordinate
Simple solution | PYTHON | Faster than 94.63% | O(n)
manav023
0
63
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,522
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2413193/Python-Faster-than-99
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: results = [] for i, coord in enumerate(points): if (x == coord[0]) or (y == coord[1]): dist = abs(x - coord[0]) + abs(y - coord[1]) results.append((i, dist)) if results: return min(results, key=lambda x: x[1])[0] return -1
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python - Faster than 99%
user4322cp
0
76
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,523
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2299307/Simpleor-Python-or-Math-Problem
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: valid = [] # to store valid points min_manhattan_d = 0 # to store minimum manhattan distance min_ = [] # to store the co-ordinates of the minimum distance found for i in points: if(i[0] == x or i[1] == y): #check the valid points valid.append(i) #print(valid) if(len(valid) == 0): #if no valid points, return -1 return -1 min_manhattan_d = abs(x - valid[0][0]) + abs(y - valid[0][1]) #initialize the manhattan vdistance and corresponding coordinates with 1st valid point min_.append([valid[0][0], valid[0][1]]) for i,j in valid[1:]: #iterate through remaining points to check if the manhattan distance is minimum than the previous one, if yes then replace it manhattan_d = abs(x - i) + abs(y - j) if manhattan_d < min_manhattan_d: min_manhattan_d = manhattan_d min_[0] = [i,j] ans = points.index(min_[0]) #get the index of that coordinate which is the nearest point return ans
find-nearest-point-that-has-the-same-x-or-y-coordinate
Simple| Python | Math Problem
AngelaInfantaJerome
0
80
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,524
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2092201/python3-or-easy-understanding
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: ans = -1 small = float('inf') for i in points[::-1]: if x == i[0] or y == i[1]: curr = abs(x-i[0]) + abs(y-i[1]) if curr <= small: small = curr ans = points.index(i) return ans
find-nearest-point-that-has-the-same-x-or-y-coordinate
python3 | easy-understanding
An_222
0
120
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,525
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2081190/Python-Two-liner-(using-list-comprehension-and-sorted)-faster-than-70
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: lookup = sorted([[points[i][0],points[i][1],abs(points[i][0]-x)+abs(points[i][1]-y)] for i in range(len(points)) if points[i][0]==x or points[i][1]==y],key=lambda x:x[2]) return points.index(lookup[0][0:2]) if lookup != [] else -1
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python Two-liner (using list comprehension and sorted) faster than 70%
Lexcapital
0
64
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,526
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/2074492/faster-than-68.73-of-Python3-online-submissions
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: # Handle Edge Caces if all( (point[0]!=x and point[1]!=y) for point in points): return -1 distance = float('inf') index = -1 for point in points: if point[0]==x: if abs(point[1]-y) < distance: distance = abs(point[1]-y) index = points.index(point) if point[1]==y: if abs(point[0]-x) < distance: distance = abs(point[0]-x) index = points.index(point) return index
find-nearest-point-that-has-the-same-x-or-y-coordinate
faster than 68.73% of Python3 online submissions
pe-mn
0
47
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,527
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1997492/Python-easy-solution
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: final = [] valid = [] for i in range(0, len(points)): curr = points[i] if(curr[0] == x or curr[1] == y): valid.append(curr) dist = abs(x - curr[0]) + abs(y - curr[1]) final.append(dist) if(len(valid) == 0): return -1 a = points.index(valid[final.index(min(final))]) return a
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python easy solution
shivbutale22
0
74
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,528
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1978162/Clean-Python-solution-with-helper-functions-oror-Time%3A-O(N)-oror-Space%3A-O(1)
class Solution: # Time: O(N) # Space: O(1) def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: def manhattan_distance(x1: int, y1: int, x2: int, y2: int) -> int: return abs(x1 - x2) + abs(y1 - y2) def is_valid_point(x1: int, y1: int, x2: int, y2: int) -> bool: return (x1 == x2) or (y1 == y2) smallest_idx = -1 smallest_distance = float("inf") for idx, point in enumerate(points): if is_valid_point(x, y, point[0], point[1]): distance = manhattan_distance(x, y, point[0], point[1]) if smallest_distance > distance: smallest_distance = distance smallest_idx = idx return smallest_idx
find-nearest-point-that-has-the-same-x-or-y-coordinate
Clean Python solution with helper functions || Time: O(N) || Space: O(1)
user3694B
0
81
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,529
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1969326/Python-or-999-FASTER
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: cur_dist = 10e4 ans = -1 for i in range(len(points)-1, -1, -1): if points[i][0] == x or points[i][1] == y: dist = abs(x-points[i][0]) + abs(y-points[i][1]) if dist <= cur_dist: cur_dist = dist ans = i return ans
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python | 99,9 % FASTER
Mikhail_Kuzmenkov
0
85
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,530
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1921702/Python3-Easy-Array-and-Heap-Solutions
Array Solution class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: minDist = math.inf ans = -1 for i in range(len(points)): if points[i][0]==x or points[i][1]==y: manDist = abs(points[i][0]-x)+abs(points[i][1]-y) if manDist<minDist: ans = i minDist = manDist return ans Heap Solution class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: distances = [] heapq.heapify(distances) i = 0 for x1,y1 in points: if x1 == x or y1 == y: man_distance = abs(x1-x) + abs(y1-y) heapq.heappush(distances, [man_distance, i] ) i+=1 if len(distances) > 0: answer = heapq.heappop(distances) return answer[1] return -1
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python3 Easy Array and Heap Solutions
emerald19
0
46
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,531
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1906736/LeetCode-1779-Python-easy-to-Understand
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: mini = 100000 ind = -1 for i in range(len(points)): if x == points[i][0] or y == points[i][1]: # check for Valid Points d = abs(x- points[i][0]) + abs(y-points[i][1]) if d < mini: print(d, mini, ind ) mini = d ind = i if mini == 100000 : return -1 else: return ind
find-nearest-point-that-has-the-same-x-or-y-coordinate
LeetCode 1779 Python easy to Understand
Jatin-kaushik
0
63
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,532
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1858953/Python-dollarolution
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: dist = float('inf') index = -1 for i in range(len(points)): if x == points[i][0] or y == points[i][1]: q = abs(points[i][0] - x) + abs(y - points[i][1]) if q < dist: dist = q index = i return index
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python $olution
AakRay
0
150
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,533
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1828020/4-Lines-Python-Solution-oror-75-Faster-oror-Memory-less-than-25
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: V=[point for point in points if point[0]==x or point[1]==y] ; ans=[] if not V: return -1 for i in range(len(V)): ans.append((abs(V[i][0]-x)+abs(V[i][1]-y), points.index(V[i]))) return sorted(ans, key=lambda x:(x[0],x[1]))[0][1]
find-nearest-point-that-has-the-same-x-or-y-coordinate
4-Lines Python Solution || 75% Faster || Memory less than 25%
Taha-C
0
171
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,534
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1821584/Python-3-%3A-Easy-single-pass-solution
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: # Init result res = [float("inf"), []] # Loop through points with single pass for idx, point in enumerate(points): if point[0] == x or point[1]==y: dist = abs(point[0]-x)+abs(point[1]-y) # Manhattan distance if dist < res[0]: # New distance for result res = [dist,[idx]] elif dist == res[0]: # Equivalent result distance res[1].append(idx) # Conditional return return res[1][0] if res[1] else -1
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python 3 : Easy single pass solution
groomaaron06
0
71
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,535
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1634685/O(n)-python-easy-to-understand
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: def valid(t): i, (x1, y1) = t return x1 == x or y1 == y def key(t): i, (x1, y1) = t d = abs(x - x1) + abs(y - y1) return d, i matches = list(filter(valid, enumerate(points))) if not matches: return -1 return next(i for i, p in sorted(matches, key=key))
find-nearest-point-that-has-the-same-x-or-y-coordinate
O(n) python, easy to understand
emwalker
0
148
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,536
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1274724/Easy-Python-Solution
class Solution: def nearestValidPoint(self, x: int, y: int, p: List[List[int]]) -> int: ans=dict() for i in range(len(p)): if p[i][0]==x or p[i][1]==y: md=abs(x - p[i][0]) + abs(y - p[i][1]) ans[i]=md if not ans: return -1 else: return min(ans, key=ans.get)
find-nearest-point-that-has-the-same-x-or-y-coordinate
Easy Python Solution
sakshigoel123
0
111
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,537
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1202952/Python3-simple-solution-beats-90-users
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: ans = -1 dist = 10**4 for n,i in enumerate(points): if i[0] == x: temp = abs(i[1]-y) if dist > temp: dist = temp ans = n elif i[1] == y: temp = abs(i[0]-x) if dist > temp: dist = temp ans = n return -1 if ans == -1 else ans
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python3 simple solution beats 90% users
EklavyaJoshi
0
78
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,538
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1185155/Python-3-99-fast-and-easy-to-read-solution
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: m=10000 for i in points: if i[0]==x or i[1]==y: if m>abs(x-i[0])+abs(y-i[1]): m=min(m,abs(x-i[0])+abs(y-i[1])) o=points.index(i) if m==10000: return -1 return o
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python 3 99% fast and easy to read solution
coderash1998
0
143
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,539
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1114668/Python-simple-for-loop-Time%3A-O(N)-Space%3A-O(1)
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: idx = -1 dist = math.inf for i, p in enumerate(points): if (x == p[0] or y == p[1]) and abs(x-p[0]) + abs(y-p[1]) < dist: idx = i dist = abs(x-p[0]) + abs(y-p[1]) return idx
find-nearest-point-that-has-the-same-x-or-y-coordinate
Python, simple for loop, Time: O(N), Space: O(1)
blue_sky5
0
94
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,540
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1108569/Simple-Python3-solution-using-list-Runtime%3A-712-ms-faster-than-100.00
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: mindist, minidx = 100000, -1 res = [] for i in range(len(points)): if x == points[i][0]: res.append([abs(y - points[i][1]), i]) elif y == points[i][1]: res.append([abs(x - points[i][0]), i]) if len(res) == 0: return minidx return min(res, key = lambda x: x[0])[1]
find-nearest-point-that-has-the-same-x-or-y-coordinate
Simple Python3 solution using list Runtime: 712 ms, faster than 100.00%
foxysachin
0
78
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,541
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/1103872/Simple-Readable-Python-(100-Runtime)
class Solution: def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int: def manhattan_dist(x1, y1, x2, y2): return abs(x1-x2) + abs(y1-y2) # It's always handy to initialize using float('inf') or float('-inf') if you want really # large or small numbers! :) min_dist = float('inf') min_idx = -1 for i in range(len(points)): # If coordinate is valid, then do checks on manhattan_dist if points[i][0] == x or points[i][1] == y: # If the valid coordinate is smaller, then update the index and min_dist dist = manhattan_dist(x, y, points[i][0], points[i][1]) if min_dist > dist: min_dist = dist min_idx = i return min_idx
find-nearest-point-that-has-the-same-x-or-y-coordinate
Simple Readable Python (100% Runtime)
Whizzing-Waffle
0
58
find nearest point that has the same x or y coordinate
1,779
0.673
Easy
25,542
https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/discuss/1341617/While-loop-99-speed
class Solution: def checkPowersOfThree(self, n: int) -> bool: while n: n, rem = divmod(n, 3) if rem == 2: return False return True
check-if-number-is-a-sum-of-powers-of-three
While loop, 99% speed
EvgenySH
3
245
check if number is a sum of powers of three
1,780
0.654
Medium
25,543
https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/discuss/1110542/Python-or-Easy-or-4-lines-or-100-runtime-or-100-space
class Solution: def checkPowersOfThree(self, n: int) -> bool: while(n>=1): if n%3==2: return False n=n//3 return True
check-if-number-is-a-sum-of-powers-of-three
Python | Easy | 4 lines | 100% runtime | 100% space
rajatrai1206
3
360
check if number is a sum of powers of three
1,780
0.654
Medium
25,544
https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/discuss/1098155/Python-3-DFS-solution-if-you-don't-know-about-%22n-3-!-2%22-intuition
class Solution: def checkPowersOfThree(self, n: int) -> bool: p = [3**i for i in range(16)] return self.dfs(0, 0, [], n, p[:(bisect.bisect_left(p, n)+1)]) def dfs(self, idx, cur, cc, n, p): for i in range(idx, len(p)): cur += p[i] cc.append(p[i]) if cur == n or (cur < n and self.dfs(i+1, cur, cc, n, p)): return True cur -= p[i] cc.pop() return False
check-if-number-is-a-sum-of-powers-of-three
[Python 3] DFS solution if you don't know about "n % 3 != 2" intuition
joysword
1
167
check if number is a sum of powers of three
1,780
0.654
Medium
25,545
https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/discuss/1096397/Python3-div-by-3
class Solution: def checkPowersOfThree(self, n: int) -> bool: while n: n, r = divmod(n, 3) if r == 2: return False return True
check-if-number-is-a-sum-of-powers-of-three
[Python3] div by 3
ye15
1
132
check if number is a sum of powers of three
1,780
0.654
Medium
25,546
https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/discuss/2522296/easy-python-solution
class Solution: def convertToTernary(self, n): if n == 0: return '0' nums = [] while n: n, r = divmod(n, 3) nums.append(str(r)) return ''.join(reversed(nums)) def checkPowersOfThree(self, n: int) -> bool: if n == 0 : return False else : if '2' in self.convertToTernary(n) : return False return True
check-if-number-is-a-sum-of-powers-of-three
easy python solution
sghorai
0
30
check if number is a sum of powers of three
1,780
0.654
Medium
25,547
https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/discuss/2472194/Python3Recursion-Easy-Solution-using-Dynamic-Programming.-Detailed-explanation-and-comments
class Solution: def checkPowersOfThree(self, n: int) -> bool: # # 1- We create a list consisting of all numbers which are powers of 3 # and are also less than n num=1 num_list=[] while num<=n: num_list.append(num) num=num*3 powers=num_list #2- Apply 0/1 knapsack here. To find the sum we have two choices. #Either to include the number or exclude it. #By substracting the number from the sum , we include it. Else we dont. #Hashmap to prevent repeated recursion calls. memo={} # Nested function to perform recursion def run(amt,arr,n): key=(amt,n) if key in memo: return memo[key] #If n<0 or amt is negative , we'll return false since we dont need such results. if n<0 or amt<0: return False # if sum is 0 then the sum is possible. if amt==0: return True #Return and save the results in the memo array memo[key]=run(amt-arr[n-1],arr,n-1) or run(amt,arr,n-1) return memo[key] return run(n,powers,len(powers))
check-if-number-is-a-sum-of-powers-of-three
[Python3][Recursion] Easy Solution using Dynamic Programming. Detailed explanation and comments
202001047
0
28
check if number is a sum of powers of three
1,780
0.654
Medium
25,548
https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/discuss/2300218/Python-Easy-Solution
class Solution: def checkPowersOfThree(self, n: int) -> bool: l = [1] while l[-1]<n: l.append(3**len(l)) for i in l[::-1]: if i <= n: n -= i return n==0
check-if-number-is-a-sum-of-powers-of-three
Python Easy Solution
htarab_b
0
58
check if number is a sum of powers of three
1,780
0.654
Medium
25,549
https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/discuss/1476847/python-oror-n-3-!-2
class Solution: def checkPowersOfThree(self, n: int) -> bool: while n > 0: while n %3 == 0: n = n //3 if n % 3 == 2: return False n = n-1 return True
check-if-number-is-a-sum-of-powers-of-three
python || n % 3 != 2
byuns9334
0
139
check if number is a sum of powers of three
1,780
0.654
Medium
25,550
https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/discuss/1340926/Python3-solution
class Solution: def checkPowersOfThree(self, n: int) -> bool: key = [] i = 0 while 3**i <= n: if 3**i == n: return True key.append(i) i += 1 m = len(key) for i in range(2**m): z = 0 x = bin(i)[2:].zfill(m) for j,k in enumerate(x): if k == '1': z += 3**key[j] if z == n: return True return False
check-if-number-is-a-sum-of-powers-of-three
Python3 solution
EklavyaJoshi
0
69
check if number is a sum of powers of three
1,780
0.654
Medium
25,551
https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/discuss/1164347/Python3-simple-solution-easy-to-understand
class Solution: def checkPowersOfThree(self, n: int) -> bool: if "2" in self.toTernary(n): # to check whether "2" in Ternary. If it is, return false return False else: return True def toTernary(self,n): # convert decimal to ternary s = "" while n > 0: s += str(n%3) n = n//3 return s
check-if-number-is-a-sum-of-powers-of-three
Python3 simple solution, easy to understand
zharfanf
0
108
check if number is a sum of powers of three
1,780
0.654
Medium
25,552
https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/discuss/1153646/Python3-Simple-Recursive-%2B-Iterative-Solution
class Solution: def checkPowersOfThree(self, n: int) -> bool: def helper(n, power): if n == 0: return True if power < 0 and n != 0: return False if n >= pow(3, power): return helper(n - pow(3, power), power - 1) elif n < pow(3, power): return helper(n, power - 1) return helper(n, 16)
check-if-number-is-a-sum-of-powers-of-three
Python3 Simple Recursive + Iterative Solution
victor72
0
183
check if number is a sum of powers of three
1,780
0.654
Medium
25,553
https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/discuss/1122651/Using-ternary-and-recursion-faster-than-100
class Solution: def checkPowersOfThree(self, n: int) -> bool: ternary = self.find_ternary(n) for i in ternary: if i == '2': return False return True def find_ternary(self, num): quotient = num / 3 remainder = num % 3 if quotient == 0: return "" else: return self.find_ternary(int(quotient)) + str(int(remainder))
check-if-number-is-a-sum-of-powers-of-three
Using ternary and recursion, faster than 100%
a-gon
0
120
check if number is a sum of powers of three
1,780
0.654
Medium
25,554
https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/discuss/1105542/Python3-Solution-100-faster-and-100-memory-efficient
class Solution: def checkPowersOfThree(self, n: int) -> bool: ''' Here we make a ternary representation of given n If n has '2' in it return False, else return True [See: hint] ''' ternary = '' #ternary string is the reverse representation of original ternary while n>0: ternary+=str(n%3) #ternary means base 3 n=n//3 if '2' in ternary: return False return True
check-if-number-is-a-sum-of-powers-of-three
Python3 Solution 100% faster and 100% memory efficient
bPapan
0
85
check if number is a sum of powers of three
1,780
0.654
Medium
25,555
https://leetcode.com/problems/sum-of-beauty-of-all-substrings/discuss/1096392/Python3-freq-table
class Solution: def beautySum(self, s: str) -> int: ans = 0 for i in range(len(s)): freq = [0]*26 for j in range(i, len(s)): freq[ord(s[j])-97] += 1 ans += max(freq) - min(x for x in freq if x) return ans
sum-of-beauty-of-all-substrings
[Python3] freq table
ye15
41
2,800
sum of beauty of all substrings
1,781
0.605
Medium
25,556
https://leetcode.com/problems/sum-of-beauty-of-all-substrings/discuss/1096325/go-brrr-oror-fast-boi
class Solution: def beautySum(self, s: str) -> int: c, n, ans = Counter(s), len(s), 0 for i in range(n-2): x=c.copy() for j in range(n-1,i+1,-1): ans+=max(x.values())-min(x.values()) if x[s[j]]==1: del x[s[j]] else: x[s[j]]-=1 if c[s[i]]==1: del c[s[i]] else: c[s[i]]-=1 return ans
sum-of-beauty-of-all-substrings
🐍🐍 go brrr || fast boi
kavishd29598
2
149
sum of beauty of all substrings
1,781
0.605
Medium
25,557
https://leetcode.com/problems/sum-of-beauty-of-all-substrings/discuss/2685643/Python-Easy-solution
class Solution: def beautySum(self, s: str) -> int: final=[] sums=0 for i in range(len(s)): temp="" for j in range(i,len(s)): temp+=s[j] final.append(temp) for i in final: values=(Counter(i)).values() sums+= max(values)-min(values) return(sums)
sum-of-beauty-of-all-substrings
Python Easy solution
envyTheClouds
0
24
sum of beauty of all substrings
1,781
0.605
Medium
25,558
https://leetcode.com/problems/sum-of-beauty-of-all-substrings/discuss/2674216/My-python-simple-approach
class Solution: def beautySum(self, s: str) -> int: res = 0 for i in range(len(s)): count = {} for j in range(i, len(s)): # new_s = s[i:j+1] # count = Counter(new_s) count[s[j]] = count.get(s[j], 0) + 1 if len(count.values()) > 1: max_ = max(count.values()) min_ = min(count.values()) res += max_-min_ return res
sum-of-beauty-of-all-substrings
My python simple approach
aryanchaurasia
0
10
sum of beauty of all substrings
1,781
0.605
Medium
25,559
https://leetcode.com/problems/sum-of-beauty-of-all-substrings/discuss/1488713/Python3-O(n2)-solution
class Solution: def beautySum(self, s: str) -> int: res = 0 for i in range(len(s)): d = [0] * 26 for j in range(i, len(s)): d[ord(s[j]) - 97] += 1 _max = max(d) _min = min([x for x in d if x != 0]) res += _max - _min return res
sum-of-beauty-of-all-substrings
[Python3] O(n^2) solution
maosipov11
0
191
sum of beauty of all substrings
1,781
0.605
Medium
25,560
https://leetcode.com/problems/sum-of-beauty-of-all-substrings/discuss/1446532/Python3-solution
class Solution: def beautySum(self, s: str) -> int: d = {} for i in range(len(s)): c = {} for j in range(i,len(s)): c[s[j]] = c.get(s[j],0) + 1 x = max(c.values()) y = min(c.values()) if x > 1 and len(c.values()) != 1: d[s[i:j+1]] = d.get(s[i:j+1],0) + x-y return sum(d.values())
sum-of-beauty-of-all-substrings
Python3 solution
EklavyaJoshi
0
111
sum of beauty of all substrings
1,781
0.605
Medium
25,561
https://leetcode.com/problems/sum-of-beauty-of-all-substrings/discuss/1341895/Defaultdict-for-frequencies-96-speed
class Solution: def beautySum(self, s: str) -> int: beauty = 0 len_s = len(s) for start in range(len_s - 1): chars_freq = defaultdict(int) chars_freq[s[start]] += 1 for end in range(start + 1, len_s): chars_freq[s[end]] += 1 beauty += max(chars_freq.values()) - min(chars_freq.values()) return beauty
sum-of-beauty-of-all-substrings
Defaultdict for frequencies, 96% speed
EvgenySH
0
188
sum of beauty of all substrings
1,781
0.605
Medium
25,562
https://leetcode.com/problems/sum-of-beauty-of-all-substrings/discuss/1096488/Python-100-Accepted-using-List-as-Frequent-Table
class Solution(object): def beautySum(self, s): """ :type s: str :rtype: int """ res = 0 h = [0 for _ in range(26)] for i in range(len(s)): h = [0 for _ in range(26)] for j in range(i, len(s)): h[ord(s[j]) - ord('a')] += 1 minv, maxv = 501, -1 for x in h: if x > 0: minv = min(minv, x) maxv = max(maxv, x) res += maxv - minv return res
sum-of-beauty-of-all-substrings
[Python] 100% Accepted using List as Frequent Table
Lancelot_
0
101
sum of beauty of all substrings
1,781
0.605
Medium
25,563
https://leetcode.com/problems/count-pairs-of-nodes/discuss/1096612/Python3-2-pointer
class Solution: def countPairs(self, n: int, edges: List[List[int]], queries: List[int]) -> List[int]: degree = [0]*n freq = defaultdict(int) for u, v in edges: degree[u-1] += 1 degree[v-1] += 1 freq[min(u-1, v-1), max(u-1, v-1)] += 1 vals = sorted(degree) ans = [] for query in queries: cnt = 0 lo, hi = 0, n-1 while lo < hi: if query < vals[lo] + vals[hi]: cnt += hi - lo # (lo, hi), (lo+1, hi), ..., (hi-1, hi) all valid hi -= 1 else: lo += 1 for u, v in freq: if degree[u] + degree[v] - freq[u, v] <= query < degree[u] + degree[v]: cnt -= 1 ans.append(cnt) return ans
count-pairs-of-nodes
[Python3] 2-pointer
ye15
14
352
count pairs of nodes
1,782
0.38
Hard
25,564
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1097225/Python-3-Check-for-%2201%22-(1-Liner)
class Solution: def checkOnesSegment(self, s: str) -> bool: return "01" not in s
check-if-binary-string-has-at-most-one-segment-of-ones
[Python 3] - Check for "01" (1 Liner)
mb557x
32
977
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,565
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1097225/Python-3-Check-for-%2201%22-(1-Liner)
class Solution: def checkOnesSegment(self, s: str) -> bool: s = s.strip("0") return ("0" not in s)
check-if-binary-string-has-at-most-one-segment-of-ones
[Python 3] - Check for "01" (1 Liner)
mb557x
32
977
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,566
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1138853/simple-solution
class Solution: def checkOnesSegment(self, s: str) -> bool: return '01' not in s
check-if-binary-string-has-at-most-one-segment-of-ones
simple solution
JulianaYo
3
173
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,567
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1447175/Python3-Faster-Than-94.73-Memory-Less-Than-88.35
class Solution: def checkOnesSegment(self, s: str) -> bool: f = False for i in s: if i == '1': if not f: continue else: return False else: f = True return True
check-if-binary-string-has-at-most-one-segment-of-ones
Python3 Faster Than 94.73%, Memory Less Than 88.35%
Hejita
2
75
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,568
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1097251/Python-1-liner
class Solution: def checkOnesSegment(self, s: str) -> bool: return '01' not in s
check-if-binary-string-has-at-most-one-segment-of-ones
Python 1-liner
lokeshsenthilkumar
2
129
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,569
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1097183/Python3-count-start
class Solution: def checkOnesSegment(self, s: str) -> bool: cnt = 0 for i, c in enumerate(s): if (i == 0 or s[i-1] == "0") and s[i] == "1": cnt += 1 if cnt > 1: return False return True
check-if-binary-string-has-at-most-one-segment-of-ones
[Python3] count start
ye15
2
107
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,570
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/2054455/Python-oneliner
class Solution: def checkOnesSegment(self, s: str) -> bool: return len([x for x in s.split('0') if x]) == 1
check-if-binary-string-has-at-most-one-segment-of-ones
Python oneliner
StikS32
1
68
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,571
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1637406/extremely-simple-python-one-line-solution
class Solution: def checkOnesSegment(self, s: str) -> bool: if '01' in s: return False return True
check-if-binary-string-has-at-most-one-segment-of-ones
extremely simple python one line solution
1579901970cg
1
60
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,572
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1493101/Loop-until-second-block-98-speed
class Solution: def checkOnesSegment(self, s: str) -> bool: ones = True for c in s: if not ones and c == "1": return False elif ones and c == "0": ones = False return True
check-if-binary-string-has-at-most-one-segment-of-ones
Loop until second block, 98% speed
EvgenySH
1
92
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,573
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1254519/Python3-simple-solution-using-single-for-loop
class Solution: def checkOnesSegment(self, s: str) -> bool: if len(s) == 1: return True c = 0 for i in s: if i == '0': c += 1 if c >= 1 and i == '1': return False return True
check-if-binary-string-has-at-most-one-segment-of-ones
Python3 simple solution using single for loop
EklavyaJoshi
1
65
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,574
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1128305/WEEB-DOES-PYTHON(FASTER-THAN-99.41)
class Solution: def checkOnesSegment(self, s: str) -> bool: count = allones = 0 length = len(s) # code is simple, just check for inflection points as it would be a different segment # however, in the case where no inflection points exists, utilize allones to return True for i in range(length-1): if s[i] == "1" and s[i+1] == "0": count+=1 elif s[i] == "0" and s[i+1] == "1": count+=1 if s[i] == "1": allones+=1 # let s=="1110" or s=="1111", it handles both cases as no new segments are made. # why add 1 to allones? Cuz the loop above was length-1 so it excluded the last digit. It doesn't matter whether its "0" or "1" for the last digit as there would still be one segment only: e.g. "1110" or "1111" if allones+1 == length: return True return count == 1 # one inflection point as the question requires only one segment
check-if-binary-string-has-at-most-one-segment-of-ones
WEEB DOES PYTHON(FASTER THAN 99.41%)
Skywalker5423
1
105
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,575
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/2834484/Python-1-line%3A-Optimal-and-Clean-with-explanation-2-ways%3A-O(n)-time-and-O(1)-space
class Solution: def checkOnesSegment(self, s: str) -> bool: return not (s.find("01") != -1 and s.find("10") != -1) def checkOnesSegment(self, s: str) -> bool: return "01" not in s
check-if-binary-string-has-at-most-one-segment-of-ones
Python 1 line: Optimal and Clean with explanation - 2 ways: O(n) time and O(1) space
topswe
0
3
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,576
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/2818119/Python3-Solution-oror-Two-Pointers
class Solution: def checkOnesSegment(self, s: str) -> bool: for i in range(0, len(s)): if s[i] == '1': first = i break for i in range(len(s)-1, -1, -1): if s[i] == '1': last = i break for i in range(first, last): if s[i] == '0': return False return True
check-if-binary-string-has-at-most-one-segment-of-ones
Python3 Solution || Two-Pointers
shashank_shashi
0
3
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,577
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/2781916/Python-orO(n)or-Loops-solution
class Solution: def checkOnesSegment(self, s: str) -> bool: if s=='1': return True c=0 for i in range(len(s)): if s[i]=='1' and c==0: c=1 if s[i]=='0' and c==1: c=2 if s[i]=='1' and c==2: c=3 if c==3: return False return True
check-if-binary-string-has-at-most-one-segment-of-ones
Python |O(n)| Loops solution
SnehaGanesh
0
3
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,578
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/2630528/Iterative
class Solution: def checkOnesSegment(self, s: str) -> bool: # one's form a contiguous segment if the first segment occurs, # then there can't be another # if you see a zero there can't be another one because the first # element is 0. So make sure there's no 1's after seeing # a 0 # time O(n) space O(1) n = len(s) if n < 2: return s == "1" seen = False for i in range(1, n): if s[i] == "0": seen = True if seen and s[i] == "1": return False return True
check-if-binary-string-has-at-most-one-segment-of-ones
Iterative
andrewnerdimo
0
3
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,579
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/2600403/Python-Groupby-One-liner-with-Explanation
class Solution: def checkOnesSegment(self, s: str) -> bool: return len(list(itertools.groupby(s))) < 3
check-if-binary-string-has-at-most-one-segment-of-ones
[Python] Groupby One-liner with Explanation
andy2167565
0
14
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,580
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/2443543/using-regex
class Solution: def checkOnesSegment(self, s: str) -> bool: return len(re.findall("1+", s)) == 1
check-if-binary-string-has-at-most-one-segment-of-ones
using regex
Potentis
0
7
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,581
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/2175533/Simplest-Approach-oror-Self-Explanatory-Solution
class Solution: def checkOnesSegment(self, s: str) -> bool: segment = [] length = len(s) ones = 0 for i in range(length): if s[i] == "1": ones += 1 else: if ones != 0: segment.append(ones) ones = 0 if s[-1] == "1": segment.append(ones) return len(segment) == 1
check-if-binary-string-has-at-most-one-segment-of-ones
Simplest Approach || Self Explanatory Solution
Vaibhav7860
0
35
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,582
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1946426/Easy-Python-Solution-with-a-Flag
class Solution: def checkOnesSegment(self, s: str) -> bool: # current segment(of 1's) number seg = 0 for c in s: if(c=='1'): if(seg==2): # we are in the second segment return False elif(seg==0): # the current segment is the first one seg = 1 else: # c == '0' if(seg==1): # we have already seen a segment of 1's seg = 2 # the next segment will be the second one return True
check-if-binary-string-has-at-most-one-segment-of-ones
Easy Python Solution with a Flag
back2square1
0
27
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,583
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1910566/Python-Solution
class Solution: def checkOnesSegment(self, s: str) -> bool: return (False, True)['01' not in s]
check-if-binary-string-has-at-most-one-segment-of-ones
Python Solution
hgalytoby
0
49
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,584
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1858966/Python-dollarolution
class Solution: def checkOnesSegment(self, s: str) -> bool: flag = 0 for i in s: if i =='1' and flag == 0: flag = 1 elif i =='0' and flag == 1: flag = 2 elif i =='1' and flag == 2: return False return True
check-if-binary-string-has-at-most-one-segment-of-ones
Python $olution
AakRay
0
31
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,585
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1702526/Python-fast
class Solution: def checkOnesSegment(self, s: str) -> bool: s_list, count = s.split('0'), 0 for i in s_list: if len(i) >= 1: count += 1 if count > 1: return False if count == 1: return True
check-if-binary-string-has-at-most-one-segment-of-ones
Python fast
SamWu93
0
84
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,586
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1634719/Fast-Python-solution-using-slices
class Solution: def checkOnesSegment(self, s: str) -> bool: r = [] for i, v in enumerate(s): if v == '0': continue if r and i-1 not in r: return False r[1:] = (i,) return True
check-if-binary-string-has-at-most-one-segment-of-ones
Fast Python solution using slices
emwalker
0
55
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,587
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1218169/Simple-solution-Python-3
class Solution: def checkOnesSegment(self, s: str) -> bool: count = -1 if len(s)==1:return True for i in s: if i=="1" and count>-1: return False elif i=="0": count+=1 return True
check-if-binary-string-has-at-most-one-segment-of-ones
Simple solution, Python 3
malav_mevada
0
100
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,588
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1164309/Python3-simple-solution
class Solution: def checkOnesSegment(self, s: str) -> bool: for i in range(len(s)): # to find first "0" in the sequence if s[i] == "0": found_0 = i break if i == len(s) - 1: # if "0" is not found, then return true return True else: i = found_0 still_0 = True while i < len(s) and still_0: # to make sure that sequence only has 1 segment of contiguous of ones if s[i] == "1": still_0 = False i += 1 return still_0
check-if-binary-string-has-at-most-one-segment-of-ones
Python3 simple solution
zharfanf
0
59
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,589
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1114684/Python-straightforward-O(N)-iteration
class Solution: def checkOnesSegment(self, s: str) -> bool: i = 0 while i < len(s) and s[i] == '1': i += 1 for i in range(i, len(s)): if s[i] == '1': return False return True
check-if-binary-string-has-at-most-one-segment-of-ones
Python, straightforward O(N) iteration
blue_sky5
0
58
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,590
https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/1098224/Super-Easy-Python3-beats-100.-Just-check-if-any-1-exists-after-a-0.
class Solution: def checkOnesSegment(self, s: str) -> bool: flag=0 for i in s: if i=='1' and flag==1: return False elif i=='0': flag=1 return True
check-if-binary-string-has-at-most-one-segment-of-ones
Super Easy Python3 beats 100%. Just check if any 1 exists after a 0.
svr300
0
41
check if binary string has at most one segment of ones
1,784
0.404
Easy
25,591
https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/discuss/1121048/Python-3-or-1-liner-or-Explanation
class Solution: def minElements(self, nums: List[int], limit: int, goal: int) -> int: return math.ceil(abs(goal - sum(nums)) / limit)
minimum-elements-to-add-to-form-a-given-sum
Python 3 | 1-liner | Explanation
idontknoooo
6
285
minimum elements to add to form a given sum
1,785
0.424
Medium
25,592
https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/discuss/1097196/Python3-1-line
class Solution: def minElements(self, nums: List[int], limit: int, goal: int) -> int: return ceil(abs(goal - sum(nums))/limit)
minimum-elements-to-add-to-form-a-given-sum
[Python3] 1-line
ye15
2
155
minimum elements to add to form a given sum
1,785
0.424
Medium
25,593
https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/discuss/1459851/PyPy3-Solution-w-comments
class Solution: def minElements(self, nums: List[int], limit: int, goal: int) -> int: """ 1) Take the sum of nums 2) Chech how many steps it is aways from goal 3) Divide the steps w.r.t limit 4) The ceiling of the division will give you no. of hopes required to reach that goal """ return math.ceil(abs(goal - sum(nums))/limit)
minimum-elements-to-add-to-form-a-given-sum
[Py/Py3] Solution w/ comments
ssshukla26
1
107
minimum elements to add to form a given sum
1,785
0.424
Medium
25,594
https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/discuss/1341922/One-liners-98-speed
class Solution: def minElements(self, nums: List[int], limit: int, goal: int) -> int: return ceil(abs(goal - sum(nums)) / limit)
minimum-elements-to-add-to-form-a-given-sum
One liners, 98% speed
EvgenySH
1
157
minimum elements to add to form a given sum
1,785
0.424
Medium
25,595
https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/discuss/1097311/Python-1-liner
class Solution: def minElements(self, nums: List[int], limit: int, goal: int) -> int: return (abs(goal - sum(nums)) + limit - 1) // limit
minimum-elements-to-add-to-form-a-given-sum
Python 1-liner
lokeshsenthilkumar
1
60
minimum elements to add to form a given sum
1,785
0.424
Medium
25,596
https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/discuss/2726050/Python3-Easy-concise-and-commented-Solution
class Solution: def minElements(self, nums: List[int], limit: int, goal: int) -> int: # we need to get the sum of the array summed = sum(nums) # calculate the distance to the target as absolute value dist = abs(summed - goal) # get the divmod # the division part tells us how often we need to use the limit # as using the limit will bring us the closest to our target # # the mod part tells us, if we reach the target just using the limit # or if we need one more number smaller than the limit res, left = divmod(dist, limit) return res + 1 if left else res
minimum-elements-to-add-to-form-a-given-sum
[Python3] - Easy, concise, and commented Solution
Lucew
0
13
minimum elements to add to form a given sum
1,785
0.424
Medium
25,597
https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/discuss/2537001/Python-3-or-2-Lines-or-Sum-%2B-Ceil-of-Division
class Solution: def minElements(self, nums: List[int], limit: int, goal: int) -> int: total = sum(nums) return ceil(abs(total - goal) / limit)
minimum-elements-to-add-to-form-a-given-sum
Python 3 | 2 Lines | Sum + Ceil of Division
leeteatsleep
0
17
minimum elements to add to form a given sum
1,785
0.424
Medium
25,598
https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/discuss/2309421/python-3-or-simple-one-liner-or-O(n)O(1)
class Solution: def minElements(self, nums: List[int], limit: int, goal: int) -> int: return math.ceil(abs(sum(nums) - goal) / limit)
minimum-elements-to-add-to-form-a-given-sum
python 3 | simple one liner | O(n)/O(1)
dereky4
0
38
minimum elements to add to form a given sum
1,785
0.424
Medium
25,599