Bro, you can set up a neighbor counter helper function that looks up, down, left, and right and returns their sum. If you think about it, that's the neighbors' count. And the perimeter for every block would be 4 - neighbors count!
@CAT-hii5 күн бұрын
Hello brother, subbed. Continue this leetcode solving series, also can you try to solve in java. Peace.
@TimothyLeeGrantEngineering5 күн бұрын
Thank you for the comment and sub. I will not be stopping this leet code journey until it is accomplished! Regarding the language, which I am using, I specifically targeted using python because that is the language which I intend to use for these leet code kind of questions. Obviously Java is going to be extremely important for me to have a good grasp on and be able to use, but I think that Java language is Relegated to a different type of domain (more backend operations. That is why I am doing my projects in Java and trying to learn that language within the specific context which the Java language lives inside of.
@bladekiller276616 күн бұрын
Wait until you try Codeforces, LC is way easier than Codeforces.
@TimothyLeeGrantEngineering16 күн бұрын
If Leetcode is easy compared to Codeforces, then I can’t even imagine what Codeforces would be like! Is Codeforces the one you normally do?
@bladekiller276616 күн бұрын
@TimothyLeeGrantEngineering I did in my high school years, now pretty rare, but it's way harder, Leetcode Hard is medium difficulty there. And you also have divisions, div3,4 are Leetcode level difficulty for begineers, div2 is the standard, and div1 are competing only the best competitive programmers in the world. Leetcode problems are quite straightforward and algorithmic, Codeforces are more creative puzzles that happen to have computational solution. BTW, you can solve the problem just going through each 1 cell and checking adjacent cells, if adj is out of grid or it's 0 you add it to the perimeter, it's another way of just checking the edges for each cell.
@CAT-hii5 күн бұрын
@@TimothyLeeGrantEngineering Codeforces lower rating questions = Leetcode harder version problems. I can't even solve one in code forces.
@samdvc273522 күн бұрын
Hey bro do you do competitive programming also
@TimothyLeeGrantEngineering19 күн бұрын
I haven’t done anything like that. I just started practicing leetcode a few months ago.
@zach660721 күн бұрын
Hey, you were super close!! For those wondering what piece of logic that Copilot added which OP did not add was, on around line 24 - 25, when visiting a node it checked while stack: i, j = stack.pop() # region Visit Check if (i, j) in visited: continue # end region I will try to post the full code soon
@zach660721 күн бұрын
KZbin is not allowing me to attach links to my leetcode solution based on OPs approach class Solution: def islandPerimeter(self, grid: List[List[int]]) -> int: if not grid: return 0 start: Coordinate = find_island(grid) if start is None: return 0 stack: List[Coordinate] = [] visited: set = set() stack.append(start) perim = 0 MAX_NEIGHBORS = 4 while stack: curr: Coordinate = stack.pop() visited.add(curr) neighbors: List[Coordinate] = get_neighbors(grid, curr) perim += MAX_NEIGHBORS - len(neighbors) unvisited: List[Coordinate] = list(filter(lambda n: n not in visited, neighbors)) visited.update(unvisited) stack += unvisited return perim class Coordinate(object): def __init__(self, row: int, col: int): self.row: int = row self.col: int = col def __eq__(self, other): if isinstance(other, Coordinate): return self.row == other.row and self.col == other.col return false def __hash__(self): return hash((self.row, self.col)) def __str__(self): return f'({self.row},{self.col})' def __repr__(self): return f'({self.row},{self.col})' def get_neighbors(grid: List[List[int]], curr: Coordinate) -> List[Coordinate]: neighbors: List[Coordinate] = [] row = curr.row col = curr.col land = 1 # check up if row is not 0 and grid[row - 1][col] is land: neighbors.append(Coordinate(row - 1, col)) # check down if row is not len(grid) - 1 and grid[row + 1][col] is land: neighbors.append(Coordinate(row + 1, col)) # check right if col is not 0 and grid[row][col - 1] is land: neighbors.append(Coordinate(row, col - 1)) # check left if col is not len(grid[row]) - 1 and grid[row][col + 1] is land: neighbors.append(Coordinate(row, col + 1)) return neighbors def find_island(grid: List[List[int]]) -> Coordinate: for row in range(len(grid)): for col in range(len(grid[row])): if grid[row][col] == 1: return Coordinate(row, col) return None
@zach660721 күн бұрын
I see OP made another video where they found this line! Love to see that you didn't get discouraged! Failing is part of learning in this field