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

[forest000014] Week 09 #994

Merged
merged 4 commits into from
Feb 9, 2025
Merged

[forest000014] Week 09 #994

merged 4 commits into from
Feb 9, 2025

Conversation

forest000014
Copy link
Contributor

답안 제출 문제

Week 8

Week 9

체크 리스트

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

@forest000014 forest000014 self-assigned this Feb 6, 2025
@forest000014 forest000014 requested a review from a team as a code owner February 6, 2025 12:24
@github-actions github-actions bot added the java label Feb 6, 2025
@forest000014 forest000014 changed the title [forest000014] Week 9 [forest000014] Week 09 Feb 6, 2025
Copy link
Contributor

@obzva obzva left a comment

Choose a reason for hiding this comment

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

안녕하세요 벌써 9주차네요 :)
남은 주차도 화이팅입니다 forest님
아직 문제 풀이 진행중이신 것 같은데, 일단 미리 approve 남겨놓고 가겠습니다
다시 리뷰가 필요하면 re-request 요청 부탁드립니다

}

while (l <= r) {
int m = (r - l) / 2 + l; // 만약 문제 조건상 l, r의 합이 int 범위를 넘어가서 overflow가 생길 수 있는 경우에, 이런 식으로 overflow를 방지할 수 있다고 알고 있습니다. 이 문제는 overflow 걱정은 없지만, 나중에 실전에서 나오면 잊지 않으려고 이렇게 구현해보았습니다.
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

}
}

return -1;
Copy link
Contributor

Choose a reason for hiding this comment

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

-1을 리턴하는 경우는 없을 것 같아요 :)

Comment on lines +25 to +34
if (m > 0 && nums[m - 1] > nums[m]) {
return nums[m];
} else if (m < nums.length - 1 && nums[m] > nums[m + 1]) {
return nums[m + 1];
} else if (nums[m] > nums[l]) {
l = m + 1;
} else {
r = m - 1;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

조건문을 세 부분으로 줄일 수는 없을까 고민해봤습니다 :)

  1. return하는 조건
  2. r를 옮기는 조건
  3. l를 옮기는 조건
Suggested change
if (m > 0 && nums[m - 1] > nums[m]) {
return nums[m];
} else if (m < nums.length - 1 && nums[m] > nums[m + 1]) {
return nums[m + 1];
} else if (nums[m] > nums[l]) {
l = m + 1;
} else {
r = m - 1;
}
}
// 문제 조건상 입력 배열은 무조건 1 이상의 rotation이 적용되어 있습니다.
// 그리고 line19에서 우리는 가지런히 정렬된 입력 배열에 대해서는 바로 return할 수 있는 조건문을 작성하였습니다
// 따라서 우리가 m == 0인 nums[m]을 return할 일은 없으므로 m > 0이라는 조건은 생략할 수 있습니다
// 기존의 두번째 조건문은 제가 보기엔 불필요한 것 같습니다
if (nums[m - 1] > nums[m]) {
return nums[m];
// nums[m]이 입력 배열 중 'rotation되지 않은 부분'에 속하는 경우입니다
// 이 경우엔 r을 줄여야 합니다
} else if (nums[m] < nums[nums.length - 1]) {
r = m - 1;
} else {
l = m + 1;
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

전반적인 사고 과정은 예전 제 풀이에서의 사고 과정과 비슷합니다 :) 혹시 도움이 될까봐 공유합니다..

(디스커션에 제가 쓴 글도 있으니 궁금하시다면 참고하셔도 됩니다!)

*/
public class Solution {
private final int VISITED = -999999;
public boolean hasCycle(ListNode head) {
Copy link
Contributor

Choose a reason for hiding this comment

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

좋은 접근인 것 같습니다 :)
하지만 만약 문제 조건이 이렇게 주어졌다면 어땠을까요?
"입력으로 주어진 ListNode에는 변경을 가하지 마시오"

리스트노드의 acyclic 여부를 판명하는 잘 알려진 알고리즘이 있으니 소개드립니다, 참고 바랍니다 :)
https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare

Copy link
Contributor

Choose a reason for hiding this comment

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

그동안 forest님 풀이를 봐오면서 느낀건데, 문제를 작은 단위로 쪼개어서 해결하는 능력과 끝까지 문제를 해결해내는 능력이 훌륭하십니다 :)

절대 이 풀이가 나쁘다는 건 아니구요, 리뷰어 입장에서 느꼈던 피드백 사항이 몇가지 있어 조심스레 남기고자 합니다

  1. 로직이 복잡합니다. 복잡한 로직이 나쁜 건 아니지만, 알고리즘 테스트를 실시간으로 (혹은 면접관 앞에서) 진행하는 상황이라면 이렇게 긴 풀이는 버그 발생 가능성이 높습니다. 최악의 경우엔 많이 꼬여서 시간 안배가 불가능하게 됩니다. (제가 그랬어요)
  2. 로직이 복잡하다는 건, 리뷰어가 해당 코드를 읽기 힘들다는 뜻이기도 합니다. 그리고 본인께서도 면접관께 설명드리기 더 어려우실 겁니다.

좀 더 간단한 로직을 사용하는 풀이를 하나 소개드리니, 참고 바랍니다 :)
https://github.com/DaleStudy/leetcode-study/blob/main/maximum-product-subarray/obzva.cpp

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@obzva
감사합니다! 사실 저도 매번 풀 때마다 막연하게 느끼던 부분이었는데, 정확하게 짚어주신 것 같습니다.
제가 주말 전후로 몸살을 심하게 앓아서 이제서야 코멘트를 봤네요. 오늘은 육아하고 잠깐 들어와봤는데, 다시 야근하러 가야 할 것 같아서 내일 마저 읽어보겠습니다 🙇

@SamTheKorean SamTheKorean merged commit 87ce663 into DaleStudy:main Feb 9, 2025
3 checks passed
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.

3 participants