Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Week_04/id_39/LeetCode_455_039.java
Original file line number Diff line number Diff line change
@@ -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<g.length && j<s.length; i++) {
while (j < s.length && g[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

}
}
34 changes: 34 additions & 0 deletions Week_04/id_39/LeetCode_746_039.java
Original file line number Diff line number Diff line change
@@ -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<cost.length+1; ++n){
healthCost[n]=Math.min(healthCost[n-1]+cost[n-1],healthCost[n-2]+cost[n-2]);
}
return healthCost[cost.length];
}

public static void main(String[] args) {
LeetCode_746_039 l = new LeetCode_746_039();

int[] cost1 = {10,15,20};
System.out.println(l.minCostClimbingStairs(cost1));//15

int[] cost2 = {1,100,1,1,1,100,1,1,100,1};
System.out.println(l.minCostClimbingStairs(cost2));//6

}
}