Skip to content

[서인] 10-3. 최대부분증가수열(LIS) #263

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 8 commits into
base: main
Choose a base branch
from

Conversation

Seoin02
Copy link
Contributor

@Seoin02 Seoin02 commented Sep 27, 2024

✔ 문제 제목: 최대부분증가수열

✔ 문제 유형: DP

✔ 문제 풀이 날짜: 2024.09.28

💡 문제 분석 요약

  • n개의 자연수로 이뤄진 수열에서 가장 길게 증가하는 원소들의 부분집합 길이를 구한다.

💡 알고리즘 설계

  • 인접한 두 요소를 비교하고, 둘 중 인덱스가 큰 요소가 더 커야한다.
  • 인덱스가 더 큰 요소를 부분집합에 넣었을 때 기존 부분집합과 비교해서 더 큰 값으로 갱신한다.

💡코드

function solution(arr) {
  let dp = Array(arr.length).fill(1);
  let answer = 0;

  for (let i = 1; i < arr.length; i++) {
    for (let j = 0; j < i; j++) {
      if (arr[i] > arr[j]) {
        dp[i] = Math.max(dp[i], dp[j] + 1);
      }
    }
    answer = Math.max(answer, dp[i])
  }
  
  return answer;
}

let arr = [5, 3, 7, 8, 6, 2, 9, 4];
console.log(solution(arr));

💡 시간복잡도

O(n^2)

💡 틀린 이유와 틀린 부분 수정 or 다른 풀이

  • 인접한 인덱스를 비교하는 것까진 생각했는데, 부분집합의 길이가 더 긴 값으로 갱신하는 것은 떠올리지 못했다.
// 다른 풀이를 작성해주세요 !

💡 느낀 점 or 기억할 정보

  • 일주일만입니다^0^;;;

Copy link
Contributor

@devwqc devwqc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋아요~ ⭐

간결하게 잘 푼 거 같습니다!!
좋았어요 👍💯👍💯👍💯👍💯👍💯👍💯👍💯👍💯👍💯👍💯👍💯👍💯

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.

2 participants