Skip to content

Commit ec309f4

Browse files
committed
提交 解决 柠檬水找零 问题
1 parent 1b8e893 commit ec309f4

6 files changed

Lines changed: 213 additions & 3 deletions

File tree

‎algorithmCode/.idea/leetcode/editor.xml‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

algorithmCode/src/main/java/weekFour4/Number102二叉树的层序遍历.java renamed to algorithmCode/src/main/java/weekFour/Number102二叉树的层序遍历.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
package weekFour4;
1+
package weekFour;
22

33
import lombok.Getter;
44
import lombok.Setter;
5-
import weekTwo.Number94二叉树的中序遍历;
65

7-
import javax.swing.tree.TreeNode;
86
import java.util.ArrayList;
97
import java.util.LinkedList;
108
import java.util.List;
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package weekFour;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
6+
/**
7+
* Number322 零钱兑换
8+
*
9+
* @author Ye-Ma
10+
*/
11+
public class Number322零钱兑换 {
12+
13+
/* public static void main(String[] args) {
14+
int[] coins = new int[]{1, 2, 5};
15+
Arrays.sort(coins);
16+
// Arrays.sort(coins, (o1, o2) -> o1 > o2 ? -1 : 1);
17+
18+
System.out.println(Arrays.toString(coins));
19+
20+
for (int i = coins.length-1; i >= 0; i--) {
21+
System.out.println(coins[i]);
22+
}
23+
24+
}*/
25+
26+
public int coinChange(int[] coins, int amount) {
27+
28+
if (amount == 0) {
29+
return 0;
30+
}
31+
// 考虑 coins 长度只有一的情况
32+
if (coins.length == 1) {
33+
int coin = coins[0];
34+
int value = amount / coin;
35+
if (value % 1 == 0) {
36+
return value;
37+
}
38+
return -1;
39+
}
40+
// amount 查看是否可以被整除(最大的数字) 拉其他的
41+
Arrays.sort(coins);
42+
43+
int coin = coins[coins.length - 1];
44+
int maxValue = amount / coin;
45+
amount--;
46+
47+
48+
return -1;
49+
50+
}
51+
52+
53+
static class Solution {
54+
public static void main(String[] args) {
55+
int[] ints = {1, 2, 5};
56+
coinChange(ints, 11);
57+
}
58+
59+
60+
public static int coinChange(int[] coins, int amount) {
61+
Arrays.sort(coins);
62+
recursion(coins, amount, 0, coins.length - 1);
63+
return minCount == Integer.MAX_VALUE ? -1 : minCount;
64+
}
65+
66+
static int minCount = Integer.MAX_VALUE;
67+
68+
/**
69+
* 1、按金额从大到小,从多到少(排序,用余数一步到位)
70+
* 2、预判低于最优解,终止递归(可以返回最优解,不过提升有限,意义不大)
71+
* 3、能整除即可返回
72+
*/
73+
static void recursion(int[] coins, int amount, int count, int index) {
74+
// 终止条件
75+
if (index < 0 || count + amount / coins[index] >= minCount) return;
76+
// amount 除以 最大值 可以被整除的情况
77+
if (amount % coins[index] == 0) {
78+
minCount = Math.min(minCount, count + amount / coins[index]);
79+
return;
80+
}
81+
// 不能被整除 用零钱来凑单
82+
for (int i = amount / coins[index]; i >= 0; i--) {
83+
recursion(coins, amount - i * coins[index], count + i, index - 1);
84+
}
85+
}
86+
}
87+
88+
static class SolutionTwo {
89+
90+
public static void main(String[] args) {
91+
92+
}
93+
94+
public static int coinChangeTwo(int[] coins, int amount) {
95+
// 排序
96+
Arrays.sort(coins);
97+
// 递归
98+
// 零钱集合 目标值
99+
recursion(coins, amount, 0, coins.length - 1);
100+
return minCount == Integer.MAX_VALUE ? -1 : minCount;
101+
}
102+
103+
// 零时变量
104+
static int minCount = Integer.MAX_VALUE;
105+
106+
/**
107+
* 1、按金额从大到小,从多到少(排序,用余数一步到位)
108+
* 2、预判低于最优解,终止递归(可以返回最优解,不过提升有限,意义不大)
109+
* 3、能整除即可返回
110+
*/
111+
static void recursion(int[] coins, int amount, int count, int index) {
112+
if (index < 0 || count + amount / coins[index] >= minCount) return;
113+
114+
// 可以直接解决
115+
if (amount / coins[index] == 0) {
116+
minCount = Math.min(minCount, count + amount / coins[index]);
117+
return;
118+
}
119+
120+
for (int i = amount / coins[index]; i >= 0; i--) {
121+
122+
}
123+
124+
125+
}
126+
127+
128+
}
129+
130+
131+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package weekFour;
2+
3+
/**
4+
* Number 860 柠檬水找零
5+
*
6+
* @author Ye-Ma
7+
*/
8+
public class Number860柠檬水找零 {
9+
public static void main(String[] args) {
10+
int five = 3, ten = 0;
11+
int bill = 20;
12+
if (bill == 5) {
13+
five++;
14+
} else if (bill == 10) {
15+
five--;
16+
ten++;
17+
} else if (ten > 0) {
18+
ten--;
19+
five--;
20+
} else {
21+
22+
five -= 3;
23+
}
24+
System.out.println("5元剩余的值的数量 " + five);
25+
System.out.println("5元剩余的值的数量 " + ten);
26+
}
27+
28+
29+
/**
30+
* 结果是否可以 找零
31+
*
32+
* @param bills
33+
* @return
34+
*/
35+
public boolean lemonadeChange(int[] bills) {
36+
/**
37+
本文我们一起用高阶函数完成1行解,看先赋值和先比较谁更快,尝试分析原因
38+
解题思路
39+
40+
n5记录5美元数量,n10记录10美元数量。遍历bills
41+
遇5:n5 + 1
42+
遇10:n10 + 1,找零 n5 - 1
43+
遇20:贪心策略找零优先大额:因为10美元找零只能用5美元,20找零10 5都可
44+
有10:找零 n10 - 1,n5 - 1
45+
没10:找零 n5 - 3
46+
5美元不够找零,返回false
47+
方案1:先找零,比较找零后n5 < 0
48+
方案2:先比较n5<需要找零的数量``
49+
50+
51+
重点模型 5 10 20
52+
* */
53+
int five = 3, ten = 0;
54+
int bill = 20;
55+
if (bill == 5) {
56+
five++;
57+
} else if (bill == 10) {
58+
five--;
59+
ten++;
60+
} else if (ten > 0) {
61+
ten--;
62+
five--;
63+
} else {
64+
65+
five -= 3;
66+
}
67+
if (five < 0 || ten < 0) {
68+
return false;
69+
}
70+
return false;
71+
}
72+
73+
}
Binary file not shown.
40 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)