Skip to content

Commit 55ef0fc

Browse files
chlng1
1 parent 7ff8504 commit 55ef0fc

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# March LeetCoding Challenge 2021
2+
In this repository provided my Python solutions of March LeetCoding Challenge problems.
3+
4+
## Introduction
5+
In this repository provided my Python solutions of March LeetCoding Challenge problems.
6+
- I highly recommend you to try and solve problems yourself before look at the solutions here.
7+
- Solutions and approaches may be not optimal.
8+
- All Solutions here are in Python 3.
9+
10+
## Notes
11+
None
12+
13+
## Week 1
14+
||Title|Solution|Difficulty|
15+
| ----: | --- | --- | --- |
16+
|575.|[Distribute Candies](https://leetcode.com/problems/distribute-candies/)|[Python](/Easy/575.DistributeCandies.py)|Easy|
17+
18+
## License
19+
The code is open-source and licensed under the [MIT License](/LICENSE).

Easy/575.DistributeCandies.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
Alice has n candies, where the ith candy is of type
3+
candyType[i]. Alice noticed that she started to gain
4+
weight, so she visited a doctor.
5+
6+
The doctor advised Alice to only eat n / 2 of the candies
7+
she has (n is always even). Alice likes her candies very
8+
much, and she wants to eat the maximum number of
9+
different types of candies while still following the
10+
doctor's advice.
11+
12+
Given the integer array candyType of length n, return the
13+
maximum number of different types of candies she can eat
14+
if she only eats n / 2 of them.
15+
16+
Example:
17+
Input: candyType = [1,1,2,2,3,3]
18+
Output: 3
19+
Explanation: Alice can only eat 6 / 2 = 3 candies. Since
20+
there are only 3 types, she can eat one of
21+
each type.
22+
23+
Example:
24+
Input: candyType = [1,1,2,3]
25+
Output: 2
26+
Explanation: Alice can only eat 4 / 2 = 2 candies. Whether
27+
she eats types [1,2], [1,3], or [2,3], she
28+
still can only eat 2 different types.
29+
30+
Example:
31+
Input: candyType = [6,6,6,6]
32+
Output: 1
33+
Explanation: Alice can only eat 4 / 2 = 2 candies. Even
34+
though she can eat 2 candies, she only has
35+
1 type.
36+
37+
Constraints:
38+
- n == candyType.length
39+
- 2 <= n <= 10^4
40+
- n is even.
41+
- -10^5 <= candyType[i] <= 10^5
42+
'''
43+
#Difficulty: Easy
44+
#206 / 206 test cases passed.
45+
#Runtime: 784 ms
46+
#Memory Usage: 16.3 MB
47+
48+
#Runtime: 784 ms, faster than 82.02% of Python3 online submissions for Distribute Candies.
49+
#Memory Usage: 16.3 MB, less than 38.32% of Python3 online submissions for Distribute Candies.
50+
51+
class Solution:
52+
def distributeCandies(self, candyType: List[int]) -> int:
53+
return min(len(candyType)//2, len(set(candyType)))

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Python solutions of LeetCode problems.
22
![Language](https://img.shields.io/badge/language-Python-blue.svg)&nbsp;
3-
![Problems Solved](https://img.shields.io/badge/problems%20solved-514%2F1609-orange)&nbsp;
3+
![Problems Solved](https://img.shields.io/badge/problems%20solved-515%2F1609-orange)&nbsp;
44
[![License](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)&nbsp;
55
![Update](https://img.shields.io/badge/update-Daily-brightgreen.svg)&nbsp;
66
<br><br>
@@ -21,6 +21,7 @@ In this repository provided my Python solutions of LeetCode problems.
2121
2021:
2222
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 27/31
2323
- [February LeetCoding Challenge](/Challenges/2021/February-LeetCoding-Challenge.md) - 23/28
24+
- [March LeetCoding Challenge](/Challenges/2021/March-LeetCoding-Challenge.md) - 1/31
2425
<br><br>
2526
## Solutions
2627
*P.S. If you like this, please leave me a star.*
@@ -264,6 +265,7 @@ In this repository provided my Python solutions of LeetCode problems.
264265
|559.|[Maximum Depth of N-ary Tree](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/)|[Python](/Easy/559.MaximumDepthofN-aryTree.py)|Easy|`BFS`|
265266
|560.|[Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k)|[Python](https://github.com/YuriSpiridonov/30-Day-LeetCoding-Challenge/blob/master/Week%204/SubarraySumEqualsK.py)|Medium|
266267
|563.|[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/)|[Python](/Easy/563.BinaryTreeTilt.py)|Easy|`Binary Tree`, `postorder`, overcode|
268+
|575.|[Distribute Candies](https://leetcode.com/problems/distribute-candies/)|[Python](/Easy/575.DistributeCandies.py)|Easy||
267269
|581.|[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/)|[Python](/Medium/581.ShortestUnsortedContinuousSubarray.py)|Medium||
268270
|589.|[N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/)|[Python](/Easy/589.N-aryTreePreorderTraversal.py)|Easy|`Recursion`|
269271
|590.|[N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/)|[Python](/Easy/590.N-aryTreePostorderTraversal.py)|Easy|`Recursion`|

0 commit comments

Comments
 (0)