Skip to content

Commit 77054e7

Browse files
Hai Hoang Danggoswami-rahul
authored andcommitted
PR to add some problem. (keon#375)
* Add next_greatest_letter to search * Move top sort test into test_sort * Add min_distance to string * Add binary_gap to bit
1 parent 77e8af7 commit 77054e7

File tree

13 files changed

+189
-41
lines changed

13 files changed

+189
-41
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ If you want to uninstall algorithms, it is as simple as:
112112
- [has_alternative_bit](algorithms/bit/has_alternative_bit.py)
113113
- [insert_bit](algorithms/bit/insert_bit.py)
114114
- [remove_bit](algorithms/bit/remove_bit.py)
115+
- [binary_gap](algorithms/bit/binary_gap.py)
115116
- [calculator](algorithms/calculator)
116117
- [math_parser](algorithms/calculator/math_parser.py)
117118
- [dfs](algorithms/dfs)
@@ -226,6 +227,7 @@ If you want to uninstall algorithms, it is as simple as:
226227
- [find_min_rotate](algorithms/search/find_min_rotate.py)
227228
- [search_rotate](algorithms/search/search_rotate.py)
228229
- [jump_search](algorithms/search/jump_search.py)
230+
- [next_greatest_letter](algorithms/search/next_greatest_letter.py)
229231
- [set](algorithms/set)
230232
- [randomized_set](algorithms/set/randomized_set.py)
231233
- [set_covering](algorithms/set/set_covering.py)
@@ -292,6 +294,7 @@ If you want to uninstall algorithms, it is as simple as:
292294
- [contain_string](algorithms/strings/contain_string.py)
293295
- [count_binary_substring](algorithms/strings/count_binary_substring.py)
294296
- [repeat_string](algorithms/strings/repeat_string.py)
297+
- [min_distance](algorithms/strings/min_distance.py)
295298
- [tree](algorithms/tree)
296299
- [bst](algorithms/tree/tree/bst)
297300
- [array2bst](algorithms/tree/bst/array2bst.py)

algorithms/bit/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
from .remove_bit import *
1616
from .count_flips_to_convert import *
1717
from .flip_bit_longest_sequence import *
18+
from .binary_gap import *

algorithms/bit/binary_gap.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Given a positive integer N, find and return the longest distance between two
3+
consecutive 1' in the binary representation of N.
4+
If there are not two consecutive 1's, return 0
5+
6+
For example:
7+
Input: 22
8+
Output: 2
9+
Explanation:
10+
22 in binary is 10110
11+
In the binary representation of 22, there are three ones, and two consecutive pairs of 1's.
12+
The first consecutive pair of 1's have distance 2.
13+
The second consecutive pair of 1's have distance 1.
14+
The answer is the largest of these two distances, which is 2
15+
"""
16+
def binary_gap(N):
17+
last = None
18+
ans = 0
19+
index = 0
20+
while N != 0:
21+
if N & 1:
22+
if last is not None:
23+
ans = max(ans, index - last)
24+
last = index
25+
index = index + 1
26+
N = N >> 1
27+
return ans

algorithms/search/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
from .find_min_rotate import *
99
from .search_rotate import *
1010
from .jump_search import *
11+
from .next_greatest_letter import *
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'''
2+
Given a list of sorted characters letters containing only lowercase letters,
3+
and given a target letter target, find the smallest element in the list that
4+
is larger than the given target.
5+
6+
Letters also wrap around. For example, if the target is target = 'z' and
7+
letters = ['a', 'b'], the answer is 'a'.
8+
9+
Input:
10+
letters = ["c", "f", "j"]
11+
target = "a"
12+
Output: "c"
13+
14+
Input:
15+
letters = ["c", "f", "j"]
16+
target = "c"
17+
Output: "f"
18+
19+
Input:
20+
letters = ["c", "f", "j"]
21+
target = "d"
22+
Output: "f"
23+
24+
Reference: https://leetcode.com/problems/find-smallest-letter-greater-than-target/description/
25+
'''
26+
27+
import bisect
28+
29+
"""
30+
Using bisect libarary
31+
"""
32+
def next_greatest_letter(letters, target):
33+
index = bisect.bisect(letters, target)
34+
return letters[index % len(letters)]
35+
36+
"""
37+
Using binary search: complexity O(logN)
38+
"""
39+
def next_greatest_letter_v1(letters, target):
40+
if letters[0] > target:
41+
return letters[0]
42+
if letters[len(letters) - 1] <= target:
43+
return letters[0]
44+
left, right = 0, len(letters) - 1
45+
while left <= right:
46+
mid = left + (right - left) // 2
47+
if letters[mid] > target:
48+
right = mid - 1
49+
else:
50+
left = mid + 1
51+
return letters[left]
52+
53+
"""
54+
Brute force: complexity O(N)
55+
"""
56+
def next_greatest_letter_v2(letters, target):
57+
for index in letters:
58+
if index > target:
59+
return index
60+
return letters[0]

algorithms/strings/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
from .contain_string import *
2929
from .count_binary_substring import *
3030
from .repeat_string import *
31-
from .text_justification import *
31+
from .text_justification import *
32+
from .min_distance import *

algorithms/strings/min_distance.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Given two words word1 and word2, find the minimum number of steps required to
3+
make word1 and word2 the same, where in each step you can delete one character
4+
in either string.
5+
6+
For example:
7+
Input: "sea", "eat"
8+
Output: 2
9+
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
10+
11+
Reference: https://leetcode.com/problems/delete-operation-for-two-strings/description/
12+
"""
13+
14+
def min_distance(word1, word2):
15+
return len(word1) + len(word2) - 2 * lcs(word1, word2, len(word1), len(word2))
16+
17+
def lcs(s1, s2, i, j):
18+
"""
19+
The length of longest common subsequence among the two given strings s1 and s2
20+
"""
21+
if i == 0 or j == 0:
22+
return 0
23+
elif s1[i - 1] == s2[j - 1]:
24+
return 1 + lcs(s1, s2, i - 1, j - 1)
25+
else:
26+
return max(lcs(s1, s2, i - 1, j), lcs(s1, s2, i, j - 1))
27+
28+
# TODO: Using dynamic programming

tests/graph/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/graph/test_topsort.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

tests/test_bit.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
find_difference,
1616
has_alternative_bit, has_alternative_bit_fast,
1717
insert_one_bit, insert_mult_bits,
18-
remove_bit
18+
remove_bit,
19+
binary_gap
1920
)
2021

2122
import unittest
@@ -238,6 +239,16 @@ def test_remove_bit(self):
238239
self.assertEqual(5, remove_bit(21, 4))
239240
self.assertEqual(10, remove_bit(21, 0))
240241

242+
def test_binary_gap(self):
243+
# 22 = 10110
244+
self.assertEqual(2, binary_gap(22))
245+
# 6 = 110
246+
self.assertEqual(1, binary_gap(6))
247+
# 8 = 1000
248+
self.assertEqual(0, binary_gap(8))
249+
# 145 = 10010001
250+
self.assertEqual(4, binary_gap(145))
251+
241252

242253
if __name__ == '__main__':
243254
unittest.main()

0 commit comments

Comments
 (0)