Skip to content

[이준희-11주차 알고리즘 스터디] #14

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 16 commits into
base: junhee325/week11
Choose a base branch
from

Conversation

JHLEE325
Copy link
Contributor

@JHLEE325 JHLEE325 commented Apr 4, 2025

🚀 싸피 15반 알고리즘 스터디 10주차 [이준희]

📌 문제 풀이 개요

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

✅ 문제 해결 여부

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

💡 풀이 방법

문제 1: 알고스팟

문제 난이도
골드 4

문제 유형
그래프

접근 방식 및 풀이
다익스트라를 이용하는 문제였습니다. 벽은 가중치가 있고 벽이 아닌경우 0가중치로 계산했습니다.

static void dijkstra() { // 우선순위큐 활용한 다익스트라 사용
		cmap[0][0] = map[0][0];
		PriorityQueue<Node> pq = new PriorityQueue<>();
		pq.add(new Node(0, 0, cmap[0][0]));
		while (!pq.isEmpty()) {
			Node cur = pq.poll();
			if (visited[cur.y][cur.x])
				continue;
			visited[cur.y][cur.x] = true;

			for (int i = 0; i < 4; i++) {
				int dy = cur.y + dir[i][0];
				int dx = cur.x + dir[i][1];

				if (dy >= 0 && dy < n && dx >= 0 && dx < m) { // 4방탐색 하면서 값 갱신
					if (cmap[dy][dx] > cmap[cur.y][cur.x] + map[dy][dx]) {
						cmap[dy][dx] = cmap[cur.y][cur.x] + map[dy][dx];
						pq.add(new Node(dy, dx, cmap[dy][dx]));
					}
				}
			}
		}
	}

문제 2: 거울설치

문제 난이도
골드 3

문제 유형
그래프

접근 방식 및 풀이
못풀었습니다. 추후 학습하여 풀이하겠습니다.


문제 3: 2×n 타일링 2

문제 난이도
실버 3

문제 유형
DP

접근 방식 및 풀이
타일링 DP 문제였습니다. 규칙을 찾아서 풀이했습니다.

arr[1] = 1; // 첫번째 경우
		arr[2] = 3; // 두번째 경우

		for (int i = 3; i <= n; i++) {
			arr[i] = (arr[i - 1] + 2 * arr[i - 2]) % 10007; // 값이 크기 때문에 모듈러 연산
		}

문제 4: LCS

문제 난이도
골드 5

문제 유형
DP

접근 방식 및 풀이
마찬가지로 DP 중 대표격인 LCS의 문제였습니다. 최장공통부분수열의 점화식을 학습하고 이를 구현했습니다.

public static int lcs(String A, String B) {
        int n = A.length();
        int m = B.length();

        int[][] dp = new int[n + 1][m + 1]; // 각 문자열 길이로 2차원 배열 만듬

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) { // 전부 탐색하면서
                if (A.charAt(i - 1) == B.charAt(j - 1)) { // 같은 문자를 발견하면
                    dp[i][j] = dp[i - 1][j - 1] + 1; // 대각선 위의 경우에서 + 1
                } else { // 문자가 같지 않은 경우
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); // 이전것들 중 큰것 선택
                }
            }
        }
        
        return dp[n][m];
    }

문제 5 : 평범한 배낭

문제 난이도
골드 5

문제 유형
DP

접근 방식 및 풀이
냅색문제였습니다. 가방 DP 점화식을 학습한 후 구현하였습니다.

//dp[i][w] = i번째 물건까지 고려했을 때 w무게에서의 가치
		int[][] dp = new int[N + 1][K + 1];

		for (int i = 1; i <= N; i++) {
			for (int w = 0; w <= K; w++) {
				if (weight[i - 1] > w) { // 물품이 제한보다 무거운 경우
					dp[i][w] = dp[i - 1][w];
				} else { // 아닌 경우 넣거나 안넣거나 해서 값 계산
					dp[i][w] = Math.max(dp[i - 1][w], dp[i - 1][w - weight[i - 1]] + value[i - 1]);
				}
			}
		}

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