Skip to content

Commit 408e966

Browse files
S
1 parent 4c1fb38 commit 408e966

File tree

20 files changed

+710
-0
lines changed

20 files changed

+710
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public int CanBeTypedWords(string text, string brokenLetters) {
3+
bool[] s = new bool[26];
4+
foreach (char c in brokenLetters) {
5+
s[c - 'a'] = true;
6+
}
7+
int ans = 0;
8+
string[] words = text.Split(' ');
9+
foreach (string w in words) {
10+
foreach (char c in w) {
11+
if (s[c - 'a']) {
12+
--ans;
13+
break;
14+
}
15+
}
16+
++ans;
17+
}
18+
return ans;
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int smallestAbsent(vector<int>& nums) {
4+
unordered_set<int> s;
5+
int sum = 0;
6+
for (int x : nums) {
7+
s.insert(x);
8+
sum += x;
9+
}
10+
int ans = max(1, sum / (int) nums.size() + 1);
11+
while (s.contains(ans)) {
12+
++ans;
13+
}
14+
return ans;
15+
}
16+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func smallestAbsent(nums []int) int {
2+
s := map[int]bool{}
3+
sum := 0
4+
for _, x := range nums {
5+
s[x] = true
6+
sum += x
7+
}
8+
ans := max(1, sum/len(nums)+1)
9+
for s[ans] {
10+
ans++
11+
}
12+
return ans
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int smallestAbsent(int[] nums) {
3+
Set<Integer> s = new HashSet<>();
4+
int sum = 0;
5+
for (int x : nums) {
6+
s.add(x);
7+
sum += x;
8+
}
9+
int ans = Math.max(1, sum / nums.length + 1);
10+
while (s.contains(ans)) {
11+
++ans;
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def smallestAbsent(self, nums: List[int]) -> int:
3+
s = set(nums)
4+
ans = max(1, sum(nums) // len(nums) + 1)
5+
while ans in s:
6+
ans += 1
7+
return ans
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function smallestAbsent(nums: number[]): number {
2+
const s = new Set<number>(nums);
3+
const sum = nums.reduce((a, b) => a + b, 0);
4+
let ans = Math.max(1, Math.floor(sum / nums.length) + 1);
5+
while (s.has(ans)) {
6+
ans++;
7+
}
8+
return ans;
9+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
---
5+
6+
<!-- problem:start -->
7+
8+
# [3683. Earliest Time to Finish One Task](https://leetcode.com/problems/earliest-time-to-finish-one-task)
9+
10+
[中文文档](/solution/3600-3699/3683.Earliest%20Time%20to%20Finish%20One%20Task/README.md)
11+
12+
## Description
13+
14+
<!-- description:start -->
15+
16+
<p>You are given a 2D integer array <code>tasks</code> where <code>tasks[i] = [s<sub>i</sub>, t<sub>i</sub>]</code>.</p>
17+
18+
<p>Each <code>[s<sub>i</sub>, t<sub>i</sub>]</code> in <code>tasks</code> represents a task with start time <code>s<sub>i</sub></code> that takes <code>t<sub>i</sub></code> units of time to finish.</p>
19+
20+
<p>Return the earliest time at which at least one task is finished.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong class="example">Example 1:</strong></p>
24+
25+
<div class="example-block">
26+
<p><strong>Input:</strong> <span class="example-io">tasks = [[1,6],[2,3]]</span></p>
27+
28+
<p><strong>Output:</strong> <span class="example-io">5</span></p>
29+
30+
<p><strong>Explanation:</strong></p>
31+
32+
<p>The first task starts at time <code>t = 1</code> and finishes at time <code>1 + 6 = 7</code>. The second task finishes at time <code>2 + 3 = 5</code>. You can finish one task at time 5.</p>
33+
</div>
34+
35+
<p><strong class="example">Example 2:</strong></p>
36+
37+
<div class="example-block">
38+
<p><strong>Input:</strong> <span class="example-io">tasks = [[100,100],[100,100],[100,100]]</span></p>
39+
40+
<p><strong>Output:</strong> <span class="example-io">200</span></p>
41+
42+
<p><strong>Explanation:</strong></p>
43+
44+
<p>All three tasks finish at time <code>100 + 100 = 200</code>.</p>
45+
</div>
46+
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= tasks.length &lt;= 100</code></li>
52+
<li><code>tasks[i] = [s<sub>i</sub>, t<sub>i</sub>]</code></li>
53+
<li><code>1 &lt;= s<sub>i</sub>, t<sub>i</sub> &lt;= 100</code></li>
54+
</ul>
55+
56+
<!-- description:end -->
57+
58+
## Solutions
59+
60+
<!-- solution:start -->
61+
62+
### Solution 1: Single Pass
63+
64+
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.
65+
66+
The time complexity is $O(n)$, where $n$ is the length of the $\textit{tasks}$ array. The space complexity is $O(1)$.
67+
68+
<!-- tabs:start -->
69+
70+
#### Python3
71+
72+
```python
73+
class Solution:
74+
def earliestTime(self, tasks: List[List[int]]) -> int:
75+
return min(s + t for s, t in tasks)
76+
```
77+
78+
#### Java
79+
80+
```java
81+
class Solution {
82+
public int earliestTime(int[][] tasks) {
83+
int ans = 200;
84+
for (var task : tasks) {
85+
ans = Math.min(ans, task[0] + task[1]);
86+
}
87+
return ans;
88+
}
89+
}
90+
```
91+
92+
#### C++
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int earliestTime(vector<vector<int>>& tasks) {
98+
int ans = 200;
99+
for (const auto& task : tasks) {
100+
ans = min(ans, task[0] + task[1]);
101+
}
102+
return ans;
103+
}
104+
};
105+
```
106+
107+
#### Go
108+
109+
```go
110+
func earliestTime(tasks [][]int) int {
111+
ans := 200
112+
for _, task := range tasks {
113+
ans = min(ans, task[0]+task[1])
114+
}
115+
return ans
116+
}
117+
```
118+
119+
#### TypeScript
120+
121+
```ts
122+
function earliestTime(tasks: number[][]): number {
123+
return Math.min(...tasks.map(task => task[0] + task[1]));
124+
}
125+
```
126+
127+
<!-- tabs:end -->
128+
129+
<!-- solution:end -->
130+
131+
<!-- problem: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+
}

0 commit comments

Comments
 (0)