Skip to content

Commit 1991b47

Browse files
author
luyunfang
committed
submit week4 homework
1 parent 9b2d6f8 commit 1991b47

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

Week4/easy/__init__.py

Whitespace-only changes.

Week4/easy/assign_cookies.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# ============================================
2+
# 455. Assign Cookies
3+
# 审题: 1.每个孩子最多只能给一块饼干。2.你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。
4+
# not seen before, not solved alone, idea following the solutions proposed in leetcode
5+
# 解体思路: 1.贪心思想。
6+
# time complexity: 1.O(nlongn) + O(n) ≈ O(nlogn)
7+
# ============================================
8+
9+
10+
class Solution(object):
11+
def findContentChildren(self, g, s):
12+
"""
13+
:type g: List[int]
14+
:type s: List[int]
15+
:rtype: int
16+
"""
17+
if (len(g) == 0) or (len(s) == 0):
18+
return 0
19+
g.sort(reverse=False)
20+
s.sort(reverse=False)
21+
child = 0
22+
bisc = 0
23+
while (bisc <= len(s)-1) and (child <= len(g)-1):
24+
if (s[bisc] >= g[child]):
25+
child += 1
26+
bisc += 1
27+
return child

Week4/easy/lemon_change.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# ============================================
2+
# 860. Lemonade Change
3+
# 审题: 1. 一开始你手头没有任何零钱; 2. 只有5, 10, 20三种类型。
4+
# not seen before, not solved alone, idea following the solutions proposed in leetcode
5+
# 解体思路: 1.枚举所有可能的找零方式; 2.找零时贪心思想。
6+
# time complexity: 1.O(n)
7+
# ============================================
8+
9+
10+
# TODO, 内存消耗太大
11+
class Solution(object):
12+
def lemonadeChange(self, bills):
13+
"""
14+
:type bills: List[int]
15+
:rtype: bool
16+
"""
17+
cash_5 = 0
18+
cash_10 = 0
19+
ct = 0
20+
for ele in bills:
21+
if ele == 5:
22+
cash_5 += 1
23+
elif ele == 10:
24+
if cash_5 < 1:
25+
return False
26+
cash_10 += 1
27+
cash_5 -= 1
28+
# ele == 20
29+
else:
30+
if (cash_10 > 0) and (cash_5 > 0):
31+
cash_10 -= 1
32+
cash_5 -= 1
33+
elif (cash_5 >= 3):
34+
cash_5 -= 3
35+
else:
36+
return False
37+
return True
38+
39+

0 commit comments

Comments
 (0)