一群孩子做游戏,现在请你根据游戏得分来发糖果,要求如下: 1. 每个孩子不管得分多少,起码分到一个糖果。 2. 任意两个相邻的孩子之间,得分较多的孩子必须拿多一些糖果。(若相同则无此限制) 给定一个数组 代表得分数组,请返回最少需要多少糖果。 要求: 时间复杂度为 空间复杂度为 数据范围: ,
示例1
输入
[1,1,2]
输出
4
说明
最优分配方案为1,1,2
示例2
输入
[1,1,1]
输出
3
说明
最优分配方案是1,1,1
加载中...
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * pick candy * @param arr int整型一维数组 the array * @return int整型 */ public int candy (int[] arr) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * pick candy * @param arr int整型vector the array * @return int整型 */ int candy(vector
& arr) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # pick candy # @param arr int整型一维数组 the array # @return int整型 # class Solution: def candy(self , arr ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * pick candy * @param arr int整型一维数组 the array * @return int整型 */ public int candy (List
arr) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * pick candy * @param arr int整型一维数组 the array * @return int整型 */ function candy( arr ) { // write code here } module.exports = { candy : candy };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # pick candy # @param arr int整型一维数组 the array # @return int整型 # class Solution: def candy(self , arr: List[int]) -> int: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * pick candy * @param arr int整型一维数组 the array * @return int整型 */ func candy( arr []int ) int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * pick candy * @param arr int整型一维数组 the array * @param arrLen int arr数组长度 * @return int整型 */ int candy(int* arr, int arrLen ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # pick candy # @param arr int整型一维数组 the array # @return int整型 # class Solution def candy(arr) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * pick candy * @param arr int整型一维数组 the array * @return int整型 */ def candy(arr: Array[Int]): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * pick candy * @param arr int整型一维数组 the array * @return int整型 */ fun candy(arr: IntArray): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * pick candy * @param arr int整型一维数组 the array * @return int整型 */ public int candy (int[] arr) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * pick candy * @param arr int整型一维数组 the array * @return int整型 */ export function candy(arr: number[]): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * pick candy * @param arr int整型一维数组 the array * @return int整型 */ func candy ( _ arr: [Int]) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * pick candy * @param arr int整型一维数组 the array * @return int整型 */ pub fn candy(&self, arr: Vec
) -> i32 { // write code here } }
[1,1,2]
4
[1,1,1]
3