Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b30a34

Browse files
feat: add solutions to lc problems: No.1899,1918 (doocs#4324)
1 parent 2ea7f88 commit 1b30a34

File tree

9 files changed

+482
-10
lines changed

9 files changed

+482
-10
lines changed

‎solution/1800-1899/1899.Merge Triplets to Form Target Triplet/README.md‎

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,20 @@ tags:
9292

9393
### 方法一:贪心
9494

95-
我们记 $target = [x, y, z],ドル初始时 $d = e = f = 0,ドル表示当前的 $a, b, c$ 的最大值
95+
我们记 $\textit{target} = [x, y, z],ドル我们需要判断是否存在三元组 $[a, b, c]$ 使得 $a \le x, b \le y, c \le z$
9696

97-
我们遍历数组 $triplets,ドル对于每个三元组 $[a, b, c],ドル如果 $a \le x, b \le y, c \le z,ドル则将 $d, e, f$ 分别更新为 $max(d, a), max(e, b), max(f, c)$。
97+
我们可以将所有的三元组分为两类:
9898

99-
最后判断 $[d, e, f]$ 是否等于 $target$ 即可。
99+
1. 满足 $a \le x, b \le y, c \le z$ 的三元组。
100+
2. 不满足 $a \le x, b \le y, c \le z$ 的三元组。
100101

101-
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 为数组 $triplets$ 的长度。
102+
对于第一类三元组,我们可以将它们的 $a, b, c$ 分别取最大值,得到一个新的三元组 $[d, e, f]$。
103+
104+
对于第二类三元组,我们可以忽略它们,因为它们无法帮助我们得到目标三元组。
105+
106+
最后,我们只需要判断 $[d, e, f]$ 是否等于 $\textit{target}$ 即可。如果等于,返回 $\textit{true},ドル否则返回 $\textit{false}$。
107+
108+
时间复杂度 $O(n),ドル其中 $n$ 为数组 $\textit{triplets}$ 的长度。空间复杂度 $O(1)$。
102109

103110
<!-- tabs:start -->
104111

@@ -193,6 +200,50 @@ function mergeTriplets(triplets: number[][], target: number[]): boolean {
193200
}
194201
```
195202

203+
#### Rust
204+
205+
```rust
206+
impl Solution {
207+
pub fn merge_triplets(triplets: Vec<Vec<i32>>, target: Vec<i32>) -> bool {
208+
let [x, y, z]: [i32; 3] = target.try_into().unwrap();
209+
let (mut d, mut e, mut f) = (0, 0, 0);
210+
211+
for triplet in triplets {
212+
if let [a, b, c] = triplet[..] {
213+
if a <= x && b <= y && c <= z {
214+
d = d.max(a);
215+
e = e.max(b);
216+
f = f.max(c);
217+
}
218+
}
219+
}
220+
221+
[d, e, f] == [x, y, z]
222+
}
223+
}
224+
```
225+
226+
#### Scala
227+
228+
```scala
229+
object Solution {
230+
def mergeTriplets(triplets: Array[Array[Int]], target: Array[Int]): Boolean = {
231+
val Array(x, y, z) = target
232+
var (d, e, f) = (0, 0, 0)
233+
234+
for (Array(a, b, c) <- triplets) {
235+
if (a <= x && b <= y && c <= z) {
236+
d = d.max(a)
237+
e = e.max(b)
238+
f = f.max(c)
239+
}
240+
}
241+
242+
d == x && e == y && f == z
243+
}
244+
}
245+
```
246+
196247
<!-- tabs:end -->
197248

198249
<!-- solution:end -->

‎solution/1800-1899/1899.Merge Triplets to Form Target Triplet/README_EN.md‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,22 @@ The target triplet [5,5,5] is now an element of triplets.
8080

8181
<!-- solution:start -->
8282

83-
### Solution 1
83+
### Solution 1: Greedy
84+
85+
Let $\textit{target} = [x, y, z]$. We need to determine whether there exists a triplet $[a, b, c]$ such that $a \leq x,ドル $b \leq y,ドル and $c \leq z$.
86+
87+
We can divide all triplets into two categories:
88+
89+
1. Triplets that satisfy $a \leq x,ドル $b \leq y,ドル and $c \leq z$.
90+
2. Triplets that do not satisfy $a \leq x,ドル $b \leq y,ドル and $c \leq z$.
91+
92+
For the first category, we can take the maximum values of $a,ドル $b,ドル and $c$ from these triplets to form a new triplet $[d, e, f]$.
93+
94+
For the second category, we can ignore these triplets because they cannot help us achieve the target triplet.
95+
96+
Finally, we just need to check whether $[d, e, f]$ is equal to $\textit{target}$. If it is, return $\textit{true}$; otherwise, return $\textit{false}$.
97+
98+
Time complexity is $O(n),ドル where $n$ is the length of the array $\textit{triplets}$. Space complexity is $O(1)$.
8499

85100
<!-- tabs:start -->
86101

@@ -175,6 +190,50 @@ function mergeTriplets(triplets: number[][], target: number[]): boolean {
175190
}
176191
```
177192

193+
#### Rust
194+
195+
```rust
196+
impl Solution {
197+
pub fn merge_triplets(triplets: Vec<Vec<i32>>, target: Vec<i32>) -> bool {
198+
let [x, y, z]: [i32; 3] = target.try_into().unwrap();
199+
let (mut d, mut e, mut f) = (0, 0, 0);
200+
201+
for triplet in triplets {
202+
if let [a, b, c] = triplet[..] {
203+
if a <= x && b <= y && c <= z {
204+
d = d.max(a);
205+
e = e.max(b);
206+
f = f.max(c);
207+
}
208+
}
209+
}
210+
211+
[d, e, f] == [x, y, z]
212+
}
213+
}
214+
```
215+
216+
#### Scala
217+
218+
```scala
219+
object Solution {
220+
def mergeTriplets(triplets: Array[Array[Int]], target: Array[Int]): Boolean = {
221+
val Array(x, y, z) = target
222+
var (d, e, f) = (0, 0, 0)
223+
224+
for (Array(a, b, c) <- triplets) {
225+
if (a <= x && b <= y && c <= z) {
226+
d = d.max(a)
227+
e = e.max(b)
228+
f = f.max(c)
229+
}
230+
}
231+
232+
d == x && e == y && f == z
233+
}
234+
}
235+
```
236+
178237
<!-- tabs:end -->
179238

180239
<!-- solution:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn merge_triplets(triplets: Vec<Vec<i32>>, target: Vec<i32>) -> bool {
3+
let [x, y, z]: [i32; 3] = target.try_into().unwrap();
4+
let (mut d, mut e, mut f) = (0, 0, 0);
5+
6+
for triplet in triplets {
7+
if let [a, b, c] = triplet[..] {
8+
if a <= x && b <= y && c <= z {
9+
d = d.max(a);
10+
e = e.max(b);
11+
f = f.max(c);
12+
}
13+
}
14+
}
15+
16+
[d, e, f] == [x, y, z]
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object Solution {
2+
def mergeTriplets(triplets: Array[Array[Int]], target: Array[Int]): Boolean = {
3+
val Array(x, y, z) = target
4+
var (d, e, f) = (0, 0, 0)
5+
6+
for (Array(a, b, c) <- triplets) {
7+
if (a <= x && b <= y && c <= z) {
8+
d = d.max(a)
9+
e = e.max(b)
10+
f = f.max(c)
11+
}
12+
}
13+
14+
d == x && e == y && f == z
15+
}
16+
}

‎solution/1900-1999/1918.Kth Smallest Subarray Sum/README.md‎

Lines changed: 113 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ tags:
7979

8080
我们注意到,题目中数组元素均为正整数,子数组的和 $s$ 越大,那么数组中子数组和小于等于 $s$ 的个数就越多。这存在一个单调性,因此我们可以考虑使用使用二分查找的方法来求解。
8181

82-
我们二分枚举子数组的和,初始化左右边界分别为数组 $nums$ 中的最小值以及所有元素之和。每次我们计算数组中子数组和小于等于当前枚举值的个数,如果个数大于等于 $k,ドル则说明当前枚举值 $s$ 可能是第 $k$ 小的子数组和,我们缩小右边界,否则我们增大左边界。枚举结束后,左边界即为第 $k$ 小的子数组和。
82+
我们二分枚举子数组的和,初始化左右边界分别为数组 $\textit{nums}$ 中的最小值以及所有元素之和。每次我们计算数组中子数组和小于等于当前枚举值的个数,如果个数大于等于 $k,ドル则说明当前枚举值 $s$ 可能是第 $k$ 小的子数组和,我们缩小右边界,否则我们增大左边界。枚举结束后,左边界即为第 $k$ 小的子数组和。
8383

8484
问题转换为计算一个数组中,有多少个子数组的和小于等于 $s,ドル我们可以通过函数 $f(s)$ 来计算。
8585

8686
函数 $f(s)$ 的计算方法如下:
8787

8888
- 初始化双指针 $j$ 和 $i,ドル分别指向当前窗口的左右边界,初始时 $j = i = 0$。初始化窗口内元素的和 $t = 0$。
89-
- 用变量 $cnt$ 记录子数组和小于等于 $s$ 的个数,初始时 $cnt = 0$。
90-
- 遍历数组 $nums,ドル每次遍历到一个元素 $nums[i],ドル我们将其加入窗口,即 $t = t + nums[i]$。如果此时 $t \gt s,ドル我们需要不断地将窗口的左边界右移,直到 $t \le s$ 为止,即不断地执行 $t -= nums[j],ドル并且 $j = j + 1$。接下来我们更新 $cnt,ドル即 $cnt = cnt + i - j + 1$。继续遍历下一个元素,直到遍历完整个数组。
89+
- 用变量 $\textit{cnt}$ 记录子数组和小于等于 $s$ 的个数,初始时 $\textit{cnt} = 0$。
90+
- 遍历数组 $\textit{nums},ドル每次遍历到一个元素 $\textit{nums}[i],ドル我们将其加入窗口,即 $t = t + \textit{nums}[i]$。如果此时 $t \gt s,ドル我们需要不断地将窗口的左边界右移,直到 $t \le s$ 为止,即不断地执行 $t -= \textit{nums}[j],ドル并且 $j = j + 1$。接下来我们更新 $\textit{cnt},ドル即 $\textit{cnt} = \textit{cnt} + i - j + 1$。继续遍历下一个元素,直到遍历完整个数组。
9191

9292
最后将 $cnt$ 作为函数 $f(s)$ 的返回值。
9393

94-
时间复杂度 $O(n \times \log S),ドル空间复杂度 $O(1)$。其中 $n$ 为数组 $nums$ 的长度,而 $S$ 为数组 $nums$ 中所有元素之和。
94+
时间复杂度 $O(n \times \log S),ドル其中 $n$ 为数组 $\textit{nums}$ 的长度,而 $S$ 为数组 $\textit{nums}$ 中所有元素之和。空间复杂度 $O(1)$
9595

9696
<!-- tabs:start -->
9797

@@ -219,6 +219,115 @@ func kthSmallestSubarraySum(nums []int, k int) int {
219219
}
220220
```
221221

222+
#### TypeScript
223+
224+
```ts
225+
function kthSmallestSubarraySum(nums: number[], k: number): number {
226+
let l = Math.min(...nums);
227+
let r = nums.reduce((sum, x) => sum + x, 0);
228+
229+
const f = (s: number): number => {
230+
let cnt = 0;
231+
let t = 0;
232+
let j = 0;
233+
234+
for (let i = 0; i < nums.length; i++) {
235+
t += nums[i];
236+
while (t > s) {
237+
t -= nums[j];
238+
j++;
239+
}
240+
cnt += i - j + 1;
241+
}
242+
return cnt;
243+
};
244+
245+
while (l < r) {
246+
const mid = (l + r) >> 1;
247+
if (f(mid) >= k) {
248+
r = mid;
249+
} else {
250+
l = mid + 1;
251+
}
252+
}
253+
return l;
254+
}
255+
```
256+
257+
#### Rust
258+
259+
```rust
260+
impl Solution {
261+
pub fn kth_smallest_subarray_sum(nums: Vec<i32>, k: i32) -> i32 {
262+
let mut l = *nums.iter().min().unwrap();
263+
let mut r: i32 = nums.iter().sum();
264+
265+
let f = |s: i32| -> i32 {
266+
let (mut cnt, mut t, mut j) = (0, 0, 0);
267+
268+
for i in 0..nums.len() {
269+
t += nums[i];
270+
while t > s {
271+
t -= nums[j];
272+
j += 1;
273+
}
274+
cnt += (i - j + 1) as i32;
275+
}
276+
cnt
277+
};
278+
279+
while l < r {
280+
let mid = (l + r) / 2;
281+
if f(mid) >= k {
282+
r = mid;
283+
} else {
284+
l = mid + 1;
285+
}
286+
}
287+
l
288+
}
289+
}
290+
```
291+
292+
#### Scala
293+
294+
```scala
295+
object Solution {
296+
def kthSmallestSubarraySum(nums: Array[Int], k: Int): Int = {
297+
var l = Int.MaxValue
298+
var r = 0
299+
300+
for (x <- nums) {
301+
l = l.min(x)
302+
r += x
303+
}
304+
305+
def f(s: Int): Int = {
306+
var cnt = 0
307+
var t = 0
308+
var j = 0
309+
310+
for (i <- nums.indices) {
311+
t += nums(i)
312+
while (t > s) {
313+
t -= nums(j)
314+
j += 1
315+
}
316+
cnt += i - j + 1
317+
}
318+
cnt
319+
}
320+
321+
while (l < r) {
322+
val mid = (l + r) / 2
323+
if (f(mid) >= k) r = mid
324+
else l = mid + 1
325+
}
326+
l
327+
}
328+
}
329+
```
330+
222331
<!-- tabs:end -->
223332

224333
<!-- solution:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /