Skip to content

Commit cb06d73

Browse files
committed
Solution of weekly contest 164
1 parent d4546ce commit cb06d73

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

Diff for: 1200-1300q/1266.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
On a plane there are n points with integer coordinates points[i] = [xi, yi]. Your task is to find the minimum time in seconds to visit all points.
3+
4+
You can move according to the next rules:
5+
6+
In one second always you can either move vertically, horizontally by one unit or diagonally (it means to move one unit vertically and one unit horizontally in one second).
7+
You have to visit the points in the same order as they appear in the array.
8+
9+
Input: points = [[1,1],[3,4],[-1,0]]
10+
Output: 7
11+
Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]
12+
Time from [1,1] to [3,4] = 3 seconds
13+
Time from [3,4] to [-1,0] = 4 seconds
14+
Total time = 7 seconds
15+
Example 2:
16+
17+
Input: points = [[3,2],[-2,2]]
18+
Output: 5
19+
20+
21+
Constraints:
22+
23+
points.length == n
24+
1 <= n <= 100
25+
points[i].length == 2
26+
-1000 <= points[i][0], points[i][1] <= 1000
27+
'''
28+
29+
class Solution(object):
30+
def minTimeToVisitAllPoints(self, points):
31+
"""
32+
:type points: List[List[int]]
33+
:rtype: int
34+
"""
35+
if not points:
36+
return 0
37+
result = 0
38+
for index in range(1, len(points)):
39+
result += max(abs(points[index][0]-points[index-1][0]), abs(points[index][1]-points[index-1][1]))
40+
return result

Diff for: 1200-1300q/1267.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'''
2+
You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column.
3+
4+
Return the number of servers that communicate with any other server.
5+
6+
Example 1:
7+
8+
Input: grid = [[1,0],[0,1]]
9+
Output: 0
10+
Explanation: No servers can communicate with others.
11+
12+
Example 2:
13+
14+
Input: grid = [[1,0],[1,1]]
15+
Output: 3
16+
Explanation: All three servers can communicate with at least one other server.
17+
18+
Example 3:
19+
20+
Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
21+
Output: 4
22+
Explanation: The two servers in the first row can communicate with each other. The two servers in the third column can communicate with each other. The server at right bottom corner can't communicate with any other server.
23+
24+
25+
Constraints:
26+
27+
m == grid.length
28+
n == grid[i].length
29+
1 <= m <= 250
30+
1 <= n <= 250
31+
grid[i][j] == 0 or 1
32+
'''
33+
34+
class Solution(object):
35+
def countServers(self, grid):
36+
"""
37+
:type grid: List[List[int]]
38+
:rtype: int
39+
"""
40+
if not grid:
41+
return 0
42+
43+
row_count = [0] * len(grid)
44+
col_count = [0] * len(grid[0])
45+
for index_r in range(len(grid)):
46+
for index_c in range(len(grid[0])):
47+
if grid[index_r][index_c] == 1:
48+
row_count[index_r] += 1
49+
col_count[index_c] += 1
50+
51+
result = 0
52+
for index_r in range(len(grid)):
53+
for index_c in range(len(grid[0])):
54+
if grid[index_r][index_c] == 1 and (row_count[index_r] > 1 or col_count[index_c] > 1):
55+
result += 1
56+
return result

Diff for: 1200-1300q/1268.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'''
2+
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.
3+
4+
Return list of lists of the suggested products after each character of searchWord is typed.
5+
6+
7+
8+
Example 1:
9+
10+
Input: products = ["mobile","mouse","moneypot","monitor","mousepad"], searchWord = "mouse"
11+
Output: [
12+
["mobile","moneypot","monitor"],
13+
["mobile","moneypot","monitor"],
14+
["mouse","mousepad"],
15+
["mouse","mousepad"],
16+
["mouse","mousepad"]
17+
]
18+
Explanation: products sorted lexicographically = ["mobile","moneypot","monitor","mouse","mousepad"]
19+
After typing m and mo all products match and we show user ["mobile","moneypot","monitor"]
20+
After typing mou, mous and mouse the system suggests ["mouse","mousepad"]
21+
Example 2:
22+
23+
Input: products = ["havana"], searchWord = "havana"
24+
Output: [["havana"],["havana"],["havana"],["havana"],["havana"],["havana"]]
25+
Example 3:
26+
27+
Input: products = ["bags","baggage","banner","box","cloths"], searchWord = "bags"
28+
Output: [["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]
29+
Example 4:
30+
31+
Input: products = ["havana"], searchWord = "tatiana"
32+
Output: [[],[],[],[],[],[],[]]
33+
34+
35+
Constraints:
36+
37+
1 <= products.length <= 1000
38+
There are no repeated elements in products.
39+
1 <= Σ products[i].length <= 2 * 10^4
40+
All characters of products[i] are lower-case English letters.
41+
1 <= searchWord.length <= 1000
42+
All characters of searchWord are lower-case English letters.
43+
'''
44+
class TrieNode(object):
45+
def __init__(self):
46+
self.words = []
47+
self.children = {}
48+
49+
class Trie(object):
50+
def __init__(self):
51+
self.root = TrieNode()
52+
53+
def insert(self, word):
54+
node = self.root
55+
for char in word:
56+
if char not in node.children:
57+
node.children[char] = TrieNode()
58+
node = node.children[char]
59+
node.words.append(word)
60+
node.words.sort()
61+
if len(node.words) > 3:
62+
node.words = node.words[:3]
63+
64+
def search(self, word):
65+
result, node = [], self.root
66+
for char in word:
67+
if char not in node.children:
68+
break
69+
node = node.children[char]
70+
result.append(node.words[:])
71+
for _ in range(len(word) - len(result)):
72+
result.append([])
73+
return result
74+
75+
class Solution(object):
76+
def suggestedProducts(self, products, searchWord):
77+
"""
78+
:type products: List[str]
79+
:type searchWord: str
80+
:rtype: List[List[str]]
81+
"""
82+
trie = Trie()
83+
for product in products:
84+
trie.insert(product)
85+
return trie.search(searchWord)

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
1414
##### [Problems 1100-1200](./1200-1300q/)
1515
| # | Title | Solution | Difficulty |
1616
|---| ----- | -------- | ---------- |
17+
|1268|[Search Suggestions System](https://leetcode.com/problems/search-suggestions-system/)|[Python](./1200-1300q/1268.py)|Medium|
18+
|1267|[Count Servers that Communicate](https://leetcode.com/problems/count-servers-that-communicate/)|[Python](./1200-1300q/1267.py)|Medium|
19+
|1266|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Python](./1200-1300q/1266.py)|Easy|
1720
|1200|[Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/)|[Python](./1200-1300q/1200.py)|Easy|
1821

1922
##### [Problems 1100-1200](./1100-1200q/)

0 commit comments

Comments
 (0)