Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit71c056e

Browse files
add 1774
1 parent15cf658 commit71c056e

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1774|[Closest Dessert Cost](https://leetcode.com/problems/closest-dessert-cost/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1774.java)||Medium|Greedy|
1112
|1773|[Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1773.java)||Easy|Array, String|
1213
|1769|[Minimum Number of Operations to Move All Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1769.java)||Medium|Array, Greedy|
1314
|1768|[Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1768.java)||Easy|String|
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packagecom.fishercoder.solutions;
2+
3+
publicclass_1774 {
4+
publicstaticclassSolution1 {
5+
intresult =0;
6+
7+
publicintclosestCost(int[]baseCosts,int[]toppingCosts,inttarget) {
8+
result =baseCosts[0];
9+
for (inti =0;i <baseCosts.length;i++) {
10+
recursion(baseCosts[i],toppingCosts,0,target);
11+
}
12+
returnresult;
13+
}
14+
15+
privatevoidrecursion(intcurrentCost,int[]toppingCosts,intindex,inttarget) {
16+
if (Math.abs(currentCost -target) <Math.abs(result -target) || (Math.abs(currentCost -target) <Math.abs(result -target) &&currentCost ==result)) {
17+
result =currentCost;
18+
}
19+
if (index ==toppingCosts.length ||currentCost ==target) {
20+
return;
21+
}
22+
recursion(currentCost,toppingCosts,index +1,target);
23+
recursion(currentCost +toppingCosts[index],toppingCosts,index +1,target);
24+
recursion(currentCost +toppingCosts[index] *2,toppingCosts,index +1,target);
25+
}
26+
}
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1774;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1774Test {
10+
privatestatic_1774.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1774.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(10,solution1.closestCost(newint[]{1,7},newint[]{3,4},10));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(17,solution1.closestCost(newint[]{2,3},newint[]{4,5,100},18));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(8,solution1.closestCost(newint[]{3,10},newint[]{2,5},9));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp