• + 0 comments

    My answer in Typescript, explain in comment

    // grid char const
    const GOOD = 'G', BAD = 'B', PLUSED = 'P';
    
    // replace string char by index
    function replaceCharAt(str: string, index: number, char: string): string {
        if (index < 0 || index >= str.length) { throw new Error("Index out of range"); }
        return str.substr(0, index) + char + str.substr(index + 1);
    }
    
    // select 1 coordinate, let pluses grow, return pluses size and new grid that pluses already growed
    function getPluses(grid: string[], startPosition: number[]): [number, string[]][] {
        const y = startPosition[0]
        const x = startPosition[1]
    
        let plused_grid = [...grid];
        let size = 0;
    
        plused_grid[y] = replaceCharAt(plused_grid[y], x, PLUSED)
    
        let result: [number, string[]][] = [[size, [...plused_grid]]]
        while (true) {
            try {
                if (plused_grid[y - (size + 1)][x] != GOOD) break;
                if (plused_grid[y + (size + 1)][x] != GOOD) break;
                if (plused_grid[y][x - (size + 1)] != GOOD) break;
                if (plused_grid[y][x + (size + 1)] != GOOD) break;
    
                plused_grid[y - (size + 1)] = replaceCharAt(plused_grid[y - (size + 1)], x, PLUSED)
                plused_grid[y] = replaceCharAt(plused_grid[y], x - (size + 1), PLUSED)
                plused_grid[y] = replaceCharAt(plused_grid[y], x + (size + 1), PLUSED)
                plused_grid[y + (size + 1)] = replaceCharAt(plused_grid[y + (size + 1)], x, PLUSED)
    
                size++;
    
                result.push([size, [...plused_grid]])
            } catch (e) {
                break;
            }
        }
        return result;
    }
    
    function twoPluses(grid: string[]): number {
        // Write your code here
    
        // store result of this problem
        let maxTotal = 0;
    
        // loop the grid, find GOOD coordinate, mean it is not in BAD or PLUSED
        for (let y1 = 0; y1 < grid.length; y1++) {
            for (let x1 = 0; x1 < grid[y1].length; x1++) {
                let cell = grid[y1][x1];
                if (cell != GOOD) continue;
    
                // got 1st pluses, try 2nd loop on a grid that placed 1st pluses
                getPluses(grid, [y1, x1]).forEach(([size_1, grid_1]) => {
                    for (let y2 = 0; y2 < grid_1.length; y2++) {
                        for (let x2 = 0; x2 < grid_1[y2].length; x2++) {
                            let cell = grid_1[y2][x2];
                            if (cell != GOOD) continue;
    
                            // got 2nd pluses, calculate size of 2 pluses, get the larger
                            getPluses(grid_1, [y2, x2]).forEach(([size_2, grid_2]) => {
                                // size is the wing length, so total is: 
                                //      size * 4 (wings) + 1 (intersection point)
                                maxTotal = Math.max((size_1 * 4 + 1) * (size_2 * 4 + 1), maxTotal);
                            })
                        }
                    }
                })
            }
        }
        
        // done
        return maxTotal
    }