Skip to content

Commit 6f56400

Browse files
committed
feat: add solutions to lc problems: No.2144,2145
* No.2144.Minimum Cost of Buying Candies With Discount * No.2145.Count the Hidden Sequences
1 parent 9bb50f7 commit 6f56400

File tree

12 files changed

+339
-4
lines changed

12 files changed

+339
-4
lines changed

solution/2100-2199/2144.Minimum Cost of Buying Candies With Discount/README.md

+55-1
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,69 @@
6868
<!-- 这里可写当前语言的特殊实现逻辑 -->
6969

7070
```python
71-
71+
class Solution:
72+
def minimumCost(self, cost: List[int]) -> int:
73+
cost.sort()
74+
ans, n = 0, len(cost)
75+
for i in range(n - 1, -1, -3):
76+
ans += cost[i]
77+
if i >= 1:
78+
ans += cost[i - 1]
79+
return ans
7280
```
7381

7482
### **Java**
7583

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

7886
```java
87+
class Solution {
88+
public int minimumCost(int[] cost) {
89+
Arrays.sort(cost);
90+
int ans = 0, n = cost.length;
91+
for (int i = n - 1; i >= 0; i -= 3) {
92+
ans += cost[i];
93+
if (i >= 1) {
94+
ans += cost[i - 1];
95+
}
96+
}
97+
return ans;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int minimumCost(vector<int>& cost) {
108+
sort(cost.begin(), cost.end());
109+
int ans = 0, n = cost.size();
110+
for (int i = n - 1; i >= 0; i -= 3)
111+
{
112+
ans += cost[i];
113+
if (i >= 1) ans += cost[i - 1];
114+
}
115+
return ans;
116+
}
117+
};
118+
```
79119
120+
### **Go**
121+
122+
```go
123+
func minimumCost(cost []int) int {
124+
sort.Ints(cost)
125+
ans, n := 0, len(cost)
126+
for i := n - 1; i >= 0; i -= 3 {
127+
ans += cost[i]
128+
if i >= 1 {
129+
ans += cost[i-1]
130+
}
131+
}
132+
return ans
133+
}
80134
```
81135

82136
### **TypeScript**

solution/2100-2199/2144.Minimum Cost of Buying Candies With Discount/README_EN.md

+55-1
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,67 @@ Hence, the minimum cost to buy all candies is 5 + 5 = 10.
6363
### **Python3**
6464

6565
```python
66-
66+
class Solution:
67+
def minimumCost(self, cost: List[int]) -> int:
68+
cost.sort()
69+
ans, n = 0, len(cost)
70+
for i in range(n - 1, -1, -3):
71+
ans += cost[i]
72+
if i >= 1:
73+
ans += cost[i - 1]
74+
return ans
6775
```
6876

6977
### **Java**
7078

7179
```java
80+
class Solution {
81+
public int minimumCost(int[] cost) {
82+
Arrays.sort(cost);
83+
int ans = 0, n = cost.length;
84+
for (int i = n - 1; i >= 0; i -= 3) {
85+
ans += cost[i];
86+
if (i >= 1) {
87+
ans += cost[i - 1];
88+
}
89+
}
90+
return ans;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
int minimumCost(vector<int>& cost) {
101+
sort(cost.begin(), cost.end());
102+
int ans = 0, n = cost.size();
103+
for (int i = n - 1; i >= 0; i -= 3)
104+
{
105+
ans += cost[i];
106+
if (i >= 1) ans += cost[i - 1];
107+
}
108+
return ans;
109+
}
110+
};
111+
```
72112
113+
### **Go**
114+
115+
```go
116+
func minimumCost(cost []int) int {
117+
sort.Ints(cost)
118+
ans, n := 0, len(cost)
119+
for i := n - 1; i >= 0; i -= 3 {
120+
ans += cost[i]
121+
if i >= 1 {
122+
ans += cost[i-1]
123+
}
124+
}
125+
return ans
126+
}
73127
```
74128

