LeetCode. Maximize Distance to Closest Person
时间:2019-11-28
本文章向大家介绍LeetCode. Maximize Distance to Closest Person,主要包括LeetCode. Maximize Distance to Closest Person使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
Description:
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
.
Accepted
45,488
Submissions
108,771
Detailed Code
class Solution { public int maxDistToClosest(int[] seats) { if(seats.length ==2){ return 1; } int result = 0; int max = Integer.MIN_VALUE; int count = 0; String s = ""; for(int i = 0; i<seats.length; i++){ s= s+ seats[i]; if(seats[i] == 0){ count++; } else{ if(count> max){ max = count; } count = 0; } } if(count> max){ max = count; } String sub = ""; for(int i =0; i<max;i++){ sub = sub+"0"; } if(sub.equals(s.substring(0,sub.length()))|| sub.equals(s.substring(s.length()-sub.length(),s.length()))){ result = max; } else{ if(max%2==0){ result = max/2; } else{ result = max/2+1; } } int sub_max_1 = 0; int sub_max_2 = 0; for(int i = 0; i<seats.length; i++){ if(seats[i]==0){ sub_max_1 ++; } else{ break; } } for(int i = seats.length-1; i>0; i--){ if(seats[i]==0){ sub_max_2++; } else{ break; } } int tmp = sub_max_1>sub_max_2? sub_max_1:sub_max_2; return result>tmp? result:tmp ; } }
原文地址:https://www.cnblogs.com/codingyangmao/p/11950258.html
- Google发布会看图的人工智能,让它来评评你的照片拍得好不好
- Git日常操作命令梳理
- Git忽略规则.gitignore梳理
- 深入浅出事件流处理NEsper(二)
- 微信推出“微信使用小助手”,中老年人也能轻松玩转微信
- Flex4中使用WCF
- storm如何分配任务和负载均衡?
- Flex4中的ModuleLoader,Alert以及TitleWindow
- Flex4中使用HDividedBox,VDividedBox
- 编程技术哪家强,百度指数帮你忙
- 2017,人们视算法为洪水猛兽;算法说:我不想背锅
- 深入浅出事件流处理NEsper(三)
- 用Flex模拟智能手机表单输入的自动放大功能
- c#4.0中的动态编程
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法