Skip to content

Conversation

@memeseo
Copy link

@memeseo memeseo commented Jul 22, 2024

배열과 연결리스트를 직접 구현해보면서 비교해보니 이해가 더 잘되네요!
문제를 먼저 다 풀어보고 과제풀이 보면서 수정했습니다.

Comment on lines +1 to 9
const solution = (numbers = []) => {
if(numbers.length === 0) return 0;

const total = numbers.reduce((acc, curr) => acc + curr, 0),
average = total / numbers.length;

return Math.floor(average);

};
Copy link
Contributor

Choose a reason for hiding this comment

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

이 문제가 Bag 자료구조를 사용해서 문제를 해결하는 것이 의도였습니다.


if(stack.isEmpty()) return false;

const savedBrackets = stack.pop();
Copy link
Contributor

Choose a reason for hiding this comment

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

�스택에서 가장 위에 요소를 하나 pop해서 현재 괄호의 짝과 같은지 비교하고 있네요. 변수 이름이 복수라서 마치 배열이나 리스트를 가리킬것 처럼 보이네요. 하나를 나타낼때는 savedBracket이라고만 해도 좋습니다.

}
}

return true;
Copy link
Contributor

Choose a reason for hiding this comment

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

검사를 모두 통과한 경우 true를 반환하고 있습니다. 만약 이런 경우의 인풋이 들어온 경우는 어떻게 될까요?

"()("

Copy link
Author

Choose a reason for hiding this comment

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

false를 반환해야 하는데 true를 반환하네요 ㅠ

Comment on lines +25 to +27
let index = 0,
stack = [...this.stack];
stack = stack.reverse();
Copy link
Contributor

Choose a reason for hiding this comment

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

reverse메서드가 원본 배열을 수정하다보니, 스택에 있는 값들을 복사한 후 reverse를 실행하셨군요.
toReversed를 사용하면 원본 배열을 수정하지 않고 순서를 바꿀 수 있습니다.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed

Comment on lines +25 to +29
setNumbers(number){
for(let i = 1; i <= number; i++){
this.enqueue(i);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Queue자료구조에 메서드를 추가하셨네요. 1주차에서 배운 추상 데이터 타입을 떠올리면서, 이 문제를 위한 추상 데이터 타입을 만들어 볼 수도 있을 것 같습니다.

class People {
  constructor(peopleCount) {
    this.queue = new Queue();
    for(let i = 1; i <= peopleCount; i++){
      this.queue.enqueue(i);
    }
  }

  lastLivePositionOf(M) {
    while (this.queue.size() > 1) {
      for (let index = 0; index < M - 1; index += 1) {
        const dequeuedNumber = this.queue.dequeue();
        this.queue.enqueue(dequeuedNumber);
      }
      this.queue.dequeue();
    }
  
    return this.queue.dequeue();
  }
}

const solution = (N, M) => {
  const people = new People(N);
  return people.lastLivePositionOf(M);
};

이렇게 하면 M번째 사람을 없앨 때 가장 오래 살아남는 사람을 구할 때 내부적으로 어떤 자료구조를 사용하는지 드러나지 않습니다.

도메인으로 기술된 내용이 많이 없어서 표현력을 좋게 못 끌어올렸는데, 최대한 표현해봤습니다

Copy link
Contributor

Choose a reason for hiding this comment

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

이런.. 문제를 다시 읽어보니 결국 마지막 사람도 죽게 되는 거군요. 최대한 오래 살아남을 수 있는 위치를 알고 싶은 거니까 longLivePosition도 괜찮겠네요. (결국 죽지만)

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