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

[jaejeong1] WEEK 09 Solutions #522

Merged
merged 2 commits into from
Oct 13, 2024
Merged

[jaejeong1] WEEK 09 Solutions #522

merged 2 commits into from
Oct 13, 2024

Conversation

jaejeong1
Copy link
Contributor

@jaejeong1 jaejeong1 commented Oct 7, 2024

답안 제출 문제

체크 리스트

  • PR을 프로젝트에 추가하고 Week를 현재 주차로 설정해주세요.
  • 바로 앞에 PR을 열어주신 분을 코드 검토자로 지정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 Status를 In Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

@jaejeong1 jaejeong1 self-assigned this Oct 7, 2024
@jaejeong1 jaejeong1 requested a review from a team as a code owner October 7, 2024 13:18
@github-actions github-actions bot added the java label Oct 7, 2024
@jaejeong1 jaejeong1 requested a review from obzva October 7, 2024 13:18
public int findMin(int[] nums) {
// TC: O(log N)
// SC: O(1)
var left = 1; // N번 회전해 이미 오름차순 정렬일 경우 0으로 시작하면 루프가 안끝남. 1로 시작
Copy link
Contributor

@obzva obzva Oct 11, 2024

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의 경계가 되도록 이진탐색이 종료됨
  }
}

Copy link
Contributor Author

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의 경계가 되도록 이진탐색이 종료됨

참고해서 리팩토링 진행해보겠습니다 :) 감사합니다!!

Copy link
Contributor

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가 나올 것 같습니다...

Copy link
Contributor

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번째 인덱스 요소 반환
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 이렇게 되면 더 좋겠다는 생각을 했습니다 :)
물론 그냥 개인적인 욕심입니다..

  1. 이진탐색으로는 N번 회전해 이미 오름차순 정렬인 경우의 답을 return할 수 없다면, 애초에 이진탐색을 수행하기 전에 nums[0]을 return 해주기
if (nums[0] < nums[nums.length - 1]) {
  return nums[0];
}
// 이후 이진탐색 관련된 로직 등장
  1. 아예 N번 회전해 이미 오름차순 정렬인 경우도 이진탐색에서 자연스럽게 도출될 수 있게끔 로직 설계

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 좋은 피드백 감사합니다. 알고리즘 문제풀때는 유독 가독성에 신경을 덜쓰게 되는 경향이 있는데, 다시 한번 중요성을 느낍니다.. 개인적으로는 1번 처럼 early return 해주는 방식을 선호합니다 :)

@jaejeong1
Copy link
Contributor Author

안녕하세요 @obzva 님, 좋은 피드백 정말 감사합니다 :) 아쉽게도 9주차가 지나버렸는데, 해당 PR approve 해주실 수 있을까요?
전달주신 피드백은 이번 10주차에 참고해서 적용해보겠습니다!! 감사합니다.

@jaejeong1 jaejeong1 merged commit b6f9956 into DaleStudy:main Oct 13, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

4 participants