Skip to content

Commit c9e6937

Browse files
committed
455
1 parent 8398897 commit c9e6937

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/python3
2+
"""
3+
Given a non-empty integer array of size n, find the minimum number of moves
4+
required to make all array elements equal, where a move is incrementing n - 1
5+
elements by 1.
6+
"""
7+
8+
9+
class Solution:
10+
def minMoves(self, nums):
11+
"""
12+
List out, find the pattern
13+
for every operation, the max number does not change, then bring the min
14+
number 1 step closer to the max.
15+
16+
:type nums: List[int]
17+
:rtype: int
18+
"""
19+
mini = min(nums)
20+
return sum(map(lambda e: e - mini, nums))
21+
22+
23+
if __name__ == "__main__":
24+
assert Solution().minMoves([1, 2, 3]) == 3

455 Assign Cookies.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/python3
2+
"""
3+
Assume you are an awesome parent and want to give your children some cookies.
4+
But, you should give each child at most one cookie. Each child i has a greed
5+
factor gi, which is the minimum size of a cookie that the child will be content
6+
with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j
7+
to the child i, and the child i will be content. Your goal is to maximize the
8+
number of your content children and output the maximum number.
9+
10+
Note:
11+
You may assume the greed factor is always positive.
12+
You cannot assign more than one cookie to one child.
13+
"""
14+
15+
16+
class Solution:
17+
def findContentChildren(self, g, s):
18+
"""
19+
Greedy
20+
21+
:type g: List[int]
22+
:type s: List[int]
23+
:rtype: int
24+
"""
25+
g.sort()
26+
s.sort()
27+
ret = 0
28+
i = 0
29+
j = 0
30+
while i < len(g) and j < len(s):
31+
if g[i] <= s[j]:
32+
ret += 1
33+
i += 1
34+
j += 1
35+
else:
36+
j += 1
37+
38+
return ret
39+
40+
41+
if __name__ == "__main__":
42+
assert Solution().findContentChildren([10,9,8,7], [5,6,7,8]) == 2

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