Skip to content

Commit 1c5705d

Browse files
committed
Add weekly contest questions
1 parent 91014d6 commit 1c5705d

File tree

4 files changed

+255
-0
lines changed

4 files changed

+255
-0
lines changed

5179_balance_bst.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
3+
5179. Balance a Binary Search Tree
4+
User Accepted:0
5+
User Tried:0
6+
Total Accepted:0
7+
Total Submissions:0
8+
Difficulty:Medium
9+
Given a binary search tree, return a balanced binary search tree with the same node values.
10+
11+
A binary search tree is balanced if and only if the depth of the two subtrees of every node never differ by more than 1.
12+
13+
If there is more than one answer, return any of them.
14+
15+
16+
Input: root = [1,null,2,null,3,null,4,null,null]
17+
Output: [2,1,3,null,null,null,4]
18+
Explanation: This is not the only correct answer, [3,1,4,null,2,null,null] is also correct.
19+
20+
Constraints:
21+
22+
The number of nodes in the tree is between 1 and 10^4.
23+
The tree nodes will have distinct values between 1 and 10^5.
24+
25+
'''
26+
# Definition for a binary tree node.
27+
class TreeNode:
28+
def __init__(self, x):
29+
self.val = x
30+
self.left = None
31+
self.right = None
32+
33+
class Solution:
34+
def balanceBST(self, root: TreeNode) -> TreeNode:
35+
36+
37+
s = Solution()
38+
m = [[3,7,8],[9,11,13],[15,16,17]]
39+
print(s.luckyNumbers(m))

5356_luck_number_in_matrix.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
3+
Given a m * n matrix of distinct numbers, return all lucky numbers in the matrix in any order.
4+
5+
A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.
6+
7+
8+
9+
Example 1:
10+
11+
Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
12+
Output: [15]
13+
Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column
14+
Example 2:
15+
16+
Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
17+
Output: [12]
18+
Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column.
19+
Example 3:
20+
21+
Input: matrix = [[7,8],[1,2]]
22+
Output: [7]
23+
24+
25+
Constraints:
26+
27+
m == mat.length
28+
n == mat[i].length
29+
1 <= n, m <= 50
30+
1 <= matrix[i][j] <= 10^5.
31+
All elements in the matrix are distinct.
32+
33+
'''
34+
class Solution(object):
35+
def luckyNumbers(self, matrix):
36+
min_nums_in_rows = set()
37+
max_nums_in_cols = set()
38+
for row in matrix:
39+
min_nums_in_rows.add(min(row))
40+
for i in range(len(matrix[0])):
41+
col = [matrix[j][i] for j in range(len(matrix))]
42+
max_nums_in_cols.add(max(col))
43+
result = min_nums_in_rows.intersection(max_nums_in_cols)
44+
45+
return list(result)
46+
47+
s = Solution()
48+
m = [[3,7,8],[9,11,13],[15,16,17]]
49+
print(s.luckyNumbers(m))

5357_design_stack.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'''
2+
3+
Design a stack which supports the following operations.
4+
5+
Implement the CustomStack class:
6+
7+
CustomStack(int maxSize) Initializes the object with maxSize which is the maximum number of elements in the stack or do nothing if the stack reached the maxSize.
8+
void push(int x) Adds x to the top of the stack if the stack hasn't reached the maxSize.
9+
int pop() Pops and returns the top of stack or -1 if the stack is empty.
10+
void inc(int k, int val) Increments the bottom k elements of the stack by val. If there are less than k elements in the stack, just increment all the elements in the stack.
11+
12+
Input
13+
["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"]
14+
[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
15+
Output
16+
[null,null,null,2,null,null,null,null,null,103,202,201,-1]
17+
Explanation
18+
CustomStack customStack = new CustomStack(3); // Stack is Empty []
19+
customStack.push(1); // stack becomes [1]
20+
customStack.push(2); // stack becomes [1, 2]
21+
customStack.pop(); // return 2 --> Return top of the stack 2, stack becomes [1]
22+
customStack.push(2); // stack becomes [1, 2]
23+
customStack.push(3); // stack becomes [1, 2, 3]
24+
customStack.push(4); // stack still [1, 2, 3], Don't add another elements as size is 4
25+
customStack.increment(5, 100); // stack becomes [101, 102, 103]
26+
customStack.increment(2, 100); // stack becomes [201, 202, 103]
27+
customStack.pop(); // return 103 --> Return top of the stack 103, stack becomes [201, 202]
28+
customStack.pop(); // return 202 --> Return top of the stack 102, stack becomes [201]
29+
customStack.pop(); // return 201 --> Return top of the stack 101, stack becomes []
30+
customStack.pop(); // return -1 --> Stack is empty return -1.
31+
32+
'''
33+
34+
class CustomStack:
35+
36+
def __init__(self, maxSize: int):
37+
self._max_size = maxSize
38+
self._stack = [None for _ in range(maxSize)]
39+
self._cur_index = 0
40+
self._cur_size = 0
41+
42+
def push(self, x: int) -> None:
43+
if self._cur_size >= self._max_size:
44+
return
45+
self._stack[self._cur_index % self._max_size] = x
46+
self._cur_index += 1
47+
self._cur_size += 1
48+
49+
def pop(self) -> int:
50+
if self._cur_size <= 0:
51+
return -1
52+
num = self._stack[self._cur_index % self._max_size - 1]
53+
self._cur_index -= 1
54+
self._cur_size -= 1
55+
return num
56+
57+
def increment(self, k: int, val: int) -> None:
58+
k = min(k, self._max_size)
59+
start_index = self._cur_index - self._cur_size
60+
for i in range(start_index, start_index + k):
61+
self._stack[i % self._max_size] += val
62+
63+
# Your CustomStack object will be instantiated and called as such:
64+
obj = CustomStack(3)
65+
obj.push(1)
66+
obj.push(2)
67+
print(obj._stack, obj._cur_size, obj._cur_index)
68+
param_2 = obj.pop()
69+
assert(param_2==2)
70+
obj.push(2)
71+
obj.push(3)
72+
obj.push(4)
73+
print(obj._stack, obj._cur_size, obj._cur_index)
74+
obj.increment(5,100)
75+
print(obj._stack, obj._cur_size, obj._cur_index)
76+
obj.increment(2,100)
77+
assert(obj.pop() == 103)
78+
assert(obj.pop() == 202)
79+
assert(obj.pop() == 201)
80+
assert(obj.pop() == -1)

