-
Notifications
You must be signed in to change notification settings - Fork 126
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
[jaejeong1] WEEK 09 Solutions #522
Conversation
public int findMin(int[] nums) { | ||
// TC: O(log N) | ||
// SC: O(1) | ||
var left = 1; // N번 회전해 이미 오름차순 정렬일 경우 0으로 시작하면 루프가 안끝남. 1로 시작 |
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.
left를 0으로 초기화하면서 재정님의 이진탐색을 자연스럽게 종료시키는 방법을 생각해보았습니다
class Solution {
public int findMin(int[] nums) {
// TC: O(log N)
// SC: O(1)
var left = 0;
var right = nums.length-1;
while (left <= right) { // left가 right보다 작거나 같을때까지
var mid = (left + right) / 2;
if (nums[mid] <= nums[right]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return nums[left]; // 만약 Rotation이 없는 배열일 경우 left는 0이 되고, Rotation이 있는 배열일 경우엔 right와 left가 엇갈리는 부분이 Rotation의 경계가 되도록 이진탐색이 종료됨
}
}
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.
좋은 피드백 및 코드 예시 감사합니다!
특히 마지막 comment 주신 부분이 큰 도움이 되었습니다.
// 만약 Rotation이 없는 배열일 경우 left는 0이 되고, Rotation이 있는 배열일 경우엔 right와 left가 엇갈리는 부분이 Rotation의 경계가 되도록 이진탐색이 종료됨
참고해서 리팩토링 진행해보겠습니다 :) 감사합니다!!
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.
@obzva 님, 제안해주신 코드에 버그가 있는 것 같은데용? 😵
예를 들어, 입력으로 [9,4,6]
이 주어지면 4
대신에 9
가 나올 것 같습니다...
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.
확인했습니다 정말이네요 😟
} | ||
} | ||
|
||
return nums[0]; // N번 회전해 이미 오름차순 정렬일 경우 0번째 인덱스 요소 반환 |
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.
저는 이렇게 되면 더 좋겠다는 생각을 했습니다 :)
물론 그냥 개인적인 욕심입니다..
- 이진탐색으로는
N번 회전해 이미 오름차순 정렬인 경우
의 답을 return할 수 없다면, 애초에 이진탐색을 수행하기 전에nums[0]
을 return 해주기
if (nums[0] < nums[nums.length - 1]) {
return nums[0];
}
// 이후 이진탐색 관련된 로직 등장
- 아예
N번 회전해 이미 오름차순 정렬인 경우
도 이진탐색에서 자연스럽게 도출될 수 있게끔 로직 설계
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.
@obzva 좋은 피드백 감사합니다. 알고리즘 문제풀때는 유독 가독성에 신경을 덜쓰게 되는 경향이 있는데, 다시 한번 중요성을 느낍니다.. 개인적으로는 1번 처럼 early return 해주는 방식을 선호합니다 :)
안녕하세요 @obzva 님, 좋은 피드백 정말 감사합니다 :) 아쉽게도 9주차가 지나버렸는데, 해당 PR approve 해주실 수 있을까요? |
답안 제출 문제
체크 리스트
In Review
로 설정해주세요.