-
Notifications
You must be signed in to change notification settings - Fork 125
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
[bus710] week 05 #872
[bus710] week 05 #872
Conversation
제일 쉬운 한 문제만 푸는지라, 아무때나 바로 리뷰 부탁 드립니다. 복잡도에 대해... 처음에는 brutal하게 풀었다가 leetcode의 검사에서 시간 초과가 떠서 고민을 조금 했었는데, 루프를 한번만 돌고도 도출할 수 있었습니다. 그래서 시간 복잡도는 O(n)이라고 생각 합니다. 사실 공간 복잡도도 크게 다르지 않을 것 같습니다. 주어진 배열 외에 추가 정수 변수 2개만 추가 되었으니 역시 O(n+2)...겠죠? 다만 궁금한 점은, 성능 평가는 |
func maxProfit(prices []int) int { | ||
min := prices[0] | ||
maxProfit := 0 | ||
|
||
for i := 1; i < len(prices); i++ { | ||
if prices[i] < min { | ||
min = prices[i] | ||
} | ||
if (prices[i] - min) > maxProfit { | ||
maxProfit = prices[i] - min | ||
} | ||
} |
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.
공간 복잡도는 말씀해주신 대로 상수 크기 변수만 사용하니까 빅오 표기법에 따라 O(1)이 될 거 같아요.
그리고 리트코드의 효율성 분석을 그대로 믿을 수 없다.. 는 글을 어디서 본 것 같아요.
저도 지금 이 코드 그대로 돌려봤는데 메모리가 10% 나오는 걸 보니 맞는 것 같고요.
지금 코드도 충분히 최적화되어 있는 듯한데요.
13번 라인에 else if 처리를 하면 불필요한 연산이 조금 줄 것 같습니다.
prices[i] < min이면 prices[i] - min이 음수가 되어서 maxProfit을 업데이트해줄 필요가 없을 거 같아요.
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.
그렇네요 브랜치 하나 더 줄일 수 있겠네요...!
pr 본문에는 3문제 푸는 것으로 쓰신 듯한데, 한 문제만 푸시는 건가요? |
@anniemon 리뷰 감사 합니다. 퍼포먼스는 각자 다르지만 모두 다 완주하면 좋겠습니다. |
답안 제출 문제
체크 리스트
In Review
로 설정해주세요.