[Swift]LeetCode849. 到最近的人的最大距离 | Maximize Distance to Closest Person
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10591933.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
In a row of seats
, 1
represents a person sitting in that seat, and 0
represents that the seat is empty.
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 closest person.
Example 1:
Input: [1,0,0,0,1,0,1]
Output: 2
Explanation:
If Alex sits in the second open seat (seats[2]), then the closest person has distance 2.
If Alex sits in any other open seat, the closest person has distance 1.
Thus, the maximum distance to the closest person is 2.
Example 2:
Input: [1,0,0,0]
Output: 3
Explanation:
If Alex sits in the last seat, the closest person is 3 seats away.
This is the maximum distance possible, so the answer is 3.
Note:
1 <= seats.length <= 20000
seats
contains only 0s or 1s, at least one0
, and at least one1
.
在一排座位( seats
)中,1
代表有人坐在座位上,0
代表座位上是空的。
至少有一个空座位,且至少有一人坐在座位上。
亚历克斯希望坐在一个能够使他与离他最近的人之间的距离达到最大化的座位上。
返回他到离他最近的人的最大距离。
示例 1:
输入:[1,0,0,0,1,0,1] 输出:2 解释: 如果亚历克斯坐在第二个空位(seats[2])上,他到离他最近的人的距离为 2 。 如果亚历克斯坐在其它任何一个空位上,他到离他最近的人的距离为 1 。 因此,他到离他最近的人的最大距离是 2 。
示例 2:
输入:[1,0,0,0] 输出:3 解释: 如果亚历克斯坐在最后一个座位上,他离最近的人有 3 个座位远。 这是可能的最大距离,所以答案是 3 。
提示:
1 <= seats.length <= 20000
seats
中只含有 0 和 1,至少有一个0
,且至少有一个1
。
1 class Solution { 2 func maxDistToClosest(_ seats: [Int]) -> Int { 3 var i:Int = 0 4 var j:Int = 0 5 var res:Int = 0 6 var n:Int = seats.count 7 while(j < n) 8 { 9 if seats[j] == 1 10 { 11 if i == 0 12 { 13 res = max(res, j - i) 14 } 15 else 16 { 17 res = max(res, (j - i + 1) / 2) 18 } 19 i = j + 1 20 } 21 j += 1 22 } 23 return max(res, n - i) 24 } 25 }
84ms
1 class Solution { 2 func maxDistToClosest(_ seats: [Int]) -> Int { 3 let count = seats.count 4 var left = -1 5 var dis = 0 6 for i in 0..<count { 7 if seats[i] == 1 { // 有人 8 if left == -1 { 9 dis = max(dis, i - left - 1) 10 } else { 11 dis = max(dis, (i - left) / 2) 12 } 13 left = i 14 } 15 } 16 dis = max(dis, count - 1 - left) 17 return dis 18 } 19 }
88ms
1 class Solution { 2 func maxDistToClosest(_ seats: [Int]) -> Int { 3 4 var maxDistance: Int? 5 var maxSeats = 0 6 for num in seats { 7 if num == 0 { 8 maxSeats += 1 9 } else { 10 if maxDistance == nil { 11 maxDistance = maxSeats 12 } else { 13 let distance = (maxSeats + 1) / 2 14 if distance > maxDistance! { 15 maxDistance = distance 16 } 17 } 18 maxSeats = 0 19 } 20 } 21 22 if maxSeats > maxDistance! { 23 maxDistance = maxSeats 24 } 25 26 return maxDistance ?? 0 27 } 28 }
92ms
1 class Solution { 2 func maxDistToClosest(_ seats: [Int]) -> Int { 3 var max0sBetween = 0 4 var max0sEdge = 0 5 for i in 0..<seats.count where seats[i] == 0 { 6 var j = i 7 while j < seats.count && seats[j] == 0 { 8 j += 1 9 } 10 if i == 0 || j == seats.count { 11 max0sEdge = max(max0sEdge, j - i) 12 } else { 13 max0sBetween = max(max0sBetween, j - i) 14 } 15 } 16 return max((max0sBetween + 1) / 2, max0sEdge) 17 } 18 }
96ms
1 class Solution { 2 func maxDistToClosest(_ seats: [Int]) -> Int { 3 var result = 0 4 enum Status { 5 case idle 6 case checkingEmpty(firstEmpty: Int) 7 case justFindPerson 8 } 9 var status = Status.idle 10 11 for (index, value) in seats.enumerated() { 12 switch value { 13 case 0: 14 switch status { 15 case .idle: 16 status = .checkingEmpty(firstEmpty: index) 17 case .checkingEmpty: 18 break 19 case .justFindPerson: 20 status = .checkingEmpty(firstEmpty: index) 21 } 22 case 1: 23 switch status { 24 case .idle: 25 status = .justFindPerson 26 case .checkingEmpty(let firstEmpty): 27 if firstEmpty == 0 { 28 result = index 29 } else { 30 result = max(result, (index - firstEmpty + 1) / 2) 31 } 32 status = .justFindPerson 33 case .justFindPerson: 34 break 35 } 36 default: 37 return 0 38 } 39 } 40 41 switch status { 42 case .checkingEmpty(let firstEmpty): 43 result = max(result, seats.count - firstEmpty) 44 default: 45 break 46 } 47 48 return result 49 } 50 }
100ms
1 class Solution { 2 func maxDistToClosest(_ seats: [Int]) -> Int { 3 var start = 0, dist = -1 4 5 var idx = 1, trackMode = seats[0], currDist = 0 6 while idx < seats.count { 7 guard seats[idx] != trackMode else { 8 idx += 1 9 continue 10 } 11 12 trackMode = seats[idx] 13 14 if trackMode == 1 { 15 currDist = idx - start 16 if start == 0 { 17 dist = currDist * 2 18 } else { 19 dist = max(currDist, dist) 20 } 21 } else { 22 start = idx 23 } 24 25 idx += 1 26 } 27 28 if let last = seats.last, last == 0 { 29 currDist = (seats.count - start) * 2 30 dist = max(dist, currDist) 31 } 32 33 return (dist - 1) / 2 + 1 34 } 35 }
108ms
1 class Solution { 2 func maxDistToClosest(_ seats: [Int]) -> Int { 3 var idx = seats.firstIndex(of: 1)! 4 var maxDist = idx 5 6 while let next = seats[(idx+1)...].firstIndex(of: 1) { 7 maxDist = max(maxDist, (next - idx)/2) 8 idx = next 9 } 10 11 return max(maxDist, seats.count - idx - 1) 12 } 13 }
102ms
1 class Solution { 2 func maxDistToClosest(_ seats: [Int]) -> Int { 3 var left: [Int] = [] 4 var right: [Int] = [] 5 for i in 0..<seats.count { 6 left.append(-1) 7 right.append(-1) 8 } 9 var leftFlag = -1 10 for i in 0..<seats.count { 11 if seats[i] == 1 { 12 leftFlag = i 13 } 14 if seats[i] == 0 { 15 if leftFlag != -1 { 16 left[i] = i - leftFlag 17 } 18 } 19 } 20 var rightFlag = -1 21 for i in (0..<seats.count).reversed() { 22 if seats[i] == 1 { 23 rightFlag = i 24 } 25 if seats[i] == 0 { 26 if rightFlag != -1 { 27 right[i] = rightFlag - i 28 } 29 } 30 } 31 var maxValue = 0 32 for i in 0..<seats.count { 33 maxValue = max(maxValue, minBiggerThan0(left[i], right[i])) 34 } 35 return maxValue 36 } 37 38 func minBiggerThan0(_ a: Int, _ b: Int) -> Int { 39 if a > 0 { 40 if b > 0 { 41 return min(a, b) 42 } 43 return a 44 } 45 if b > 0 { 46 return b 47 } 48 return -1 49 } 50 }
148ms
1 class Solution { 2 func maxDistToClosest(_ seats: [Int]) -> Int { 3 var prev = -1 4 var res = 1 5 6 for (i, v) in seats.enumerated() { 7 if v == 1 { 8 if prev < 0 { 9 res = i 10 } else { 11 res = max(res, (i - prev) / 2) 12 } 13 prev = i 14 } 15 } 16 17 return max(res, seats.count - 1 - prev) 18 } 19 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了