From 53a01adfd86e43f9829236635b1115225af8aa26 Mon Sep 17 00:00:00 2001 From: Paula Lin <1248357331@qq.com> Date: Sun, 12 May 2019 09:22:52 +0800 Subject: [PATCH] 039 to submit week004 homework --- Week_04/id_39/LeetCode_455_039.java | 54 +++++++++++++++++++++++++++++ Week_04/id_39/LeetCode_746_039.java | 34 ++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 Week_04/id_39/LeetCode_455_039.java create mode 100644 Week_04/id_39/LeetCode_746_039.java diff --git a/Week_04/id_39/LeetCode_455_039.java b/Week_04/id_39/LeetCode_455_039.java new file mode 100644 index 00000000..3abd0a34 --- /dev/null +++ b/Week_04/id_39/LeetCode_455_039.java @@ -0,0 +1,54 @@ +/** + * @author Paula + * + */ +package com.paula.algorithmsAndDataStructure.greedyAlgorithm; + +import java.util.Arrays; +/** + * 分饼干 + * @author Paula + * + */ +public class LeetCode_455_039{ + /** + * + * @param g 小孩的胃口 + * @param s 饼干的大小尺寸 + * @return + */ + public int findContentChildren(int[] g, int[] s) { + if(null == g || null == s || g.length <= 0 || s.length <=0) return 0; + + Arrays.sort(g); + Arrays.sort(s); + int count = 0; + + for(int i=0, j=0; i s[j]) { + ++j; + } + if(j == s.length) break; + ++count; + ++j; + } + return count; + } + + public static void main(String[] args) { + LeetCode_455_039 l = new LeetCode_455_039(); + + int[] g1 = {5,10,2,9,15}; + int[] s1 = {6,1,20,3,8}; + System.out.println("g1 = " + Arrays.toString(g1) + ", s1 = " + Arrays.toString(s1) + ", findContentChildren = " + l.findContentChildren(g1, s1));//3 + + int[] g2 = {1,2,3}; + int[] s2 = {1,1}; + System.out.println("g2 = " + Arrays.toString(g2) + ", s2 = " + Arrays.toString(s2) + ", findContentChildren = " + l.findContentChildren(g2, s2));//1 + + int[] g3 = {1,2}; + int[] s3 = {1,2,3}; + System.out.println("g3 = " + Arrays.toString(g3) + ", s3 = " + Arrays.toString(s3) + ", findContentChildren = " + l.findContentChildren(g3, s3));//2 + + } +} \ No newline at end of file diff --git a/Week_04/id_39/LeetCode_746_039.java b/Week_04/id_39/LeetCode_746_039.java new file mode 100644 index 00000000..3f77e63e --- /dev/null +++ b/Week_04/id_39/LeetCode_746_039.java @@ -0,0 +1,34 @@ +/** + * @author Paula + * + */ +package com.paula.algorithmsAndDataStructure.greedyAlgorithm; + +import java.util.Arrays; +/** + * 使用最小花费爬楼梯 + * @author Paula + * + */ +public class LeetCode_746_039{ + public int minCostClimbingStairs(int[] cost) { + int healthCost[]=new int[cost.length+1];//存储爬到某个台阶时所消耗的体力值 + healthCost[0]=0; + healthCost[1]=0; + for(int n=2; n