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
... | 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
d... | 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... | 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
... | 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)
... | 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:
... | 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 < min... | 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:... | 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(validDist... | 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) :
... | 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[... | 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... | 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])
... | 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 ... | 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 ... | 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)
... | 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... | 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
... | 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<... | 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:
... | 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:
... | 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:
... | 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))
... | 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 f... | 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:
smal... | 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 point... | 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 ... | 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 ... | 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:... | 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])
... | 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)
... | 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])
... | 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... | 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(... | 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:
... | 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
mat... | 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:... | 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
an... | 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... | 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(... | 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 ... | 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! :)
... | 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, ... | 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 :
... | 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- Ap... | 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:].zfi... | 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 ... | 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, ... | 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 quo... | 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 rep... | 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]]
... | 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()
... | 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
... | 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])
... | 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())... | 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
... | 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')] +=... | 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
val... | 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] =... | 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... | 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
... | 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
#... | 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)
... | 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 fi... | 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:
... | 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 rea... | 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 te... | 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 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.