Skip to content

[WEEK 04-2] 추슬기#13

Merged
doitchuu merged 2 commits intomainfrom
doitchuu
Mar 6, 2026
Merged

[WEEK 04-2] 추슬기#13
doitchuu merged 2 commits intomainfrom
doitchuu

Conversation

@doitchuu
Copy link
Member

@doitchuu doitchuu commented Mar 3, 2026

이렇게 풀었어요

1. Climbing Stairs

  • 문제를 풀었어요.
  • 풀이 시간 : 4분

1) 복잡도 계산

  • 시간 복잡도: O(n)
    → 한 번의 반복문으로 dp 배열 채움
  • 공간 복잡도: O(n)
    → dp 배열 사용

2) 접근 아이디어

  • n번째 계단에 도달하는 방법은
    (n-1번째에서 1칸 올라오기) + (n-2번째에서 2칸 올라오기)

3) 회고

이 문제는 대표적인 DP(피보나치 유형) 문제라 DP로 풀었다..
이해보다 외운 느낌 ㅠ



2. Longest Palindrome

  • 문제를 풀었어요.
  • 풀이 시간 : 19분

1) 복잡도 계산

  • 시간 복잡도: O(n)
    → 문자열 한 번 순회 + map 순회
  • 공간 복잡도: O(k)
    → 서로 다른 문자 개수만큼 Map 사용

2) 접근 아이디어

팰린드롬의 구조는 다음과 같다:

  • 짝수 개 문자는 모두 사용 가능
  • 홀수 개 문자는 하나를 제외하고 사용 가능
  • 홀수 문자가 하나라도 있으면 가운데에 1개 추가 가능

3) 회고

처음에는 홀수 처리 로직이 헷갈렸는데
짝수 부분만 쓰고, 홀수는 하나만 중앙에 가능하다는
Palidrome 구조를 이해하니 명확해졌다.

for (const [key, value] of map) 대신
for (const value of map.values())를 쓰면 더 간결했을 것 같다.

다른 사람 풀이에서는 다음처럼 Math.floor(count / 2) * 2를 활용해 짝수 부분만 계산했다.

var longestPalindrome = function(s) {
  const map = new Map()

  for (const char of s) {
    map.set(char, (map.get(char) || 0) + 1)
  }

  let length = 0
  let hasOdd = false

  for (const count of map.values()) {
    length += Math.floor(count / 2) * 2
    if (count % 2 === 1) hasOdd = true
  }

  if (hasOdd) length += 1

  return length
};

 방식이  수학적으로 명확해 보인다.

Copy link
Collaborator

@raejun92 raejun92 left a comment

Choose a reason for hiding this comment

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

고생하셨습니다ㅎ

Copy link
Collaborator

Choose a reason for hiding this comment

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

오우 완전 깔끔하네요

Copy link
Collaborator

Choose a reason for hiding this comment

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

오우 저랑 생각이 같으셨군요!

* @return {number}
*/
var climbStairs = function(n) {
const dp = [1, 2];
Copy link
Collaborator

Choose a reason for hiding this comment

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

오옷 이렇게 바로 집어넣을수도 있는데 전 생각 못하고 Array 생성하고 하나씩 넣었네요 알아갑니당!!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Map으로 문자 빈도수를 세고, 짝수는 모두 사용 / 홀수는 하나 제외하고 사용한 뒤 마지막에 center 여부만 한 번 처리한 점이 좋은 것 같습니다 ㅎㅎ 다만 for (const [key, value] of map)에서 key를 사용하지 않으니 map.values()로 순회하면 조금 더 깔끔할 것 같아요!

@doitchuu doitchuu merged commit 35c8d86 into main Mar 6, 2026
1 check passed
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.

3 participants