从座位预订系统获取连续3个值?

时间:2018-07-17 15:15:30

标签: java arrays algorithm multidimensional-array

我正在使用飞机预定系统,飞机有10行。每排的座位从A到K。第5个座位后有一个过道。

就我而言,已经预定了1A,9C,10E座位。

请考虑一个人必须预订三个相邻的座位(不允许在过道上就座)。

现在我需要返回可用的座位,例如(1B,1C,1D&1C,1D,1E&1F,1G,1H&...)

我如何从该阵列连续获得3个席位?

enter image description here

代码

公共舱飞机{

let path = '/ok/this/is/the/path';
let parentPath = '/ok/this/is';
let parentPath2 = '/ok/this/is/';
let parentPath3 = '/notok/this/is/different';

console.log("/ok/this/is/the/path' is child of /ok/this/is => " + isPathChildOf(path, parentPath));
console.log("/ok/this/is/the/path' is child of /ok/this/is/=> " + isPathChildOf(path, parentPath2));
console.log("/ok/this/is/' is child of /ok/this/is/ => " + isPathChildOf(parentPath2, parentPath2));
console.log("/ok/this/is/the/path' is child of /notok/this/is/different => " + isPathChildOf(path, parentPath3));

// test number 2:

console.log('test number 2 : ');
console.log("=============================");

let pthParent = '/look/at/this/path';
let pth = '/look/at/this/patholabi/hola'; // in normal use of indexof it will return true (know too we didn't use indexof just to support the different path separators, otherwise we would have used indexof in our function)

//expected result is false
console.log(`${pth}  is a child of ${pthParent}  ===>  ${isPathChildOf(pth, pthParent)}`);


let pthParent2 = '/look/at/this/path';
let pth2 = '/look/at/this/path/hola'; 

//expected result is true
console.log(`${pth2}  is a child of ${pthParent2}  ===>  ${isPathChildOf(pth2, pthParent2)}`);


let pthParent3 = '/look/at/this/path';
let pth3 = '/look/at/this/pathholabi'; 

//expected result is false
console.log(`${pth3}  is a child of ${pthParent3}  ===>  ${isPathChildOf(pth3, pthParent3)}`);

// test 3: equality
console.log('\ntest 3 : equality');
console.log("==========================");

let pParent =  "/this/is/same/Path";
let p =  "/this\\is/same/Path/";

console.log(`${p} is child of  ${pParent}   ====> ${isPathChildOf(p, pParent, true)}`);

}

1 个答案:

答案 0 :(得分:0)

考虑此方法:

   /**
     * @param row The row of seats considered
     * @param sectionLength the length of each section split by aisles
     * @param numConsecutive the number of consecutive seats to consider
     */
    public static void getConsecutiveSeats(char[] row, int sectionLength, int numConsecutive) {
        int endWindow = numConsecutive;
        for (int startWindow = 0; endWindow <= row.length; startWindow++) {
            char[] consecutiveSeats = Arrays.copyOfRange(row, startWindow, endWindow);
            boolean validConsecutiveSeats = (startWindow >= sectionLength && endWindow >= sectionLength) ||
                    (startWindow <= sectionLength && endWindow <= sectionLength);
            if (!Arrays.toString(consecutiveSeats).contains("X") && validConsecutiveSeats) {
                System.out.println(consecutiveSeats);
            }
            endWindow++;
        }
    }

这将根据提供的参数获得每一行的连续座位。您要做的就是提供该行,并配置输出的外观。 startWindowendWindow指的是长度为numConsecutive的滑动窗口,这是您要考虑的连续座位数(例如,您说的是3)。

经过如下测试:

public static void main(String[] args) {
     char[] row = {'X', 'B', 'C', 'D', 'E', 'A', 'X', 'C', 'D', 'E'};
     getConsecutiveSeats(row, 5, 3);
}

哪个输出:

BCD
CDE
CDE

在变量row下正确。