Sort by

recency

|

327 Discussions

|

  • + 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;
    }
    
  • + 0 comments

    MY SOLUTION IN PHP

      $totalrow = count($grid);
        
        $columnArr = array();
        $allIndex = array();
    
        for ($i=0; $i <$totalrow ; $i++) { 
             $arr = str_split($grid[$i]);
             $columnArr[]  = $arr;
        }
        $columnLength = count($columnArr[0]);
    
    
        for ($i=1; $i < $totalrow-1; $i++) { 
    
            for ($j=1; $j <$columnLength-1 ; $j++) { 
                $rowis = $i+1;
                $columnIs = $j+1;
                
                $down = $totalrow-$rowis;
                $up = $totalrow - ($down +1);
                $right = $columnLength - ($columnIs);
                $left = $columnLength - ($right+1);
                $min = min($down,$up,$right,$left);
                
                
                for ($k=$min; $k >0 ; $k--) { 
                    
                     $totalValue = 0;
                     $flag = 0;
                     $indexArr = array();
                     $innerArr = array();
                     for ($l=0; $l <$k; $l++) { 
                         
                         
                          $val = $l+1;
                          $newupRow = $rowis -$val-1;
                          $newDownRow = $rowis+$val-1;
                         
                          $columnUpValue = $columnArr[$newupRow][$columnIs-1];
                         
    
                          $columnDownvalue = $columnArr[$newDownRow][$columnIs-1];
                         
    
                         
                          $colLeft = ($columnIs-1)-($l+1);
                           $columnLeftValue = $columnArr[$rowis-1][$colLeft];
                          
    
                           $colRight = ($columnIs-1)+($l+1);
                            $columnRightValue = $columnArr[$rowis-1][$colRight];
                         
    
                           if($columnUpValue === 'G' && $columnDownvalue === 'G' && $columnLeftValue === 'G' && $columnRightValue === 'G' && $columnArr[$i][$j] === 'G' && $l === 0 )
                            {
                                $flag = 1;
                                $totalValue+= 4;
                           
                             $innerArr[] =$newupRow.','.$columnIs-1;
                             $innerArr[] =$newDownRow.','.$columnIs-1;
                             $innerArr[] =($rowis-1).','.$colLeft;
                             $innerArr[] =($rowis-1).','.$colRight;
                             $innerArr[] =$i.','.$j;
    
                            }
                            else if($columnUpValue === 'G' && $columnDownvalue === 'G' && $columnLeftValue === 'G' && $columnRightValue === 'G'  && $columnArr[$i][$j] === 'G' && $flag === 1 )
                            {
                                $flag = 1;
                                $totalValue+= 4;
                            
                            $innerArr[] =$newupRow.','.$columnIs-1;
                             $innerArr[] =$newDownRow.','.$columnIs-1;
                             $innerArr[] =($rowis-1).','.$colLeft;
                             $innerArr[] =($rowis-1).','.$colRight;
                           
                           
                            }
                             else
                            {
                                break;
                            }
    
                           if(count($innerArr)>0)
                           {
                                $indexArr[] = $innerArr;
    
                           } 
    
    
                     }
                    
                    
                       if(count($indexArr)>0)
                           {
                                $allIndex[] = $indexArr;
    
                           } 
                      
                    
    
    
                }
                
            }
    
            
        }
    
    
     
        
    
    $result = array();
    foreach ($allIndex as $outer) {
        foreach ($outer as $inner) {
            $result[] = $inner;
        }
    }
        
    
        $unique = [];
    foreach ($result as $sub) {
        $temp = $sub;
        sort($temp);                
        $key = json_encode($temp);  
        $unique[$key] = $sub;    
    }
    
    
    $result = array_values($unique);
    
    
    $resultLen = count($result);
    
    $ans = 0;
    if ($resultLen == 1) {
        $ans = count($result[0]);
    }
    else
    {
        $productArray = [];
    for ($i=0; $i < $resultLen-1; $i++) { 
        
        $first = $result[$i];
        $firstLen = count($first);
        
         for ($j=$i+1; $j < $resultLen ; $j++) { 
            $second = $result[$j];
            $secondLen = count($second);
    
            $common = array_intersect($first, $second);
    
            if (!empty($common)) {
                $productArray[] = max($firstLen,$secondLen);
              
            } else {
                $secondLen = count($second);
                $productArray[] = $firstLen*$secondLen;
               
            }
    
         }
    }
    
    $ans = count($productArray) > 0 ? max($productArray) : 1;
    
    }
    
    return $ans;  
    
  • + 0 comments

    If you fail only a few cases, consider smaller size pluses with the same center, as they might not overlapse while the biggest one does. Also, you might be curious about how much pearls are worth when selecting decorative elements for your design.