Skip to content

Commit cb5197b

Browse files
committed
feat: add solutions to lc problem: No.1798
No.1798.Maximum Number of Consecutive Values You Can Make
1 parent 93b5622 commit cb5197b

File tree

6 files changed

+124
-37
lines changed

6 files changed

+124
-37
lines changed

solution/1700-1799/1798.Maximum Number of Consecutive Values You Can Make/README.md

+48-17
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,15 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63-
先对 `coins` 数组进行排序。
63+
**方法一:排序 + 贪心**
6464

65-
假设前 i 个数所有构造的的连续整数的个数为 res,初始化为 1
65+
我们先对数组进行排序。然后定义 $ans$ 表示当前能够构造的连续整数的个数,初始化为 $1$
6666

67-
遍历排序后的 `coins` 数组:
67+
遍历数组,对于当前遍历到的元素 $v$,如果 $v \gt ans$,说明无法构造出 $ans+1$ 个连续的整数,因此直接跳出循环,返回 $ans$ 即可。否则,说明可以构造出 $ans+v$ 个连续的整数,因此更新 $ans$ 为 $ans+v$。
6868

69-
-`coins[i] > res`,说明接下来无法组成 `res + 1` 个连续整数,跳出循环
69+
最后返回 $ans$ 即可
7070

71-
> 对于 `1, 3`,若遍历到 3,此时前面的连续整数个数为 2,即连续整数为:`0, 1`。此时 3 大于 2,无法构成连续整数 `0, 1, 2`,所以最大连续整数个数为 2。
72-
73-
-`coins[i] <= res`,说明有 `coins[i]` 个数也能构成连续整数。
71+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组的长度。
7472

7573
<!-- tabs:start -->
7674

@@ -81,12 +79,12 @@
8179
```python
8280
class Solution:
8381
def getMaximumConsecutive(self, coins: List[int]) -> int:
84-
res = 1
85-
for coin in sorted(coins):
86-
if coin > res:
82+
ans = 1
83+
for v in sorted(coins):
84+
if v > ans:
8785
break
88-
res += coin
89-
return res
86+
ans += v
87+
return ans
9088
```
9189

9290
### **Java**
@@ -96,16 +94,49 @@ class Solution:
9694
```java
9795
class Solution {
9896
public int getMaximumConsecutive(int[] coins) {
99-
int res = 1;
10097
Arrays.sort(coins);
101-
for (int coin : coins) {
102-
if (coin > res) {
98+
int ans = 1;
99+
for (int v : coins) {
100+
if (v > ans) {
103101
break;
104102
}
105-
res += coin;
103+
ans += v;
104+
}
105+
return ans;
106+
}
107+
}
108+
```
109+
110+
### **C++**
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
int getMaximumConsecutive(vector<int>& coins) {
116+
sort(coins.begin(), coins.end());
117+
int ans = 1;
118+
for (int& v : coins) {
119+
if (v > ans) break;
120+
ans += v;
106121
}
107-
return res;
122+
return ans;
108123
}
124+
};
125+
```
126+
127+
### **Go**
128+
129+
```go
130+
func getMaximumConsecutive(coins []int) int {
131+
sort.Ints(coins)
132+
ans := 1
133+
for _, v := range coins {
134+
if v > ans {
135+
break
136+
}
137+
ans += v
138+
}
139+
return ans
109140
}
110141
```
111142

solution/1700-1799/1798.Maximum Number of Consecutive Values You Can Make/README_EN.md

+43-10
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,65 @@ You can make 8 consecutive integer values starting from 0.</pre>
6161
```python
6262
class Solution:
6363
def getMaximumConsecutive(self, coins: List[int]) -> int:
64-
res = 1
65-
for coin in sorted(coins):
66-
if coin > res:
64+
ans = 1
65+
for v in sorted(coins):
66+
if v > ans:
6767
break
68-
res += coin
69-
return res
68+
ans += v
69+
return ans
7070
```
7171

7272
### **Java**
7373

7474
```java
7575
class Solution {
7676
public int getMaximumConsecutive(int[] coins) {
77-
int res = 1;
7877
Arrays.sort(coins);
79-
for (int coin : coins) {
80-
if (coin > res) {
78+
int ans = 1;
79+
for (int v : coins) {
80+
if (v > ans) {
8181
break;
8282
}
83-
res += coin;
83+
ans += v;
8484
}
85-
return res;
85+
return ans;
8686
}
8787
}
8888
```
8989

90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
int getMaximumConsecutive(vector<int>& coins) {
96+
sort(coins.begin(), coins.end());
97+
int ans = 1;
98+
for (int& v : coins) {
99+
if (v > ans) break;
100+
ans += v;
101+
}
102+
return ans;
103+
}
104+
};
105+
```
106+
107+
### **Go**
108+
109+
```go
110+
func getMaximumConsecutive(coins []int) int {
111+
sort.Ints(coins)
112+
ans := 1
113+
for _, v := range coins {
114+
if v > ans {
115+
break
116+
}
117+
ans += v
118+
}
119+
return ans
120+
}
121+
```
122+
90123
### **...**
91124

92125
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int getMaximumConsecutive(vector<int>& coins) {
4+
sort(coins.begin(), coins.end());
5+
int ans = 1;
6+
for (int& v : coins) {
7+
if (v > ans) break;
8+
ans += v;
9+
}
10+
return ans;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func getMaximumConsecutive(coins []int) int {
2+
sort.Ints(coins)
3+
ans := 1
4+
for _, v := range coins {
5+
if v > ans {
6+
break
7+
}
8+
ans += v
9+
}
10+
return ans
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public int getMaximumConsecutive(int[] coins) {
3-
int res = 1;
43
Arrays.sort(coins);
5-
for (int coin : coins) {
6-
if (coin > res) {
4+
int ans = 1;
5+
for (int v : coins) {
6+
if (v > ans) {
77
break;
88
}
9-
res += coin;
9+
ans += v;
1010
}
11-
return res;
11+
return ans;
1212
}
1313
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def getMaximumConsecutive(self, coins: List[int]) -> int:
3-
res = 1
4-
for coin in sorted(coins):
5-
if coin > res:
3+
ans = 1
4+
for v in sorted(coins):
5+
if v > ans:
66
break
7-
res += coin
8-
return res
7+
ans += v
8+
return ans

0 commit comments

Comments
 (0)