Sort by

recency

|

329 Discussions

|

  • + 0 comments

    Python3 Section

    def plusSection(r, c, k):
        cells = {(r, c)}
        for i in range(1, k+1):
            cells.add((r+i, c))
            cells.add((r-i, c))
            cells.add((r, c+i))
            cells.add((r, c-i))
        return cells
    
    
    def areaOfTwoPluses(p1, p2):
        r1, c1, area1, count1 = p1
        r2, c2, area2, count2 = p2
    
        cells1 = plusSection(r1, c1, count1)
        cells2 = plusSection(r2, c2, count2)
    
        if cells1 & cells2: 
            return max(area1, area2)
    
        return area1 * area2
    
        
    def savePlusess(r, c, grid):
        row, col = len(grid), len(grid[0])
        count = 1
        
        plusess = []
        
        while True: 
            if r + count >= row:
                break
            if r - count < 0:
                break
            if c + count >= col:
                break
            if c - count < 0:
                break
            if grid[r+count][c] != "G":
                break
            if grid[r-count][c] != "G":
                break 
            if grid[r][c+count] != "G":
                break
            if grid[r][c-count] != "G":
                break
            
            count += 1
            
            plusess.append((1 + 4*(count-1), count-1))
                
        return plusess
    
    def twoPluses(grid):
        row, col = len(grid), len(grid[0])
        plusess = []
    
        for r in range(row):
            for c in range(col):
                if grid[r][c] == "G":
                    currPlusess = savePlusess(r, c, grid)
                    for pluses in currPlusess:
                        area = pluses[0]
                        count = pluses[1]
                        plusess.append((r, c, area, count))
          
        if len(plusess) == 1:
            return plusess[0][2]
        
        res = 1
        for i in range(len(plusess)):
            for j in range(i+1, len(plusess)):
                area = areaOfTwoPluses(plusess[i], plusess[j])
                if area > res:
                    res = area
            
        return res
    
  • + 0 comments

    python solution (it took me way more time than expected):

    class Plus:
        def __init__(self, i, j, k):
            self.i = i
            self.j = j
            self.k = k
        
        def is_disjoint(self, another):
            if self.i == another.i:
                if self.j > another.j and self.j - self.k <= another.j + another.k:
                    return False
                if self.j < another.j and self.j + self.k >= another.j - another.k:
                    return False
            if self.j == another.j:
                if self.i > another.i and self.i - self.k <= another.i + another.k:
                    return False
                if self.i < another.i and self.i + self.k >= another.i - another.k:
                    return False
            
            if another.i - another.k <= self.i <= another.i + another.k:
                if self.j - self.k <= another.j <= self.j + self.k:
                    return False
            if another.j - another.k <= self.j <= another.j + another.k:
                if self.i - self.k <= another.i <= self.i + self.k:
                    return False
            return True
            
    def get_pluses(grid, i, j, k_range):
        n = len(grid)
        m = len(grid[0])
        
        pluses = [Plus(i, j, 0)]
        for k in range(1, k_range):
            if i - k < 0 or i + k >= n:
                return pluses
            if j - k < 0 or j + k >= m:
                return pluses
                    
            positions = [(i - k, j), (i, j + k), (i + k, j), (i, j - k)]
            for pos in positions:
                if grid[pos[0]][pos[1]] != 'G':
                    return pluses
            pluses.append(Plus(i, j, k))
        return pluses
    
    def twoPluses(grid):
        n = len(grid)
        m = len(grid[0])
        minimum_size = min(n, m)
        pluses = []
        
        for i in range(n):
            for j in range(m):
                if grid[i][j] != 'G':
                    continue
                pluses += get_pluses(grid, i, j, (minimum_size+1)//2)
        
        maximum_area = 0
        for index_plus0 in range(len(pluses) - 1):
            for index_plus1 in range(index_plus0 + 1, len(pluses)):
                if pluses[index_plus0].is_disjoint(pluses[index_plus1]):
                    area0 = pluses[index_plus0].k * 4 + 1
                    area1 = pluses[index_plus1].k * 4 + 1
                    current_area = area0 * area1
                    if current_area > maximum_area:
                        maximum_area = current_area
        return maximum_area
    
  • + 0 comments

    **Solution in Python 3 **

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'twoPluses' function below.
    #
    # The function is expected to return an INTEGER.
    # The function accepts STRING_ARRAY grid as parameter.
    #
    
    def twoPluses(grid):
        n, m = len(grid), len(grid[0])
        up = [[0]*m for _ in range(n)]
        down = [[0]*m for _ in range(n)]
        left = [[0]*m for _ in range(n)]
        right = [[0]*m for _ in range(n)] 
        for i in range(n):
            for j in range(m):
                if grid[i][j] == 'G':
                    up[i][j] = 1 + (up[i-1][j] if i > 0 else 0)
                    left[i][j] = 1 + (left[i][j-1] if j > 0 else 0)
        for i in reversed(range(n)):
            for j in reversed(range(m)):
                if grid[i][j] == 'G':
                    down[i][j] = 1 + (down[i+1][j] if i < n-1 else 0)
                    right[i][j] = 1 + (right[i][j+1] if j < m-1 else 0)
        pluses = []
        for i in range(n):
            for j in range(m):
                if grid[i][j] == 'G':
                    arm_len = min(up[i][j], down[i][j], left[i][j], right[i][j]) - 1
                    for length in range(arm_len + 1):
                        pluses.append((i, j, length, 4*length + 1))  
        
        def cells_of_plus(r, c, arm):
            cells = {(r, c)}
            for k in range(1, arm+1):
                cells.add((r+k, c))
                cells.add((r-k, c))
                cells.add((r, c+k))
                cells.add((r, c-k))
            return cells
        max_product = 0
        for i in range(len(pluses)):
            r1, c1, arm1, area1 = pluses[i]
            cells1 = cells_of_plus(r1, c1, arm1)
            for j in range(i+1, len(pluses)):
                r2, c2, arm2, area2 = pluses[j]
                cells2 = cells_of_plus(r2, c2, arm2)
                if cells1.isdisjoint(cells2):
                    product = area1 * area2
                    if product > max_product:
                        max_product = product
        return max_product
    
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        first_multiple_input = input().rstrip().split()
    
        n = int(first_multiple_input[0])
    
        m = int(first_multiple_input[1])
    
        grid = []
    
        for _ in range(n):
            grid_item = input()
            grid.append(grid_item)
    
        result = twoPluses(grid)
    
        fptr.write(str(result) + '\n')
    
        fptr.close()
    
  • + 0 comments

    Emergency Locksmith provides fast and reliable solutions when you’re locked out of your home, office, or car. Available 24/7, skilled professionals handle urgent lock repairs, key replacements, and security upgrades with precision and care. Just like Ema’s Supercomputer delivers power and efficiency in advanced computing, Emergency Locksmith ensures quick, intelligent, and effective responses to your lock emergencies, restoring safety and peace of mind when you need it most.

  • + 0 comments

    Respectfully, this problem is not "medium", it's freaking hard! (ಥ﹏ಥ)

    Heres is my c++ solutions, hope is helps someone.

    struct Plus {
        int area;
        vector<pair<int,int>> cells;
    };
    
    vector<Plus> allPlussesAt(int y, int x, const vector<string>& grid) {
        vector<Plus> result;
        if (grid[y][x] != 'G') return result;
    
        int arm = 0;
        int rows = grid.size(), cols = grid[0].size();
    
        while (y-arm >= 0 && y+arm < rows &&
               x-arm >= 0 && x+arm < cols &&
               grid[y-arm][x] == 'G' &&
               grid[y+arm][x] == 'G' &&
               grid[y][x-arm] == 'G' &&
               grid[y][x+arm] == 'G') {
            
            vector<pair<int,int>> cells;
            cells.push_back({y,x});
            for (int k=1; k<=arm; k++) {
                cells.push_back({y-k,x});
                cells.push_back({y+k,x});
                cells.push_back({y,x-k});
                cells.push_back({y,x+k});
            }
            
            result.push_back({1 + 4*arm, cells});
            arm++;
        }
    
        return result;
    }
    
    vector<Plus> allPlusses(const vector<string>& grid) {
        vector<Plus> plusses;
        for (int y=0; y<grid.size(); y++) {
            for (int x=0; x<grid[0].size(); x++) {
                auto p = allPlussesAt(y, x, grid);
                plusses.insert(plusses.end(), p.begin(), p.end());
            }
        }
        return plusses;
    }
    
    bool overlap(const Plus& a, const Plus& b) {
        for (auto &cellA : a.cells) {
            for (auto &cellB : b.cells) {
                if (cellA == cellB) return true;
            }
        }
        return false;
    }
    
    int maxPlusAreaAt(int y, int x, const vector<string>& grid) {
        if (grid[y][x] != 'G') return 0;
    
        int arm = 0;
        int rows = grid.size(), cols = grid[0].size();
    
        while (y-arm >= 0 && y+arm < rows &&
               x-arm >= 0 && x+arm < cols &&
               grid[y-arm][x] == 'G' &&
               grid[y+arm][x] == 'G' &&
               grid[y][x-arm] == 'G' &&
               grid[y][x+arm] == 'G') {
            arm++;
        }
        arm--; 
        
        return 1 + 4*arm;
    }
    
    int twoPluses(vector<string> grid) {
        vector<Plus> plusses = allPlusses(grid);
    
        int best = 0;
        for (int i=0; i<plusses.size(); i++) {
            for (int j=i+1; j<plusses.size(); j++) {
                if (!overlap(plusses[i], plusses[j])) {
                    best = max(best, plusses[i].area * plusses[j].area);
                }
            }
        }
    
        return best;
    }