Given n
points on a 2D plane, find if there is such a line parallel to the y-axis that reflects the given points symmetrically.
In other words, answer whether if there exists a line that after reflecting all points over the given line, the original points' set is the same as the reflected ones.
Note that there can be repeated points.
def isReflected(points):
min_x, max_x = float('inf'), float('-inf')
point_set = set()
for x, y in points:
min_x = min(min_x, x)
max_x = max(max_x, x)
point_set.add((x, y))
s = min_x + max_x
for x, y in points:
if (s - x, y) not in point_set:
return False
return True
Maximize Distance to Closest Person
You are given an array representing a row of seats
where seats[i] = 1
represents a person sitting in the ith
seat, and seats[i] = 0
represents that the ith
seat is empty (0-indexed).
There is at least one empty seat, and at least one person sitting.
Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.
Return that maximum distance to the closest person.
def maxDistToClosest(seats: List[int]) -> int:
max_dist, not_empty = 0, -1
for (i, s) in enumerate(seats):
if s:
max_dist = int(max(max_dist, i if not_empty < 0 else (i - not_empty) / 2))
not_empty = i
return max(max_dist, len(seats) - not_empty - 1)
Longest Subarray of 1's After Deleting One Element
Given a binary array nums
, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1
's in the resulting array. Return 0
if there is no such subarray.
def longestSubarray(nums: List[int]) -> int:
l, ans, sum = 0, 0, 0 #sum of nums[l~r]
for (r, n) in enumerate(nums):
sum += n
# Maintain sum >= r - l, at most 1 zero.
while l < r and sum < r - l:
sum -= nums[l]
l += 1
ans = max(ans, r - l)
return ans
You are given a sorted unique integer array nums
.
A range [a,b]
is the set of all integers from a
to b
(inclusive).