Skip to content

Commit e554e6c

Browse files
committed
feat: add solutions to lc problems: No.3683,3684
1 parent 8b08fcc commit e554e6c

File tree

14 files changed

+363
-16
lines changed

14 files changed

+363
-16
lines changed

solution/3600-3699/3683.Earliest Time to Finish One Task/README.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,69 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3683.Ea
6262

6363
<!-- solution:start -->
6464

65-
### 方法一
65+
### 方法一:一次遍历
66+
67+
我们遍历 $\textit{tasks}$ 数组,对于每个任务,计算其完成时间 $s_i + t_i$,求出所有任务完成时间的最小值,即为至少完成一个任务的最早时间。
68+
69+
时间复杂度 $O(n)$,其中 $n$ 为 $\textit{tasks}$ 数组的长度。空间复杂度 $O(1)$。
6670

6771
<!-- tabs:start -->
6872

6973
#### Python3
7074

7175
```python
72-
76+
class Solution:
77+
def earliestTime(self, tasks: List[List[int]]) -> int:
78+
return min(s + t for s, t in tasks)
7379
```
7480

7581
#### Java
7682

7783
```java
78-
84+
class Solution {
85+
public int earliestTime(int[][] tasks) {
86+
int ans = 200;
87+
for (var task : tasks) {
88+
ans = Math.min(ans, task[0] + task[1]);
89+
}
90+
return ans;
91+
}
92+
}
7993
```
8094

8195
#### C++
8296

8397
```cpp
84-
98+
class Solution {
99+
public:
100+
int earliestTime(vector<vector<int>>& tasks) {
101+
int ans = 200;
102+
for (const auto& task : tasks) {
103+
ans = min(ans, task[0] + task[1]);
104+
}
105+
return ans;
106+
}
107+
};
85108
```
86109
87110
#### Go
88111
89112
```go
113+
func earliestTime(tasks [][]int) int {
114+
ans := 200
115+
for _, task := range tasks {
116+
ans = min(ans, task[0]+task[1])
117+
}
118+
return ans
119+
}
120+
```
121+
122+
#### TypeScript
90123

124+
```ts
125+
function earliestTime(tasks: number[][]): number {
126+
return Math.min(...tasks.map(task => task[0] + task[1]));
127+
}
91128
```
92129

93130
<!-- tabs:end -->

solution/3600-3699/3683.Earliest Time to Finish One Task/README_EN.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,69 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3683.Ea
6060

6161
<!-- solution:start -->
6262

63-
### Solution 1
63+
### Solution 1: Single Pass
64+
65+
We iterate through the $\textit{tasks}$ array and, for each task, calculate its completion time $s_i + t_i$. The minimum of all task completion times is the earliest time to finish at least one task.
66+
67+
The time complexity is $O(n)$, where $n$ is the length of the $\textit{tasks}$ array. The space complexity is $O(1)$.
6468

6569
<!-- tabs:start -->
6670

6771
#### Python3
6872

6973
```python
70-
74+
class Solution:
75+
def earliestTime(self, tasks: List[List[int]]) -> int:
76+
return min(s + t for s, t in tasks)
7177
```
7278

7379
#### Java
7480

7581
```java
76-
82+
class Solution {
83+
public int earliestTime(int[][] tasks) {
84+
int ans = 200;
85+
for (var task : tasks) {
86+
ans = Math.min(ans, task[0] + task[1]);
87+
}
88+
return ans;
89+
}
90+
}
7791
```
7892

7993
#### C++
8094

8195
```cpp
82-
96+
class Solution {
97+
public:
98+
int earliestTime(vector<vector<int>>& tasks) {
99+
int ans = 200;
100+
for (const auto& task : tasks) {
101+
ans = min(ans, task[0] + task[1]);
102+
}
103+
return ans;
104+
}
105+
};
83106
```
84107
85108
#### Go
86109
87110
```go
111+
func earliestTime(tasks [][]int) int {
112+
ans := 200
113+
for _, task := range tasks {
114+
ans = min(ans, task[0]+task[1])
115+
}
116+
return ans
117+
}
118+
```
119+
120+
#### TypeScript
88121

122+
```ts
123+
function earliestTime(tasks: number[][]): number {
124+
return Math.min(...tasks.map(task => task[0] + task[1]));
125+
}
89126
```
90127

