We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Cavity Map
  5. Discussions

Cavity Map

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

Sort 778 Discussions, By:

recency

Please Login in order to post a comment

  • Unfettered_one
    1 day ago+ 0 comments

    Simple Python solution:

     n=len(grid)
        c=0
        for i in range(1,n-1):
            for j in range(1,n-1):
                if grid[i][j]>grid[i][j-1] and grid[i][j]>grid[i][j+1] and grid[i][j]>grid[i-1][j] and grid[i][j]>grid[i+1][j]:
                    p=list(grid[i])
                    p[j]='X'
                    grid[i]=''.join(p)
        return grid
    
    0|
    Permalink
  • eiduladh_a_wish1
    2 weeks ago+ 0 comments

    Can i use this algorithm for my car tracking app? As they can calculate the distances between things. So when i install tracker in my car it can calculate the distance between me and my car and exact distance we travel and generate a complete logs in pdf file and send to whatsapp.

    0|
    Permalink
  • chouhanabhishek6
    3 weeks ago+ 1 comment
     int left = 1;
        int right = grid[0].size() - 2;
        int top = 1;
        int bottom = grid.size() - 2;
    
        for (int i = top; i <= bottom; i++)
        {
            for (int j = left; j <= right; j++)
            {
                if (grid[i - 1][j] < grid[i][j] and grid[i + 1][j] < grid[i][j] and grid[i][j - 1] < grid[i][j] and grid[i][j + 1] < grid[i][j]){
                    // cout<<"here";
                    grid[i][j] = 'X';
                }
                
            }
        }
        return grid;
    
    0|
    Permalink
  • augustoribeiro_1
    3 weeks ago+ 0 comments
    public static List<String> cavityMap(List<String> grid) {
            
            List<String> result = new ArrayList<>();
            result.add(grid.get(0));
            
            for(int i = 1; i < grid.size() - 1; i++) {
                
                String temp = "";
                
                char topRow[] = grid.get(i - 1).toCharArray();
                char curRow[] = grid.get(i).toCharArray();
                char bottomRow[] = grid.get(i + 1).toCharArray();
                
                // border
                temp += String.valueOf(curRow[0]);
                
                for(int j = 1; j < curRow.length - 1; j++) {
                    
                    char top = topRow[j];
                    char left = curRow[j - 1];
                    char current = curRow[j];
                    char right = curRow[j + 1];
                    char bottom = bottomRow[j];
                    
                    if(current > left 
                        && current > right
                        && current > top
                        && current > bottom) {
                        
                        temp += "X";
                    } else {
                        
                        temp += String.valueOf(current);
                    }
                    
                }
                
                // border
                temp += String.valueOf(curRow[curRow.length - 1]);
                result.add(temp);
            }
            
            if(grid.size() > 1) {
                result.add(grid.get(grid.size() - 1));
            }
        
            return result;
        }
    
    0|
    Permalink
  • lokzy
    3 weeks ago+ 0 comments

    Python3:

    def cavityMap(grid):
    size = len(grid)
    list = []
    for i in range(1,size-1):
        for j in range(1,size-1):
            if all([ grid[i][j] > adj for adj in [grid[i+1][j], grid[i-1][j], grid[i][j-1], grid[i][j+1]] ]):
                grid[i]=grid[i][:j]+'X'+grid[i][j+1:]
    return grid
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy