Skip to content

Commit 2110e01

Browse files
authored
feat: add solutions to lc problems: No.1351+ (doocs#2156)
1 parent 6bc43e4 commit 2110e01

File tree

27 files changed

+537
-69
lines changed

27 files changed

+537
-69
lines changed

solution/1300-1399/1351.Count Negative Numbers in a Sorted Matrix/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@
5252

5353
遍历结束,返回答案。
5454

55-
时间复杂度 $O(m + n)$其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
55+
时间复杂度 $O(m + n)$其中 $m$ 和 $n$ 分别为矩阵的行数和列数。空间复杂度 $O(1)$
5656

5757
**方法二:二分查找**
5858

5959
遍历每一行,二分查找每一行第一个小于 $0$ 的位置,从该位置开始往右的所有元素均为负数,累加负数个数到答案中。
6060

6161
遍历结束,返回答案。
6262

63-
时间复杂度 $O(m \times \log n)$其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
63+
时间复杂度 $O(m \times \log n)$其中 $m$ 和 $n$ 分别为矩阵的行数和列数。空间复杂度 $O(1)$
6464

6565
<!-- tabs:start -->
6666

solution/1300-1399/1351.Count Negative Numbers in a Sorted Matrix/README_EN.md

+18
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@
3737

3838
## Solutions
3939

40+
**Solution 1: Start Traversing from the Bottom Left or Top Right**
41+
42+
According to the characteristic that **both rows and columns are arranged in non-increasing order**, we can start traversing from the **bottom left corner** towards the **top right direction**.
43+
44+
When encountering a negative number, it indicates that all elements to the right of the current position in this row are negative. We add the number of remaining elements in this row to the answer, that is, $n - j$, and move up a row, that is, $i \leftarrow i - 1$. Otherwise, move to the right column, that is, $j \leftarrow j + 1$.
45+
46+
After the traversal is over, return the answer.
47+
48+
The time complexity is $O(m + n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$.
49+
50+
**Solution 2: Binary Search**
51+
52+
Traverse each row, use binary search to find the first position less than $0$ in each row. All elements to the right of this position are negative, and add the number of negative numbers to the answer.
53+
54+
After the traversal is over, return the answer.
55+
56+
The time complexity is $O(m \times \log n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$.
57+
4058
<!-- tabs:start -->
4159

4260
### **Python3**

solution/1300-1399/1352.Product of the Last K Numbers/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ productOfNumbers.getProduct(2); // return 32. The product of the last 2 numbers
5353

5454
## Solutions
5555

56+
**Solution 1: Prefix Product**
57+
58+
We initialize an array $s$, where $s[i]$ represents the product of the first $i$ numbers.
59+
60+
When calling `add(num)`, we judge whether `num` is $0$. If it is, we set $s$ to `[1]`. Otherwise, we multiply the last element of $s$ by `num` and add the result to the end of $s$.
61+
62+
When calling `getProduct(k)`, we now judge whether the length of $s$ is less than or equal to $k$. If it is, we return $0$. Otherwise, we return the last element of $s$ divided by the $k + 1$th element from the end of $s$. That is, $s[-1] / s[-k - 1]$.
63+
64+
The time complexity is $O(1)$, and the space complexity is $O(n)$. Where $n$ is the number of times `add` is called.
65+
5666
<!-- tabs:start -->
5767

5868
### **Python3**

solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
然后从优先队列中取出结束时间最小的会议,即为当前时间可以参加的会议,累加答案数。如果优先队列为空,则说明当前时间没有可以参加的会议。
5959

60-
时间复杂度 $O(m\log n)$。其中 $m$, $n$ 分别表示会议的最大结束时间,以及会议的数量。
60+
时间复杂度 $O(m \times \log n)$,空间复杂度 $O(n)$。其中 $m$, $n$ 分别表示会议的最大结束时间,以及会议的数量。
6161

6262
<!-- tabs:start -->
6363

solution/1300-1399/1353.Maximum Number of Events That Can Be Attended/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ Attend the third event on day 3.
4141

4242
## Solutions
4343

44+
**Solution 1: Hash Table + Greedy + Priority Queue**
45+
46+
Define a hash table to record the start and end times of each meeting, where the key is the start time of the meeting, and the value is a list of end times.
47+
48+
Enumerate the current time $s$, find all meetings that start at the current time, and add their end times to the priority queue (min heap). At the same time, the priority queue needs to remove all meetings that end before the current time.
49+
50+
Then, take out the meeting with the smallest end time from the priority queue, which is the meeting that can be attended at the current time, and accumulate the answer count. If the priority queue is empty, it means that there are no meetings that can be attended at the current time.
51+
52+
The time complexity is $O(m \times \log n)$, and the space complexity is $O(n)$. Where $m$ and $n$ represent the maximum end time of the meetings and the number of meetings, respectively.
53+
4454
<!-- tabs:start -->
4555

4656
### **Python3**

solution/1300-1399/1354.Construct Target Array With Multiple Sums/README.md

+130-20
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:逆向构造 + 优先队列(大根堆)**
60+
61+
我们发现,如果从数组 $arr$ 开始正向构造目标数组 $target$,每次都不好确定选择哪个下标 $i$,问题比较复杂。而如果我们从数组 $target$ 开始逆向构造,每次构造都一定是选择当前数组中最大的元素,这样就可以保证每次构造都是唯一的,问题比较简单。
62+
63+
因此,我们可以使用优先队列(大根堆)来存储数组 $target$ 中的元素,用一个变量 $s$ 记录数组 $target$ 中所有元素的和。每次从优先队列中取出最大的元素 $mx$,计算当前数组中除 $mx$ 以外的所有元素之和 $t$,如果 $t \lt 1$ 或者 $mx - t \lt 1$,则说明无法构造目标数组 $target$,返回 `false`。否则,我们计算 $mx \bmod t$,如果 $mx \bmod t = 0$,则令 $x = t$,否则令 $x = mx \bmod t$,将 $x$ 加入优先队列中,并更新 $s$ 的值,重复上述操作,直到优先队列中的所有元素都变为 $1$,此时返回 `true`
64+
65+
时间复杂度 $O(n \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $target$ 的长度。
66+
5967
<!-- tabs:start -->
6068

6169
### **Python3**
@@ -65,36 +73,138 @@
6573
```python
6674
class Solution:
6775
def isPossible(self, target: List[int]) -> bool:
68-
if len(target) == 1:
69-
return target[0] == 1
70-
71-
summ = sum(target)
72-
maxHeap = [-num for num in target]
73-
heapq.heapify(maxHeap)
74-
75-
while -maxHeap[0] > 1:
76-
maxi = -heapq.heappop(maxHeap)
77-
restSum = summ - maxi
78-
# Only occurs if n == 2.
79-
if restSum == 1:
80-
return True
81-
updated = maxi % restSum
82-
# Updated == 0 (invalid) or didn't change.
83-
if updated == 0 or updated == maxi:
76+
s = sum(target)
77+
pq = [-x for x in target]
78+
heapify(pq)
79+
while -pq[0] > 1:
80+
mx = -heappop(pq)
81+
t = s - mx
82+
if t == 0 or mx - t < 1:
8483
return False
85-
heapq.heappush(maxHeap, -updated)
86-
summ = summ - maxi + updated
87-
84+
x = (mx % t) or t
85+
heappush(pq, -x)
86+
s = s - mx + x
8887
return True
89-
9088
```
9189

9290
### **Java**
9391

9492
<!-- 这里可写当前语言的特殊实现逻辑 -->
9593

9694
```java
95+
class Solution {
96+
public boolean isPossible(int[] target) {
97+
PriorityQueue<Long> pq = new PriorityQueue<>(Collections.reverseOrder());
98+
long s = 0;
99+
for (int x : target) {
100+
s += x;
101+
pq.offer((long) x);
102+
}
103+
while (pq.peek() > 1) {
104+
long mx = pq.poll();
105+
long t = s - mx;
106+
if (t == 0 || mx - t < 1) {
107+
return false;
108+
}
109+
long x = mx % t;
110+
if (x == 0) {
111+
x = t;
112+
}
113+
pq.offer(x);
114+
s = s - mx + x;
115+
}
116+
return true;
117+
}
118+
}
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
class Solution {
125+
public:
126+
bool isPossible(vector<int>& target) {
127+
priority_queue<int> pq;
128+
long long s = 0;
129+
for (int i = 0; i < target.size(); i++) {
130+
s += target[i];
131+
pq.push(target[i]);
132+
}
133+
while (pq.top() != 1) {
134+
int mx = pq.top();
135+
pq.pop();
136+
long long t = s - mx;
137+
if (t < 1 || mx - t < 1) {
138+
return false;
139+
}
140+
int x = mx % t;
141+
if (x == 0) {
142+
x = t;
143+
}
144+
pq.push(x);
145+
s = s - mx + x;
146+
}
147+
return true;
148+
}
149+
};
150+
```
151+
152+
### **Go**
153+
154+
```go
155+
func isPossible(target []int) bool {
156+
pq := &hp{target}
157+
s := 0
158+
for _, x := range target {
159+
s += x
160+
}
161+
heap.Init(pq)
162+
for target[0] > 1 {
163+
mx := target[0]
164+
t := s - mx
165+
if t < 1 || mx-t < 1 {
166+
return false
167+
}
168+
x := mx % t
169+
if x == 0 {
170+
x = t
171+
}
172+
target[0] = x
173+
heap.Fix(pq, 0)
174+
s = s - mx + x
175+
}
176+
return true
177+
}
178+
179+
type hp struct{ sort.IntSlice }
180+
181+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
182+
func (hp) Pop() (_ any) { return }
183+
func (hp) Push(any) {}
184+
```
97185

186+
### **TypeScript**
187+
188+
```ts
189+
function isPossible(target: number[]): boolean {
190+
const pq = new MaxPriorityQueue();
191+
let s = 0;
192+
for (const x of target) {
193+
s += x;
194+
pq.enqueue(x);
195+
}
196+
while (pq.front().element > 1) {
197+
const mx = pq.dequeue().element;
198+
const t = s - mx;
199+
if (t < 1 || mx - t < 1) {
200+
return false;
201+
}
202+
const x = mx % t || t;
203+
pq.enqueue(x);
204+
s = s - mx + x;
205+
}
206+
return true;
207+
}
98208
```
99209

100210
### **...**

0 commit comments

Comments
 (0)