Skip to content

Commit 82ccf76

Browse files
committed
feat: add solutions to lc problems: No.1952,1953
1 parent 523a99a commit 82ccf76

File tree

14 files changed

+284
-6
lines changed

14 files changed

+284
-6
lines changed

solution/1900-1999/1952.Three Divisors/README.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,60 @@
4444
<!-- 这里可写当前语言的特殊实现逻辑 -->
4545

4646
```python
47-
47+
class Solution:
48+
def isThree(self, n: int) -> bool:
49+
cnt = 0
50+
for i in range(2, n):
51+
if n % i == 0:
52+
cnt += 1
53+
return cnt == 1
4854
```
4955

5056
### **Java**
5157

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

5460
```java
61+
class Solution {
62+
public boolean isThree(int n) {
63+
int cnt = 0;
64+
for (int i = 2; i < n; i++) {
65+
if (n % i == 0) {
66+
++cnt;
67+
}
68+
}
69+
return cnt == 1;
70+
}
71+
}
72+
```
73+
74+
### **C++**
75+
76+
```cpp
77+
class Solution {
78+
public:
79+
bool isThree(int n) {
80+
int cnt = 0;
81+
for (int i = 2; i < n; ++i) {
82+
if (n % i == 0) ++cnt;
83+
}
84+
return cnt == 1;
85+
}
86+
};
87+
```
5588
89+
### **Go**
90+
91+
```go
92+
func isThree(n int) bool {
93+
cnt := 0
94+
for i := 2; i < n; i++ {
95+
if n%i == 0 {
96+
cnt++
97+
}
98+
}
99+
return cnt == 1
100+
}
56101
```
57102

58103
### **...**

solution/1900-1999/1952.Three Divisors/README_EN.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,58 @@
3939
### **Python3**
4040

4141
```python
42-
42+
class Solution:
43+
def isThree(self, n: int) -> bool:
44+
cnt = 0
45+
for i in range(2, n):
46+
if n % i == 0:
47+
cnt += 1
48+
return cnt == 1
4349
```
4450

4551
### **Java**
4652

4753
```java
54+
class Solution {
55+
public boolean isThree(int n) {
56+
int cnt = 0;
57+
for (int i = 2; i < n; i++) {
58+
if (n % i == 0) {
59+
++cnt;
60+
}
61+
}
62+
return cnt == 1;
63+
}
64+
}
65+
```
66+
67+
### **C++**
68+
69+
```cpp
70+
class Solution {
71+
public:
72+
bool isThree(int n) {
73+
int cnt = 0;
74+
for (int i = 2; i < n; ++i) {
75+
if (n % i == 0) ++cnt;
76+
}
77+
return cnt == 1;
78+
}
79+
};
80+
```
4881
82+
### **Go**
83+
84+
```go
85+
func isThree(n int) bool {
86+
cnt := 0
87+
for i := 2; i < n; i++ {
88+
if n%i == 0 {
89+
cnt++
90+
}
91+
}
92+
return cnt == 1
93+
}
4994
```
5095