75129
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int minimumCost(vector<int>& cost) {
4+
sort(cost.begin(), cost.end());
5+
int ans = 0, n = cost.size();
6+
for (int i = n - 1; i >= 0; i -= 3)
7+
{
8+
ans += cost[i];
9+
if (i >= 1) ans += cost[i - 1];
10+
}
11+
return ans;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func minimumCost(cost []int) int {
2+
sort.Ints(cost)
3+
ans, n := 0, len(cost)
4+
for i := n - 1; i >= 0; i -= 3 {
5+
ans += cost[i]
6+
if i >= 1 {
7+
ans += cost[i-1]
8+
}
9+
}
10+
return ans
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int minimumCost(int[] cost) {
3+
Arrays.sort(cost);
4+
int ans = 0, n = cost.length;
5+
for (int i = n - 1; i >= 0; i -= 3) {
6+
ans += cost[i];
7+
if (i >= 1) {
8+
ans += cost[i - 1];
9+
}
10+
}
11+
return ans;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def minimumCost(self, cost: List[int]) -> int:
3+
cost.sort()
4+
ans, n = 0, len(cost)
5+
for i in range(n - 1, -1, -3):
6+
ans += cost[i]
7+
if i >= 1:
8+
ans += cost[i - 1]
9+
return ans

solution/2100-2199/2145.Count the Hidden Sequences/README.md

+64-1
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,78 @@
7676
<!-- 这里可写当前语言的特殊实现逻辑 -->
7777

7878
```python
79-
79+
class Solution:
80+
def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
81+
num = mi = mx = 0
82+
for d in differences:
83+
num += d
84+
mi = min(mi, num)
85+
mx = max(mx, num)
86+
return max(0, upper - lower - (mx - mi) + 1)
8087
```
8188

8289
### **Java**
8390

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

8693
```java
94+
class Solution {
95+
public int numberOfArrays(int[] differences, int lower, int upper) {
96+
long num = 0, mi = 0, mx = 0;
97+
for (int d : differences) {
98+
num += d;
99+
mi = Math.min(mi, num);
100+
mx = Math.max(mx, num);
101+
}
102+
return Math.max(0, (int) (upper - lower - (mx - mi) + 1));
103+
}
104+
}
105+
```
106+
107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int numberOfArrays(vector<int>& differences, int lower, int upper) {
113+
long long num = 0, mi = 0, mx = 0;
114+
for (int& d : differences)
115+
{
116+
num += d;
117+
mi = min(mi, num);
118+
mx = max(mx, num);
119+
}
120+
return max(0, (int) (upper - lower - (mx - mi) + 1));
121+
}
122+
};
123+
```
87124
125+
### **Go**
126+
127+
```go
128+
func numberOfArrays(differences []int, lower int, upper int) int {
129+
num, mi, mx := 0, 0, 0
130+
for _, d := range differences {
131+
num += d
132+
mi = min(mi, num)
133+
mx = max(mx, num)
134+
}
135+
return max(0, upper-lower-(mx-mi)+1)
136+
}
137+
138+
func max(a, b int) int {
139+
if a > b {
140+
return a
141+
}
142+
return b
143+
}
144+
145+
func min(a, b int) int {
146+
if a < b {
147+
return a
148+
}
149+
return b
150+
}
88151
```
89152

90153
### **TypeScript**

solution/2100-2199/2145.Count the Hidden Sequences/README_EN.md

+64-1
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,76 @@ Thus, we return 4.
7070
### **Python3**
7171

7272
```python
73-
73+
class Solution:
74+
def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:
75+
num = mi = mx = 0
76+
for d in differences:
77+
num += d
78+
mi = min(mi, num)
79+
mx = max(mx, num)
80+
return max(0, upper - lower - (mx - mi) + 1)
7481
```
7582

7683
### **Java**
7784

7885
```java
86+
class Solution {
87+
public int numberOfArrays(int[] differences, int lower, int upper) {
88+
long num = 0, mi = 0, mx = 0;
89+
for (int d : differences) {
90+
num += d;
91+
mi = Math.min(mi, num);
92+
mx = Math.max(mx, num);
93+
}
94+
return Math.max(0, (int) (upper - lower - (mx - mi) + 1));
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int numberOfArrays(vector<int>& differences, int lower, int upper) {
105+
long long num = 0, mi = 0, mx = 0;
106+
for (int& d : differences)
107+
{
108+
num += d;
109+
mi = min(mi, num);
110+
mx = max(mx, num);
111+
}
112+
return max(0, (int) (upper - lower - (mx - mi) + 1));
113+
}
114+
};
115+
```
79116
117+
### **Go**
118+
119+
```go
120+
func numberOfArrays(differences []int, lower int, upper int) int {
121+
num, mi, mx := 0, 0, 0
122+
for _, d := range differences {
123+
num += d
124+
mi = min(mi, num)
125+
mx = max(mx, num)
126+
}
127+
return max(0, upper-lower-(mx-mi)+1)
128+
}
129+
130+
func max(a, b int) int {
131+
if a > b {
132+
return a
133+
}
134+
return b
135+
}
136+
137+
func min(a, b int) int {
138+
if a < b {
139+
return a
140+
}
141+
return b
142+
}
80143
```
81144

82145
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int numberOfArrays(vector<int>& differences, int lower, int upper) {
4+
long long num = 0, mi = 0, mx = 0;
5+
for (int& d : differences)
6+
{
7+
num += d;
8+
mi = min(mi, num);
9+
mx = max(mx, num);
10+
}
11+
return max(0, (int) (upper - lower - (mx - mi) + 1));
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func numberOfArrays(differences []int, lower int, upper int) int {
2+
num, mi, mx := 0, 0, 0
3+
for _, d := range differences {
4+
num += d
5+
mi = min(mi, num)
6+
mx = max(mx, num)
7+
}
8+
return max(0, upper-lower-(mx-mi)+1)
9+
}
10+
11+
func max(a, b int) int {
12+
if a > b {
13+
return a
14+
}
15+
return b
16+
}
17+
18+
func min(a, b int) int {
19+
if a < b {
20+
return a
21+
}
22+
return b
23+
}

0 commit comments

Comments
 (0)