# 数据结构与算法分类练习－－贪心算法

• 将问题分解为一系列子问题，同时定义子问题的最优解结构；
• 应用贪心原则确定每个子问题的局部最优解，用子问题的局部最优解堆叠出全局最优解。

Example 1:

Input: [1,2,3], [1,1]

Output: 1

Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.

And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.

You need to output 1.

class Solution(object):
def findContentChildren(self, g, s):
"""
:type g: List[int]
:type s: List[int]
:rtype: int
"""
g.sort()
s.sort()

i = 0
for j in range(len(s)):
if i == len(g):
break
if s[j] >= g[i]:
i += 1
return i

### Remove K Digits

Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.

Input: num = "1432219", k = 3

Output: "1219"

class Solution(object):
def removeKdigits(self, num, k):
"""
:type num: str
:type k: int
:rtype: str
"""
result = []
for d in num:
while k and result and result[-1] > d:
result.pop()
k -= 1
result.append(d)
# 删掉剩余的k位，如果删光则返回0
return ''.join(result).lstrip('0')[:-k or None] or '0' 

### Gas Station

There are N gas stations along a circular route, where the amount of gas at station i is gas[i].

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.

class Solution(object):
def canCompleteCircuit(self, gas, cost):
"""
:type gas: List[int]
:type cost: List[int]
:rtype: int
"""
start, cur_sum, total_sum = 0, 0 ,0
for i in range(len(gas)):
diff = gas[i] - cost[i]
cur_sum += diff
total_sum += diff
if cur_sum < 0:
start = i + 1
cur_sum = 0
if total_sum >= 0:
return start
else:
return -1

### Candy

There are N children standing in a line. Each child is assigned a rating value.

Each child must have at least one candy.

Children with a higher rating get more candies than their neighbors.

What is the minimum candies you must give?

ratings是由一系列递增，递减或等高子序列组成。等高子序列即平级，每人分一个即可；递增／递减子序列从局部最低点分隔成子域，那么每个子域中的最高点的发放个数应该从序列较长一侧开始计算。

class Solution(object):
def candy(self, ratings):
"""
:type ratings: List[int]
:rtype: int
"""
def count_candy(s1, s2):
# inc，dec中较大的从1开始计算，自然可以保证较小的符合条件
if s1 < s2:
s1, s2 = s2, s1
count = 0
count += ((1 + (s1 + 1)) * (s1 + 1)) / 2
count += ((1 + s2) * s2) / 2
return count

if len(ratings) < 2:
return len(ratings)
inc, dec, count, extra = 0, 0, 0, 0
for i in range(len(ratings) - 1):
if ratings[i] <= ratings[i + 1]:
if dec != 0 or ratings[i] == ratings[i + 1]:
count += count_candy(inc, dec)
inc, dec = 0, 0
if ratings[i] != ratings[i+1]:
inc = 1
# 转折点重复计算了一次
extra += 1
else:
inc += 1
else:
dec += 1
count += count_candy(inc, dec) - extra
return count

©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客