5196
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
bool isThree(int n) {
4+
int cnt = 0;
5+
for (int i = 2; i < n; ++i) {
6+
if (n % i == 0) ++cnt;
7+
}
8+
return cnt == 1;
9+
}
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func isThree(n int) bool {
2+
cnt := 0
3+
for i := 2; i < n; i++ {
4+
if n%i == 0 {
5+
cnt++
6+
}
7+
}
8+
return cnt == 1
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public boolean isThree(int n) {
3+
int cnt = 0;
4+
for (int i = 2; i < n; i++) {
5+
if (n % i == 0) {
6+
++cnt;
7+
}
8+
}
9+
return cnt == 1;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def isThree(self, n: int) -> bool:
3+
cnt = 0
4+
for i in range(2, n):
5+
if n % i == 0:
6+
cnt += 1
7+
return cnt == 1

solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/README.md

+54-1
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,68 @@
7474
<!-- 这里可写当前语言的特殊实现逻辑 -->
7575

7676
```python
77-
77+
class Solution:
78+
def numberOfWeeks(self, milestones: List[int]) -> int:
79+
mx, s = max(milestones), sum(milestones)
80+
rest = s - mx
81+
return rest * 2 + 1 if mx > rest + 1 else s
7882
```
7983

8084
### **Java**
8185

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

8488
```java
89+
class Solution {
90+
public long numberOfWeeks(int[] milestones) {
91+
int mx = 0;
92+
long s = 0;
93+
for (int e : milestones) {
94+
s += e;
95+
mx = Math.max(mx, e);
96+
}
97+
long rest = s - mx;
98+
return mx > rest + 1 ? rest * 2 + 1 : s;
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
long long numberOfWeeks(vector<int>& milestones) {
109+
int mx = *max_element(milestones.begin(), milestones.end());
110+
long long s = accumulate(milestones.begin(), milestones.end(), 0LL);
111+
long long rest = s - mx;
112+
return mx > rest + 1 ? rest * 2 + 1 : s;
113+
}
114+
};
115+
```
85116
117+
### **Go**
118+
119+
```go
120+
func numberOfWeeks(milestones []int) int64 {
121+
mx, s := 0, 0
122+
for _, e := range milestones {
123+
mx = max(mx, e)
124+
s += e
125+
}
126+
rest := s - mx
127+
if mx > rest+1 {
128+
return int64(rest*2 + 1)
129+
}
130+
return int64(s)
131+
}
132+
133+
func max(a, b int) int {
134+
if a > b {
135+
return a
136+
}
137+
return b
138+
}
86139
```
87140

88141
### **...**

solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/README_EN.md

+54-1
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,66 @@ Thus, one milestone in project 0 will remain unfinished.
6767
### **Python3**
6868

6969
```python
70-
70+
class Solution:
71+
def numberOfWeeks(self, milestones: List[int]) -> int:
72+
mx, s = max(milestones), sum(milestones)
73+
rest = s - mx
74+
return rest * 2 + 1 if mx > rest + 1 else s
7175
```
7276

7377
### **Java**
7478

7579
```java
80+
class Solution {
81+
public long numberOfWeeks(int[] milestones) {
82+
int mx = 0;
83+
long s = 0;
84+
for (int e : milestones) {
85+
s += e;
86+
mx = Math.max(mx, e);
87+
}
88+
long rest = s - mx;
89+
return mx > rest + 1 ? rest * 2 + 1 : s;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
long long numberOfWeeks(vector<int>& milestones) {
100+
int mx = *max_element(milestones.begin(), milestones.end());
101+
long long s = accumulate(milestones.begin(), milestones.end(), 0LL);
102+
long long rest = s - mx;
103+
return mx > rest + 1 ? rest * 2 + 1 : s;
104+
}
105+
};
106+
```
76107
108+
### **Go**
109+
110+
```go
111+
func numberOfWeeks(milestones []int) int64 {
112+
mx, s := 0, 0
113+
for _, e := range milestones {
114+
mx = max(mx, e)
115+
s += e
116+
}
117+
rest := s - mx
118+
if mx > rest+1 {
119+
return int64(rest*2 + 1)
120+
}
121+
return int64(s)
122+
}
123+
124+
func max(a, b int) int {
125+
if a > b {
126+
return a
127+
}
128+
return b
129+
}
77130
```
78131

79132
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
long long numberOfWeeks(vector<int>& milestones) {
4+
int mx = *max_element(milestones.begin(), milestones.end());
5+
long long s = accumulate(milestones.begin(), milestones.end(), 0LL);
6+
long long rest = s - mx;
7+
return mx > rest + 1 ? rest * 2 + 1 : s;
8+
}
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func numberOfWeeks(milestones []int) int64 {
2+
mx, s := 0, 0
3+
for _, e := range milestones {
4+
mx = max(mx, e)
5+
s += e
6+
}
7+
rest := s - mx
8+
if mx > rest+1 {
9+
return int64(rest*2 + 1)
10+
}
11+
return int64(s)
12+
}
13+
14+
func max(a, b int) int {
15+
if a > b {
16+
return a
17+
}
18+
return b
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public long numberOfWeeks(int[] milestones) {
3+
int mx = 0;
4+
long s = 0;
5+
for (int e : milestones) {
6+
s += e;
7+
mx = Math.max(mx, e);
8+
}
9+
long rest = s - mx;
10+
return mx > rest + 1 ? rest * 2 + 1 : s;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def numberOfWeeks(self, milestones: List[int]) -> int:
3+
mx, s = max(milestones), sum(milestones)
4+
rest = s - mx
5+
return rest * 2 + 1 if mx > rest + 1 else s

solution/1900-1999/1954.Minimum Garden Perimeter to Collect Enough Apples/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<p>&nbsp;</p>
2323

2424
<p><strong>示例 1:</strong></p>
25-
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1954.Minimum%20Garden%20Perimeter%20to%20Collect%20Enough%20Apples/images/1527_example_1_2.png" style="width: 442px; height: 449px;" />
25+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1954.Minimum%20Garden%20Perimeter%20to%20Collect%20Enough%20Apples/images/1627790803-qcBKFw-image.png" style="width: 442px; height: 449px;" />
2626
<pre>
2727
<b>输入:</b>neededApples = 1
2828
<b>输出:</b>8

solution/1900-1999/1954.Minimum Garden Perimeter to Collect Enough Apples/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<p>&nbsp;</p>
2121
<p><strong>Example 1:</strong></p>
22-
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1954.Minimum%20Garden%20Perimeter%20to%20Collect%20Enough%20Apples/images/1527_example_1_2.png" style="width: 442px; height: 449px;" />
22+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1900-1999/1954.Minimum%20Garden%20Perimeter%20to%20Collect%20Enough%20Apples/images/1627790803-qcBKFw-image.png" style="width: 442px; height: 449px;" />
2323
<pre>
2424
<strong>Input:</strong> neededApples = 1
2525
<strong>Output:</strong> 8

0 commit comments

Comments
 (0)