Skip to content

Commit 2c4113c

Browse files
authored
feat: add solutions to lc problems: No.2224~2226
* No.2224.Minimum Number of Operations to Convert Time * No.2225.Find Players With Zero or One Losses * No.2226.Maximum Candies Allocated to K Children
1 parent 62f58c8 commit 2c4113c

File tree

18 files changed

+620
-6
lines changed

18 files changed

+620
-6
lines changed

solution/2200-2299/2224.Minimum Number of Operations to Convert Time/README.md

+61-1
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,82 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
**方法一:贪心**
51+
5052
<!-- tabs:start -->
5153

5254
### **Python3**
5355

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

5658
```python
57-
59+
class Solution:
60+
def convertTime(self, current: str, correct: str) -> int:
61+
a = int(current[:2]) * 60 + int(current[3:])
62+
b = int(correct[:2]) * 60 + int(correct[3:])
63+
ans, d = 0, b - a
64+
for i in [60, 15, 5, 1]:
65+
ans += d // i
66+
d %= i
67+
return ans
5868
```
5969

6070
### **Java**
6171

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

6474
```java
75+
class Solution {
76+
public int convertTime(String current, String correct) {
77+
int a = Integer.parseInt(current.substring(0, 2)) * 60 + Integer.parseInt(current.substring(3));
78+
int b = Integer.parseInt(correct.substring(0, 2)) * 60 + Integer.parseInt(correct.substring(3));
79+
int ans = 0, d = b - a;
80+
for (int i : Arrays.asList(60, 15, 5, 1)) {
81+
ans += d / i;
82+
d %= i;
83+
}
84+
return ans;
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
int convertTime(string current, string correct) {
95+
int a = stoi(current.substr(0, 2)) * 60 + stoi(current.substr(3, 2));
96+
int b = stoi(correct.substr(0, 2)) * 60 + stoi(correct.substr(3, 2));
97+
int ans = 0, d = b - a;
98+
vector<int> inc = {60, 15, 5, 1};
99+
for (int i : inc)
100+
{
101+
ans += d / i;
102+
d %= i;
103+
}
104+
return ans;
105+
}
106+
};
107+
```
65108
109+
### **Go**
110+
111+
```go
112+
func convertTime(current string, correct string) int {
113+
parse := func(s string) int {
114+
h := int(s[0] - '0') * 10 + int(s[1] - '0')
115+
m := int(s[3] - '0') * 10 + int(s[4] - '0')
116+
return h * 60 + m
117+
}
118+
a, b := parse(current), parse(correct)
119+
ans, d := 0, b - a
120+
for _, i := range []int{60, 15, 5, 1} {
121+
ans += d / i
122+
d %= i
123+
}
124+
return ans
125+
}
66126
```
67127

68128
### **TypeScript**

solution/2200-2299/2224.Minimum Number of Operations to Convert Time/README_EN.md

