Skip to content

[WEEK05-1] 추슬기#17

Merged
doitchuu merged 2 commits intopnt-fe-study:mainfrom
doitchuu:doitchuu
Mar 11, 2026
Merged

[WEEK05-1] 추슬기#17
doitchuu merged 2 commits intopnt-fe-study:mainfrom
doitchuu:doitchuu

Conversation

@doitchuu
Copy link
Member

@doitchuu doitchuu commented Mar 8, 2026

이렇게 풀었어요

1. Reverse Linked List

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

1) 복잡도 계산

  • 시간 복잡도: O(n)
  • 공간 복잡도: O(1)

2) 접근 아이디어

  1. 입력 데이터를 한 번 순회하며 필요한 상태를 갱신했다.
  2. 조건 분기에서 정답 후보를 업데이트하거나 탐색 범위를 줄였다.
  3. 순회 종료 후 누적된 상태를 정리해 최종 결과를 반환했다.

3) 회고

포인터가 반복마다 어디를 가리키는지 추적하는 과정이 가장 어려웠다. 1 → 2 → 3 → 4 → null에서 prev = null, current = 1로 시작해 next = current.next, current.next = prev를 수행하면 1 → null과 2 → 3 → 4로 분리된다. 이 과정을 current가 null이 될 때까지 반복하면 링크 방향이 완전히 뒤집혀 4 → 3 → 2 → 1 → null이 된다. 반복 종료 시점에 prev는 새 head(4), current는 null을 가리키므로 반환값은 prev가 맞다.



2. Majority Element

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

1) 복잡도 계산

  • 시간 복잡도: O(n)
  • 공간 복잡도: O(n)

2) 접근 아이디어

  1. 문자열/배열을 한 번 순회하며 Map 또는 Set에 필요한 상태를 누적했다.
  2. 순회 중 조건을 만족하는 시점에 정답에 필요한 값을 갱신했다.
  3. 누적된 상태를 바탕으로 최종 결과를 계산해 반환했다.

3) 회고

Boyer–Moore Voting Algorithm을 새로 배웠다. 서로 다른 값을 만나면 표를 상쇄하고, cnt가 0이 되면 후보를 교체하는 방식이라 로직이 간결했다. 다수 원소가 반드시 존재한다는 조건에서 마지막 후보가 정답이 되는 이유도 명확하게 이해됐다.

다른 사람 풀이:

/**
 * @param {number[]} nums
 * @return {number}
 */
var majorityElement = function(nums) {
    let max_num = nums[0];
    let cnt = 1;

    for(let i=1 ; i<nums.length ; i++){
        if(nums[i] === max_num){
            cnt = cnt+1;
        }else{
            cnt=cnt-1;
        }

        if(cnt === 0){
            max_num = nums[i];
            cnt=1;
        }
    }
    return max_num;

};

다른 사람 풀이 핵심: 풀이 흐름을 단계별로 분리해 조건 확인과 상태 갱신을 명확히 나눈 점이 핵심이었다.
내 생각: cnt가 0이 될 때 후보를 교체하는 규칙이 단순해서 구현 실수를 줄이기 좋았다. 다음에는 해시맵 풀이와 투표 알고리즘을 같이 구현해 복잡도와 가독성을 비교해 보려고 한다.



@doitchuu doitchuu self-assigned this Mar 9, 2026
@raejun92 raejun92 requested review from raejun92 and sik9252 March 9, 2026 15:51
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.

정석의 풀이 그 자체군요!

for (let i = 0; i < nums.length; i++) {
map.set(nums[i], (map.get(nums[i]) || 0) + 1);

if (map.get(nums[i]) > nums.length / 2) {
Copy link
Collaborator

@sik9252 sik9252 Mar 9, 2026

Choose a reason for hiding this comment

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

Map으로 개수를 세면서 과반수를 넘는 순간 바로 반환하는 방식 좋은 것 같아요~

Copy link
Collaborator

Choose a reason for hiding this comment

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

prev, current, next 포인터를 사용한 정석적인 반복 풀이로 잘 구현하신 것 같습니다!

@doitchuu doitchuu merged commit 1f5a811 into pnt-fe-study: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.

4 participants