当前位置:网站首页>[C language brush leetcode] 1744 Can you eat your favorite candy on your favorite day (m)
[C language brush leetcode] 1744 Can you eat your favorite candy on your favorite day (m)
2022-07-18 21:20:00 【kinbo88】
【
I'll give you a subscript from 0 Starting array of positive integers candiesCount , among candiesCount[i] It means that you have no i The number of candies . And give you a two-dimensional array queries , among queries[i] = [favoriteTypei, favoriteDayi, dailyCapi] .
You play a game according to the following rules :
You start with 0 One day I started eating candy .
Are you eating all The first i - 1 Before candy , You can't Eat any one of them i Candy like .
Before eating all the candy , You have to... Every day At least eat One candy .
Please build a Boolean array answer , To give queries The corresponding answer to each item in . This array satisfies :
answer.length == queries.length .answer[i] yes queries[i] The answer .
answer[i] by true Is the condition of : Eat every day No more than dailyCapi On the premise of a candy , You can do it at favoriteDayi In the first day favoriteTypei Candy like ; otherwise answer[i] by false .
Be careful , As long as you satisfy the above 3 The second rule of the rules , You can eat different types of candy on the same day .
Please return the resulting array answer .
Example 1:
Input :candiesCount = [7,4,5,3,8], queries = [[0,2,2],[4,2,4],[2,13,1000000000]]
Output :[true,false,true]
Tips :
1- In the 0 God eat 2 Candy ( type 0), The first 1 God eat 2 Candy ( type 0), The first 2 God, you can eat the type 0 Of candy .
2- The most you eat every day is 4 Candy . Even if the 0 God eat 4 Candy ( type 0), The first 1 God eat 4 Candy ( type 0 And type 1), You can't be in the second 2 God eat type 4 Of candy . In other words , You can't eat every day 4 Under the limit of one candy, in the second 2 In the first day 4 Candy like .
3- If you eat... Every day 1 Candy , You can do it at 13 God eat type 2 Of candy .
Example 2:
Input :candiesCount = [5,2,6,4,1], queries = [[3,1,2],[4,10,3],[3,10,100],[4,100,30],[1,3,1]]
Output :[false,true,true,false,false]
Tips :
1 <= candiesCount.length <= 105
1 <= candiesCount[i] <= 105
1 <= queries.length <= 105
queries[i].length == 3
0 <= favoriteTypei < candiesCount.length
0 <= favoriteDayi <= 109
1 <= dailyCapi <= 109
source : Power button (LeetCode)
link :https://leetcode.cn/problems/can-you-eat-your-favorite-candy-on-your-favorite-day
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
】
One is to eat as much as possible , Edible range , One is to eat at a minimum , Edible range , Compare and judge the conditions between the two ranges and the meaning of the topic . The focus is on boundary treatment .
bool* canEat(int* candiesCount, int candiesCountSize, int** queries, int queriesSize, int* queriesColSize, int* returnSize){
int i;
bool *retarr = NULL;
int idx = 0;
long long presum[candiesCountSize + 1];
retarr = (bool *)malloc(sizeof(bool) * queriesSize);
*returnSize = queriesSize;
memset(presum, 0, sizeof(presum));
presum[0] = 0;
for (i = 0; i < candiesCountSize; i++) {
presum[i + 1] = presum[i] + candiesCount[i];
}
for (i = 0; i < queriesSize; i++) {
long long min = (queries[i][1]) * 1; // Take one in front every day ,
long long max = (queries[i][1] + 1) * ((long long)queries[i][2]); // Eat the most every day , Including today
long long left = presum[queries[i][0]]; // The maximum amount of the last candy type
long long right = presum[queries[i][0] + 1]; // The maximum amount of candy you want
// If you eat as much as you can every day, you will only finish the last kind of candy , Or eat at least the current type of candy in the previous days
if ((max <= left) || (min >= right)) {
retarr[idx] = false;
} else {
retarr[idx] = true;
}
idx++;
}
return retarr;
}边栏推荐
- scrapy 快速下载
- R language uses LM function to build regression model and BoxCox function of mass package to find the best power transformation to improve the fitting degree of the model (determine the best λ Paramet
- 关于#sql#的问题:orcale sql, 为什么MERCHANT table的外键inventory—id 语句是无效的
- Sword finger offer 53 - ii Missing numbers from 0 to n-1
- R语言使用data.table包对dataframe行数据进行排序(基于多字段、变量进行数据行排序,不重新排序实际的数据变化)、并计算排序后分组的累积加和值
- R语言ggplot2可视化:使用ggpubr包的gghistogram函数可视化直方图、使用add参数在直方图中添加均值虚线竖线、横轴添加轴须图(rug plot)
- [JS encapsulates a simple asynchronous API to obtain asynchronous operation results and process parsing]
- 高数下|二重积分的计算1|高数叔|手写笔记
- 初始 Redis(认识Redis以及常见命令)
- The first ide overlord in the universe, replaced...
猜你喜欢
![[JS encapsulates a simple asynchronous API to obtain asynchronous operation results and process parsing]](/img/98/fa6006639acfff1b49f47b9a64688e.png)
[JS encapsulates a simple asynchronous API to obtain asynchronous operation results and process parsing]

高数下|二重积分的计算1|高数叔|手写笔记

The first ide overlord in the universe, replaced...

Sword finger offer 57 And are two numbers of S

Part 13: implementation of STM32 I2C serial bus communication

Mathematical modeling - Classification Model (based on logistic regression)

Sword finger offer 57 - ii Continuous positive sequence with sum s

LeetCode+ 86 - 90 双指针、回溯、区间 dp 专题

Sword finger offer 55 - I. depth of binary tree

Tableau JDBC连接GraphDB
随机推荐
(手工)【sqli-labs54-57】限制注入次数:联合注入、报错回显、GET注入
OpenCV 教程 02: OpenCV 的核心操作
Offre de doigts 57. Et deux chiffres pour s
Based on servlet project -- blog system
产业园区增强企业服务能力的三大路径
Dynamically adding routes and refreshing the page will show a blank screen
Amaslab-epic-kbs9 industrial computer brushing document
Jenkins installation
R语言ggplot2可视化:使用ggpubr包的ggballoonplot函数可视化分面气球图(可视化由两个分类变量组成的列联表)、facet.by参数指定分面变量
sentinel
VMware 恢复快照出现 无法创建 5040 MB 的匿名分页文件: 系统资源不足,无法完成请求的服务
Win10 how to convert FAT32 format disks into NFTs format without formatting
About SQL: orcale SQL, why is the foreign key inventory ID statement of the merchant table invalid
Sword finger offer 53 - ii Missing numbers from 0 to n-1
JS speed up video playback
Initial redis (know redis and common commands)
Learning notes of Transe model
On array method reconstruction and re encapsulation -foreach map -- push (), unshift (), shift (), map (), filter (), every (), some (), reduce ()
scrapy 快速下载
putchar()