Greedy algorithm introduced:
When problem solving, always make the best choice in the current view
The basic idea:
A mathematical model to describe the problem
Solving the problem is divided into several sub-problems
For each sub-problem solving, to get sub-optimal solution to the problem of local
A handle for Solution local optimal solution synthesis of the original solution to the problem
Premise greedy policy applies: there are enough local strategy can lead to global optimal solution.
The main difference between the greedy algorithm and dynamic programming algorithm:
Dynamic programming algorithm is usually a bottom-up way to solve the sub-problems, and be greedy algorithm through a top-down way, in an iterative manner to select successive greedy, greedy each make a choice on the scale of the problem is reduced to seeking smaller sub-problems.
Problem Description 1: find coins:
With n different coins, now come to use the money denomination of the coin, various denominations of the coin denomination 1,2,5,10,20,50,100,500,1000 {}, for any amount of money, a design with the method of the least coin find the money.
def FindClion(V): aviable = [1,2,5,10,20,50,100,200,500,1000] result = [] for i in aviable[::-1]: while(V >= i): V -= i result.append(i) return result V = 98 print(FindClion(V))
Problem Description 2: Activity Issues
N active, the start time for each activity si, the end time is fi. If si> = fj or sj> = fi defines two events do not conflict. Try to find a way to find the most active non-conflicting set of (S). That is not to find the set S ', such that | S' |> | S |.
S = 1 3 0 5 8 5
F = 2 4 6 7 9 9
def printMaxActivities(acts): n = len(acts) sort_acts = sorted(acts,key=lambda tup:tup[1]) pre = sort_acts[0] print(pre) for curr in sort_acts: if curr[0] >= pre[1]: print(curr) pre = curr acts = [(0, 6), (3, 6), (1, 2), (5, 7), (8, 9), (5, 9)] printMaxActivities(acts)
Problem Description 3:
How to find a minimum number, and the number of digits of the number of digits and give:
For example: Input: s = 9, m = 2
output:18
def FindSmallest(m , s): if s == 0: if m == 1: print("Smallest number is 0") else: print("Not possible") return if s > m * 9: return result = [0 for i in range(m+1)] s -= 1 for i in range(m-1, 0, -1): if s <= 9: result[i] = s s = 0 else: result[i] = 9 s -= 9 result[0] = s + 1 for i in range(m): print(result[i], end="") s = 20 m = 3 FindSmallest(m, s)
Problem Description 4:
An array split into two numbers, and seeking the minimum of two numbers
例 Input:[6 8 4 5 2 3] Output:604 The minimum sum is formed by numbers 358 and 246
import heapq def minSum(a): heapq.heapify(a) num1 = 0 num2 = 0 while(a): num1 = num1 * 10 + heapq.heappop(a) if a: num2 = num2 * 10 + heapq.heappop(a) return num1 + num2 a = [6, 8, 4, 5, 2, 3] print(minSum(a))
Problem Description 5:
Minimum cost to connect some of the rope.
import heapq def ropeCost(ropes): if len(ropes) == 1: return 0 heapq.heapify(ropes) total = 0 while ropes: first = heapq.heappop(ropes) second = heapq.heappop(ropes) local = first + second total += local if not ropes: break heapq.heappush(ropes, local) return total ropes = [4, 3, 2, 6] print(ropeCost(ropes))