Skip to content

Commit 7dd196b

Browse files
authored
feat: add solutions to lc problems: No.1708~1714 (#2122)
1 parent 87761c2 commit 7dd196b

File tree

14 files changed

+193
-132
lines changed

14 files changed

+193
-132
lines changed

solution/1700-1799/1708.Largest Subarray Length K/README.md

+41-42
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565

6666
数组中所有整数都不同,我们可以先在 $[0,..n-k]$ 范围内找到最大的元素的下标,然后从该下标开始取 $k$ 个元素即可。
6767

68-
时间复杂度 $O(n)$,忽略答案的空间消耗,空间复杂度 $O(1)$。其中 $n$ 为数组的长度
68+
时间复杂度 $O(n)$,其中 $n$ 为数组的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。
6969

7070
<!-- tabs:start -->
7171

@@ -76,8 +76,7 @@
7676
```python
7777
class Solution:
7878
def largestSubarray(self, nums: List[int], k: int) -> List[int]:
79-
mx = max(nums[: len(nums) - k + 1])
80-
i = nums.index(mx)
79+
i = nums.index(max(nums[: len(nums) - k + 1]))
8180
return nums[i : i + k]
8281
```
8382

@@ -88,18 +87,13 @@ class Solution:
8887
```java
8988
class Solution {
9089
public int[] largestSubarray(int[] nums, int k) {
91-
int i = 0, mx = 0;
92-
for (int j = 0; j < nums.length - k + 1; ++j) {
93-
if (mx < nums[j]) {
94-
mx = nums[j];
95-
i = j;
90+
int j = 0;
91+
for (int i = 1; i < nums.length - k + 1; ++i) {
92+
if (nums[j] < nums[i]) {
93+
j = i;
9694
}
9795
}
98-
int[] ans = new int[k];
99-
for (int j = 0; j < k; ++j) {
100-
ans[j] = nums[i + j];
101-
}
102-
return ans;
96+
return Arrays.copyOfRange(nums, j, j + k);
10397
}
10498
}
10599
```
@@ -110,48 +104,53 @@ class Solution {
110104
class Solution {
111105
public:
112106
vector<int> largestSubarray(vector<int>& nums, int k) {
113-
auto pos = max_element(nums.begin(), nums.begin() + nums.size() - k + 1);
114-
return {pos, pos + k};
107+
auto i = max_element(nums.begin(), nums.end() - k + 1);
108+
return {i, i + k};
115109
}
116110
};
117111
```
118112
119-
### **Rust**
113+
### **Go**
120114
121-
```rust
122-
impl Solution {
123-
#[allow(dead_code)]
124-
pub fn largest_subarray(nums: Vec<i32>, k: i32) -> Vec<i32> {
125-
let mut ret_vec = vec![i32::MIN];
126-
let n = nums.len();
115+
```go
116+
func largestSubarray(nums []int, k int) []int {
117+
j := 0
118+
for i := 1; i < len(nums)-k+1; i++ {
119+
if nums[j] < nums[i] {
120+
j = i
121+
}
122+
}
123+
return nums[j : j+k]
124+
}
125+
```
127126

128-
if n == (k as usize) {
129-
return nums;
130-
}
127+
### **TypeScript**
131128

132-
for i in 0..=n - (k as usize) {
133-
if nums[i] > ret_vec[0] {
134-
ret_vec = nums[i..i + (k as usize)].to_vec();
135-
}
129+
```ts
130+
function largestSubarray(nums: number[], k: number): number[] {
131+
let j = 0;
132+
for (let i = 1; i < nums.length - k + 1; ++i) {
133+
if (nums[j] < nums[i]) {
134+
j = i;
136135
}
137-
138-
ret_vec
139136
}
137+
return nums.slice(j, j + k);
140138
}
141139
```
142140

143-
### **Go**
141+
### **Rust**
144142

145-
```go
146-
func largestSubarray(nums []int, k int) []int {
147-
i, mx := 0, 0
148-
for j := 0; j < len(nums)-k+1; j++ {
149-
if mx < nums[j] {
150-
mx = nums[j]
151-
i = j
152-
}
153-
}
154-
return nums[i : i+k]
143+
```rust
144+
impl Solution {
145+
pub fn largest_subarray(nums: Vec<i32>, k: i32) -> Vec<i32> {
146+
let mut j = 0;
147+
for i in 1..=nums.len() - (k as usize) {
148+
if nums[i] > nums[j] {
149+
j = i;
150+
}
151+
}
152+
nums[j..j + (k as usize)].to_vec()
153+
}
155154
}
156155
```
157156

solution/1700-1799/1708.Largest Subarray Length K/README_EN.md

+46-41
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,20 @@ Of these, [4,5,2,3] is the largest.</pre>
5555

5656
## Solutions
5757

58+
**Solution 1: Simulation**
59+
60+
All integers in the array are distinct, so we can first find the index of the maximum element in the range $[0,..n-k]$, and then take $k$ elements starting from this index.
61+
62+
The time complexity is $O(n)$, where $n$ is the length of the array. Ignoring the space consumption of the answer, the space complexity is $O(1)$.
63+
5864
<!-- tabs:start -->
5965

6066
### **Python3**
6167

6268
```python
6369
class Solution:
6470
def largestSubarray(self, nums: List[int], k: int) -> List[int]:
65-
mx = max(nums[: len(nums) - k + 1])
66-
i = nums.index(mx)
71+
i = nums.index(max(nums[: len(nums) - k + 1]))
6772
return nums[i : i + k]
6873
```
6974

@@ -72,18 +77,13 @@ class Solution:
7277
```java
7378
class Solution {
7479
public int[] largestSubarray(int[] nums, int k) {
75-
int i = 0, mx = 0;
76-
for (int j = 0; j < nums.length - k + 1; ++j) {
77-
if (mx < nums[j]) {
78-
mx = nums[j];
79-
i = j;
80+
int j = 0;
81+
for (int i = 1; i < nums.length - k + 1; ++i) {
82+
if (nums[j] < nums[i]) {
83+
j = i;
8084
}
8185
}
82-
int[] ans = new int[k];
83-
for (int j = 0; j < k; ++j) {
84-
ans[j] = nums[i + j];
85-
}
86-
return ans;
86+
return Arrays.copyOfRange(nums, j, j + k);
8787
}
8888
}
8989
```
@@ -94,48 +94,53 @@ class Solution {
9494
class Solution {
9595
public:
9696
vector<int> largestSubarray(vector<int>& nums, int k) {
97-
auto pos = max_element(nums.begin(), nums.begin() + nums.size() - k + 1);
98-
return {pos, pos + k};
97+
auto i = max_element(nums.begin(), nums.end() - k + 1);
98+
return {i, i + k};
9999
}
100100
};
101101
```
102102
103-
### **Rust**
103+
### **Go**
104104
105-
```rust
106-
impl Solution {
107-
#[allow(dead_code)]
108-
pub fn largest_subarray(nums: Vec<i32>, k: i32) -> Vec<i32> {
109-
let mut ret_vec = vec![i32::MIN];
110-
let n = nums.len();
105+
```go
106+
func largestSubarray(nums []int, k int) []int {
107+
j := 0
108+
for i := 1; i < len(nums)-k+1; i++ {
109+
if nums[j] < nums[i] {
110+
j = i
111+
}
112+
}
113+
return nums[j : j+k]
114+
}
115+
```
111116

112-
if n == (k as usize) {
113-
return nums;
114-
}
117+
### **TypeScript**
115118

116-
for i in 0..=n - (k as usize) {
117-
if nums[i] > ret_vec[0] {
118-
ret_vec = nums[i..i + (k as usize)].to_vec();
119-
}
119+
```ts
120+
function largestSubarray(nums: number[], k: number): number[] {
121+
let j = 0;
122+
for (let i = 1; i < nums.length - k + 1; ++i) {
123+
if (nums[j] < nums[i]) {
124+
j = i;
120125
}
121-
122-
ret_vec
123126
}
127+
return nums.slice(j, j + k);
124128
}
125129
```
126130

127-
### **Go**
131+
### **Rust**
128132

129-
```go
130-
func largestSubarray(nums []int, k int) []int {
131-
i, mx := 0, 0
132-
for j := 0; j < len(nums)-k+1; j++ {
133-
if mx < nums[j] {
134-
mx = nums[j]
135-
i = j
136-
}
137-
}
138-
return nums[i : i+k]
133+
```rust
134+
impl Solution {
135+
pub fn largest_subarray(nums: Vec<i32>, k: i32) -> Vec<i32> {
136+
let mut j = 0;
137+
for i in 1..=nums.len() - (k as usize) {
138+
if nums[i] > nums[j] {
139+
j = i;
140+
}
141+
}
142+
nums[j..j + (k as usize)].to_vec()
143+
}
139144
}
140145
```
141146

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
class Solution {
2-
public:
3-
vector<int> largestSubarray(vector<int>& nums, int k) {
4-
auto pos = max_element(nums.begin(), nums.begin() + nums.size() - k + 1);
5-
return {pos, pos + k};
6-
}
1+
class Solution {
2+
public:
3+
vector<int> largestSubarray(vector<int>& nums, int k) {
4+
auto i = max_element(nums.begin(), nums.end() - k + 1);
5+
return {i, i + k};
6+
}
77
};
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
func largestSubarray(nums []int, k int) []int {
2-
i, mx := 0, 0
3-
for j := 0; j < len(nums)-k+1; j++ {
4-
if mx < nums[j] {
5-
mx = nums[j]
6-
i = j
2+
j := 0
3+
for i := 1; i < len(nums)-k+1; i++ {
4+
if nums[j] < nums[i] {
5+
j = i
76
}
87
}
9-
return nums[i : i+k]
8+
return nums[j : j+k]
109
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
class Solution {
2-
public int[] largestSubarray(int[] nums, int k) {
3-
int i = 0, mx = 0;
4-
for (int j = 0; j < nums.length - k + 1; ++j) {
5-
if (mx < nums[j]) {
6-
mx = nums[j];
7-
i = j;
8-
}
9-
}
10-
int[] ans = new int[k];
11-
for (int j = 0; j < k; ++j) {
12-
ans[j] = nums[i + j];
13-
}
14-
return ans;
15-
}
1+
class Solution {
2+
public int[] largestSubarray(int[] nums, int k) {
3+
int j = 0;
4+
for (int i = 1; i < nums.length - k + 1; ++i) {
5+
if (nums[j] < nums[i]) {
6+
j = i;
7+
}
8+
}
9+
return Arrays.copyOfRange(nums, j, j + k);
10+
}
1611
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
class Solution:
2-
def largestSubarray(self, nums: List[int], k: int) -> List[int]:
3-
mx = max(nums[: len(nums) - k + 1])
4-
i = nums.index(mx)
5-
return nums[i : i + k]
1+
class Solution:
2+
def largestSubarray(self, nums: List[int], k: int) -> List[int]:
3+
i = nums.index(max(nums[: len(nums) - k + 1]))
4+
return nums[i : i + k]
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
impl Solution {
2-
#[allow(dead_code)]
32
pub fn largest_subarray(nums: Vec<i32>, k: i32) -> Vec<i32> {
4-
let mut ret_vec = vec![i32::MIN];
5-
let n = nums.len();
6-
7-
if n == (k as usize) {
8-
return nums;
9-
}
10-
11-
for i in 0..=n - (k as usize) {
12-
if nums[i] > ret_vec[0] {
13-
ret_vec = nums[i..i + (k as usize)].to_vec();
3+
let mut j = 0;
4+
for i in 1..=nums.len() - (k as usize) {
5+
if nums[i] > nums[j] {
6+
j = i;
147
}
158
}
16-
17-
ret_vec
9+
nums[j..j + (k as usize)].to_vec()
1810
}
1911
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function largestSubarray(nums: number[], k: number): number[] {
2+
let j = 0;
3+
for (let i = 1; i < nums.length - k + 1; ++i) {
4+
if (nums[j] < nums[i]) {
5+
j = i;
6+
}
7+
}
8+
return nums.slice(j, j + k);
9+
}

solution/1700-1799/1710.Maximum Units on a Truck/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858

5959
然后从前往后遍历 `boxTypes`,选择最多 `truckSize` 个箱子,累加单元数。
6060

61-
时间复杂度 $O(n\times \log n)$,其中 $n$ 表示二维数组 `boxTypes` 的长度。
61+
时间复杂度 $O(n \times \log n)$,其中 $n$ 表示二维数组 `boxTypes` 的长度。
6262

6363
**方法二:计数排序**
6464

solution/1700-1799/1710.Maximum Units on a Truck/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
4747

4848
## Solutions
4949

50+
**Solution 1: Greedy + Sorting**
51+
52+
According to the problem, we should choose as many units as possible. Therefore, we first sort `boxTypes` in descending order of the number of units.
53+
54+
Then we traverse `boxTypes` from front to back, choose up to `truckSize` boxes, and accumulate the number of units.
55+
56+
The time complexity is $O(n \times \log n)$, where $n$ is the length of the two-dimensional array `boxTypes`.
57+
58+
**Solution 2: Counting Sort**
59+
60+
We can also use the idea of counting sort, create an array $cnt$ of length $1001$, where $cnt[b]$ represents the number of boxes with $b$ units.
61+
62+
Then starting from the box with the maximum number of units, choose up to `truckSize` boxes, and accumulate the number of units.
63+
64+
The time complexity is $O(M)$, where $M$ is the maximum number of units. In this problem, $M=1000$.
65+
5066
<!-- tabs:start -->
5167

5268
### **Python3**

0 commit comments

Comments
 (0)