Skip to content

Commit 8632959

Browse files
committed
Add solution #1196
1 parent bb263cc commit 8632959

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@
11141114
1190|[Reverse Substrings Between Each Pair of Parentheses](./solutions/1190-reverse-substrings-between-each-pair-of-parentheses.js)|Medium|
11151115
1191|[K-Concatenation Maximum Sum](./solutions/1191-k-concatenation-maximum-sum.js)|Medium|
11161116
1192|[Critical Connections in a Network](./solutions/1192-critical-connections-in-a-network.js)|Hard|
1117+
1196|[How Many Apples Can You Put into the Basket](./solutions/1196-how-many-apples-can-you-put-into-the-basket.js)|Easy|
11171118
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|
11181119
1201|[Ugly Number III](./solutions/1201-ugly-number-iii.js)|Medium|
11191120
1202|[Smallest String With Swaps](./solutions/1202-smallest-string-with-swaps.js)|Medium|
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 1196. How Many Apples Can You Put into the Basket
3+
* https://leetcode.com/problems/how-many-apples-can-you-put-into-the-basket/
4+
* Difficulty: Easy
5+
*
6+
* You have some apples and a basket that can carry up to 5000 units of weight.
7+
*
8+
* Given an integer array weight where weight[i] is the weight of the ith apple,
9+
* return the maximum number of apples you can put in the basket.
10+
*/
11+
12+
/**
13+
* @param {number[]} weight
14+
* @return {number}
15+
*/
16+
var maxNumberOfApples = function(weight) {
17+
weight.sort((a, b) => a - b);
18+
19+
let totalWeight = 0;
20+
let result = 0;
21+
22+
for (const appleWeight of weight) {
23+
if (totalWeight + appleWeight <= 5000) {
24+
totalWeight += appleWeight;
25+
result++;
26+
} else {
27+
break;
28+
}
29+
}
30+
31+
return result;
32+
};

0 commit comments

Comments
 (0)