forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add next_greatest_letter to search * Move top sort test into test_sort * Add min_distance to string * Add binary_gap to bit
- Loading branch information
1 parent
77e8af7
commit 77054e7
Showing
13 changed files
with
189 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
""" | ||
Given a positive integer N, find and return the longest distance between two | ||
consecutive 1' in the binary representation of N. | ||
If there are not two consecutive 1's, return 0 | ||
For example: | ||
Input: 22 | ||
Output: 2 | ||
Explanation: | ||
22 in binary is 10110 | ||
In the binary representation of 22, there are three ones, and two consecutive pairs of 1's. | ||
The first consecutive pair of 1's have distance 2. | ||
The second consecutive pair of 1's have distance 1. | ||
The answer is the largest of these two distances, which is 2 | ||
""" | ||
def binary_gap(N): | ||
last = None | ||
ans = 0 | ||
index = 0 | ||
while N != 0: | ||
if N & 1: | ||
if last is not None: | ||
ans = max(ans, index - last) | ||
last = index | ||
index = index + 1 | ||
N = N >> 1 | ||
return ans |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
''' | ||
Given a list of sorted characters letters containing only lowercase letters, | ||
and given a target letter target, find the smallest element in the list that | ||
is larger than the given target. | ||
Letters also wrap around. For example, if the target is target = 'z' and | ||
letters = ['a', 'b'], the answer is 'a'. | ||
Input: | ||
letters = ["c", "f", "j"] | ||
target = "a" | ||
Output: "c" | ||
Input: | ||
letters = ["c", "f", "j"] | ||
target = "c" | ||
Output: "f" | ||
Input: | ||
letters = ["c", "f", "j"] | ||
target = "d" | ||
Output: "f" | ||
Reference: https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/ | ||
''' | ||
|
||
import bisect | ||
|
||
""" | ||
Using bisect libarary | ||
""" | ||
def next_greatest_letter(letters, target): | ||
index = bisect.bisect(letters, target) | ||
return letters[index % len(letters)] | ||
|
||
""" | ||
Using binary search: complexity O(logN) | ||
""" | ||
def next_greatest_letter_v1(letters, target): | ||
if letters[0] > target: | ||
return letters[0] | ||
if letters[len(letters) - 1] <= target: | ||
return letters[0] | ||
left, right = 0, len(letters) - 1 | ||
while left <= right: | ||
mid = left + (right - left) // 2 | ||
if letters[mid] > target: | ||
right = mid - 1 | ||
else: | ||
left = mid + 1 | ||
return letters[left] | ||
|
||
""" | ||
Brute force: complexity O(N) | ||
""" | ||
def next_greatest_letter_v2(letters, target): | ||
for index in letters: | ||
if index > target: | ||
return index | ||
return letters[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" | ||
Given two words word1 and word2, find the minimum number of steps required to | ||
make word1 and word2 the same, where in each step you can delete one character | ||
in either string. | ||
For example: | ||
Input: "sea", "eat" | ||
Output: 2 | ||
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea". | ||
Reference: https://leetcode.com/problems/delete-operation-for-two-strings/description/ | ||
""" | ||
|
||
def min_distance(word1, word2): | ||
return len(word1) + len(word2) - 2 * lcs(word1, word2, len(word1), len(word2)) | ||
|
||
def lcs(s1, s2, i, j): | ||
""" | ||
The length of longest common subsequence among the two given strings s1 and s2 | ||
""" | ||
if i == 0 or j == 0: | ||
return 0 | ||
elif s1[i - 1] == s2[j - 1]: | ||
return 1 + lcs(s1, s2, i - 1, j - 1) | ||
else: | ||
return max(lcs(s1, s2, i - 1, j), lcs(s1, s2, i, j - 1)) | ||
|
||
# TODO: Using dynamic programming |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters