GeezerVison is showing github.com — Added binary search solutions · johnsonhit/coding-problems@a9bd3b9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: LICENSE +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | MIT License | 2 | 2 |
| 3 | | -Copyright (c) 2019 Мето Трајковски | | 3 | +Copyright (c) 2019 Meto Trajkovski | 4 | 4 |
| 5 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy | 6 | 6 | of this software and associated documentation files (the "Software"), to deal | |
Collapse file Copy file name to clipboard +58Lines changed: 58 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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)) |
Collapse file Copy file name to clipboard +69Lines changed: 69 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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)) |
Collapse file Copy file name to clipboardExpand all lines: README.md +7-9Lines changed: 7 additions & 9 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -79,15 +79,13 @@ print(name_of_solution_2('example2')) | 79 | 79 |
| 80 | 80 | Each solution/problem in this repo belongs to one of these categories: | 81 | 81 |
| 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. | 91 | 89 |
| 92 | 90 |
| 93 | 91 | ## Learning Resources | |
|