Skip to content

Commit a9bd3b9

Browse files
author
Мето Трајковски
committed
Added binary search solutions
1 parent be79796 commit a9bd3b9

File tree

4 files changed

+135
-10
lines changed

4 files changed

+135
-10
lines changed

Diff for: LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Мето Трајковски
3+
Copyright (c) 2019 Meto Trajkovski
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Diff for: Lists/find_element_range_sorted_array.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'''
2+
Find First and Last Position of Element in Sorted Array
3+
4+
Given an array of integers nums sorted in ascending order,
5+
find the starting and ending position of a given target value.
6+
If the target is not found in the array, return [-1, -1].
7+
8+
Input: [5, 7, 7, 8, 8, 10], 8
9+
Output: [3, 4]
10+
11+
Input: [5, 7, 7, 8, 8, 10], 6
12+
Output: [-1, -1]
13+
14+
=========================================
15+
Use binary search twice to find the range.
16+
Time Complexity: O(LogN)
17+
Space Complexity: O(1)
18+
'''
19+
20+
21+
############
22+
# Solution #
23+
############
24+
25+
def search_range(nums, target):
26+
left_idx = binary_search(nums, target, True)
27+
if (left_idx == len(nums)) or (nums[left_idx] != target):
28+
return [-1, -1]
29+
30+
right_idx = binary_search(nums, target, False) - 1
31+
return [left_idx, right_idx]
32+
33+
def binary_search(nums, target, equal=True):
34+
left = 0
35+
right = len(nums)
36+
37+
while left < right:
38+
mid = (left + right) // 2
39+
40+
if (nums[mid] > target) or (equal and nums[mid] == target):
41+
right = mid
42+
else:
43+
left = mid + 1
44+
45+
return left
46+
47+
48+
###########
49+
# Testing #
50+
###########
51+
52+
# Test 1
53+
# Correct result => [3, 4]
54+
print(search_range([5, 7, 7, 8, 8, 10], 8))
55+
56+
# Test 2
57+
# Correct result => [-1, -1]
58+
print(search_range([5, 7, 7, 8, 8, 10], 6))

Diff for: Lists/search_rotated_sorted_array.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'''
2+
Search in Rotated Sorted Array
3+
4+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
5+
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
6+
You are given a target value to search. If found in the array return its index, otherwise return -1.
7+
You may assume no duplicate exists in the array.
8+
9+
Input: [4, 5, 6, 7, 0, 1, 2], 0
10+
Output: 4
11+
12+
Input: [4, 5, 6, 7, 0, 1, 2], 3
13+
Output: -1
14+
15+
=========================================
16+
Use binary search twice, first time to find the pivot (index where the array is rotated)
17+
and the second time to find the target.
18+
Time Complexity: O(LogN)
19+
Space Complexity: O(1)
20+
'''
21+
22+
23+
############
24+
# Solution #
25+
############
26+
27+
def search_rotated_sorted_array(nums, target):
28+
n = len(nums)
29+
pivot = find_pivot(nums, 0, n) + 1
30+
if pivot > n:
31+
return -1
32+
33+
if nums[0] <= target:
34+
return find_element(nums, 0, pivot - 1, target)
35+
return find_element(nums, pivot, n - 1, target)
36+
37+
def find_pivot(nums, left, right):
38+
while left < right - 1:
39+
mid = left + (right - left) // 2
40+
41+
if nums[left] < nums[mid]:
42+
left = mid
43+
else:
44+
right = mid
45+
46+
return left
47+
48+
def find_element(nums, left, right, target):
49+
while left <= right:
50+
mid = left + (right - left) // 2
51+
52+
if nums[mid] == target:
53+
return mid
54+
55+
if nums[mid] < target:
56+
left = mid + 1
57+
else:
58+
right = mid - 1
59+
60+
return -1
61+
62+
63+
###########
64+
# Testing #
65+
###########
66+
67+
# Test 1
68+
# Correct result => 4
69+
print(search_rotated_sorted_array([4, 5, 6, 7, 0, 1, 2], 0))

Diff for: README.md

+7-9
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,13 @@ print(name_of_solution_2('example2'))
7979

8080
Each solution/problem in this repo belongs to one of these categories:
8181

82-
1. [Strings](/)
83-
2. [Arrays](/)
84-
3. [Linked Lists](/)
85-
4. [Trees](/)
86-
5. [Dynamic Programming](/)
87-
6. [Backtracking](/)
88-
7. [Graphs](/)
89-
8. [Math](/)
90-
9. [Other](/)
82+
1. [Arrays](/) - Array Manipulations, Sorting, Binary Search, Divide and Conquer, Sliding Window, etc.
83+
2. [Linked Lists](/) - Linked List Searching, Pointer Manipulations, etc.
84+
3. [Trees](/) - Binary Search Trees, Tree Traversals: Breadth-First (Level Order) Traversal, Depth-First Traversal (Inorder, Preorder, Postorder), etc.
85+
4. [Dynamic Programming](/) - 2D and 1D Dynamic Programming, LCS, LIS, Knapsack, etc.
86+
5. [Strings](/) - String Manipulations, Reversing, Encodings/Decodings, etc.
87+
6. [Math](/) - GCD, LCM, Prime Factors, Math Formulas, etc.
88+
7. [Other](/) - Backtracking, BFS, DFS, Queues, Stacks, Matrices, etc.
9189

9290

9391
## Learning Resources

0 commit comments

Comments
 (0)