-
-
Notifications
You must be signed in to change notification settings - Fork 246
[hyer0705] WEEK 07 solutions #1879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- using recursive
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코드가 예쁩니다! ===
를 잘 쓰시는데 혹시 ==
를 써야 하는 때도 만나보셨는지 궁금해요!
maxLen = Math.max(maxLen, windowEnd - windowStart + 1); | ||
} | ||
|
||
return maxLen; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
첨언할 게 없게 잘 짜셔서 이거라도 써봅니다.
maxLen
을 0
대신 -1
로 초기화하면 다음과 같이 덧셈 연산 반복을 줄일 수 있겠어요:
maxLen = Math.max(maxLen, windowEnd - windowStart + 1); | |
} | |
return maxLen; | |
maxLen = Math.max(maxLen, windowEnd - windowStart); | |
} | |
return maxLen + 1; |
while (queue.length > 0) { | ||
const [cx, cy] = queue.shift()!; |
There was a problem hiding this comment.
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)
이 되는 일을 피할 수 있지 않을까요?
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
There was a problem hiding this comment.
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()
메서드를 사용했는데 시간 초과가 나지 않고 정답인 이유가 궁금한게 맞을까요..? 저도 이유는 모르겠지만 테스트케이스가 시간 초과가 되지 않는 정도만 있는게 아닐까요...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오.. 그랬군요. 반대로 Bracket으로 Property access 할 때는 undefined
가 아니라 !
이 필요 없을 수 있겠네요. 감사합니다!
질문 해주셨을 때, 동등 연산자를 써야 하는 경우가 생각나지 않는 걸로 봐서 아직은 만나보지 못한 것 같습니다. 동등 연산자( 저는 주로 일치 연산자( |
답안 제출 문제
작성자 체크 리스트
In Review
로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!