t02贪心算法_分发饼干
Sub 贪心算法_分发饼干() g = Array(1, 2) s = Array(1, 2, 3) Debug.Print (findContectChildren(g, s)) Debug.Print (findContentChilren(g, s)) End Sub Function findContentChilren(g, s) g = Arrays.sort(g) s = Arrays.sort(s) Dim count: count = 0 Dim start: start = UBound(s) For Index = UBound(g) To 0 Step -1 If (start >= 0 And g(Index) < s(start)) Then start = start - 1 count = count + 1 End If Next findContentChilren = count End Function Function findContectChildren(g, s) g = Arrays.sort(g) s = Arrays.sort(s) Dim start: start = 0 Dim count: count = 0 For i = 0 To UBound(s) + 1 If i < UBound(s) + 1 And start < UBound(g) + 1 Then If (s(i) >= g(start)) Then start = start + 1 count = count + 1 End If End If Next findContectChildren = count End Function