minimum element rotated sorted array pivot

minimum element rotated sorted array pivot

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 01234567

Find the minimum element.

The array may contain duplicates.

A binary search based algorithm is given to find the minimum in the LeetCode Rotated Sorted Array. It runs in O(N) in worst case, but runs in O(logN) time if no duplicates.

[[code]]czo0Mjk6XCINCjxlbT5pbnQgZmluZE1pbih2ZWN0b3ImbHQ7aW50Jmd0OyAmYW1wO251bSkgezwvZW0+DQo8ZW0+IGlmKG51bS5zaXp7WyYqJl19ZSgpID09MCkgPC9lbT4NCjxlbT4gcmV0dXJuIDA7PC9lbT4NCjxlbT4gaW50IG1pbj1JTlRfTUlOOzwvZW0+DQo8ZW0+IGZvcihpbntbJiomXX10IGk9MDtpICZsdDsgbnVtLnNpemUoKS0xO2krKyk8L2VtPg0KPGVtPiB7PC9lbT4NCjxlbT4gaWYobnVtW2krMV0gJmx0OyBudW1be1smKiZdfWldKTwvZW0+DQo8ZW0+IHs8L2VtPg0KPGVtPiBtaW49bnVtW2krMV07IDwvZW0+DQo8ZW0+IGJyZWFrOzwvZW0+DQo8ZW0+IH08L2V7WyYqJl19bT4NCjxlbT4gfTwvZW0+DQo8ZW0+IGlmKG1pbiA9PSBJTlRfTUlOKTwvZW0+DQo8ZW0+IHs8L2VtPg0KPGVtPiBtaW49bnVtWzBdO3tbJiomXX08L2VtPg0KPGVtPiB9PC9lbT4NCjxlbT4gcmV0dXJuIG1pbjs8L2VtPg0KPGVtPiB9PC9lbT4NClwiO3tbJiomXX0=[[/code]]

Leave a Reply

Your email address will not be published. Required fields are marked *