베어_
TechBear
베어_
전체 방문자
오늘
어제
  • 분류 전체보기 (336)
    • Spring (33)
      • 개념 (13)
      • Security (5)
      • 실습 (1)
      • 토비 스프링 (11)
    • JPA (6)
    • 프로젝트 기록 (24)
    • DB (13)
    • JAVA (18)
    • 알고리즘 (50)
      • 유형정리 (8)
      • Baekjoon (21)
      • LeetCode (18)
    • 디자인패턴 (0)
    • 개발서적 (79)
      • Effective Java (78)
      • 객체지향의 사실과 오해 (1)
    • 독후감 (4)
    • 보안 (2)
    • 운영체제(OS) (53)
      • 공룡책 (53)
    • 컴퓨터 네트워크 (28)
      • 컴퓨터 네트워크 하향식 접근 (23)
    • 자료구조 (1)
    • DevOps (2)
    • 앱 개발 (20)
      • 안드로이드 스튜디오 (20)

블로그 메뉴

    공지사항

    인기 글

    태그

    • 자바8
    • 스프링
    • 데이터베이스
    • java
    • 스프링시큐리티
    • dfs
    • 이펙티브자바
    • BFS
    • 토비스프링
    • 코드업
    • 자바
    • 알고리즘
    • 운영체제
    • C++
    • leetcode
    • 스레드
    • 백준
    • jpa
    • Spring
    • 함수형인터페이스

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    베어_

    TechBear

    JAVA

    [LeetCode] Pacific Atlantic Water Flow (Java)

    2023. 7. 27. 21:13

    ✉️문제

    https://leetcode.com/problems/pacific-atlantic-water-flow/

     

    Pacific Atlantic Water Flow - LeetCode

    Can you solve this real interview question? Pacific Atlantic Water Flow - There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches

    leetcode.com

     

    🗝 문제풀이

    class Solution {
    
        final int[] dr = { -1, 0, 0, 1}, dc = {0, -1, 1, 0};
        final int DIRECTION = 4;
    
        public List<List<Integer>> pacificAtlantic(int[][] heights) {
            int m = heights.length;
            int n = heights[0].length;
            System.out.println(m + " : " + n);
            boolean[][] pacific = new boolean[m][n];
            boolean[][] atlantic = new boolean[m][n];
    
            for(int i = 0; i < m; i++) {
                DFS(heights, pacific, i, 0);
                DFS(heights, atlantic, i, n - 1);
            }
    
            for(int i = 0; i < n; i++) {
                DFS(heights, pacific, 0, i);
                DFS(heights, atlantic, m - 1, i);
            }
    
            List<List<Integer>> answer = new ArrayList<>();
            for(int i = 0; i < m; i++) {
                for(int j = 0; j < n; j++) {
                    if(pacific[i][j] && atlantic[i][j]) answer.add(List.of(i, j));
                }
            }
    
            return answer;
        }
    
        public void DFS(int[][] heights, boolean[][] mem, int r, int c) {
            
            mem[r][c] = true;
    
            for(int i = 0; i < DIRECTION; i++) {
                int nr = r + dr[i];
                int nc = c + dc[i];
                if(nr >= 0 && nr < heights.length && nc >= 0 && nc < heights[0].length && !mem[nr][nc]) {
                    if(heights[r][c] <= heights[nr][nc])
                        DFS(heights, mem, nr, nc);
                }
            }
        }
    
    }
    저작자표시 비영리 변경금지 (새창열림)
      'JAVA' 카테고리의 다른 글
      • [LeetCode] Longest Repeating Character Replacement (Java)
      • [Java] 람다와 스트림
      • [Java] Volatile 키워드
      • [Java] 쓰레드의 동기화
      베어_
      베어_
      Today I learned | 문제를 해결하는 개발자

      티스토리툴바