Skip to content

[WEEK05-1] 이지현#16

Merged
sik9252 merged 2 commits intomainfrom
sik9252
Mar 11, 2026
Merged

[WEEK05-1] 이지현#16
sik9252 merged 2 commits intomainfrom
sik9252

Conversation

@sik9252
Copy link
Collaborator

@sik9252 sik9252 commented Mar 8, 2026

이렇게 풀었어요

1. Reverse Linked List

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

1) 복잡도 계산

시간 복잡도: O(n)

공간 복잡도: O(1)


2) 접근 아이디어

단방향 연결 리스트의 next 방향을 모두 반대로 바꾸는 문제다.
각 노드의 포인터를 반대로 돌려주면 될 것 같다.

Linked List는 보통 이런 구조를 가지고 있는데,

node {
  val
  next
}

이 구조에 의해,

1.next = 2
2.next = 3
3.next = null

이런 형태로 존재하게 된다. 이것을 뒤집어서

1.next = null
2.next = 1
3.next = 2

이렇게 표현되도록 하면 될 것 같다.

temp, curr을 선언해서 swap 하는 문제처럼 적용하면 될 것 같다.


3) 회고

저번에 Linked List 문제가 몇 번 나왔던 것 같은데 그거 아니였음 못풀었을 것 같다. Linked List를 뒤집는 과정에서 prev, curr, next 포인터를 어떻게 이동시키는지가 핵심이라는 것을 이해할 수 있었고, 포인터를 활용한 리스트 조작에 익숙해질 수 있었다.


2. Majority Element

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

1) 복잡도 계산

시간 복잡도: O(log n)

공간 복잡도: O(log n)


2) 접근 아이디어

오름차순 정렬하면 majority element는 항상 가운데 위치하게되는 특성이 있음을 활용했다.


3) 회고

다른 방식으로 풀었지만 공간복잡도를 O(1)로 풀 수 있는 방법이 있냐는걸 묻는걸 보아 사실 이 문제는 Boyer-Moore Voting Algorithm를 사용하는 풀이를 연습하도록 낸 문제인듯 하다. 이전에 백준 문제에서 풀어봤던 기억이 있는거 같은데 다시 새겨두어야겠다.

Boyer-Moore Voting Algorithm의 핵심 로직은 아래와 같다.

같은 숫자면 +1
다른 숫자면 -1
count가 0이면 후보 교체

majority element는 다른 모든 숫자보다 많기 때문에 가능한 현상이다.

var majorityElement = function (nums) {
  let candidate = null;
  let count = 0;

  for (const num of nums) {
    if (count === 0) {
      candidate = num;
    }

    count += num === candidate ? 1 : -1;
  }

  return candidate;
};

3) 회고

알고리즘 문제 해결법에는 다양한 방법이 있고, 사람들마다 다양하게 푸는 것도 신기하다.
같은 문제라도 HashMap, 정렬, Boyer-Moore 등 다양한 풀이 방법이 있다는 점이 인상적이었고, 특히 공간 복잡도를 줄일 수 있는 Boyer-Moore 알고리즘을 알게 된 것이 좋았다.

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.

LGTM!

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 Author

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
Member

@doitchuu doitchuu left a comment

Choose a reason for hiding this comment

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

MajorityElement 풀이 신박하고 좋네요 👍 고생하셨습니다 :)

var majorityElement = function (nums) {
nums.sort((a, b) => a - b);
return nums[Math.floor(nums.length / 2)];
};
Copy link
Member

Choose a reason for hiding this comment

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

오 이렇게도 풀 수 있네요 👍 신박하다

@sik9252 sik9252 merged commit 8b9421c into main Mar 11, 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