Skip to content
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

Merged
merged 1 commit into from
Jan 9, 2025
Merged

[bus710] week 05 #872

merged 1 commit into from
Jan 9, 2025

Conversation

bus710
Copy link
Contributor

@bus710 bus710 commented Jan 8, 2025

답안 제출 문제

체크 리스트

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

@bus710 bus710 requested a review from a team as a code owner January 8, 2025 23:09
@github-actions github-actions bot added the go label Jan 8, 2025
@bus710
Copy link
Contributor Author

bus710 commented Jan 8, 2025

제일 쉬운 한 문제만 푸는지라, 아무때나 바로 리뷰 부탁 드립니다.

복잡도에 대해...

처음에는 brutal하게 풀었다가 leetcode의 검사에서 시간 초과가 떠서 고민을 조금 했었는데, 루프를 한번만 돌고도 도출할 수 있었습니다. 그래서 시간 복잡도는 O(n)이라고 생각 합니다. 사실 공간 복잡도도 크게 다르지 않을 것 같습니다. 주어진 배열 외에 추가 정수 변수 2개만 추가 되었으니 역시 O(n+2)...겠죠?

다만 궁금한 점은, 성능 평가는 beat 100% 였는데, 메모리 사용률은 beat 70%라서, 도대체 여기서 어떻게 메모리를 더 아낄 수 있나? 하는 부분을 고민하면 좋을 것 같습니다.

Comment on lines +5 to +16
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
}
}
Copy link
Contributor

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을 업데이트해줄 필요가 없을 거 같아요.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

그렇네요 브랜치 하나 더 줄일 수 있겠네요...!

@anniemon
Copy link
Contributor

anniemon commented Jan 9, 2025

pr 본문에는 3문제 푸는 것으로 쓰신 듯한데, 한 문제만 푸시는 건가요?
그러면 어프루브 드리겠습니다! 고생하셨습니다.

@bus710 bus710 merged commit 8140869 into DaleStudy:main Jan 9, 2025
3 checks passed
@bus710
Copy link
Contributor Author

bus710 commented Jan 10, 2025

@anniemon
뒤늦게 보았네요. 네, 한문제만 풀었는데 괄호 지우는 것을 깜빡 했습니다 (당당)

리뷰 감사 합니다. 퍼포먼스는 각자 다르지만 모두 다 완주하면 좋겠습니다.

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