TheaterVisit

/*Problem Statement

You want to buy two neighboring tickets in the first row of the theater so that one of the tickets is as far from the aisles as possible.

You will be given a String describing the first row of the theater where '.' represents an empty seat and 'X' represents an occupied seat.

Your task is to return the index (from 0) of the empty seat that is furthest from the aisles (the two ends of the String) and is also next to an empty seat.

If there are multiple possible seats, return the one with the smallest index. Return -1 if there are no seats that satisfy your requirements.


Definition

Class:            TheaterVisit
Method:            chooseSeat
Parameters:        String
Returns:        int
Method signature:    int chooseSeat(String row)
(be sure your method is public)


Constraints
-    row will contain between 1 and 50 characters, inclusive.
-    Each character in row will be either '.' or 'X'.


Examples
0)"....."    Returns: 2
You can buy either tickets with indexes 1 and 2 or tickets with indexes 2 and 3.
1)"......"    Returns: 2
2)"..X..."    Returns: 3
You should buy tickets with indexes 3 and 4.
3)".X.X..."    Returns: 4
4)"X.XX.X"    Returns: -1
5)".."        Returns: 0

This problem statement is the exclusive and proprietary property of TopCoder, Inc.
Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited.
(c)2003, TopCoder, Inc. All rights reserved.*/

package theaterVisit;

public class TheaterVisit {

    public static int chooseSeat(String row) {
        char[] signs = row.toCharArray();

        int result = -1;
        int min = -1;

        int i = 0;
        while (i < signs.length) {
            if (signs[i] == '.') {
                if ((i - 1 > 0 && signs[i - 1] == '.') || (i + 1 < signs.length && signs[i + 1] == '.')) {
                    int tempMin = ((signs.length - i - 1) < (i - 0)) ? (signs.length - i - 1) : i;

                    if (tempMin > min) {
                        result = i;
                        min = tempMin;
                    }
                }
            }
            i++;
        }

        return result;
    }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值