Skip to content

Commit 29af283

Browse files
add 1176
1 parent b2724bc commit 29af283

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ _If you like this project, please leave me a star._ ★
5151
|1185|[Day of the Week](https://leetcode.com/problems/day-of-the-week/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1185.java) | |Easy||
5252
|1184|[Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1184.java) | [:tv:](https://www.youtube.com/watch?v=RFq7yA5iyhI)|Easy||
5353
|1180|[Count Substrings with Only One Distinct Letter](https://leetcode.com/problems/count-substrings-with-only-one-distinct-letter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1180.java) | |Easy|Math, String|
54+
|1176|[Diet Plan Performance](https://leetcode.com/problems/diet-plan-performance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1176.java) | |Easy|Array, Sliding Window|
5455
|1175|[Prime Arrangements](https://leetcode.com/problems/prime-arrangements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1175.java) | |Easy||
5556
|1165|[Single-Row Keyboard](https://leetcode.com/problems/single-row-keyboard/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1165.java) | |Easy||
5657
|1160|[Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1160.java)| |Easy||
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1176. Diet Plan Performance
5+
*
6+
* A dieter consumes calories[i] calories on the i-th day.
7+
* Given an integer k, for every consecutive sequence of k days (calories[i], calories[i+1], ..., calories[i+k-1]
8+
* for all 0 <= i <= n-k), they look at T, the total calories consumed during that sequence of k days (calories[i] + calories[i+1] + ... + calories[i+k-1]):
9+
* If T < lower, they performed poorly on their diet and lose 1 point;
10+
* If T > upper, they performed well on their diet and gain 1 point;
11+
* Otherwise, they performed normally and there is no change in points.
12+
* Initially, the dieter has zero points. Return the total number of points the dieter has after dieting for calories.length days.
13+
*
14+
* Note that the total points can be negative.
15+
*
16+
* Example 1:
17+
* Input: calories = [1,2,3,4,5], k = 1, lower = 3, upper = 3
18+
* Output: 0
19+
* Explanation: Since k = 1, we consider each element of the array separately and compare it to lower and upper.
20+
* calories[0] and calories[1] are less than lower so 2 points are lost.
21+
* calories[3] and calories[4] are greater than upper so 2 points are gained.
22+
*
23+
* Example 2:
24+
* Input: calories = [3,2], k = 2, lower = 0, upper = 1
25+
* Output: 1
26+
* Explanation: Since k = 2, we consider subarrays of length 2.
27+
* calories[0] + calories[1] > upper so 1 point is gained.
28+
*
29+
* Example 3:
30+
* Input: calories = [6,5,0,0], k = 2, lower = 1, upper = 5
31+
* Output: 0
32+
* Explanation:
33+
* calories[0] + calories[1] > upper so 1 point is gained.
34+
* lower <= calories[1] + calories[2] <= upper so no change in points.
35+
* calories[2] + calories[3] < lower so 1 point is lost.
36+
*
37+
* Constraints:
38+
* 1 <= k <= calories.length <= 10^5
39+
* 0 <= calories[i] <= 20000
40+
* 0 <= lower <= upper
41+
* */
42+
public class _1176 {
43+
public static class Solution1 {
44+
public int dietPlanPerformance(int[] calories, int k, int lower, int upper) {
45+
int sum = 0;
46+
int points = 0;
47+
for (int i = 0, j = 0; i <= calories.length - k && j < calories.length; j++) {
48+
sum += calories[j];
49+
if (j - i > k - 1) {
50+
sum -= calories[i++];
51+
}
52+
if (j - i == k - 1) {
53+
if (sum > upper) {
54+
points++;
55+
} else if (sum < lower) {
56+
points--;
57+
}
58+
}
59+
}
60+
return points;
61+
}
62+
}
63+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1176;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
9+
public class _1176Test {
10+
private static _1176.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1176.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(0, solution1.dietPlanPerformance(new int[]{1, 2, 3, 4, 5}, 1, 3, 3));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(1, solution1.dietPlanPerformance(new int[]{3, 2}, 2, 0, 1));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(0, solution1.dietPlanPerformance(new int[]{6, 5, 0, 0}, 2, 1, 5));
30+
}
31+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy