Skip to content

Commit d1ef027

Browse files
committed
feat: add solutions to lc problem: No.1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
1 parent 01ac8a6 commit d1ef027

File tree

6 files changed

+116
-21
lines changed

6 files changed

+116
-21
lines changed

solution/1600-1699/1689.Partitioning Into Minimum Number Of Deci-Binary Numbers/README.md

+43-2
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,68 @@
4141
<li><code>n</code> 不含任何前导零并总是表示正整数</li>
4242
</ul>
4343

44-
4544
## 解法
4645

4746
<!-- 这里可写通用的实现逻辑 -->
4847

48+
题目等价于找字符串中的最大数。
49+
4950
<!-- tabs:start -->
5051

5152
### **Python3**
5253

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

5556
```python
56-
57+
class Solution:
58+
def minPartitions(self, n: str) -> int:
59+
return int(max(n))
5760
```
5861

5962
### **Java**
6063

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

6366
```java
67+
class Solution {
68+
public int minPartitions(String n) {
69+
int res = 0;
70+
for (char c : n.toCharArray()) {
71+
res = Math.max(res, c - '0');
72+
}
73+
return res;
74+
}
75+
}
76+
```
77+
78+
### **C++**
79+
80+
```cpp
81+
class Solution {
82+
public:
83+
int minPartitions(string n) {
84+
int res = 0;
85+
for (auto& c : n) {
86+
res = max(res, c - '0');
87+
}
88+
return res;
89+
}
90+
};
91+
```
6492
93+
### **Go**
94+
95+
```go
96+
func minPartitions(n string) int {
97+
res := 0
98+
for _, c := range n {
99+
t := int(c - '0')
100+
if t > res {
101+
res = t
102+
}
103+
}
104+
return res
105+
}
65106
```
66107

67108
### **...**

solution/1600-1699/1689.Partitioning Into Minimum Number Of Deci-Binary Numbers/README_EN.md

+41-19
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,12 @@
66

77
<p>A decimal number is called <strong>deci-binary</strong> if each of its digits is either <code>0</code> or <code>1</code> without any leading zeros. For example, <code>101</code> and <code>1100</code> are <strong>deci-binary</strong>, while <code>112</code> and <code>3001</code> are not.</p>
88

9-
10-
119
<p>Given a string <code>n</code> that represents a positive decimal integer, return <em>the <strong>minimum</strong> number of positive <strong>deci-binary</strong> numbers needed so that they sum up to </em><code>n</code><em>.</em></p>
1210

13-
14-
1511
<p>&nbsp;</p>
1612

1713
<p><strong>Example 1:</strong></p>
1814

19-
20-
2115
<pre>
2216

2317
<strong>Input:</strong> n = &quot;32&quot;
@@ -28,12 +22,8 @@
2822

2923
</pre>
3024

31-
32-
3325
<p><strong>Example 2:</strong></p>
3426

35-
36-
3727
<pre>
3828

3929
<strong>Input:</strong> n = &quot;82734&quot;
@@ -42,12 +32,8 @@
4232

4333
</pre>
4434

45-
46-
4735
<p><strong>Example 3:</strong></p>
4836

49-
50-
5137
<pre>
5238

5339
<strong>Input:</strong> n = &quot;27346209830709182346&quot;
@@ -56,14 +42,10 @@
5642

5743
</pre>
5844

59-
60-
6145
<p>&nbsp;</p>
6246

6347
<p><strong>Constraints:</strong></p>
6448

65-
66-
6749
<ul>
6850
<li><code>1 &lt;= n.length &lt;= 10<sup>5</sup></code></li>
6951
<li><code>n</code> consists of only digits.</li>
@@ -77,13 +59,53 @@
7759
### **Python3**
7860

7961
```python
80-
62+
class Solution:
63+
def minPartitions(self, n: str) -> int:
64+
return int(max(n))
8165
```
8266

8367
### **Java**
8468

8569
```java
70+
class Solution {
71+
public int minPartitions(String n) {
72+
int res = 0;
73+
for (char c : n.toCharArray()) {
74+
res = Math.max(res, c - '0');
75+
}
76+
return res;
77+
}
78+
}
79+
```
80+
81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
int minPartitions(string n) {
87+
int res = 0;
88+
for (auto& c : n) {
89+
res = max(res, c - '0');
90+
}
91+
return res;
92+
}
93+
};
94+
```
8695
96+
### **Go**
97+
98+
```go
99+
func minPartitions(n string) int {
100+
res := 0
101+
for _, c := range n {
102+
t := int(c - '0')
103+
if t > res {
104+
res = t
105+
}
106+
}
107+
return res
108+
}
87109
```
88110

89111
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int minPartitions(string n) {
4+
int res = 0;
5+
for (auto& c : n) {
6+
res = max(res, c - '0');
7+
}
8+
return res;
9+
}
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func minPartitions(n string) int {
2+
res := 0
3+
for _, c := range n {
4+
t := int(c - '0')
5+
if t > res {
6+
res = t
7+
}
8+
}
9+
return res
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int minPartitions(String n) {
3+
int res = 0;
4+
for (char c : n.toCharArray()) {
5+
res = Math.max(res, c - '0');
6+
}
7+
return res;
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def minPartitions(self, n: str) -> int:
3+
return int(max(n))

0 commit comments

Comments
 (0)