[LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS

In a row of seats1 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. 1 <= seats.length <= 20000
  2. seats contains only 0s or 1s, at least one 0, and at least one 1.

思路就是类似于[LeetCode] 286. Walls and Gates_Medium tag: BFS

Code: T: O(n) S; O(n)

class Solution:
    def maxDistToClosest(self, seats):
        """
        :type seats: List[int]
        :rtype: int
        """
        queue, ans, visited, lr = collections.deque(), 0, set(), len(seats)
        for i in range(lr):
            if seats[i]:
                queue.append((i,0))
                visited.add(i)

        while queue:
            node, dis = queue.popleft()
            for c in [-1,1]:
                nr = node + c
                if 0 <= nr < lr and nr not in visited and not seats[nr]:
                    queue.append((nr, dis+1))
                    visited.add(nr)
                    ans = max(ans, dis+1)
        return ans

原文地址:https://www.cnblogs.com/Johnsonxiong/p/9499618.html

时间: 2024-08-29 09:42:01

[LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS的相关文章

849. Maximize Distance to Closest Person ——weekly contest 87

849. Maximize Distance to Closest Person 题目链接:https://leetcode.com/problems/maximize-distance-to-closest-person/description/ 思路:pre[i]存放i之前离最近的1的距离.post记录之后的. res = max(min(pre[i],[post[i])) 注意点:初始nst需要设计极大或极小值. 1 int maxDistToClosest(vector<int>&am

【Leetcode_easy】849. Maximize Distance to Closest Person

problem 849. Maximize Distance to Closest Person 参考 1. Leetcode_easy_849. Maximize Distance to Closest Person; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11214905.html

849. Maximize Distance to Closest Person

1 class Solution 2 { 3 public: 4 int maxDistToClosest(vector<int>& seats) 5 { 6 int count=0; 7 int maxseat=0; 8 for(int i:seats) //count the max length of continuous 0 9 { 10 if(i==0) 11 count++; 12 else 13 { 14 maxseat=max(maxseat,count); 15 co

LeetCode算法题-Maximize Distance to Closest Person(Java实现)

这是悦乐书的第328次更新,第351篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第198题(顺位题号是849).在一排座位中,1表示一个人坐在该座位上,0表示座位是空的.在这些座位中,至少有一个空座位,至少有一个人坐着.Alex想坐在座位上,以便他和离他最近的人之间的距离最远.返回距离最近的人的最大距离.例如: 输入:[1,0,0,0,1,0,1] 输出:2 说明:如果Alex坐在第二个空座位(seats[2]),那么离最近的人距离为2.如果Alex坐在任何其他空

[LeetCode] 613. Shortest Distance in a Line_Easy tag: SQL

Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Write a query to find the shortest distance between two points in these points. | x | |-----| | -1 | | 0 | | 2 | The shortest distance is '1' obviously, w

[LeetCode] 102. Binary Tree Level Order Traversal_Medium tag: BFS

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ]

【leetcode】Edit Distance 详解

下图为TI C6xx DSP Nyquist总线拓扑图,总线连接了master与slave,提供了高速的数据传输.有很多种速率不同的总线,如图中的红色方框,最高速总线为CPU/2 TeraNet SCR(即VBUSM SCR),带宽为256bit,其他低速总线为CPU/3,CPU/6,带宽参考图中所示.总线之间用Bridge(桥)连接,作用包括转换总线的速率,使之与所流向总线的速率相同等. 在具体应用中,各种速率的总线完全可以满足复杂的数据传输,而数据传输的瓶颈往往在于连接总线之间的Bridge

[LeetCode] 126. Word Ladder II_Hard tag: BFS&amp;DFS

Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a time Each transformed word must exist in the word list. Note

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig