zoukankan      html  css  js  c++  java
  • [LeetCode] 1431. Kids With the Greatest Number of Candies

    Given the array candies and the integer extraCandies, where candies[i] represents the number of candies that the ith kid has.

    For each kid check if there is a way to distribute extraCandies among the kids such that he or she can have the greatest number of candies among them. Notice that multiple kids can have the greatest number of candies.

    Example 1:

    Input: candies = [2,3,5,1,3], extraCandies = 3
    Output: [true,true,true,false,true] 
    Explanation: 
    Kid 1 has 2 candies and if he or she receives all extra candies (3) will have 5 candies --- the greatest number of candies among the kids. 
    Kid 2 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids. 
    Kid 3 has 5 candies and this is already the greatest number of candies among the kids. 
    Kid 4 has 1 candy and even if he or she receives all extra candies will only have 4 candies. 
    Kid 5 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids. 
    

    Example 2:

    Input: candies = [4,2,1,1,2], extraCandies = 1
    Output: [true,false,false,false,false] 
    Explanation: There is only 1 extra candy, therefore only kid 1 will have the greatest number of candies among the kids regardless of who takes the extra candy.
    

    Example 3:

    Input: candies = [12,1,12], extraCandies = 10
    Output: [true,false,true] 

    Constraints:

    • 2 <= candies.length <= 100
    • 1 <= candies[i] <= 100
    • 1 <= extraCandies <= 50

    拥有最多糖果的孩子。儿童节来一道儿童题吧。题意是给一个数组表示几个孩子每个人手里目前有几个糖果,同时有一个extraCandies变量。如果这个extraCandies给了任何一个儿童,问是否能让当前这个儿童成为拥有糖果最多的孩子。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
     3         int max = 0;
     4         for (int candy : candies) {
     5             max = Math.max(max, candy);
     6         }
     7         List<Boolean> res = new ArrayList<>();
     8         for (int candy : candies) {
     9             if (candy + extraCandies >= max) {
    10                 res.add(true);
    11             } else {
    12                 res.add(false);
    13             }
    14         }
    15         return res;
    16     }
    17 }

    JavaScript实现

     1 /**
     2  * @param {number[]} candies
     3  * @param {number} extraCandies
     4  * @return {boolean[]}
     5  */
     6 var kidsWithCandies = function(candies, extraCandies) {
     7     var max = 0;
     8     var res = [];
     9     for (var i = 0; i < candies.length; i++) {
    10         max = Math.max(max, candies[i]);
    11     }
    12     console.log(max);
    13     for (var i = 0; i < candies.length; i++) {
    14         if (candies[i] + extraCandies >= max) {
    15             res.push(true);
    16         } else {
    17             res.push(false);
    18         }
    19     }
    20     return res;
    21 };

    LeetCode 题目总结

  • 相关阅读:
    MySQL监控脚本
    django学习笔记【004】创建带有model的app
    MySQL-group-replication 配置
    MySQL配置文件模板
    Python 正则表达式
    MYSQL的安全模式:sql_safe_updates介绍
    Git branch && Git checkout常见用法
    git rebase和git merge的用法
    Git最牛最全详解
    理解npm run
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13024561.html
Copyright © 2011-2022 走看看