Skip to content

Commit 4ed7553

Browse files
add 1196
1 parent d537578 commit 4ed7553

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ _If you like this project, please leave me a star._ ★
2323
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java) | O(n) | O(1) | [:tv:](https://www.youtube.com/watch?v=_NYimlZY1PE)|Easy||
2424
|1200|[Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1200.java) | || [:tv:](https://www.youtube.com/watch?v=mH1aEjOEjcQ)|Easy||
2525
|1198|[Find Smallest Common Element in All Rows](https://leetcode.com/problems/find-smallest-common-element-in-all-rows/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1198.java) | | | [:tv:](https://www.youtube.com/watch?v=RMiofZrTmWo)|Easy||
26+
|1196|[How Many Apples Can You Put into the Basket](https://leetcode.com/problems/how-many-apples-can-you-put-into-the-basket/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1196.java) | | | |Easy||
2627
|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1189.java) | | | |Easy||
2728
|1185|[Day of the Week](https://leetcode.com/problems/day-of-the-week/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1185.java) | | | |Easy||
2829
|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||
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 1196. How Many Apples Can You Put into the Basket
7+
*
8+
* You have some apples, where arr[i] is the weight of the i-th apple. You also have a basket that can carry up to 5000 units of weight.
9+
* Return the maximum number of apples you can put in the basket.
10+
*
11+
* Example 1:
12+
* Input: arr = [100,200,150,1000]
13+
* Output: 4
14+
* Explanation: All 4 apples can be carried by the basket since their sum of weights is 1450.
15+
*
16+
* Example 2:
17+
* Input: arr = [900,950,800,1000,700,800]
18+
* Output: 5
19+
* Explanation: The sum of weights of the 6 apples exceeds 5000 so we choose any 5 of them.
20+
*
21+
* Constraints:
22+
* 1 <= arr.length <= 10^3
23+
* 1 <= arr[i] <= 10^3
24+
* */
25+
public class _1196 {
26+
public static class Solution1 {
27+
public int maxNumberOfApples(int[] arr) {
28+
Arrays.sort(arr);
29+
int sum = 0;
30+
int i = 0;
31+
for (; i < arr.length; i++) {
32+
if (sum + arr[i] < 5000) {
33+
sum += arr[i];
34+
} else {
35+
break;
36+
}
37+
}
38+
return i;
39+
}
40+
}
41+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1196;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1196Test {
10+
private static _1196.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1196.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(4, solution1.maxNumberOfApples(new int[]{100, 200, 150, 1000}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(5, solution1.maxNumberOfApples(new int[]{900, 950, 800, 1000, 700, 800}));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)