mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-26 10:43:31 +08:00
update 0135.分发糖果:修改错字 和 更正 go 代码markdown语法错误
This commit is contained in:
@ -65,7 +65,7 @@ for (int i = 1; i < ratings.size(); i++) {
|
|||||||
|
|
||||||
如果 ratings[i] > ratings[i + 1],此时candyVec[i](第i个小孩的糖果数量)就有两个选择了,一个是candyVec[i + 1] + 1(从右边这个加1得到的糖果数量),一个是candyVec[i](之前比较右孩子大于左孩子得到的糖果数量)。
|
如果 ratings[i] > ratings[i + 1],此时candyVec[i](第i个小孩的糖果数量)就有两个选择了,一个是candyVec[i + 1] + 1(从右边这个加1得到的糖果数量),一个是candyVec[i](之前比较右孩子大于左孩子得到的糖果数量)。
|
||||||
|
|
||||||
那么又要贪心了,局部最优:取candyVec[i + 1] + 1 和 candyVec[i] 最大的糖果数量,保证第i个小孩的糖果数量即大于左边的也大于右边的。全局最优:相邻的孩子中,评分高的孩子获得更多的糖果。
|
那么又要贪心了,局部最优:取candyVec[i + 1] + 1 和 candyVec[i] 最大的糖果数量,保证第i个小孩的糖果数量既大于左边的也大于右边的。全局最优:相邻的孩子中,评分高的孩子获得更多的糖果。
|
||||||
|
|
||||||
局部最优可以推出全局最优。
|
局部最优可以推出全局最优。
|
||||||
|
|
||||||
@ -172,63 +172,44 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
```golang
|
```go
|
||||||
func candy(ratings []int) int {
|
func candy(ratings []int) int {
|
||||||
/**先确定一边,再确定另外一边
|
/**先确定一边,再确定另外一边
|
||||||
1.先从左到右,当右边的大于左边的就加1
|
1.先从左到右,当右边的大于左边的就加1
|
||||||
2.再从右到左,当左边的大于右边的就再加1
|
2.再从右到左,当左边的大于右边的就再加1
|
||||||
**/
|
**/
|
||||||
need:=make([]int,len(ratings))
|
need := make([]int, len(ratings))
|
||||||
sum:=0
|
sum := 0
|
||||||
//初始化(每个人至少一个糖果)
|
// 初始化(每个人至少一个糖果)
|
||||||
for i:=0;i<len(ratings);i++{
|
for i := 0; i < len(ratings); i++ {
|
||||||
need[i]=1
|
need[i] = 1
|
||||||
}
|
}
|
||||||
//1.先从左到右,当右边的大于左边的就加1
|
// 1.先从左到右,当右边的大于左边的就加1
|
||||||
for i:=0;i<len(ratings)-1;i++{
|
for i := 0; i < len(ratings) - 1; i++ {
|
||||||
if ratings[i]<ratings[i+1]{
|
if ratings[i] < ratings[i+1] {
|
||||||
need[i+1]=need[i]+1
|
need[i+1] = need[i] + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//2.再从右到左,当左边的大于右边的就右边加1,但要花费糖果最少,所以需要做下判断
|
// 2.再从右到左,当左边的大于右边的就右边加1,但要花费糖果最少,所以需要做下判断
|
||||||
for i:=len(ratings)-1;i>0;i--{
|
for i := len(ratings)-1; i > 0; i-- {
|
||||||
if ratings[i-1]>ratings[i]{
|
if ratings[i-1] > ratings[i] {
|
||||||
need[i-1]=findMax(need[i-1],need[i]+1)
|
need[i-1] = findMax(need[i-1], need[i]+1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//计算总共糖果
|
//计算总共糖果
|
||||||
for i:=0;i<len(ratings);i++{
|
for i := 0; i < len(ratings); i++ {
|
||||||
sum+=need[i]
|
sum += need[i]
|
||||||
}
|
}
|
||||||
return sum
|
return sum
|
||||||
}
|
}
|
||||||
func findMax(num1 int ,num2 int) int{
|
func findMax(num1 int, num2 int) int {
|
||||||
if num1>num2{
|
if num1 > num2 {
|
||||||
return num1
|
return num1
|
||||||
}
|
}
|
||||||
return num2
|
return num2
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Rust
|
|
||||||
```rust
|
|
||||||
pub fn candy(ratings: Vec<i32>) -> i32 {
|
|
||||||
let mut candies = vec![1i32; ratings.len()];
|
|
||||||
for i in 1..ratings.len() {
|
|
||||||
if ratings[i - 1] < ratings[i] {
|
|
||||||
candies[i] = candies[i - 1] + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i in (0..ratings.len()-1).rev() {
|
|
||||||
if ratings[i] > ratings[i + 1] {
|
|
||||||
candies[i] = candies[i].max(candies[i + 1] + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
candies.iter().sum()
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Javascript:
|
### Javascript:
|
||||||
```Javascript
|
```Javascript
|
||||||
var candy = function(ratings) {
|
var candy = function(ratings) {
|
||||||
@ -255,6 +236,25 @@ var candy = function(ratings) {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Rust
|
||||||
|
```rust
|
||||||
|
pub fn candy(ratings: Vec<i32>) -> i32 {
|
||||||
|
let mut candies = vec![1i32; ratings.len()];
|
||||||
|
for i in 1..ratings.len() {
|
||||||
|
if ratings[i - 1] < ratings[i] {
|
||||||
|
candies[i] = candies[i - 1] + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i in (0..ratings.len()-1).rev() {
|
||||||
|
if ratings[i] > ratings[i + 1] {
|
||||||
|
candies[i] = candies[i].max(candies[i + 1] + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
candies.iter().sum()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### C
|
### C
|
||||||
```c
|
```c
|
||||||
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
||||||
|
Reference in New Issue
Block a user