Skip to content

Conversation

@alirz-pixel
Copy link
Member

🚀 이슈 번호

Resolve: {#2293}

🧩 문제 해결

스스로 해결:

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지: 재귀 + dp
  • 🔹 어떤 방식으로 접근했는지

투포인터 방식을 채택하여 팰린드롬을 계산한다.
s == e 인 경우, 여기서는 추가 삽입이 필요 없으므로 s + 1, e - 1로 재귀 접근
s != e 인 경우, 왼쪽에 같은 숫자를 삽입하는 경우 (s, e - 1) | 오른쪽에 같은 숫자를 삽입하는 경우 (s + 1, e)로 분류하여 재귀 접근

을 진행한다.

또한 중복 계산되는 부분이 발생할 수 있으므로 dp를 start / end까지의 연산 결과를 저장하도록 한다.

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(N ^ 2)
  • 이유:

재귀를 이용하여 s->e로 가는 케이스, e->s로 가는 케이스를 커버하고 있으므로
중복되어 계산하는 부분이 생략된다.

따라서 N^2 시간복잡도가 소요된다.

💻 구현 코드

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int recursive(vector<int>& arr, vector<vector<int>>& dp, int s, int e) {
	if (s >= e) return 0;
	if (dp[s][e]) return dp[s][e];

	if (arr[s] == arr[e])
		return recursive(arr, dp, s + 1, e - 1);

	return dp[s][e] = min(recursive(arr, dp, s + 1, e) + 1, recursive(arr, dp, s, e - 1) + 1);
}

int main() {
	int N;
	cin >> N;

	vector<int> arr(N);
	for (auto& e : arr)
		cin >> e;

	vector<vector<int>> dp(N, vector<int>(N));
	cout << recursive(arr, dp, 0, N - 1);

	return 0;
}

@alirz-pixel alirz-pixel self-assigned this Jan 9, 2026
@alirz-pixel alirz-pixel linked an issue Jan 9, 2026 that may be closed by this pull request
@alirz-pixel alirz-pixel merged commit 6276d22 into main Jan 12, 2026
1 check passed
@alirz-pixel alirz-pixel deleted the munhyeong/2293/2 branch January 12, 2026 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

260109 : 코딩테스트

2 participants