From 5ee7aa0cf96bf8eb26b31653f41cff9d12b7bb88 Mon Sep 17 00:00:00 2001 From: Rytxxx Date: Sun, 30 Nov 2025 16:59:01 -0800 Subject: [PATCH] 153-find-minimum-in-rotated-sorted-array --- .../memo.md | 56 +++++++++++++++++++ .../step1.py | 11 ++++ .../step2.py | 15 +++++ .../step3.py | 14 +++++ 4 files changed, 96 insertions(+) create mode 100644 153-find-minimum-in-rotated-sorted-array/memo.md create mode 100644 153-find-minimum-in-rotated-sorted-array/step1.py create mode 100644 153-find-minimum-in-rotated-sorted-array/step2.py create mode 100644 153-find-minimum-in-rotated-sorted-array/step3.py diff --git a/153-find-minimum-in-rotated-sorted-array/memo.md b/153-find-minimum-in-rotated-sorted-array/memo.md new file mode 100644 index 0000000..3cd965b --- /dev/null +++ b/153-find-minimum-in-rotated-sorted-array/memo.md @@ -0,0 +1,56 @@ +## 問題文 +Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: + +[4,5,6,7,0,1,2] if it was rotated 4 times. +[0,1,2,4,5,6,7] if it was rotated 7 times. +Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. + +Given the sorted rotated array nums of unique elements, return the minimum element of this array. + +You must write an algorithm that runs in O(log n) time. + + + +Example 1: + +Input: nums = [3,4,5,1,2] +Output: 1 +Explanation: The original array was [1,2,3,4,5] rotated 3 times. +Example 2: + +Input: nums = [4,5,6,7,0,1,2] +Output: 0 +Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times. +Example 3: + +Input: nums = [11,13,15,17] +Output: 11 +Explanation: The original array was [11,13,15,17] and it was rotated 4 times. + +## step1で考えたこと +1回rotateすると、一番後ろのnumberが先頭に来て、それ以降が一つずれる +1ずつIndexをずらすには? +Binary Searchを使った解法が思いつかない + +return the minimum element of this array. +-> この部分に関してはsortしたListの先頭をただreturnすれば良いのでは? + +## step1でのコード +```py +class Solution: + def findMin(self, nums: List[int]) -> int: + sorted_nums = sorted(nums) + min = sorted_nums[0] + rotate_time = 0 + + while nums != sorted_nums: + sorted_nums = + rotate_time += 1 + + return min + +``` + +## step2での気づき +やはり、大小を比較するときに、=を含めるか否かが判断できていない。 +returnの位置を間違えていた。 \ No newline at end of file diff --git a/153-find-minimum-in-rotated-sorted-array/step1.py b/153-find-minimum-in-rotated-sorted-array/step1.py new file mode 100644 index 0000000..1318552 --- /dev/null +++ b/153-find-minimum-in-rotated-sorted-array/step1.py @@ -0,0 +1,11 @@ +class Solution: + def findMin(self, nums: List[int]) -> int: + sorted_nums = sorted(nums) + min = sorted_nums[0] + rotate_time = 0 + + while nums != sorted_nums: + sorted_nums = + rotate_time += 1 + + return min diff --git a/153-find-minimum-in-rotated-sorted-array/step2.py b/153-find-minimum-in-rotated-sorted-array/step2.py new file mode 100644 index 0000000..f877568 --- /dev/null +++ b/153-find-minimum-in-rotated-sorted-array/step2.py @@ -0,0 +1,15 @@ +## 解答を見た後に自力で書いたコード +class Solution: + def findMin(self, nums: List[int]) -> int: + left = 0 + right = len(nums) - 1 + + while left < right: + mid = (left + right) // 2 + + if nums[mid] < nums[right]: + right = mid + else: + left = mid + 1 + + return nums[left] \ No newline at end of file diff --git a/153-find-minimum-in-rotated-sorted-array/step3.py b/153-find-minimum-in-rotated-sorted-array/step3.py new file mode 100644 index 0000000..1e51b7f --- /dev/null +++ b/153-find-minimum-in-rotated-sorted-array/step3.py @@ -0,0 +1,14 @@ +class Solution: + def findMin(self, nums: List[int]) -> int: + left = 0 + right = len(nums) - 1 + + while left < right: + mid = (left + right) // 2 + + if nums[mid] < nums[right]: + right = mid + else: + left = mid + 1 + + return nums[left] \ No newline at end of file