+59-1
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,71 @@ It can be proven that it is not possible to convert current to correct in fewer
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def convertTime(self, current: str, correct: str) -> int:
53+
a = int(current[:2]) * 60 + int(current[3:])
54+
b = int(correct[:2]) * 60 + int(correct[3:])
55+
ans, d = 0, b - a
56+
for i in [60, 15, 5, 1]:
57+
ans += d // i
58+
d %= i
59+
return ans
5260
```
5361

5462
### **Java**
5563

5664
```java
65+
class Solution {
66+
public int convertTime(String current, String correct) {
67+
int a = Integer.parseInt(current.substring(0, 2)) * 60 + Integer.parseInt(current.substring(3));
68+
int b = Integer.parseInt(correct.substring(0, 2)) * 60 + Integer.parseInt(correct.substring(3));
69+
int ans = 0, d = b - a;
70+
for (int i : Arrays.asList(60, 15, 5, 1)) {
71+
ans += d / i;
72+
d %= i;
73+
}
74+
return ans;
75+
}
76+
}
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
int convertTime(string current, string correct) {
85+
int a = stoi(current.substr(0, 2)) * 60 + stoi(current.substr(3, 2));
86+
int b = stoi(correct.substr(0, 2)) * 60 + stoi(correct.substr(3, 2));
87+
int ans = 0, d = b - a;
88+
vector<int> inc = {60, 15, 5, 1};
89+
for (int i : inc)
90+
{
91+
ans += d / i;
92+
d %= i;
93+
}
94+
return ans;
95+
}
96+
};
97+
```
5798
99+
### **Go**
100+
101+
```go
102+
func convertTime(current string, correct string) int {
103+
parse := func(s string) int {
104+
h := int(s[0] - '0') * 10 + int(s[1] - '0')
105+
m := int(s[3] - '0') * 10 + int(s[4] - '0')
106+
return h * 60 + m
107+
}
108+
a, b := parse(current), parse(correct)
109+
ans, d := 0, b - a
110+
for _, i := range []int{60, 15, 5, 1} {
111+
ans += d / i
112+
d %= i
113+
}
114+
return ans
115+
}
58116
```
59117

60118
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int convertTime(string current, string correct) {
4+
int a = stoi(current.substr(0, 2)) * 60 + stoi(current.substr(3, 2));
5+
int b = stoi(correct.substr(0, 2)) * 60 + stoi(correct.substr(3, 2));
6+
int ans = 0, d = b - a;
7+
vector<int> inc = {60, 15, 5, 1};
8+
for (int i : inc)
9+
{
10+
ans += d / i;
11+
d %= i;
12+
}
13+
return ans;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func convertTime(current string, correct string) int {
2+
parse := func(s string) int {
3+
h := int(s[0] - '0') * 10 + int(s[1] - '0')
4+
m := int(s[3] - '0') * 10 + int(s[4] - '0')
5+
return h * 60 + m
6+
}
7+
a, b := parse(current), parse(correct)
8+
ans, d := 0, b - a
9+
for _, i := range []int{60, 15, 5, 1} {
10+
ans += d / i
11+
d %= i
12+
}
13+
return ans
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int convertTime(String current, String correct) {
3+
int a = Integer.parseInt(current.substring(0, 2)) * 60 + Integer.parseInt(current.substring(3));
4+
int b = Integer.parseInt(correct.substring(0, 2)) * 60 + Integer.parseInt(correct.substring(3));
5+
int ans = 0, d = b - a;
6+
for (int i : Arrays.asList(60, 15, 5, 1)) {
7+
ans += d / i;
8+
d %= i;
9+
}
10+
return ans;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def convertTime(self, current: str, correct: str) -> int:
3+
a = int(current[:2]) * 60 + int(current[3:])
4+
b = int(correct[:2]) * 60 + int(correct[3:])
5+
ans, d = 0, b - a
6+
for i in [60, 15, 5, 1]:
7+
ans += d // i
8+
d %= i
9+
return ans

solution/2200-2299/2225.Find Players With Zero or One Losses/README.md

+87-1
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,108 @@
6565

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

68+
**方法一:哈希表**
69+
6870
<!-- tabs:start -->
6971

7072
### **Python3**
7173

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

7476
```python
75-
77+
class Solution:
78+
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
79+
cnt = Counter()
80+
for a, b in matches:
81+
if a not in cnt:
82+
cnt[a] = 0
83+
cnt[b] += 1
84+
ans = [[], []]
85+
for u, v in cnt.items():
86+
if v < 2:
87+
ans[v].append(u)
88+
ans[0].sort()
89+
ans[1].sort()
90+
return ans
7691
```
7792

7893
### **Java**
7994

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

8297
```java
98+
class Solution {
99+
public List<List<Integer>> findWinners(int[][] matches) {
100+
Map<Integer, Integer> cnt = new HashMap<>();
101+
for (int[] m : matches) {
102+
int a = m[0], b = m[1];
103+
cnt.putIfAbsent(a, 0);
104+
cnt.put(b, cnt.getOrDefault(b, 0) + 1);
105+
}
106+
List<List<Integer>> ans = new ArrayList<>();
107+
ans.add(new ArrayList<>());
108+
ans.add(new ArrayList<>());
109+
for (Map.Entry<Integer, Integer> entry : cnt.entrySet()) {
110+
int u = entry.getKey();
111+
int v = entry.getValue();
112+
if (v < 2) {
113+
ans.get(v).add(u);
114+
}
115+
}
116+
Collections.sort(ans.get(0));
117+
Collections.sort(ans.get(1));
118+
return ans;
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
vector<vector<int>> findWinners(vector<vector<int>>& matches) {
129+
unordered_map<int, int> cnt;
130+
for (auto& m : matches)
131+
{
132+
int a = m[0], b = m[1];
133+
if (!cnt.count(a)) cnt[a] = 0;
134+
++cnt[b];
135+
}
136+
vector<vector<int>> ans(2);
137+
for (auto& [u, v] : cnt)
138+
{
139+
if (v < 2) ans[v].push_back(u);
140+
}
141+
sort(ans[0].begin(), ans[0].end());
142+
sort(ans[1].begin(), ans[1].end());
143+
return ans;
144+
}
145+
};
146+
```
83147
148+
### **Go**
149+
150+
```go
151+
func findWinners(matches [][]int) [][]int {
152+
cnt := map[int]int{}
153+
for _, m := range matches {
154+
a, b := m[0], m[1]
155+
if _, ok := cnt[a]; !ok {
156+
cnt[a] = 0
157+
}
158+
cnt[b]++
159+
}
160+
ans := make([][]int, 2)
161+
for u, v := range cnt {
162+
if v < 2 {
163+
ans[v] = append(ans[v], u)
164+
}
165+
}
166+
sort.Ints(ans[0])
167+
sort.Ints(ans[1])
168+
return ans
169+
}
84170
```
85171

86172
### **TypeScript**

0 commit comments

Comments
 (0)