Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1979699

Browse files
authored
feat: add solutions to lc problems: No.2933,2934 (doocs#1962)
* No.2933.High-Access Employees * No.2934.Minimum Operations to Maximize Last Elements in Arrays
1 parent 5d9dc6a commit 1979699

File tree

14 files changed

+662
-12
lines changed

14 files changed

+662
-12
lines changed

solution/2900-2999/2933.High-Access Employees/README.md

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,34 +65,145 @@
6565

6666
<!-- 这里可写通用的实现逻辑 -->
6767

68+
**方法一:哈希表 + 排序**
69+
70+
我们用一个哈希表 $d$ 来存储每个员工的所有访问时间,其中键为员工的姓名,值为一个整数数组,表示该员工的所有访问时间,该时间为从当天 00:00 开始的分钟数。
71+
72+
对于每个员工,我们将其所有访问时间按照从小到大的顺序进行排序。然后我们遍历该员工的所有访问时间,如果存在连续的三个访问时间 $t_1, t_2, t_3$,满足 $t_3 - t_1 < 60$,则该员工为高访问员工,我们将其姓名加入答案数组中。
73+
74+
最后,返回答案数组即可。
75+
76+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为访问记录的数量。
77+
6878
<!-- tabs:start -->
6979

7080
### **Python3**
7181

7282
<!-- 这里可写当前语言的特殊实现逻辑 -->
7383

7484
```python
75-
85+
class Solution:
86+
def findHighAccessEmployees(self, access_times: List[List[str]]) -> List[str]:
87+
d = defaultdict(list)
88+
for name, t in access_times:
89+
d[name].append(int(t[:2]) * 60 + int(t[2:]))
90+
ans = []
91+
for name, ts in d.items():
92+
ts.sort()
93+
if any(ts[i] - ts[i - 2] < 60 for i in range(2, len(ts))):
94+
ans.append(name)
95+
return ans
7696
```
7797

7898
### **Java**
7999

80100
<!-- 这里可写当前语言的特殊实现逻辑 -->
81101

82102
```java
83-
103+
class Solution {
104+
public List<String> findHighAccessEmployees(List<List<String>> access_times) {
105+
Map<String, List<Integer>> d = new HashMap<>();
106+
for (var e : access_times) {
107+
String name = e.get(0), s = e.get(1);
108+
int t = Integer.valueOf(s.substring(0, 2)) * 60 + Integer.valueOf(s.substring(2));
109+
d.computeIfAbsent(name, k -> new ArrayList<>()).add(t);
110+
}
111+
List<String> ans = new ArrayList<>();
112+
for (var e : d.entrySet()) {
113+
String name = e.getKey();
114+
var ts = e.getValue();
115+
Collections.sort(ts);
116+
for (int i = 2; i < ts.size(); ++i) {
117+
if (ts.get(i) - ts.get(i - 2) < 60) {
118+
ans.add(name);
119+
break;
120+
}
121+
}
122+
}
123+
return ans;
124+
}
125+
}
84126
```
85127

86128
### **C++**
87129

88130
```cpp
89-
131+
class Solution {
132+
public:
133+
vector<string> findHighAccessEmployees(vector<vector<string>>& access_times) {
134+
unordered_map<string, vector<int>> d;
135+
for (auto& e : access_times) {
136+
auto name = e[0];
137+
auto s = e[1];
138+
int t = stoi(s.substr(0, 2)) * 60 + stoi(s.substr(2, 2));
139+
d[name].emplace_back(t);
140+
}
141+
vector<string> ans;
142+
for (auto& [name, ts] : d) {
143+
sort(ts.begin(), ts.end());
144+
for (int i = 2; i < ts.size(); ++i) {
145+
if (ts[i] - ts[i - 2] < 60) {
146+
ans.emplace_back(name);
147+
break;
148+
}
149+
}
150+
}
151+
return ans;
152+
}
153+
};
90154
```
91155
92156
### **Go**
93157
94158
```go
159+
func findHighAccessEmployees(access_times [][]string) (ans []string) {
160+
d := map[string][]int{}
161+
for _, e := range access_times {
162+
name, s := e[0], e[1]
163+
h, _ := strconv.Atoi(s[:2])
164+
m, _ := strconv.Atoi(s[2:])
165+
t := h*60 + m
166+
d[name] = append(d[name], t)
167+
}
168+
for name, ts := range d {
169+
sort.Ints(ts)
170+
for i := 2; i < len(ts); i++ {
171+
if ts[i]-ts[i-2] < 60 {
172+
ans = append(ans, name)
173+
break
174+
}
175+
}
176+
}
177+
return
178+
}
179+
```
95180

181+
### **TypeScript**
182+
183+
```ts
184+
function findHighAccessEmployees(access_times: string[][]): string[] {
185+
const d: Map<string, number[]> = new Map();
186+
for (const [name, s] of access_times) {
187+
const h = parseInt(s.slice(0, 2), 10);
188+
const m = parseInt(s.slice(2), 10);
189+
const t = h * 60 + m;
190+
if (!d.has(name)) {
191+
d.set(name, []);
192+
}
193+
d.get(name)!.push(t);
194+
}
195+
const ans: string[] = [];
196+
for (const [name, ts] of d) {
197+
ts.sort((a, b) => a - b);
198+
for (let i = 2; i < ts.length; ++i) {
199+
if (ts[i] - ts[i - 2] < 60) {
200+
ans.push(name);
201+
break;
202+
}
203+
}
204+
}
205+
return ans;
206+
}
96207
```
97208

98209
### **...**

solution/2900-2999/2933.High-Access Employees/README_EN.md

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,141 @@ So the answer is [&quot;ab&quot;,&quot;cd&quot;].</pre>
5959

6060
## Solutions
6161

62+
**Solution 1: Hash Table + Sorting**
63+
64+
We use a hash table $d$ to store all access times of each employee, where the key is the employee's name, and the value is an integer array, representing all access times of the employee, which are the number of minutes from the start of the day at 00:00.
65+
66+
For each employee, we sort all their access times in ascending order. Then we traverse all access times of the employee. If there are three consecutive access times $t_1, t_2, t_3$ that satisfy $t_3 - t_1 < 60$, then the employee is a high-frequency visitor, and we add their name to the answer array.
67+
68+
Finally, we return the answer array.
69+
70+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of access records.
71+
6272
<!-- tabs:start -->
6373

6474
### **Python3**
6575

6676
```python
67-
77+
class Solution:
78+
def findHighAccessEmployees(self, access_times: List[List[str]]) -> List[str]:
79+
d = defaultdict(list)
80+
for name, t in access_times:
81+
d[name].append(int(t[:2]) * 60 + int(t[2:]))
82+
ans = []
83+
for name, ts in d.items():
84+
ts.sort()
85+
if any(ts[i] - ts[i - 2] < 60 for i in range(2, len(ts))):
86+
ans.append(name)
87+
return ans
6888
```
6989

7090
### **Java**
7191

7292
```java
73-
93+
class Solution {
94+
public List<String> findHighAccessEmployees(List<List<String>> access_times) {
95+
Map<String, List<Integer>> d = new HashMap<>();
96+
for (var e : access_times) {
97+
String name = e.get(0), s = e.get(1);
98+
int t = Integer.valueOf(s.substring(0, 2)) * 60 + Integer.valueOf(s.substring(2));
99+
d.computeIfAbsent(name, k -> new ArrayList<>()).add(t);
100+
}
101+
List<String> ans = new ArrayList<>();
102+
for (var e : d.entrySet()) {
103+
String name = e.getKey();
104+
var ts = e.getValue();
105+
Collections.sort(ts);
106+
for (int i = 2; i < ts.size(); ++i) {
107+
if (ts.get(i) - ts.get(i - 2) < 60) {
108+
ans.add(name);
109+
break;
110+
}
111+
}
112+
}
113+
return ans;
114+
}
115+
}
74116
```
75117

76118
### **C++**
77119

78120
```cpp
79-
121+
class Solution {
122+
public:
123+
vector<string> findHighAccessEmployees(vector<vector<string>>& access_times) {
124+
unordered_map<string, vector<int>> d;
125+
for (auto& e : access_times) {
126+
auto name = e[0];
127+
auto s = e[1];
128+
int t = stoi(s.substr(0, 2)) * 60 + stoi(s.substr(2, 2));
129+
d[name].emplace_back(t);
130+
}
131+
vector<string> ans;
132+
for (auto& [name, ts] : d) {
133+
sort(ts.begin(), ts.end());
134+
for (int i = 2; i < ts.size(); ++i) {
135+
if (ts[i] - ts[i - 2] < 60) {
136+
ans.emplace_back(name);
137+
break;
138+
}
139+
}
140+
}
141+
return ans;
142+
}
143+
};
80144
```
81145
82146
### **Go**
83147
84148
```go
149+
func findHighAccessEmployees(access_times [][]string) (ans []string) {
150+
d := map[string][]int{}
151+
for _, e := range access_times {
152+
name, s := e[0], e[1]
153+
h, _ := strconv.Atoi(s[:2])
154+
m, _ := strconv.Atoi(s[2:])
155+
t := h*60 + m
156+
d[name] = append(d[name], t)
157+
}
158+
for name, ts := range d {
159+
sort.Ints(ts)
160+
for i := 2; i < len(ts); i++ {
161+
if ts[i]-ts[i-2] < 60 {
162+
ans = append(ans, name)
163+
break
164+
}
165+
}
166+
}
167+
return
168+
}
169+
```
85170

171+
### **TypeScript**
172+
173+
```ts
174+
function findHighAccessEmployees(access_times: string[][]): string[] {
175+
const d: Map<string, number[]> = new Map();
176+
for (const [name, s] of access_times) {
177+
const h = parseInt(s.slice(0, 2), 10);
178+
const m = parseInt(s.slice(2), 10);
179+
const t = h * 60 + m;
180+
if (!d.has(name)) {
181+
d.set(name, []);
182+
}
183+
d.get(name)!.push(t);
184+
}
185+
const ans: string[] = [];
186+
for (const [name, ts] of d) {
187+
ts.sort((a, b) => a - b);
188+
for (let i = 2; i < ts.length; ++i) {
189+
if (ts[i] - ts[i - 2] < 60) {
190+
ans.push(name);
191+
break;
192+
}
193+
}
194+
}
195+
return ans;
196+
}
86197
```
87198

88199
### **...**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<string> findHighAccessEmployees(vector<vector<string>>& access_times) {
4+
unordered_map<string, vector<int>> d;
5+
for (auto& e : access_times) {
6+
auto name = e[0];
7+
auto s = e[1];
8+
int t = stoi(s.substr(0, 2)) * 60 + stoi(s.substr(2, 2));
9+
d[name].emplace_back(t);
10+
}
11+
vector<string> ans;
12+
for (auto& [name, ts] : d) {
13+
sort(ts.begin(), ts.end());
14+
for (int i = 2; i < ts.size(); ++i) {
15+
if (ts[i] - ts[i - 2] < 60) {
16+
ans.emplace_back(name);
17+
break;
18+
}
19+
}
20+
}
21+
return ans;
22+
}
23+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func findHighAccessEmployees(access_times [][]string) (ans []string) {
2+
d := map[string][]int{}
3+
for _, e := range access_times {
4+
name, s := e[0], e[1]
5+
h, _ := strconv.Atoi(s[:2])
6+
m, _ := strconv.Atoi(s[2:])
7+
t := h*60 + m
8+
d[name] = append(d[name], t)
9+
}
10+
for name, ts := range d {
11+
sort.Ints(ts)
12+
for i := 2; i < len(ts); i++ {
13+
if ts[i]-ts[i-2] < 60 {
14+
ans = append(ans, name)
15+
break
16+
}
17+
}
18+
}
19+
return
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public List<String> findHighAccessEmployees(List<List<String>> access_times) {
3+
Map<String, List<Integer>> d = new HashMap<>();
4+
for (var e : access_times) {
5+
String name = e.get(0), s = e.get(1);
6+
int t = Integer.valueOf(s.substring(0, 2)) * 60 + Integer.valueOf(s.substring(2));
7+
d.computeIfAbsent(name, k -> new ArrayList<>()).add(t);
8+
}
9+
List<String> ans = new ArrayList<>();
10+
for (var e : d.entrySet()) {
11+
String name = e.getKey();
12+
var ts = e.getValue();
13+
Collections.sort(ts);
14+
for (int i = 2; i < ts.size(); ++i) {
15+
if (ts.get(i) - ts.get(i - 2) < 60) {
16+
ans.add(name);
17+
break;
18+
}
19+
}
20+
}
21+
return ans;
22+
}
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def findHighAccessEmployees(self, access_times: List[List[str]]) -> List[str]:
3+
d = defaultdict(list)
4+
for name, t in access_times:
5+
d[name].append(int(t[:2]) * 60 + int(t[2:]))
6+
ans = []
7+
for name, ts in d.items():
8+
ts.sort()
9+
if any(ts[i] - ts[i - 2] < 60 for i in range(2, len(ts))):
10+
ans.append(name)
11+
return ans

0 commit comments

Comments
 (0)