2020年4月9日 星期四

[LeetCode] Easy - 849. Maximize Distance to Closest Person (?)

Source From Here
Question
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.
Solution

Naive
  1. class Solution:  
  2.     def maxDistToClosest(self, seats):  
  3.         max_dist = 0  
  4.   
  5.         def dist2cp(zero_seq, is_edged):  
  6.             if is_edged:  
  7.                 return zero_seq  
  8.             else:  
  9.                 return (zero_seq-1) // 2 + 1  
  10.   
  11.         zc = 0  
  12.         is_edge = True  
  13.         for v in seats:  
  14.             if v == 1:  
  15.                 dist = dist2cp(zc, is_edge)  
  16.                 if dist > max_dist:  
  17.                     max_dist = dist  
  18.   
  19.                 zc = 0  
  20.                 is_edge = False  
  21.             else:  
  22.                 zc += 1  
  23.   
  24.         if zc > 0:  
  25.             dist = dist2cp(zc, True)  
  26.             if dist > max_dist:  
  27.                 max_dist = dist  
  28.   
  29.         return max_dist  
  30.   
  31. if __name__ == '__main__':  
  32.     s = Solution()  
  33.     for seats, ans in [  
  34.                         ([1, 0, 0, 0, 1, 0, 1], 2),  
  35.                         ([0, 0, 0, 1, 0, 0, 0, 1], 3),  
  36.                         ([1, 0, 0, 1, 1], 1)  
  37.                       ]:  
  38.         rel = s.maxDistToClosest(seats)  
  39.         print("{}: {}".format(seats, rel))  
  40.         assert ans == rel  
Runtime: 120 ms, faster than 99.47% of Python3 online submissions for Maximize Distance to Closest Person.
Memory Usage: 14.5 MB, less than 8.33% of Python3 online submissions for Maximize Distance to Closest Person.

Discussion - Solution in Python 3 (link)
  1. class Solution:  
  2.     def maxDistToClosest(self, seats: List[int]) -> int:  
  3.         L = len(seats)  
  4.           
  5.         # 1) Get seat of each person  
  6.         # e.g seats = [1, 0, 0, 0, 1, 0, 1]  
  7.         #     S = [0, 4, 6]  
  8.         S = [i for i in range(L) if seats[i]]  
  9.           
  10.         # 2) Calculate distance between two sequential person  
  11.         #    S = [0, 4, 6]  
  12.         #    d = [(4-0//2)=2, (6-4)//2=1] = [2, 1]  
  13.         d = [S[i+1]-S[i] for i in range(len(S)-1)] if len(S) > 1 else [0]  
  14.           
  15.         # 3) Find maximum distance  
  16.         return max(max(d)//2, S[0], L-1-S[-1])  
Runtime: 136 ms, faster than 78.43% of Python3 online submissions for Maximize Distance to Closest Person.
Memory Usage: 14.7 MB, less than 8.33% of Python3 online submissions for Maximize Distance to Closest Person.


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...