Skip to content

Commit d79fd2e

Browse files
committed
Adding solution of 1029, 1030, 1031, 1032
1 parent aa888b5 commit d79fd2e

File tree

5 files changed

+234
-0
lines changed

5 files changed

+234
-0
lines changed

1000-1100q/1029.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
There are 2N people a company is planning to interview. The cost of flying the i-th person to city A is costs[i][0], and the cost of flying the i-th person to city B is costs[i][1].
3+
4+
Return the minimum cost to fly every person to a city such that exactly N people arrive in each city.
5+
6+
7+
8+
Example 1:
9+
10+
Input: [[10,20],[30,200],[400,50],[30,20]]
11+
Output: 110
12+
Explanation:
13+
The first person goes to city A for a cost of 10.
14+
The second person goes to city A for a cost of 30.
15+
The third person goes to city B for a cost of 50.
16+
The fourth person goes to city B for a cost of 20.
17+
18+
The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.
19+
20+
21+
Note:
22+
23+
1 <= costs.length <= 100
24+
It is guaranteed that costs.length is even.
25+
1 <= costs[i][0], costs[i][1] <= 1000
26+
'''
27+
28+
class Solution(object):
29+
def twoCitySchedCost(self, costs):
30+
"""
31+
:type costs: List[List[int]]
32+
:rtype: int
33+
"""
34+
result = 0
35+
costs = sorted(costs, key=lambda x : x[0] - x[1])
36+
for index in range(len(costs)):
37+
if index < len(costs)//2:
38+
result += costs[index][0]
39+
else:
40+
result += costs[index][1]
41+
return result

1000-1100q/1030.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'''
2+
We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 <= r < R and 0 <= c < C.
3+
4+
Additionally, we are given a cell in that matrix with coordinates (r0, c0).
5+
6+
Return the coordinates of all cells in the matrix, sorted by their distance from (r0, c0) from smallest distance to largest distance. Here, the distance between two cells (r1, c1) and (r2, c2) is the Manhattan distance, |r1 - r2| + |c1 - c2|. (You may return the answer in any order that satisfies this condition.)
7+
8+
9+
10+
Example 1:
11+
12+
Input: R = 1, C = 2, r0 = 0, c0 = 0
13+
Output: [[0,0],[0,1]]
14+
Explanation: The distances from (r0, c0) to other cells are: [0,1]
15+
Example 2:
16+
17+
Input: R = 2, C = 2, r0 = 0, c0 = 1
18+
Output: [[0,1],[0,0],[1,1],[1,0]]
19+
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2]
20+
The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.
21+
Example 3:
22+
23+
Input: R = 2, C = 3, r0 = 1, c0 = 2
24+
Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
25+
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2,2,3]
26+
There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].
27+
28+
29+
Note:
30+
31+
1 <= R <= 100
32+
1 <= C <= 100
33+
0 <= r0 < R
34+
0 <= c0 < C
35+
'''
36+
37+
class Solution(object):
38+
def allCellsDistOrder(self, R, C, r0, c0):
39+
"""
40+
:type R: int
41+
:type C: int
42+
:type r0: int
43+
:type c0: int
44+
:rtype: List[List[int]]
45+
"""
46+
cells = [[x, y] for x in range(R) for y in range(C)]
47+
distance = {}
48+
for cell in cells:
49+
diff = abs(cell[0]-r0) + abs(cell[1]-c0)
50+
if diff in distance:
51+
distance[diff].append(cell)
52+
else:
53+
distance[diff] = [cell]
54+
result = []
55+
for key in sorted(distance):
56+
for value in distance[key]:
57+
result.append(value)
58+
return result

1000-1100q/1031.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'''
2+
Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M. (For clarification, the L-length subarray could occur before or after the M-length subarray.)
3+
4+
Formally, return the largest V for which V = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1] + ... + A[j+M-1]) and either:
5+
6+
0 <= i < i + L - 1 < j < j + M - 1 < A.length, or
7+
0 <= j < j + M - 1 < i < i + L - 1 < A.length.
8+
9+
10+
Example 1:
11+
12+
Input: A = [0,6,5,2,2,5,1,9,4], L = 1, M = 2
13+
Output: 20
14+
Explanation: One choice of subarrays is [9] with length 1, and [6,5] with length 2.
15+
Example 2:
16+
17+
Input: A = [3,8,1,3,2,1,8,9,0], L = 3, M = 2
18+
Output: 29
19+
Explanation: One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.
20+
Example 3:
21+
22+
Input: A = [2,1,5,6,0,9,5,0,3,8], L = 4, M = 3
23+
Output: 31
24+
Explanation: One choice of subarrays is [5,6,0,9] with length 4, and [3,8] with length 3.
25+
26+
27+
Note:
28+
29+
L >= 1
30+
M >= 1
31+
L + M <= A.length <= 1000
32+
0 <= A[i] <= 1000
33+
'''
34+
35+
class Solution(object):
36+
def maxSumTwoNoOverlap(self, A, L, M):
37+
"""
38+
:type A: List[int]
39+
:type L: int
40+
:type M: int
41+
:rtype: int
42+
"""
43+
cumm_sum = [0]
44+
for index in range(len(A)):
45+
cumm_sum.append(cumm_sum[index]+A[index])
46+
result = 0
47+
48+
def valid(index_i, index_j):
49+
return index_i+L <=len(A) and index_j+M <= len(A) and(index_j>=index_i+L or index_i>=index_j+M)
50+
51+
for index_i in range(len(A)):
52+
for index_j in range(len(A)):
53+
if valid(index_i, index_j):
54+
result = max(result, cumm_sum[index_i+L]-cumm_sum[index_i] + cumm_sum[index_j+M]-cumm_sum[index_j])
55+
return result

1000-1100q/1032.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'''
2+
Implement the StreamChecker class as follows:
3+
4+
StreamChecker(words): Constructor, init the data structure with the given words.
5+
query(letter): returns true if and only if for some k >= 1, the last k characters queried (in order from oldest to newest, including this letter just queried) spell one of the words in the given list.
6+
7+
8+
Example:
9+
10+
StreamChecker streamChecker = new StreamChecker(["cd","f","kl"]); // init the dictionary.
11+
streamChecker.query('a'); // return false
12+
streamChecker.query('b'); // return false
13+
streamChecker.query('c'); // return false
14+
streamChecker.query('d'); // return true, because 'cd' is in the wordlist
15+
streamChecker.query('e'); // return false
16+
streamChecker.query('f'); // return true, because 'f' is in the wordlist
17+
streamChecker.query('g'); // return false
18+
streamChecker.query('h'); // return false
19+
streamChecker.query('i'); // return false
20+
streamChecker.query('j'); // return false
21+
streamChecker.query('k'); // return false
22+
streamChecker.query('l'); // return true, because 'kl' is in the wordlist
23+
24+
25+
Note:
26+
27+
1 <= words.length <= 2000
28+
1 <= words[i].length <= 2000
29+
Words will only consist of lowercase English letters.
30+
Queries will only consist of lowercase English letters.
31+
The number of queries is at most 40000.
32+
'''
33+
34+
class Trie(object):
35+
def __init__(self):
36+
self.nodes = {}
37+
self.word = False
38+
39+
class StreamChecker(object):
40+
41+
def __init__(self, words):
42+
"""
43+
:type words: List[str]
44+
"""
45+
self.trie_node = Trie()
46+
for word in words:
47+
ptr = self.trie_node
48+
for char in reversed(word):
49+
if char not in ptr.nodes:
50+
ptr.nodes[char] = Trie()
51+
ptr = ptr.nodes[char]
52+
ptr.word = True
53+
self.stream = []
54+
55+
56+
def query(self, letter):
57+
"""
58+
:type letter: str
59+
:rtype: bool
60+
"""
61+
self.stream.append(letter)
62+
root = self.trie_node
63+
for char in reversed(self.stream):
64+
if char not in root.nodes:
65+
return False
66+
if root.nodes[char].word:
67+
return True
68+
root = root.nodes[char]
69+
70+
return root.word
71+
72+
73+
74+
# Your StreamChecker object will be instantiated and called as such:
75+
# obj = StreamChecker(words)
76+
# param_1 = obj.query(letter)

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
77
##### [Problems 1000-1100](./1000-1100q/)
88
| # | Title | Solution | Difficulty |
99
|---| ----- | -------- | ---------- |
10+
|1032|[Stream of Characters](https://leetcode.com/problems/stream-of-characters)|[Python](./1000-1100q/1032.py)|Hard|
11+
|1031|[Maximum Sum of Two Non-Overlapping Subarrays](https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays)|[Python](./1000-1100q/1031.py)|Medium|
12+
|1030|[Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order)|[Python](./1000-1100/1030.py)|Easy|
13+
|1029|[Two City Scheduling](https://leetcode.com/problems/two-city-scheduling)|[Python](./1000-1100q/1029.py)|Easy|
1014
|1028|[Recover a Tree From Preorder Traversal](https://leetcode.com/problems/recover-a-tree-from-preorder-traversal)|[Python](./1000-1100q/1028.py)|Hard|
1115
|1027|[Longest Arithmetic Sequence](https://leetcode.com/problems/longest-arithmetic-sequence)|[Python](./1000-1100q/1027.py)|Medium|
1216
|1026|[Maximum Difference Between Node and Ancestor](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor)|[Python](./1000-1100q/1026.py)|Medium|

0 commit comments

Comments
 (0)