commit 402ade51c13c7265d53d843bbfb58e5c38c648ef
parent c7608fed58e870c99566313ce4994afce2aeda7f
Author: Jack Sangdahl <jack@pngu.org>
Date: Sat, 16 May 2026 08:04:06 +0300
154. find minimum in rotated sorted array 2
Diffstat:
2 files changed, 61 insertions(+), 0 deletions(-)
diff --git a/find_min_rotated_arr2.c b/find_min_rotated_arr2.c
@@ -0,0 +1,33 @@
+// 154. Find Minimum in Rotated Sorted Array II
+#include "leetcode.h"
+
+/*
+ * suppose an array of length 'n' sorted in ascending order is rotated between 1
+ * and 'n' times. given the sorted rotated array 'nums' that may contain
+ * duplicates, return the minimum changes of this array. you must decrease the
+ * overall operation steps as much as possible.
+ */
+
+int findMin(int *nums, int numsSize) {
+ int left = 0, right = numsSize - 1;
+ while (left < right) {
+ int mid = left + (right - left) / 2;
+ if (nums[mid] > nums[right])
+ left = mid + 1;
+ else if (nums[mid] < nums[right])
+ right = mid;
+ else
+ right--;
+ }
+ return nums[left];
+}
+
+int main() {
+ int n1[] = {1, 3, 5}, n2[] = {2, 2, 2, 0, 1};
+ int r1 = findMin(n1, ARRAY_SIZE(n1));
+ int r2 = findMin(n2, ARRAY_SIZE(n2));
+ printf("%d\n", r1);
+ assert(r1 == 1);
+ printf("%d\n", r2);
+ assert(r2 == 0);
+}
diff --git a/find_min_rotated_arr2.py b/find_min_rotated_arr2.py
@@ -0,0 +1,28 @@
+# 154. Find Minimum in Rotated Sorted Array II
+
+"""
+suppose an array of length 'n' sorted in ascending order is rotated between 1
+and 'n' times. given the sorted rotated array 'nums' that may contain
+duplicates, return the minimum changes of this array. you must decrease the
+overall operation steps as much as possible.
+"""
+
+
+class Solution:
+ def findMin(self, nums: List[int]) -> int:
+ left, right = 0, len(nums) - 1
+ while left < right:
+ mid = left + (right - left) // 2
+ if nums[mid] > nums[right]:
+ left = mid + 1
+ elif nums[mid] < nums[right]:
+ right = mid
+ else:
+ right -= 1
+ return nums[left]
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.findMin(nums=[1, 3, 5]))
+ print(obj.findMin(nums=[2, 2, 2, 0, 1]))