Skip to content

[정민서-15주차 알고리즘 스터디] #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 50 commits into
base: minseojeong/week15
Choose a base branch
from

Conversation

minseojeong1012
Copy link
Contributor

🚀 싸피 15반 알고리즘 스터디 15주차 [정민서]

📌 문제 풀이 개요

  • 이번 PR에서는 다음 5문제의 풀이를 포함합니다.
  • 각 문제에 대한 풀이 과정과 접근 방식을 설명합니다.

✅ 문제 해결 여부

  • 문제 1
  • 문제 2
  • 문제 3
  • 문제 4
  • 문제 5

💡 풀이 방법

문제 1: 점프왕 쩰리 (Small)

문제 난이도
실버 4

문제 유형
구현

접근 방식 및 풀이

배열에서 -1을 만나면 도달 가능한 것이므로 즉시 탐색을 종료하는 방식으로 dfs 진행했습니다

    static void dfs(int x, int y) {
        if (x < 0 || y < 0 || x >= n || y >= n || visited[x][y] || canReach) {
            return;
        }

        if (map[x][y] == -1) {
            canReach = true;
            return;
        }

        visited[x][y] = true;
        int jump = map[x][y];

        dfs(x + jump, y); // 아래로 점프
        dfs(x, y + jump); // 오른쪽으로 점프
    }

문제 2: 빗물

문제 유형
구현

문제 난이도
골드 5

접근 방식 및 풀이

현재 위치에서 왼쪽과 오른쪽의 최대 높이를 각각 구합니다.

두 최대 높이 중 작은 값을 찾고, 만약 작은 값이 현재 위치의 높이보다 크다면, 이 차이만큼 물이 고입니다.

모든 위치를 탐색한 뒤, 고인 물의 총합을 출력하는 방식으로 진행했습니다

        // 각 위치에서 좌우 최대 높이 찾기
        for(int i = 1; i < W - 1; i++) {
            int leftMax = 0;
            int rightMax = 0;

            // 왼쪽에서 최대 높이 찾기
            for(int j = 0; j < i; j++) {
                leftMax = Math.max(leftMax, blocks[j]);
            }

            // 오른쪽에서 최대 높이 찾기
            for(int j = i + 1; j < W; j++) {
                rightMax = Math.max(rightMax, blocks[j]);
            }

            int minHeight = Math.min(leftMax, rightMax);

            if(minHeight > blocks[i]) {
                total += minHeight - blocks[i];
            }
        }

        System.out.println(total);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant