Skip to content

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

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/week11
Choose a base branch
from

Conversation

minseojeong1012
Copy link
Contributor

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

📌 문제 풀이 개요

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

✅ 문제 해결 여부

  • 문제 1 : 알고스팟
  • 문제 2 : 거울 설치
  • 문제 3 : 2xn 타일링2
  • 문제 4 : LCS
  • 문제 5 : 평범한 배낭

💡 풀이 방법

문제 1: 알고스팟

문제 난이도
골드 4

문제 유형
다익스트

접근 방식 및 풀이

우선순위 큐를 활용한 다익스트라 알고리즘을 사용해 문제를 해결했습니다.

벽으로는 이동할 수 없기에 조건을 주고, 운영진이 모두 같은 방에 있어야 한다는 조건 아래 구현해나갔습니다.
2차원 배열을 사용해서 거리를 관리해주었습니다.

		while (!pQueue.isEmpty()) {
			
			Node next = pQueue.poll();
			
			if (distance[next.x][next.y] < next.w) continue;
			
			for (int i = 0; i < 4; i++) {
				int nx = next.x + dx[i];
				int ny = next.y + dy[i];
				
				if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
				
				if (distance[nx][ny] > distance[next.x][next.y] + map[nx][ny] ) {
					
					
					distance[nx][ny] = distance[next.x][next.y] + map[nx][ny];
					pQueue.add(new Node(nx, ny, distance[nx][ny]));
					
					
				}
			}
		}

문제 2: 2×n 타일링 2

문제 유형
DP

문제 난이도
실버 3

접근 방식 및 풀이

DP의 가장 핵심적인 내용이라고 생각합니다.

끝에 세로 타일 하나 추가될 경우, 끝에 가로 타일 2개가 추가될 경우, 2X2 타일이 추가될 경우를 고려하면

arr[i - 1] + 2 * arr[i - 2])

이런 점화식이 도출됩니다

        //n이 1 일 경우
        if (n >= 2) {
            arr[2] = 3;
        }

        for (int i = 3; i <= n; i++) {

            arr[i] = (arr[i - 1] + 2 * arr[i - 2]) % 10007;
        }

문제 3: LCS

문제 유형
DP

문제 난이도
골드 5

접근 방식 및 풀이

문자가 같으면

dp[i][j] = dp[i-1][j-1] + 1

문자가 다르면

dp[i][j] = max(dp[i-1][j], dp[i][j-1])

이런방식으로 반복문을 돌렸습니다

마지막으로 배열에 있는 최종값을 꺼내 출력했습니다

         for (int i = 1; i <= line1.length(); i++) {
            for (int j = 1; j <= line2.length(); j++) {
                if (line1.charAt(i - 1) == line2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }

문제 4: 평범한 배낭

문제 유형
DP

문제 난이도
골드 5

접근 방식 및 풀이

물건 i 안 넣는다
→ dp[i][w] = dp[i-1][w]

물건 i 넣는다 (단, 무게 여유가 있을 때)
→ dp[i][w] = dp[i-1][w - weight[i]] + value[i]

이렇게 두개의 식을 구성하고 두 경우의 수 중 MAX 값을 구해야하기 때문에 MAX 연산을 추가하였습니다.
dp[i][w] = max(dp[i-1][w], dp[i-1][w - weight[i]] + value[i])

        for (int i = 1; i <= n; i++) {
            for (int w = 0; w <= k; w++) {
                if (w >= weight[i]) {
                    dp[i][w] = Math.max(dp[i - 1][w], dp[i - 1][w - weight[i]] + value[i]);
                } else {
                    dp[i][w] = dp[i - 1][w];
                }
            }
        }

문제 5:

문제 유형

문제 난이도

접근 방식 및 풀이

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