This is the first day of my participation in the wenwen Challenge

Give you an array of positive integers with subscripts starting from 0, candiesCount[I], where candiesCount[I] represents the number of class I candies you own. Queries [I] = [favoriteTypei, favoriteDayi, dailyCapi]

You play a game according to these rules:

You start eating candy on day 0. You should not eat any Category I candy until you have eaten all category I-1 candy. You must eat at least one candy a day before eating all the candy. Create a Boolean array answer that answers. Length == queries.length. Answer [I] iS true if you eat no more than dailyCapi candy every day. You can eat favoriteTypei candy on favoriteDayi day. Otherwise answer[I] is false. Note that you can eat different types of candy on the same day as long as you meet the second of the three rules above.

Please return the array answer you got.

 

Example 1:

Input: candiesCount = [7,4,5,3,8], queries = [[0,2,2],[4,2,4],[2,13 1000000000]] output: [true,false,true]

Tip:

  1. Eat 2 sweets on day 0 (type 0), 2 sweets on day 1 (type 0), and on day 2 you can eat sweets of type 0.
  2. You can’t eat more than four sweets a day. Even if you eat 4 sweets on day 0 (type 0) and 4 sweets on day 1 (type 0 and type 1), you will not be able to eat sweets of type 4 on day 2. In other words, you can’t eat category 4 candy the next day under the limit of 4 candy a day.
  3. If you eat 1 candy a day, you can eat type 2 candy on day 13.
  • Example 2:

    Input: candiesCount =,2,6,4,1 [5], the queries = [,1,2 [3], [4,10,3], [3,10,100], 4100, 30], [[1,3,1]] output: [false,true,true,false,false]Copy the code

Their thinking

  1. Figure out how many days it takes to eat favoriteType favoriteType.
  2. Calculate how many days it takes to eat favoriteType at the slowest speed (eat one favoriteType a day)
  3. The number of days in between is the number of days we can reach

To speed things up, we can maintain a prefix and record how many other candies we need to eat before favoriteType. The minimum and maximum number of days are determined by the favoriteType number of sweets

  • One candy a day favoriteType+1
  • DailyCap +1 Sum [favoriteType]/dailyCap+1

code

func canEat(candiesCount []int, queries [][]int) []bool {

	bools := make([]bool.len(queries))
	sum := make([]int.len(candiesCount)+1)
	for i := 1; i <= len(candiesCount); i++ {
		sum[i]+=candiesCount[i- 1]+sum[i- 1]}for j, query := range queries {

		// If the number of days is appropriate, the number of days can be achieved
		// Because the statistics start from 0, the required number of days should be favoriteDay+1
		favoriteType,favoriteDay,dailyCap:=query[0],query[1] +1,query[2]
		
		
		if favoriteDay>=sum[favoriteType] / dailyCap+1&&favoriteDay<=sum[favoriteType+1]{
			bools[j]=true}}return bools
}
Copy the code