849. Maximize Distance to Closest Person

2023-12-21 16:39

本文主要是介绍849. Maximize Distance to Closest Person,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

849. 到最近的人的最大距离

在一排座位( 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. 1 <= seats.length <= 20000
  2. seats 中只含有 0 和 1,至少有一个 0,且至少有一个 1

解法一

//时产复杂度O(n), 空间复杂度O(n)
class Solution {
public:int maxDistToClosest(vector<int>& seats) {int n = seats.size();vector<int> rec;for(int i = 0; i < n; i++) {if(seats[i]) rec.push_back(i);}int m = rec.size();int maxDist = max(rec[0], n - 1 - rec[m - 1]);for(int i = 0; i < m - 1; i++) {maxDist = max(maxDist, (rec[i + 1] - rec[i]) / 2);}return maxDist;}
};

解法二

class Solution {
public:int maxDistToClosest(vector<int>& seats) {int n = seats.size(), i = 0;while(!seats[i]) i++;int j = i, maxDist = i;while(j < n) {if(seats[j]) {maxDist = max(maxDist, (j - i) / 2);i = j;}j++;}maxDist = max(maxDist, j - i - 1);return maxDist;}
};

一次遍历,比较相邻两个为1的元素之间的距离的一半,记下其最大值。返回。

2019/07/15 20:40

这篇关于849. Maximize Distance to Closest Person的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/520736

相关文章

[LeetCode] 863. All Nodes Distance K in Binary Tree

题:https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/ 题目大意 求给树中,距给定 结点 指定长度的 所有结点的val 思路 tree -> graph 、 bfs 先遍历树,并用map记录每个结点的父结点 ,将树变为图,然后 bfs。 /*** Definition for a binary tree

【HDU】5102 The K-th Distance bfs

传送门:【HDU】5102 The K-th Distance 题目分析:思路秒出,5101写完不到20分钟这题就AC了。。将所有点扔进队列中,记录前驱,步数,每次扩展的时候不走前驱,这样就相当于n棵树同时在扩展。注意到一条路径会被重复走两次,所以k*2,ans/2。注意姿势不对就会MLE。 代码如下: #include <map>#include <vector>

【codechef】 Prime Distance On Tree【求树上路经长度为i的路径条数】【点分治+FFT】

传送门:【codechef】 Prime Distance On Tree 点分治+FFT水题……竟然n*n爆int没发现…… 而且NTT TLE,FFT跑的超级快…… my  code: my~~code: #include <bits/stdc++.h>using namespace std ;typedef long long LL ;#define clr( a , x ) m

LeetCode 16 3Sum Closest

题意: 给出数组s和目标target,要求从中选出3个数字,使其相加的和最接近target。 思路: x sum系列的题目,在这里做一个总结。 最经典的情况为2 sum问题,即给出s和target找出s[i] + s[j] = target。 可以使用枚举s[i]判断target - s[i]是否在s中出现且与s[i]不同的O(nlogn)方法,用map或排序后二分查找的方式均可。

B. Ehab Is an Odd Person

B. Ehab Is an Odd Person You’re given an array a of length n. You can perform the following operation on it as many times as you want: Pick two integers i and j (1≤i,j≤n)(1≤i,j≤n) such that ai+aj is

Closest Leaf in a Binary Tree

Input:root = [1,2,3,4,null,null,null,5,null,6], k = 2Diagram of binary tree:1/ \2 3/4/5/6Output: 3Explanation: The leaf node with value 3 (and not the leaf node with value 6) is nearest to the no

Shortest Distance to a Character

Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. Example 1: Input: S = "loveleetcode", C = 'e'Output: [3, 2, 1,

Signed distance fields (SDFs) and Truncated Signed Distance Field(TSDF)

1. Signed distance fields (SDFs) 笔记来源: [1] Signed distance fields (SDFs) [2] Signed Distance Function (SDF): Implicit curves or surfaces [3] Ray Marching and Signed Distance Functions [4] Truncated S

度量学习(Distance Metric Learning)介绍

原文:度量学习(Distance Metric Learning)介绍 http://blog.csdn.net/lzt1983/article/details/7884553 一直以来都想写一篇metric learning(DML)的综述文章,对DML的意义、方法论和经典论文做一个介绍,同时对我的研究经历和思考做一个总结。可惜一直没有把握自己能够写好,因此拖到现在。

【Java】—— Java面向对象基础:Person类实例操作

目录 一、定义Person类 二、创建Person对象并操作 三、理解对象之间的关系 四、总结         在Java编程中,面向对象编程(OOP)是一种非常核心且广泛使用的编程范式。它允许我们通过类(Class)来定义对象的属性和行为,从而模拟现实世界的实体和它们之间的交互。本文将通过一个简单的Person类实例,展示如何在Java中创建对象、设置属性、调用方法,并体会