91128
<!-- tabs:end -->
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int earliestTime(vector<vector<int>>& tasks) {
4+
int ans = 200;
5+
for (const auto& task : tasks) {
6+
ans = min(ans, task[0] + task[1]);
7+
}
8+
return ans;
9+
}
10+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func earliestTime(tasks [][]int) int {
2+
ans := 200
3+
for _, task := range tasks {
4+
ans = min(ans, task[0]+task[1])
5+
}
6+
return ans
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int earliestTime(int[][] tasks) {
3+
int ans = 200;
4+
for (var task : tasks) {
5+
ans = Math.min(ans, task[0] + task[1]);
6+
}
7+
return ans;
8+
}
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def earliestTime(self, tasks: List[List[int]]) -> int:
3+
return min(s + t for s, t in tasks)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function earliestTime(tasks: number[][]): number {
2+
return Math.min(...tasks.map(task => task[0] + task[1]));
3+
}

solution/3600-3699/3684.Maximize Sum of At Most K Distinct Elements/README.md

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,32 +75,114 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3684.Ma
7575

7676
<!-- solution:start -->
7777

78-
### 方法一
78+
### 方法一:排序
79+
80+
我们先对数组 $\textit{nums}$ 进行排序,然后从后向前遍历,选择最大的 $k$ 个不同的元素。由于我们需要严格递减的顺序,因此在选择时需要跳过重复的元素。
81+
82+
时间复杂度 $O(n \times \log n)$,其中 $n$ 为 $\textit{nums}$ 数组的长度。忽略答案的空间消耗,空间复杂度 $O(\log n)$。
7983

8084
<!-- tabs:start -->
8185

8286
#### Python3
8387

8488
```python
85-
89+
class Solution:
90+
def maxKDistinct(self, nums: List[int], k: int) -> List[int]:
91+
nums.sort()
92+
n = len(nums)
93+
ans = []
94+
for i in range(n - 1, -1, -1):
95+
if i + 1 < n and nums[i] == nums[i + 1]:
96+
continue
97+
ans.append(nums[i])
98+
k -= 1
99+
if k == 0:
100+
break
101+
return ans
86102
```
87103

88104
#### Java
89105

90106
```java
91-
107+
class Solution {
108+
public int[] maxKDistinct(int[] nums, int k) {
109+
Arrays.sort(nums);
110+
int n = nums.length;
111+
List<Integer> ans = new ArrayList<>();
112+
for (int i = n - 1; i >= 0; --i) {
113+
if (i + 1 < n && nums[i] == nums[i + 1]) {
114+
continue;
115+
}
116+
ans.add(nums[i]);
117+
if (--k == 0) {
118+
break;
119+
}
120+
}
121+
return ans.stream().mapToInt(x -> x).toArray();
122+
}
123+
}
92124
```
93125

94126
#### C++
95127

96128
```cpp
97-
129+
class Solution {
130+
public:
131+
vector<int> maxKDistinct(vector<int>& nums, int k) {
132+
ranges::sort(nums);
133+
int n = nums.size();
134+
vector<int> ans;
135+
for (int i = n - 1; ~i; --i) {
136+
if (i + 1 < n && nums[i] == nums[i + 1]) {
137+
continue;
138+
}
139+
ans.push_back(nums[i]);
140+
if (--k == 0) {
141+
break;
142+
}
143+
}
144+
return ans;
145+
}
146+
};
98147
```
99148
100149
#### Go
101150
102151
```go
152+
func maxKDistinct(nums []int, k int) (ans []int) {
153+
slices.Sort(nums)
154+
n := len(nums)
155+
for i := n - 1; i >= 0; i-- {
156+
if i+1 < n && nums[i] == nums[i+1] {
157+
continue
158+
}
159+
ans = append(ans, nums[i])
160+
if k--; k == 0 {
161+
break
162+
}
163+
}
164+
return
165+
}
166+
```
103167

168+
#### TypeScript
169+
170+
```ts
171+
function maxKDistinct(nums: number[], k: number): number[] {
172+
nums.sort((a, b) => a - b);
173+
const ans: number[] = [];
174+
const n = nums.length;
175+
for (let i = n - 1; ~i; --i) {
176+
if (i + 1 < n && nums[i] === nums[i + 1]) {
177+
continue;
178+
}
179+
ans.push(nums[i]);
180+
if (--k === 0) {
181+
break;
182+
}
183+
}
184+
return ans;
185+
}
104186
```
105187

106188
<!-- tabs:end -->

0 commit comments

Comments
 (0)