首页 > 其他分享 >leetcode-1103-easy

leetcode-1103-easy

时间:2023-05-18 20:34:10浏览次数:42  
标签:people candies turn int 1103 easy ans array leetcode

Distribute Candies to People

We distribute some number of candies, to a row of n = num_people people in the following way:

We then give 1 candy to the first person, 2 candies to the second person, and so on until we give n candies to the last person.

Then, we go back to the start of the row, giving n + 1 candies to the first person, n + 2 candies to the second person, and so on until we give 2 * n candies to the last person.

This process repeats (with us giving one more candy each time, and moving to the start of the row after we reach the end) until we run out of candies. The last person will receive all of our remaining candies (not necessarily one more than the previous gift).

Return an array (of length num_people and sum candies) that represents the final distribution of candies.

Example 1:

Input: candies = 7, num_people = 4
Output: [1,2,3,1]
Explanation:
On the first turn, ans[0] += 1, and the array is [1,0,0,0].
On the second turn, ans[1] += 2, and the array is [1,2,0,0].
On the third turn, ans[2] += 3, and the array is [1,2,3,0].
On the fourth turn, ans[3] += 1 (because there is only one candy left), and the final array is [1,2,3,1].
Example 2:

Input: candies = 10, num_people = 3
Output: [5,2,3]
Explanation: 
On the first turn, ans[0] += 1, and the array is [1,0,0].
On the second turn, ans[1] += 2, and the array is [1,2,0].
On the third turn, ans[2] += 3, and the array is [1,2,3].
On the fourth turn, ans[0] += 4, and the final array is [5,2,3].
Constraints:

1 <= candies <= 10^9
1 <= num_people <= 1000

思路一: 模拟

    public int[] distributeCandies(int candies, int num_people) {
        int[] ans = new int[num_people];

        int n = 1;
        while (candies > 0) {
            int idx = 0;
            while (idx < ans.length) {

                if (candies > n) {
                    candies -= n;
                } else {
                    n = candies;
                    candies = 0;
                }
                ans[idx++] += n++;
            }
        }

        return ans;
    }

标签:people,candies,turn,int,1103,easy,ans,array,leetcode
From: https://www.cnblogs.com/iyiluo/p/17413214.html

相关文章

  • #yyds干货盘点# LeetCode程序员面试金典:相交链表
    1.简述:给你两个单链表的头节点 headA和headB,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回null。图示两个链表在节点c1开始相交:题目数据保证整个链式结构中不存在环。注意,函数返回结果后,链表必须保持其原始结构。自定义评测:评测系统的输入......
  • #yyds干货盘点# LeetCode程序员面试金典:从中序与后序遍历序列构造二叉树
    题目:给定两个整数数组inorder和postorder,其中inorder是二叉树的中序遍历,postorder是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。 示例1:输入:inorder=[9,3,15,20,7],postorder=[9,15,7,20,3]输出:[3,9,20,null,null,15,7]示例2:输入:inorder=[-1],postorder......
  • 动态规划算法基础及leetcode例题
    01基础理论题型:动规基础(斐波那契数列or爬楼梯);背包问题;打家劫舍;股票问题;子序列问题动规误区:只要看懂递推就ok(递推公式只是一部分)解决动态规划应该要思考的几步:状态转移的DP数组以及下标的含义递推公式DP数组为何初始化遍历顺序打印DP数组02例题基础题目509.斐波那......
  • leetcode:二叉树的最大深度
    题目描述给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树[3,9,20,null,null,15,7],3/\920/\157返回它的最大深度 3。题目链接:104.二叉树......
  • Leetcode-Easy 806. Number of Lines To Write String
    题目描述给一个字符串S,从左到右将它们排列行,每行最大长度为100,,同时给定一个数组withds,widths[0]对应着a的宽度,widths[1]对应着b的宽度,...,widths[25]对应着z的宽度。求:至少需要多少行以及最后一行的长度下面是一个实例:Example:Input:widths=[10,10,10,10,10,10,1......
  • leetcode 1321 餐館營業額變化增長
    leetcode1321餐館營業額變化增長selectdistinctc2.visited_on,(selectsum(amount)fromCustomerc1wherec1.visited_on<=c2.visited_onandc1.visited_on>=date_sub(c2.visited_on,interval6day))asamount,round((selectsum(amount)fromCustomerc1......
  • 二刷Leetcode-Days04
    数组:/***27.移除元素*@paramnums*@paramval*@return很多考察数组、链表、字符串等操作的面试题,都使用双指针法。*/publicintremoveElement(int[]nums,intval){intleft=0;for(inti=0;i<nums.l......
  • SpringBoot使用EasyExcel将Excel数据直接转换为类对象
    背景相比于读取excel到List<List<String>>对象中,抽象一个方法将excel数据直接一步读取到指定的类对象中,更为方便。代码通过类Class读取excel数据到对象/***使用Class来读取Excel**@paraminputStreamExcel的输入流*@paramexcelTypeEnumExcel的格式(XLS或XLSX......
  • 视频汇聚融合平台EasyCVR视频快照增加设备和通道信息的实现过程分享
    EasyCVR视频融合平台基于云边端协同架构,具有强大的数据接入、处理及分发能力。平台可支持多协议、多类型的前端设备接入,包括:IPC、NVR、视频编码器、视频平台、车载终端、车机设备、警用移动执法仪、手持智能移动终端、无人机等;支持采用设备树对设备进行分组、分级管理,支持设备状态......
  • AI智慧安监EasyCVR视频融合平台免登录定制的实现过程
    EasyCVR视频融合平台基于云边端一体化架构,部署轻快、功能灵活,平台可支持多协议、多类型设备接入,包括:国标GB28181、RTMP、RTSP/Onvif、海康Ehome、海康SDK、大华SDK、宇视SDK等。在视频能力上,可实现视频直播、录像、回放、检索、云存储、告警上报、语音对讲、集群、智能分析以及平......