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>& seats) {
 2         vector<int> pre,post;
 3         int n = seats.size();
 4         pre.assign(n,0);
 5         post.assign(n,0);
 6         int nst = -200000;
 7         for(int i = 0; i < n; i++){
 8             if(seats[i] == 1){
 9                 nst = i;
10             }else{
11                 pre[i] = i - nst;
12             }
13         }
14         nst = 200000;
15         for(int i = n - 1; i >= 0; i--){
16             if(seats[i]==1){
17                 nst = i;
18             }else{
19                 post[i] = nst - i;
20             }
21         }
22         int res = 0;
23         for(int i = 0; i<n; i++ ){
24             int temp = min(pre[i],post[i]);
25             res = max(res,temp);
26         }
27         return res;
28     }

原文地址:https://www.cnblogs.com/jinjin-2018/p/9165164.html

时间: 2024-08-01 02:10:03

849. Maximize Distance to Closest Person ——weekly contest 87的相关文章

【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

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

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 closes

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 Weekly Contest 86

Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 "幻方" 子矩阵?(每个子矩阵都是连续的). 直接模拟即可,本来是签到题,由于粗心,浪费了时间. 1 class Solution { 2 public: 3 int numMagicSquaresInside(vector&l

[Swift Weekly Contest 118]LeetCode973. 最接近原点的 K 个点 | K Closest Points to Origin

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

[Swift Weekly Contest 127]LeetCode1005. K 次取反后最大化的数组和 | Maximize Sum Of Array After K Negations

Given an array A of integers, we must modify the array in the following way: we choose an i and replace A[i] with -A[i], and we repeat this process K times in total.  (We may choose the same index i multiple times.) Return the largest possible sum of

Weekly Contest 119

第一题: 973. K Closest Points to Origin We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The an

【leetcode】Weekly Contest 91

leetcode周赛,早上起来发现没网,用热点意识模糊的a了三个水题. 1.Lemonade Change 简单模拟题,收到十元用五元钱找回,收到20元时优先用一张10一张5,如果10不够用3张5,如果没有就返回flase(贪心). 1 public boolean lemonadeChange(int[] bills) { 2 int five = 0; 3 int ten = 0; 4 int twenty = 0; 5 for (int i = 0; i < bills.length; i