Skip to content

Commit 0cc7ee0

Browse files
committed
added Treasure Island II and Search Suggestions System
1 parent 2612e0a commit 0cc7ee0

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Languages used: Java and Python
130130
- [Longest Common Prefix](String/LongestCommonPrefix)
131131
- [Minimum Steps to Make Two Strings Anagram](String/MinStepsToMakeTwoStringsAnagram)
132132
- [Rearrange Words in Sentence](String/RearrangeWordsInSentence)
133+
- [Search Suggestions System](String/SearchSuggestionsSystem)
133134
- [String Matching in Array](String/StringMatchingInArray)
134135
- [Substring with Exactly K Distinct Characters](String/SubstrWithExactKDistinctChars)
135136

Recursion/TreasureIsland2/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Treasure Island II
2+
3+
[Leetcode Link](https://leetcode.com/discuss/interview-question/356150)
4+
5+
## Problem:
6+
7+
You have a map that marks the locations of treasure islands. Some of the map area has jagged rocks and dangerous reefs. Other areas are safe to sail in. There are other explorers trying to find the treasure. So you must figure out a shortest route to one of the treasure islands.
8+
9+
Assume the map area is a two dimensional grid, represented by a matrix of characters. You must start from one of the starting point (marked as S) of the map and can move one block up, down, left or right at a time. The treasure island is marked as X. Any block with dangerous rocks or reefs will be marked as D. You must not enter dangerous blocks. You cannot leave the map area. Other areas O are safe to sail in. Output the minimum number of steps to get to any of the treasure islands.
10+
11+
## Example:
12+
13+
```
14+
Input:
15+
[['S', 'O', 'O', 'S', 'S'],
16+
['D', 'O', 'D', 'O', 'D'],
17+
['O', 'O', 'O', 'O', 'X'],
18+
['X', 'D', 'D', 'O', 'O'],
19+
['X', 'D', 'D', 'D', 'O']]
20+
21+
Output: 3
22+
Explanation:
23+
You can start from (0,0), (0, 3) or (0, 4). The treasure locations are (2, 4) (3, 0) and (4, 0). Here the shortest route is (0, 3), (1, 3), (2, 3), (2, 4).
24+
```

Recursion/TreasureIsland2/solution.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
SOURCE = 'S'
2+
DANGER = 'D'
3+
TREASURE = 'X'
4+
5+
6+
def shortestRoute(map):
7+
routes = set()
8+
9+
def dfs(map, currentRow, currentCol, visited=list()):
10+
# check for out of bound or dangerous or visited
11+
if currentRow < 0 or currentRow >= len(map) or currentCol < 0 or currentCol >= len(map[currentRow]) or map[currentRow][currentCol] == DANGER or (currentRow, currentCol) in visited:
12+
return
13+
if map[currentRow][currentCol] == TREASURE:
14+
# optional print route for debugging (if not used, visited can be set() instead of list() for a tiny performance improvement)
15+
route = [v for v in visited]
16+
route.append((currentRow, currentCol))
17+
print("Route:", route)
18+
routes.add(len(visited))
19+
return
20+
# navigate
21+
# print(f'{map[currentRow][currentCol]} ({currentRow}, {currentCol})')
22+
visited.append((currentRow, currentCol))
23+
dfs(map, currentRow-1, currentCol, visited) # up
24+
dfs(map, currentRow, currentCol+1, visited) # right
25+
dfs(map, currentRow+1, currentCol, visited) # down
26+
dfs(map, currentRow, currentCol-1, visited) # left
27+
visited.remove((currentRow, currentCol))
28+
29+
for i, row in enumerate(map):
30+
for j, mark in enumerate(row):
31+
if mark == SOURCE:
32+
dfs(map, i, j)
33+
return min(routes) if routes else None
34+
35+
36+
map = [['S', 'O', 'O', 'S', 'S'],
37+
['D', 'O', 'D', 'O', 'D'],
38+
['O', 'O', 'O', 'O', 'X'],
39+
['X', 'D', 'D', 'O', 'O'],
40+
['X', 'D', 'D', 'D', 'O']]
41+
42+
print(shortestRoute(map))
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Search Suggestions System
2+
3+
[Leetcode Link](https://leetcode.com/problems/search-suggestions-system/)
4+
5+
## Problem:
6+
7+
Given an array of strings `products` and a string `searchWord`. We want to design a system that suggests at most three product names from `products` after each character of `searchWord` is typed. Suggested `products` should have common prefix with the `searchWord`. If there are more than three `products` with a common prefix return the three lexicographically minimums `products`.
8+
9+
Return `list of lists` of the suggested products after each character of `searchWord` is typed.
10+
11+
## Example:
12+
13+
```
14+
Input: products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
15+
Output: [
16+
["mobile","moneypot","monitor"],
17+
["mobile","moneypot","monitor"],
18+
["mouse","mousepad"],
19+
["mouse","mousepad"],
20+
["mouse","mousepad"]
21+
]
22+
Explanation: products sorted lexicographically = ["mobile","moneypot","monitor","mouse","mousepad"]
23+
After typing m and mo all products match and we show user ["mobile","moneypot","monitor"]
24+
After typing mou, mous and mouse the system suggests ["mouse","mousepad"]
25+
```
26+
27+
```
28+
Input: products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"
29+
Output: [["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]
30+
```
31+
32+
```
33+
Input: products = ["havana"], searchWord = "tatiana"
34+
Output: [[],[],[],[],[],[],[]]
35+
```
36+
37+
## Note:
38+
39+
- 1 <= products.length <= 1000
40+
- There are no repeated elements in products.
41+
- 1 <= Σ products[i].length <= 2 \* 10^4
42+
- All characters of products[i] are lower-case English letters.
43+
- 1 <= searchWord.length <= 1000
44+
- All characters of searchWord are lower-case English letters.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:
6+
searchLists = list()
7+
# lexigraphical sort
8+
products.sort()
9+
typed = ""
10+
for ch in searchWord:
11+
typed += ch
12+
searchLists.append(
13+
[p for p in products if p.startswith(typed)][:3])
14+
return searchLists
15+
16+
17+
sol = Solution()
18+
products = ["mobile", "mouse", "moneypot", "monitor", "mousepad"]
19+
searchWord = "mouse"
20+
print(sol.suggestedProducts(products, searchWord))
21+
22+
products = ["bags", "baggage", "banner", "box", "cloths"]
23+
searchWord = "bags"
24+
print(sol.suggestedProducts(products, searchWord))
25+
26+
products = ["havana"]
27+
searchWord = "tatiana"
28+
print(sol.suggestedProducts(products, searchWord))

0 commit comments

Comments
 (0)