5359_max_perf.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'''
2+
3+
There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7.
4+
5+
The performance of a team is the sum of their engineers' speeds multiplied by the minimum efficiency among their engineers.
6+
7+
Example 1:
8+
9+
Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2
10+
Output: 60
11+
Explanation:
12+
We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60.
13+
Example 2:
14+
15+
Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3
16+
Output: 68
17+
Explanation:
18+
This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68.
19+
Example 3:
20+
21+
Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4
22+
Output: 72
23+
24+
25+
Constraints:
26+
27+
1 <= n <= 10^5
28+
speed.length == n
29+
efficiency.length == n
30+
1 <= speed[i] <= 10^5
31+
1 <= efficiency[i] <= 10^8
32+
1 <= k <= n
33+
34+
'''
35+
class Solution(object):
36+
def maxPerfRec(self, n, k):
37+
assert(n > 0 and k > 0)
38+
speed, efficiency = self.speed, self.efficiency
39+
if (n,k) in self.ht:
40+
return self.ht[(n,k)]
41+
result = None
42+
if n == 1:
43+
result = speed[0], efficiency[0]
44+
elif k == 1:
45+
cur_max_perf = 0
46+
for i in range(n):
47+
perf = speed[i-1] * efficiency[i-1]
48+
if perf > cur_max_perf:
49+
cur_max_perf = perf
50+
result = speed[i-1], efficiency[i-1]
51+
else:
52+
s_no_new_p, e_no_new_p = self.maxPerfRec(n - 1, k)
53+
perf_no_new_p = s_no_new_p * e_no_new_p
54+
55+
s_less_p, e_less_p = self.maxPerfRec(n, k - 1)
56+
perf_less_p = s_less_p * e_less_p
57+
58+
prev_s, prev_e = self.maxPerfRec(n - 1, k - 1)
59+
choose_s = prev_s + speed[n-1]
60+
choose_e = min(prev_e, efficiency[n-1])
61+
perf_choose_p = choose_s * choose_e
62+
63+
if perf_choose_p > perf_no_new_p and perf_choose_p > perf_less_p:
64+
result = choose_s, choose_e
65+
elif perf_no_new_p > perf_choose_p and perf_no_new_p > perf_less_p:
66+
result = s_no_new_p, e_no_new_p
67+
else:
68+
result = s_less_p, e_less_p
69+
self.ht[(n, k)] = result
70+
return result
71+
72+
def maxPerformance(self, n, speed, efficiency, k):
73+
self.speed = speed
74+
self.efficiency = efficiency
75+
self.ht = dict()
76+
max_s, max_e = self.maxPerfRec(n, k)
77+
78+
return (max_s * max_e) % (10 ** 9 + 7)
79+
80+
s = Solution()
81+
# speed = [2,10,3,1,5,8]
82+
# efficiency = [5,4,3,9,7,2]
83+
# print(s.maxPerformance(3, speed, efficiency, 2))
84+
speed = [1,4,1,9,4,4,4]
85+
efficiency = [8,2,1,7,1,8,4]
86+
print(s.maxPerformance(7, speed, efficiency, 6))
87+
print(s.ht)

0 commit comments

Comments
 (0)