{ "cells": [ { "cell_type": "markdown", "source": [ "Suppose an array of length `n` sorted in ascending order is **rotated**\n", "between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]`\n", "might become:\n", "\n", " * `[4,5,6,7,0,1,2]` if it was rotated `4` times.\n", " * `[0,1,2,4,5,6,7]` if it was rotated `7` times.\n", "\n", "Notice that **rotating** an array `[a[0], a[1], a[2], ..., a[n-1]]` 1 time\n", "results in the array `[a[n-1], a[0], a[1], a[2], ..., a[n-2]]`.\n", "\n", "Given the sorted rotated array `nums`, return _the minimum element of this\n", "array_.\n", "\n", "\n", "\n", "**Example 1:**\n", "\n", "\n", "\n", " Input: nums = [3,4,5,1,2]\n", " Output: 1\n", " Explanation: The original array was [1,2,3,4,5] rotated 3 times.\n", "\n", "\n", "**Example 2:**\n", "\n", "\n", "\n", " Input: nums = [4,5,6,7,0,1,2]\n", " Output: 0\n", " Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.\n", "\n", "\n", "**Example 3:**\n", "\n", "\n", "\n", " Input: nums = [11,13,15,17]\n", " Output: 11\n", " Explanation: The original array was [11,13,15,17] and it was rotated 4 times.\n", "\n", "\n", "\n", "\n", "**Constraints:**\n", "\n", " * `n == nums.length`\n", " * `1 <= n <= 5000`\n", " * `-5000 <= nums[i] <= 5000`\n", " * All the integers of `nums` are **unique**.\n", " * `nums` is sorted and rotated between `1` and `n` times." ], "metadata": {} }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "find_min_153 (generic function with 1 method)" }, "metadata": {}, "execution_count": 1 } ], "cell_type": "code", "source": [ "# @lc code=start\n", "using LeetCode\n", "\n", "function find_min_153(nums::Vector{Int})\n", " left, right = 1, length(nums)\n", " while left < right\n", " mid = (left + right) รท 2\n", " (nums[mid] < nums[right]) ? (right = mid) : (left = mid + 1)\n", " end\n", " nums[left]\n", "end\n", "# @lc code=end" ], "metadata": {}, "execution_count": 1 }, { "cell_type": "markdown", "source": [ "---\n", "\n", "*This notebook was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*" ], "metadata": {} } ], "nbformat_minor": 3, "metadata": { "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.10.1" }, "kernelspec": { "name": "julia-1.10", "display_name": "Julia 1.10.1", "language": "julia" } }, "nbformat": 4 }