Skip to content

Commit 7cb4873

Browse files
committed
1744. Can You Eat Your Favorite Candy on Your Favorite Day?: AC
1 parent 52e830e commit 7cb4873

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,3 +1314,4 @@ mod s1738_find_kth_largest_xor_coordinate_value;
13141314
mod s1739_building_boxes;
13151315
mod s1742_maximum_number_of_balls_in_a_box;
13161316
mod s1743_restore_the_array_from_adjacent_pairs;
1317+
mod s1744_can_you_eat_your_favorite_candy_on_your_favorite_day;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* [1744] Can You Eat Your Favorite Candy on Your Favorite Day?
3+
*
4+
* You are given a (0-indexed) array of positive integers candiesCount where candiesCount[i] represents the number of candies of the i^th type you have. You are also given a 2D array queries where queries[i] = [favoriteTypei, favoriteDayi, dailyCapi].
5+
* You play a game with the following rules:
6+
*
7+
* You start eating candies on day 0.
8+
* You cannot eat any candy of type i unless you have eaten all candies of type i - 1.
9+
* You must eat at least one candy per day until you have eaten all the candies.
10+
*
11+
* Construct a boolean array answer such that answer.length == queries.length and answer[i] is true if you can eat a candy of type favoriteTypei on day favoriteDayi without eating more than dailyCapi candies on any day, and false otherwise. Note that you can eat different types of candy on the same day, provided that you follow rule 2.
12+
* Return the constructed array answer.
13+
*
14+
* Example 1:
15+
*
16+
* Input: candiesCount = [7,4,5,3,8], queries = [[0,2,2],[4,2,4],[2,13,1000000000]]
17+
* Output: [true,false,true]
18+
* Explanation:
19+
* 1- If you eat 2 candies (type 0) on day 0 and 2 candies (type 0) on day 1, you will eat a candy of type 0 on day 2.
20+
* 2- You can eat at most 4 candies each day.
21+
* If you eat 4 candies every day, you will eat 4 candies (type 0) on day 0 and 4 candies (type 0 and type 1) on day 1.
22+
* On day 2, you can only eat 4 candies (type 1 and type 2), so you cannot eat a candy of type 4 on day 2.
23+
* 3- If you eat 1 candy each day, you will eat a candy of type 2 on day 13.
24+
*
25+
* Example 2:
26+
*
27+
* Input: candiesCount = [5,2,6,4,1], queries = [[3,1,2],[4,10,3],[3,10,100],[4,100,30],[1,3,1]]
28+
* Output: [false,true,true,false,false]
29+
*
30+
*
31+
* Constraints:
32+
*
33+
* 1 <= candiesCount.length <= 10^5
34+
* 1 <= candiesCount[i] <= 10^5
35+
* 1 <= queries.length <= 10^5
36+
* queries[i].length == 3
37+
* 0 <= favoriteTypei < candiesCount.length
38+
* 0 <= favoriteDayi <= 10^9
39+
* 1 <= dailyCapi <= 10^9
40+
*
41+
*/
42+
pub struct Solution {}
43+
44+
// problem: https://leetcode.com/problems/can-you-eat-your-favorite-candy-on-your-favorite-day/
45+
// discuss: https://leetcode.com/problems/can-you-eat-your-favorite-candy-on-your-favorite-day/discuss/?currentPage=1&orderBy=most_votes&query=
46+
47+
// submission codes start here
48+
49+
impl Solution {
50+
pub fn can_eat(candies_count: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<bool> {
51+
let (m, n) = (candies_count.len(), queries.len());
52+
53+
let mut sum = vec![0i64; m + 1];
54+
for i in 0..m {
55+
sum[i + 1] = sum[i] + candies_count[i] as i64;
56+
}
57+
58+
let mut result = vec![false; n];
59+
60+
for i in 0..n {
61+
let (u, v, w) = (queries[i][0], queries[i][1], queries[i][2]);
62+
63+
if sum[u as usize + 1] <= v as i64 {
64+
continue;
65+
}
66+
67+
if sum[u as usize] - w as i64 >= v as i64 * w as i64 {
68+
continue;
69+
}
70+
71+
result[i] = true;
72+
}
73+
74+
result
75+
}
76+
}
77+
78+
// submission codes end
79+
80+
#[cfg(test)]
81+
mod tests {
82+
use super::*;
83+
84+
#[test]
85+
fn test_1744_example_1() {
86+
let candies_count = vec![7, 4, 5, 3, 8];
87+
let queries = vec![vec![0, 2, 2], vec![4, 2, 4], vec![2, 13, 1000000000]];
88+
89+
let result = vec![true, false, true];
90+
91+
assert_eq!(Solution::can_eat(candies_count, queries), result);
92+
}
93+
94+
#[test]
95+
fn test_1744_example_2() {
96+
let candies_count = vec![5, 2, 6, 4, 1];
97+
let queries = vec![
98+
vec![3, 1, 2],
99+
vec![4, 10, 3],
100+
vec![3, 10, 100],
101+
vec![4, 100, 30],
102+
vec![1, 3, 1],
103+
];
104+
105+
let result = vec![false, true, true, false, false];
106+
107+
assert_eq!(Solution::can_eat(candies_count, queries), result);
108+
}
109+
}

0 commit comments

Comments
 (0)