Skip to content

feat: add solutions to lc problems: No.1708~1714 #2122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 41 additions & 42 deletions solution/1700-1799/1708.Largest Subarray Length K/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

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

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

<!-- tabs:start -->

Expand All @@ -76,8 +76,7 @@
```python
class Solution:
def largestSubarray(self, nums: List[int], k: int) -> List[int]:
mx = max(nums[: len(nums) - k + 1])
i = nums.index(mx)
i = nums.index(max(nums[: len(nums) - k + 1]))
return nums[i : i + k]
```

Expand All @@ -88,18 +87,13 @@ class Solution:
```java
class Solution {
public int[] largestSubarray(int[] nums, int k) {
int i = 0, mx = 0;
for (int j = 0; j < nums.length - k + 1; ++j) {
if (mx < nums[j]) {
mx = nums[j];
i = j;
int j = 0;
for (int i = 1; i < nums.length - k + 1; ++i) {
if (nums[j] < nums[i]) {
j = i;
}
}
int[] ans = new int[k];
for (int j = 0; j < k; ++j) {
ans[j] = nums[i + j];
}
return ans;
return Arrays.copyOfRange(nums, j, j + k);
}
}
```
Expand All @@ -110,48 +104,53 @@ class Solution {
class Solution {
public:
vector<int> largestSubarray(vector<int>& nums, int k) {
auto pos = max_element(nums.begin(), nums.begin() + nums.size() - k + 1);
return {pos, pos + k};
auto i = max_element(nums.begin(), nums.end() - k + 1);
return {i, i + k};
}
};
```

### **Rust**
### **Go**

```rust
impl Solution {
#[allow(dead_code)]
pub fn largest_subarray(nums: Vec<i32>, k: i32) -> Vec<i32> {
let mut ret_vec = vec![i32::MIN];
let n = nums.len();
```go
func largestSubarray(nums []int, k int) []int {
j := 0
for i := 1; i < len(nums)-k+1; i++ {
if nums[j] < nums[i] {
j = i
}
}
return nums[j : j+k]
}
```

if n == (k as usize) {
return nums;
}
### **TypeScript**

for i in 0..=n - (k as usize) {
if nums[i] > ret_vec[0] {
ret_vec = nums[i..i + (k as usize)].to_vec();
}
```ts
function largestSubarray(nums: number[], k: number): number[] {
let j = 0;
for (let i = 1; i < nums.length - k + 1; ++i) {
if (nums[j] < nums[i]) {
j = i;
}

ret_vec
}
return nums.slice(j, j + k);
}
```

### **Go**
### **Rust**

```go
func largestSubarray(nums []int, k int) []int {
i, mx := 0, 0
for j := 0; j < len(nums)-k+1; j++ {
if mx < nums[j] {
mx = nums[j]
i = j
}
}
return nums[i : i+k]
```rust
impl Solution {
pub fn largest_subarray(nums: Vec<i32>, k: i32) -> Vec<i32> {
let mut j = 0;
for i in 1..=nums.len() - (k as usize) {
if nums[i] > nums[j] {
j = i;
}
}
nums[j..j + (k as usize)].to_vec()
}
}
```

Expand Down
87 changes: 46 additions & 41 deletions solution/1700-1799/1708.Largest Subarray Length K/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,20 @@ Of these, [4,5,2,3] is the largest.</pre>

## Solutions

**Solution 1: Simulation**

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.

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)$.

<!-- tabs:start -->

### **Python3**

```python
class Solution:
def largestSubarray(self, nums: List[int], k: int) -> List[int]:
mx = max(nums[: len(nums) - k + 1])
i = nums.index(mx)
i = nums.index(max(nums[: len(nums) - k + 1]))
return nums[i : i + k]
```

Expand All @@ -72,18 +77,13 @@ class Solution:
```java
class Solution {
public int[] largestSubarray(int[] nums, int k) {
int i = 0, mx = 0;
for (int j = 0; j < nums.length - k + 1; ++j) {
if (mx < nums[j]) {
mx = nums[j];
i = j;
int j = 0;
for (int i = 1; i < nums.length - k + 1; ++i) {
if (nums[j] < nums[i]) {
j = i;
}
}
int[] ans = new int[k];
for (int j = 0; j < k; ++j) {
ans[j] = nums[i + j];
}
return ans;
return Arrays.copyOfRange(nums, j, j + k);
}
}
```
Expand All @@ -94,48 +94,53 @@ class Solution {
class Solution {
public:
vector<int> largestSubarray(vector<int>& nums, int k) {
auto pos = max_element(nums.begin(), nums.begin() + nums.size() - k + 1);
return {pos, pos + k};
auto i = max_element(nums.begin(), nums.end() - k + 1);
return {i, i + k};
}
};
```

### **Rust**
### **Go**

```rust
impl Solution {
#[allow(dead_code)]
pub fn largest_subarray(nums: Vec<i32>, k: i32) -> Vec<i32> {
let mut ret_vec = vec![i32::MIN];
let n = nums.len();
```go
func largestSubarray(nums []int, k int) []int {
j := 0
for i := 1; i < len(nums)-k+1; i++ {
if nums[j] < nums[i] {
j = i
}
}
return nums[j : j+k]
}
```

if n == (k as usize) {
return nums;
}
### **TypeScript**

for i in 0..=n - (k as usize) {
if nums[i] > ret_vec[0] {
ret_vec = nums[i..i + (k as usize)].to_vec();
}
```ts
function largestSubarray(nums: number[], k: number): number[] {
let j = 0;
for (let i = 1; i < nums.length - k + 1; ++i) {
if (nums[j] < nums[i]) {
j = i;
}

ret_vec
}
return nums.slice(j, j + k);
}
```

### **Go**
### **Rust**

```go
func largestSubarray(nums []int, k int) []int {
i, mx := 0, 0
for j := 0; j < len(nums)-k+1; j++ {
if mx < nums[j] {
mx = nums[j]
i = j
}
}
return nums[i : i+k]
```rust
impl Solution {
pub fn largest_subarray(nums: Vec<i32>, k: i32) -> Vec<i32> {
let mut j = 0;
for i in 1..=nums.len() - (k as usize) {
if nums[i] > nums[j] {
j = i;
}
}
nums[j..j + (k as usize)].to_vec()
}
}
```

Expand Down
12 changes: 6 additions & 6 deletions solution/1700-1799/1708.Largest Subarray Length K/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Solution {
public:
vector<int> largestSubarray(vector<int>& nums, int k) {
auto pos = max_element(nums.begin(), nums.begin() + nums.size() - k + 1);
return {pos, pos + k};
}
class Solution {
public:
vector<int> largestSubarray(vector<int>& nums, int k) {
auto i = max_element(nums.begin(), nums.end() - k + 1);
return {i, i + k};
}
};
11 changes: 5 additions & 6 deletions solution/1700-1799/1708.Largest Subarray Length K/Solution.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
func largestSubarray(nums []int, k int) []int {
i, mx := 0, 0
for j := 0; j < len(nums)-k+1; j++ {
if mx < nums[j] {
mx = nums[j]
i = j
j := 0
for i := 1; i < len(nums)-k+1; i++ {
if nums[j] < nums[i] {
j = i
}
}
return nums[i : i+k]
return nums[j : j+k]
}
25 changes: 10 additions & 15 deletions solution/1700-1799/1708.Largest Subarray Length K/Solution.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
class Solution {
public int[] largestSubarray(int[] nums, int k) {
int i = 0, mx = 0;
for (int j = 0; j < nums.length - k + 1; ++j) {
if (mx < nums[j]) {
mx = nums[j];
i = j;
}
}
int[] ans = new int[k];
for (int j = 0; j < k; ++j) {
ans[j] = nums[i + j];
}
return ans;
}
class Solution {
public int[] largestSubarray(int[] nums, int k) {
int j = 0;
for (int i = 1; i < nums.length - k + 1; ++i) {
if (nums[j] < nums[i]) {
j = i;
}
}
return Arrays.copyOfRange(nums, j, j + k);
}
}
9 changes: 4 additions & 5 deletions solution/1700-1799/1708.Largest Subarray Length K/Solution.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class Solution:
def largestSubarray(self, nums: List[int], k: int) -> List[int]:
mx = max(nums[: len(nums) - k + 1])
i = nums.index(mx)
return nums[i : i + k]
class Solution:
def largestSubarray(self, nums: List[int], k: int) -> List[int]:
i = nums.index(max(nums[: len(nums) - k + 1]))
return nums[i : i + k]
18 changes: 5 additions & 13 deletions solution/1700-1799/1708.Largest Subarray Length K/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
impl Solution {
#[allow(dead_code)]
pub fn largest_subarray(nums: Vec<i32>, k: i32) -> Vec<i32> {
let mut ret_vec = vec![i32::MIN];
let n = nums.len();

if n == (k as usize) {
return nums;
}

for i in 0..=n - (k as usize) {
if nums[i] > ret_vec[0] {
ret_vec = nums[i..i + (k as usize)].to_vec();
let mut j = 0;
for i in 1..=nums.len() - (k as usize) {
if nums[i] > nums[j] {
j = i;
}
}

ret_vec
nums[j..j + (k as usize)].to_vec()
}
}
9 changes: 9 additions & 0 deletions solution/1700-1799/1708.Largest Subarray Length K/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function largestSubarray(nums: number[], k: number): number[] {
let j = 0;
for (let i = 1; i < nums.length - k + 1; ++i) {
if (nums[j] < nums[i]) {
j = i;
}
}
return nums.slice(j, j + k);
}
2 changes: 1 addition & 1 deletion solution/1700-1799/1710.Maximum Units on a Truck/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

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

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

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

Expand Down
16 changes: 16 additions & 0 deletions solution/1700-1799/1710.Maximum Units on a Truck/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.

## Solutions

**Solution 1: Greedy + Sorting**

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.

Then we traverse `boxTypes` from front to back, choose up to `truckSize` boxes, and accumulate the number of units.

The time complexity is $O(n \times \log n)$, where $n$ is the length of the two-dimensional array `boxTypes`.

**Solution 2: Counting Sort**

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.

Then starting from the box with the maximum number of units, choose up to `truckSize` boxes, and accumulate the number of units.

The time complexity is $O(M)$, where $M$ is the maximum number of units. In this problem, $M=1000$.

<!-- tabs:start -->

### **Python3**
Expand Down
Loading