Skip to content

Commit 910eb53

Browse files
committed
leetcode 34
python
1 parent 4f3c026 commit 910eb53

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages (M
3838
| 28 | Easy | [Implement strStr()](leetcode/28-Easy-Implement-strStr/problem.md) | [C](leetcode/28-Easy-Implement-strStr/answer.c), [Python](leetcode/28-Easy-Implement-strStr/answer.py) |
3939
| 29 | Medium | [Divide Two Integers](leetcode/29-Medium-Divide-Two-Integers/problem.md) | [Python](leetcode/29-Medium-Divide-Two-Integers/answer.py) |
4040
| 33 | Medium | [Search in Rotated Sorted Array](leetcode/33-Medium-Search-In-Rotated-Sorted-Array/problem.md) | [Python](leetcode/33-Medium-Search-In-Rotated-Sorted-Array/answer.py) |
41+
| 34 | Medium | [Find First and Last Position of Element in Sorted Array](leetcode/34-Medium-Find-First-And-Last-Position-Of-Element-In-Sorted-Array/problem.md) | [Python](leetcode/34-Medium-Find-First-And-Last-Position-Of-Element-In-Sorted-Array/answer.py) |
4142
| 35 | Easy | [Search Insert Position](leetcode/35-Easy-Search-Insert-Position/problem.md) | [Go](leetcode/35-Easy-Search-Insert-Position/answer.go), [Java](leetcode/35-Easy-Search-Insert-Position/answer.java), [JavaScript](leetcode/35-Easy-Search-Insert-Position/answer.js), [Scala](leetcode/35-Easy-Search-Insert-Position/answer.scala) |
4243
| 36 | Medium | [Valid Sudoku](leetcode/36-Medium-Valid-Sudoku/problem.md) | [Python](leetcode/36-Medium-Valid-Sudoku/answer.py) |
4344
| 39 | Medium | [Combination Sum](leetcode/39-Medium-Combination-Sum/problem.md) | [Python](leetcode/39-Medium-Combination-Sum/answer.py) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/python3
2+
3+
#------------------------------------------------------------------------------
4+
5+
# This first solution is technically O(log(n)+k) where k is the number of occurances of target
6+
class Solution:
7+
def searchRange(self, nums, target):
8+
"""
9+
:type nums: List[int]
10+
:type target: int
11+
:rtype: List[int]
12+
"""
13+
left = self.binarySearch(nums, target, 0, len(nums)-1)
14+
if left == -1: return [-1, -1]
15+
right = left
16+
17+
while nums[left] == target and left > 0:
18+
left -= 1
19+
if nums[left] != target:
20+
left += 1
21+
while nums[right] == target and right < len(nums)-1:
22+
right += 1
23+
if nums[right] != target:
24+
right -= 1
25+
26+
return [left, right]
27+
28+
def binarySearch(self, nums, target, left, right):
29+
"""
30+
Binary Search for log(n) time
31+
"""
32+
if right >= left:
33+
mid = (left + right ) // 2
34+
35+
if nums[mid] == target:
36+
return mid
37+
elif nums[mid] > target:
38+
return self.binarySearch(nums, target, left, mid-1)
39+
else:
40+
return self.binarySearch(nums, target, mid+1, right)
41+
else:
42+
return -1
43+
44+
#------------------------------------------------------------------------------
45+
46+
# This second solution is O(2log(n))
47+
class Solution:
48+
def searchRange(self, nums, target):
49+
def binarySearchLeft(A, x):
50+
left, right = 0, len(A) - 1
51+
while left <= right:
52+
mid = (left + right) // 2
53+
if x > A[mid]: left = mid + 1
54+
else: right = mid - 1
55+
return left
56+
57+
def binarySearchRight(A, x):
58+
left, right = 0, len(A) - 1
59+
while left <= right:
60+
mid = (left + right) // 2
61+
if x >= A[mid]: left = mid + 1
62+
else: right = mid - 1
63+
return right
64+
65+
left, right = binarySearchLeft(nums, target), binarySearchRight(nums, target)
66+
return (left, right) if left <= right else [-1, -1]
67+
68+
#------------------------------------------------------------------------------
69+
#Testing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Problem 34: Find First and Last Position of Element in Sorted Array
2+
3+
#### Difficulty: Medium
4+
5+
#### Problem
6+
7+
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
8+
9+
Your algorithm's runtime complexity must be in the order of O(log n).
10+
11+
If the target is not found in the array, return [-1, -1].
12+
13+
#### Example
14+
15+
<pre>
16+
17+
Input: nums = [5,7,7,8,8,10], target = 8
18+
19+
Output: [3,4]
20+
21+
</pre>

0 commit comments

Comments
 (0)