Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -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]
37 changes: 37 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -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]
40 changes: 40 additions & 0 deletions Problem3.py
Original file line number Diff line number Diff line change
@@ -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<high):
mid = (low + high)//2
if nums[mid] < nums[mid+1]:
low = mid + 1
else:
high = mid
return low