Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 722b388

Browse files
feat: add solutions to lcp problems: No.06,07 (doocs#1649)
* No.06.拿硬币 * No.07.传递信息
1 parent 0a9265b commit 722b388

File tree

15 files changed

+306
-99
lines changed

15 files changed

+306
-99
lines changed

‎lcp/LCP 06. 拿硬币/README.md‎

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@
3535

3636
<!-- 这里可写通用的实现逻辑 -->
3737

38+
**方法一:数学**
39+
40+
我们可以发现,每堆力扣币拿完的最少次数,等于该堆力扣币数量除以 2ドル$ 向上取整的结果之和。
41+
42+
因此,我们只需要遍历每堆力扣币 $x_i,ドル计算每堆力扣币拿完的最少次数 $\left \lceil x_i/2 \right \rceil,ドル然后累加即可。
43+
44+
时间复杂度 $O(n),ドル其中 $n$ 是数组 $coins$ 的长度。空间复杂度 $O(1)$。
45+
3846
<!-- tabs:start -->
3947

4048
### **Python3**
@@ -44,7 +52,7 @@
4452
```python
4553
class Solution:
4654
def minCount(self, coins: List[int]) -> int:
47-
return sum((coin + 1) //2 for coin in coins)
55+
return sum((x + 1) >>1 for x in coins)
4856
```
4957

5058
### **Java**
@@ -55,8 +63,8 @@ class Solution:
5563
class Solution {
5664
public int minCount(int[] coins) {
5765
int ans = 0;
58-
for (int coin : coins) {
59-
ans += (coin + 1) /2;
66+
for (int x : coins) {
67+
ans += (x + 1) >>1;
6068
}
6169
return ans;
6270
}
@@ -70,7 +78,9 @@ class Solution {
7078
public:
7179
int minCount(vector<int>& coins) {
7280
int ans = 0;
73-
for (int coin : coins) ans += (coin + 1) / 2;
81+
for (int x : coins) {
82+
ans += (x + 1) >> 1;
83+
}
7484
return ans;
7585
}
7686
};
@@ -79,38 +89,43 @@ public:
7989
### **Go**
8090
8191
```go
82-
func minCount(coins []int) int {
83-
ans := 0
84-
for _, coin := range coins {
85-
ans += (coin + 1) / 2
92+
func minCount(coins []int) (ans int) {
93+
for _, x := range coins {
94+
ans += (x + 1) >> 1
8695
}
87-
return ans
96+
return
8897
}
8998
```
9099

91-
### **C**
100+
### **TypeScript**
92101

93-
```c
94-
int minCount(int* coins, int coinsSize) {
95-
int res = 0;
96-
for (int i = 0; i < coinsSize; i++) {
97-
int coin = coins[i];
98-
if (coin % 2 == 1) {
99-
res++;
100-
}
101-
res += coin / 2;
102+
```ts
103+
function minCount(coins: number[]): number {
104+
let ans = 0;
105+
for (const x of coins) {
106+
ans += (x + 1) >> 1;
102107
}
103-
return res;
108+
return ans;
104109
}
105110
```
106111

107-
### **TypeScript**
112+
### **Rust**
108113

109-
```ts
110-
function minCount(coins: number[]): number {
111-
let ans = 0;
112-
for (const coin of coins) {
113-
ans += Math.floor((coin + 1) / 2);
114+
```rust
115+
impl Solution {
116+
pub fn min_count(coins: Vec<i32>) -> i32 {
117+
coins.iter().map(|&x| (x + 1) >> 1).sum::<i32>()
118+
}
119+
}
120+
```
121+
122+
### **C**
123+
124+
```c
125+
int minCount(int* coins, int coinsSize) {
126+
int ans = 0;
127+
for (int i = 0; i < coinsSize; ++i) {
128+
ans += (coins[i] + 1) >> 1;
114129
}
115130
return ans;
116131
}
@@ -125,11 +140,11 @@ class Solution {
125140
* @return Integer
126141
*/
127142
function minCount($coins) {
128-
$cnt = 0;
129-
for ($i = 0; $i < count($coins); $i++) {
130-
$cnt += floor($coins[$i] / 2) + ($coins[$i] % 2);
143+
$ans = 0;
144+
foreach ($coins as $x) {
145+
$ans += $x + 1 >> 1;
131146
}
132-
return $cnt;
147+
return $ans;
133148
}
134149
}
135150
```

‎lcp/LCP 06. 拿硬币/Solution.c‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
int minCount(int* coins, int coinsSize) {
2-
int res = 0;
3-
for (int i = 0; i < coinsSize; i++) {
4-
int coin = coins[i];
5-
if (coin % 2 == 1) {
6-
res++;
7-
}
8-
res += coin / 2;
9-
}
10-
return res;
11-
}
1+
int minCount(int* coins, int coinsSize) {
2+
int ans = 0;
3+
for (int i = 0; i < coinsSize; ++i) {
4+
ans += (coins[i] + 1) >> 1;
5+
}
6+
return ans;
7+
}

‎lcp/LCP 06. 拿硬币/Solution.cpp‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
class Solution {
2-
public:
3-
int minCount(vector<int>& coins) {
4-
int ans = 0;
5-
for (int coin : coins) ans += (coin + 1) / 2;
6-
return ans;
7-
}
1+
class Solution {
2+
public:
3+
int minCount(vector<int>& coins) {
4+
int ans = 0;
5+
for (int x : coins) {
6+
ans += (x + 1) >> 1;
7+
}
8+
return ans;
9+
}
810
};

‎lcp/LCP 06. 拿硬币/Solution.go‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
func minCount(coins []int) int {
2-
ans := 0
3-
for _, coin := range coins {
4-
ans += (coin + 1) / 2
1+
func minCount(coins []int) (ans int) {
2+
for _, x := range coins {
3+
ans += (x + 1) >> 1
54
}
6-
returnans
5+
return
76
}

‎lcp/LCP 06. 拿硬币/Solution.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
class Solution {
2-
public int minCount(int[] coins) {
3-
int ans = 0;
4-
for (int coin : coins) {
5-
ans += (coin + 1) / 2;
6-
}
7-
return ans;
8-
}
1+
class Solution {
2+
public int minCount(int[] coins) {
3+
int ans = 0;
4+
for (int x : coins) {
5+
ans += (x + 1) >> 1;
6+
}
7+
return ans;
8+
}
99
}

‎lcp/LCP 06. 拿硬币/Solution.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ class Solution {
44
* @return Integer
55
*/
66
function minCount($coins) {
7-
$cnt = 0;
8-
for ($i=0; $i<count($coins); $i++) {
9-
$cnt += floor($coins[$i] /2) + ($coins[$i] %2);
7+
$ans = 0;
8+
foreach ($coinsas$x) {
9+
$ans += $x+1>>1;
1010
}
11-
return $cnt;
11+
return $ans;
1212
}
1313
}

‎lcp/LCP 06. 拿硬币/Solution.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
class Solution:
2-
def minCount(self, coins: List[int]) -> int:
3-
return sum((coin + 1) //2 for coin in coins)
1+
class Solution:
2+
def minCount(self, coins: List[int]) -> int:
3+
return sum((x + 1) >>1 for x in coins)

‎lcp/LCP 06. 拿硬币/Solution.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn min_count(coins: Vec<i32>) -> i32 {
3+
coins.iter().map(|&x| (x + 1) >> 1).sum::<i32>()
4+
}
5+
}

‎lcp/LCP 06. 拿硬币/Solution.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function minCount(coins: number[]): number {
22
let ans = 0;
3-
for (const coin of coins) {
4-
ans += Math.floor((coin + 1) /2);
3+
for (const x of coins) {
4+
ans += (x + 1) >>1;
55
}
66
return ans;
77
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /