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 9baa050

Browse files
committed
Merge pull request #385 from 0xff-dev/2389
Add solution and test-cases for problem 2389
2 parents 104da8b + c4762db commit 9baa050

File tree

3 files changed

+42
-25
lines changed

3 files changed

+42
-25
lines changed

‎leetcode/2301-2400/2389.Longest-Subsequence-With-Limited-Sum/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [2389.Longest Subsequence With Limited Sum][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer array `nums` of length `n`, and an integer array `queries` of length `m`.
5+
6+
Return an array `answer` of length `m` where `answer[i]` is the **maximum** size of a **subsequence** that you can take from `nums` such that the **sum** of its elements is less than or equal to `queries[i]`.
7+
8+
A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [4,5,2,1], queries = [3,10,21]
14+
Output: [2,3,4]
15+
Explanation: We answer the queries as follows:
16+
- The subsequence [2,1] has a sum less than or equal to 3. It can be proven that 2 is the maximum size of such a subsequence, so answer[0] = 2.
17+
- The subsequence [4,5,1] has a sum less than or equal to 10. It can be proven that 3 is the maximum size of such a subsequence, so answer[1] = 3.
18+
- The subsequence [4,5,2,1] has a sum less than or equal to 21. It can be proven that 4 is the maximum size of such a subsequence, so answer[2] = 4.
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Longest Subsequence With Limited Sum
23-
```go
2423
```
25-
24+
Input: nums = [2,3,4,5], queries = [1]
25+
Output: [0]
26+
Explanation: The empty subsequence is the only subsequence that has a sum less than or equal to 1, so answer[0] = 0.
27+
```
2628

2729
## 结语
2830

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int, queries []int) []int {
6+
ans := make([]int, len(queries))
7+
sort.Ints(nums)
8+
for i := 1; i < len(nums); i++ {
9+
nums[i] += nums[i-1]
10+
}
11+
for idx, q := range queries {
12+
sum := 0
13+
for ; sum < len(nums); sum++ {
14+
if nums[sum] > q {
15+
break
16+
}
17+
}
18+
ans[idx] = sum
19+
}
20+
return ans
521
}

‎leetcode/2301-2400/2389.Longest-Subsequence-With-Limited-Sum/Solution_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputsbool
14-
expect bool
12+
name string
13+
inputs, queries []int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{4, 5, 2, 1}, []int{3, 10, 21}, []int{2, 3, 4}},
17+
{"TestCase2", []int{2, 3, 4, 5}, []int{1}, []int{0}},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.inputs, c.queries)
2524
if !reflect.DeepEqual(got, c.expect) {
2625
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2726
c.expect, got, c.inputs)
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
//压力测试
32+
//压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
//使用案列
36+
//使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
(0)

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