Browse Source

Add binary search in rotated array and fix a bug in binary search

Snow 8 years ago
parent
commit
0d5c73eb45

+ 34 - 0
arrays-and-strings/remove-duplicates.py

@@ -0,0 +1,34 @@
+# Design an algorithm that removes the duplicate characters in a string
+# without using additional buffer.
+
+import pytest
+
+def test_remove_duplicates():
+    case1 = ''
+    case2 = 'a'
+    case3 = 'asdfghjkl'
+    case4 = 'aasdee'
+    assert remove_duplicates(case1) == ''
+    assert remove_duplicates(case2) == 'a'
+    assert remove_duplicates(case3) == 'asdfghjkl'
+    assert remove_duplicates(case4) == 'asde'
+
+pytest.main()
+
+# use two indicies to track non-duplicates in place.
+# time complexity: O(n)
+# space complexity: O(1)
+def remove_duplicates(input):
+    len_input = len(input)
+    if len_input <= 1:
+        return input
+    else:
+        list_input = list(input)
+        len_non_dup = 1
+        for j in range(1, len_input):
+            if list_input[j] in list_input[:len_non_dup]:
+                pass
+            else:
+                list_input[len_non_dup] = input[j]
+                len_non_dup += 1
+        return ''.join(list_input[:len_non_dup])

+ 24 - 0
search-and-sort/binary-search.py

@@ -0,0 +1,24 @@
+# Binary search for the target in a sorted array, assume the array has no dups
+# Time complexity: O(logn)
+# Space complexity: O(1)
+
+import pytest
+
+def test_binary_search():
+    case1 = [0,1,2,3]
+    assert binary_search(case1,1) == 1
+    assert binary_search(case1,0) == 0
+    assert binary_search(case1, 0.5) == -1
+
+def binary_search(l, target):
+    left = 0
+    right = len(l)-1
+    while right >= left:
+        m = (right - left )//2
+        if l[m] == target:
+            return m
+        if target < l[m]:
+            right = m - 1
+        else:
+            left = m + 1
+    return -1

+ 40 - 0
search-and-sort/search_in_rotated.py

@@ -0,0 +1,40 @@
+# Search in a rotated sorted list, require time complexity O(logn)
+
+import pytest
+
+def test_search_in_rotated():
+    case1 = [5,7,0,3,4]
+    assert search_in_rotated(case1, 7) == 1
+    assert search_in_rotated(case1, 6) == -1
+    assert search_in_rotated(case1, 5) == 0
+
+def search_in_rotated(l, target):
+    left = 0
+    right = len(l) - 1
+    while right > left:
+        m = (right - left) // 2
+        if l[m] == target:
+            return m
+        if l[right] == target:
+            return right
+        if l[left] == target:
+            return left
+        # right portion is sorted
+        if l[m] < l[right] and l[m] < l[left]:
+            if target < l[right] and target > l[m]:
+                left = m + 1
+            else:
+                right = m - 1
+        # left portion is sorted
+        elif l[m] > l[right] and l[m] > l[left]:
+            if target > l[left] and target < l[m]:
+                right = m - 1
+            else:
+                left = m + 1
+        # list is sorted
+        else:
+            if target > l[m]:
+                left = m + 1
+            else:
+                right = m - 1
+    return -1