diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..b587bcbf --- /dev/null +++ b/Problem1.py @@ -0,0 +1,47 @@ +""" +(https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) + +Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. + +Your algorithm's runtime complexity must be in the order of O(log n). + +If the target is not found in the array, return [-1, -1]. +""" +class Solution(object): + def searchRange(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + def findFirst(nums, target): + low, high = 0, len(nums) - 1 + first = -1 + while low <= high: + mid = (low + high) // 2 + if nums[mid] == target: + first = mid + high = mid - 1 # keep going left + elif nums[mid] < target: + low = mid + 1 + else: + high = mid - 1 + return first + + def findLast(nums, target): + low, high = 0, len(nums) - 1 + last = -1 + while low <= high: + mid = (low + high) // 2 + if nums[mid] == target: + last = mid + low = mid + 1 # keep going right + elif nums[mid] < target: + low = mid + 1 + else: + high = mid - 1 + return last + + first = findFirst(nums, target) + last = findLast(nums, target) + return [first, last] \ No newline at end of file diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..11ca05c1 --- /dev/null +++ b/Problem2.py @@ -0,0 +1,37 @@ + +""" +## Problem 2: (https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) + +Suppose an array sorted in ascending order 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]). + +Find the minimum element. + +You may assume no duplicate exists in the array. + +Example 1: +Input: [3,4,5,1,2] +Output: 1 + +Example 2: +Input: [4,5,6,7,0,1,2] +Output: 0 +""" + +class Solution(object): + def findMin(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + low, high = 0, len(nums) - 1 + + while low < high: + mid = (low + high)//2 + if nums[mid] > nums[high]: + low = mid + 1 + else: + high = mid + + return nums[low] \ No newline at end of file diff --git a/Problem3.py b/Problem3.py new file mode 100644 index 00000000..e1633c0b --- /dev/null +++ b/Problem3.py @@ -0,0 +1,40 @@ +""" +## Problem 3: (https://leetcode.com/problems/find-peak-element/) +A peak element is an element that is greater than its neighbors. + +Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. + +The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. + +You may imagine that nums[-1] = nums[n] = -∞. + +Example 1: + +Input: nums = [1,2,3,1] +Output: 2 +Explanation: 3 is a peak element and your function should return the index number 2. +Example 2: + +Input: nums = [1,2,1,3,5,6,4] +Output: 1 or 5 +Explanation: Your function can return either index number 1 where the peak element is 2, + + or index number 5 where the peak element is 6. +Note: + +""" + +class Solution(object): + def findPeakElement(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + low, high = 0, len(nums)-1 + while(low