forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13ec934
commit 8e4689e
Showing
5 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/1196-How-Many-Apples-Can-You-Put-into-the-Basket/1196.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution | ||
{ | ||
public: | ||
int maxNumberOfApples(vector<int>& arr) | ||
{ | ||
sort(arr.begin(), arr.end()); | ||
int res = 0, t = 0; | ||
for (int i : arr) | ||
{ | ||
t += i; | ||
if (t > 5000) break; | ||
res++; | ||
} | ||
return res; | ||
} | ||
}; |
26 changes: 26 additions & 0 deletions
26
src/1196-How-Many-Apples-Can-You-Put-into-the-Basket/1196.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
func maxNumberOfApples(arr []int) int { | ||
sort.Sort(IntSlice(arr)) | ||
res, t := 0, 0 | ||
for _, v := range arr { | ||
t += v | ||
if t > 5000 { | ||
break | ||
} | ||
res++ | ||
} | ||
return res | ||
} | ||
|
||
type IntSlice []int | ||
|
||
func (s IntSlice) Len() int { | ||
return len(s) | ||
} | ||
|
||
func (s IntSlice) Swap(i, j int) { | ||
s[i], s[j] = s[j], s[i] | ||
} | ||
|
||
func (s IntSlice) Less(i, j int) bool { | ||
return s[i] < s[j] | ||
} |
12 changes: 12 additions & 0 deletions
12
src/1196-How-Many-Apples-Can-You-Put-into-the-Basket/1196.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var maxNumberOfApples = function(arr) { | ||
arr.sort(function sortNumber(a,b) { | ||
return a - b; | ||
}); | ||
var res = 0, t = 0; | ||
for (let i of arr) { | ||
t += i; | ||
if (t > 5000) break; | ||
res++; | ||
} | ||
return res; | ||
}; |
10 changes: 10 additions & 0 deletions
10
src/1196-How-Many-Apples-Can-You-Put-into-the-Basket/1196.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution: | ||
def maxNumberOfApples(self, arr: List[int]) -> int: | ||
arr.sort() | ||
res, t = 0, 0 | ||
for v in arr: | ||
t += v | ||
if t > 5000: | ||
break | ||
res += 1 | ||
return res |