TheaterVisit

原创 2008年07月28日 17:31:00
/*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;
    }
}
 

相关文章推荐

内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:TheaterVisit
举报原因:
原因补充:

(最多只允许输入30个字)