Skip to content

Conversation

hyer0705
Copy link
Contributor

@hyer0705 hyer0705 commented Sep 1, 2025

답안 제출 문제

작성자 체크 리스트

  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 StatusIn Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

검토자 체크 리스트

Important

본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!

  • 바로 이전에 올라온 PR에 본인을 코드 리뷰어로 추가해주세요.
  • 본인이 검토해야하는 PR의 답안 코드에 피드백을 주세요.
  • 토요일 전까지 PR을 병합할 수 있도록 승인해주세요.

- using recursive
@yhkee0404 yhkee0404 self-requested a review September 1, 2025 11:15
@hyer0705 hyer0705 moved this from Solving to In Review in 리트코드 스터디 5기 Sep 3, 2025
Copy link
Contributor

@yhkee0404 yhkee0404 left a comment

Choose a reason for hiding this comment

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

코드가 예쁩니다! ===를 잘 쓰시는데 혹시 ==를 써야 하는 때도 만나보셨는지 궁금해요!

Comment on lines +15 to +18
maxLen = Math.max(maxLen, windowEnd - windowStart + 1);
}

return maxLen;
Copy link
Contributor

Choose a reason for hiding this comment

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

첨언할 게 없게 잘 짜셔서 이거라도 써봅니다.
maxLen0 대신 -1로 초기화하면 다음과 같이 덧셈 연산 반복을 줄일 수 있겠어요:

Suggested change
maxLen = Math.max(maxLen, windowEnd - windowStart + 1);
}
return maxLen;
maxLen = Math.max(maxLen, windowEnd - windowStart);
}
return maxLen + 1;

Comment on lines +26 to +27
while (queue.length > 0) {
const [cx, cy] = queue.shift()!;
Copy link
Contributor

Choose a reason for hiding this comment

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

Array.prototype.shift()의 시간 복잡도는 선형 O(n)이 아닌가요?
그래서 다음과 같이 고쳐 주셔야 시간 복잡도가 O(n^2)이 되는 일을 피할 수 있지 않을까요?

Suggested change
while (queue.length > 0) {
const [cx, cy] = queue.shift()!;
let head = 0;
while (head !== queue.length) {
const [cx, cy] = queue[head++];

그리고 혹시 shift!는 어떤 효과가 있나요?
그런데 shift로 시간이 초과하기는커녕 거의 차이가 없는 것이 매우 신기해요.
이유를 아시는 분이 계시다면 알려 주세요!
제가 브라우저에서 실험해 보니 이렇게 나와요.

const n = 100000;

let arr;
let start, end;
let x;

arr = Array(n).fill(1);
x = 0;
start = Date.now();
for (let i = 0; i < n; i++) {
    x += arr.shift();
}
end = Date.now();
console.log('shift:', end - start, 'ms, x:', x);

arr = Array(n).fill(1);
y = 0;
start = Date.now();
let front = 0;
for (let i = 0; i < n; i++) {
    y += arr[front++]
}
end = Date.now();
console.log('front:', end - start, 'ms, y:', y);

// shift: 3273 ms, x: 100000
// front: 2 ms, y: 100000

Copy link
Contributor Author

@hyer0705 hyer0705 Sep 6, 2025

Choose a reason for hiding this comment

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

긴 코멘트 감사합니다! 제 코드에서 개선할 점도 알게 되고 저도 고민해볼 거리가 생긴거 같네요! 질문 남기신 것 중에 제가 답변해드릴 수 있는 부분은 아래와 같이 정리를 해봤습니다.

평소에 그냥 Array.prototype.shift() 메서드를 쓰는거에 익숙해서 일단은 풀어서 정답이 나오면 바로 넘어가서 이런 코드가 남겨진거 같네요! 다음엔 말씀해주신 방법을 사용해서 문제를 풀이해보도록 하겠습니다.

shift() 뒤에 ! 연산자의 경우, TypeScript에서 Non Null Assertion 연산자로 불리는 연산자로 해당 값은 undefined 혹은 null 값이 아니다 라는 걸 단언하는 연산자입니다. Array.prototype.shift() 메서드는 해당 메서드를 호출한 배열이 비어있는경우 undefined를 반환합니다. 저는 cx, cy에 undefined 값이 들어가지 않는다는 것을 단언하기 위해 해당 연산자를 사용했습니다. IDE에서는 해당 연산자를 쓰지 않으면 cx, cy에서 빨간 밑줄이 표시되거든요!
Non-null Assertion Operator (Postfix !) | TypeScript Documentation

shift() 메서드를 사용했는데 시간 초과가 나지 않고 정답인 이유가 궁금한게 맞을까요..? 저도 이유는 모르겠지만 테스트케이스가 시간 초과가 되지 않는 정도만 있는게 아닐까요...?

Copy link
Contributor

@yhkee0404 yhkee0404 Sep 6, 2025

Choose a reason for hiding this comment

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

오.. 그랬군요. 반대로 Bracket으로 Property access 할 때는 undefined가 아니라 !이 필요 없을 수 있겠네요. 감사합니다!

@hyer0705
Copy link
Contributor Author

hyer0705 commented Sep 6, 2025

코드가 예쁩니다! ===를 잘 쓰시는데 혹시 ==를 써야 하는 때도 만나보셨는지 궁금해요!
@yhkee0404

질문 해주셨을 때, 동등 연산자를 써야 하는 경우가 생각나지 않는 걸로 봐서 아직은 만나보지 못한 것 같습니다.

동등 연산자(==)를 쓴다고 한다면 타입 변환을 해서 값을 비교해야하는 경우에 쓸 것 같습니다. '1' == 1 이런식으로 문자열 이지만 숫자 형태인 값과 숫자를 비교해야할 경우에 쓸 것 같아요.

저는 주로 일치 연산자(===)를 쓰는 편인데 그러는 이유가 같은 타입의 값을 비교하는 걸 더 선호 하기 때문입니다. 일치 연산자는 타입 변환을 하지 않고 비교하기에 타입이 같지 않으면 false를 반환하는 거로 알고 있는데 같은 타입의 값을 비교해야 덜 혼란스럽고 값의 오류를 찾을 때 더 편하지 않을까 하는 생각이 들어서요!

@hyer0705 hyer0705 merged commit bc5b926 into DaleStudy:main Sep 6, 2025
1 check passed
@github-project-automation github-project-automation bot moved this from In Review to Completed in 리트코드 스터디 5기 Sep 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

2 participants