(Easy) Diet Plan Performance LeetCode Contest

Description:

5174. Diet Plan Performance

My SubmissionsBack to Contest

  • User Accepted:0
  • User Tried:0
  • Total Accepted:0
  • Total Submissions:0
  • Difficulty:Easy

A dieter consumes calories[i] calories on the i-th day.  For every consecutive sequence of k days, they look at T, the total calories consumed during that sequence of k days:

  • If T < lower, they performed poorly on their diet and lose 1 point;
  • If T > upper, they performed well on their diet and gain 1 point;
  • Otherwise, they performed normally and there is no change in points.

Return the total number of points the dieter has after all calories.length days.

Note that: The total points could be negative.

Example 1:

Input: calories = [1,2,3,4,5], k = 1, lower = 3, upper = 3
Output: 0
Explaination: calories[0], calories[1] < lower and calories[3], calories[4] > upper, total points = 0.

Example 2:

Input: calories = [3,2], k = 2, lower = 0, upper = 1
Output: 1
Explaination: calories[0] + calories[1] > upper, total points = 1.

Example 3:

Input: calories = [6,5,0,0], k = 2, lower = 1, upper = 5
Output: 0
Explaination: calories[0] + calories[1] > upper, calories[2] + calories[3] < lower, total points = 0.

Constraints:

  • 1 <= k <= calories.length <= 10^5
  • 0 <= calories[i] <= 20000
  • 0 <= lower <= upper

Solution:

class Solution {
    public int dietPlanPerformance(int[] calories, int k, int lower, int upper) {

        if(calories==null||calories.length ==0){
            return 0;
        }
        int points=0;

        int i = 0;
        int times = calories.length /k;

        for( i = 0; i+k<=calories.length; i++){

                int [] a = new int[k];
                int count =0;
                int tmp_sum = 0;
                for(int j = i; count< k; j++ ){
                    tmp_sum = tmp_sum + calories[j];
                    count++;

                }

                if(tmp_sum<lower){
                    points = points -1;
                }

                if(tmp_sum>upper){

                    points = points+1;
                }

            }
         return points;

        }

        public int sum (int[] a ){
        int sum = 0;
        for(int i = 0; i<a.length; i++){
            sum = sum+a[i];
        }

        return sum;
    }

    }

原文地址:https://www.cnblogs.com/codingyangmao/p/11441778.html

时间: 2024-10-08 19:21:20

(Easy) Diet Plan Performance LeetCode Contest的相关文章

(Easy) Can Make Palindrome - LeetCode Contest (in progress)

Description: Given a string s, we make queries on substrings of s. For each query queries[i] = [left, right, k], we may rearrange the substring s[left], ..., s[right], and then choose up to k of them to replace with any lowercase English letter. If t

Leetcode-5174 Diet Plan Performance(健身计划评估)

1 #define _for(i,a,b) for(int i = (a);i < b;i ++) 2 #define _rep(i,a,b) for(int i = (a);i > b;i --) 3 4 class Solution 5 { 6 public: 7 int dietPlanPerformance(vector<int>& calories, int k, int lower, int upper) 8 { 9 long long sum[100003];

(Easy) To Lower Case LeetCode

Description Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here" Example 3: In

(Easy) Last Stone Weight LeetCode

class Solution { public int lastStoneWeight(int[] stones) { int len = stones.length; int i = len -1; int minus = 0; int remain = len; if(stones.length ==1){ return stones[0]; } else { Arrays.sort(stones); do{ if(stones[i]==stones[i-1]){ stones[i] = 0

(Easy) Occurences After Bigram LeetCode

Description: Given words first and second, consider occurrences in some text of the form "first second third", where second comes immediately after first, and third comes immediately after second. For each such occurrence, add "third"

(Easy) Flipping an Image LeetCode

Description: Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally re

(Easy) Find Common Characters LeetCode

Description Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates).  For example, if a character occurs 3 times in all strings but not 4 times,

(Easy) Self Dividing Numbers LeetCode

Description: A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Also, a self-dividing number is not allowed to contain the

(Easy) Long Pressed Name LeetCode

Description: Your friend is typing his name into a keyboard.  Sometimes, when typing a character c, the key might get long pressed, and the character will be typed 1 or more times. You examine the typed characters of the keyboard.  Return True if it