Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions find-minimum-in-rotated-sorted-array/DaleSeo.rs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Binary Search
  • 설명: 회전된 정렬 배열에서 최소 원소를 이분탐색으로 찾는 문제로, 중간값과 양 끝 값을 비교해 탐색 범위를 절반으로 줄이는 패턴이다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(log n) O(log n)
Space O(1) O(1)

피드백: 배열의 특성을 이용해 양쪽 끝 비교로 범위를 절반으로 축소한다. 중간 인덱스와 끝 값을 비교해 분기 조건을 결정한다.

개선 제안: 현재 구현이 적절해 보입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// TC: O(log n)
// SC: O(1)
impl Solution {
pub fn find_min(nums: Vec<i32>) -> i32 {
let (mut lo, mut hi) = (0, nums.len() - 1);
while lo < hi {
if nums[lo] < nums[hi] {
return nums[lo];
}
let mid = (lo + hi) / 2;
if nums[mid] > nums[hi] {
lo = mid + 1;
} else {
hi = mid;
}
}
Comment on lines +6 to +16

@parkhojeong parkhojeong Jul 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

조금 더 최적화 한다면 lo, mid, hi 중 lo에 해당하는 값이 가장 작을 때에는 바로 리턴해도 될 거 같습니다.

case 1

1 2 3 4 5 --> 이 케이스 
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4

case 2

1 2 3  --> 이 케이스
2 3 1
3 1 2

대략 이런 코드가 되겠네요.

Suggested change
while lo < hi {
let mid = (lo + hi) / 2;
if nums[mid] > nums[hi] {
lo = mid + 1;
} else {
hi = mid;
}
}
while lo < hi {
let mid = (lo + hi) / 2;
if nums[lo] < nums[mid] && nums[mid] < nums[hi]{
return nums[lo]
}
if nums[mid] > nums[hi] {
lo = mid + 1;
} else {
hi = mid;
}
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@parkhojeong 님, 좋은 제안 감사합니다! 🙏
탐색 구간이 이미 정렬돼 있으면 맨 앞이 최소값이니 바로 반환할 수 있겠네요.
그런데 mid까지 볼 필요 없이 nums[lo] < nums[hi] 한 번의 비교만으로 동일한 조기 반환을 할 수 있더라고요.
그래서 아래처럼 좀 더 단순화하여 반영하겠습니다.

Suggested change
while lo < hi {
let mid = (lo + hi) / 2;
if nums[mid] > nums[hi] {
lo = mid + 1;
} else {
hi = mid;
}
}
while lo < hi {
if nums[lo] < nums[hi] {
return nums[lo];
}
let mid = (lo + hi) / 2;
if nums[mid] > nums[hi] {
lo = mid + 1;
} else {
hi = mid;
}
}

nums[lo]
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

이진탐색으로 해결해주셨군요, 코드 잘 봤습니다. 고생하셨습니다!

Loading