Skip to content

Commit 16f7fdf

Browse files
refactor 1300
1 parent 1091348 commit 16f7fdf

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ _If you like this project, please leave me a star._ ★
392392
|1305|[All Elements in Two Binary Search Trees](https://leetcode.com/problems/all-elements-in-two-binary-search-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1305.java) | |Medium||
393393
|1304|[Find N Unique Integers Sum up to Zero](https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1304.java) | |Easy||
394394
|1302|[Deepest Leaves Sum](https://leetcode.com/problems/deepest-leaves-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1302.java) | |Medium||
395-
|1300|[Sum of Mutated Array Closest to Target](https://leetcode.com/problems/sum-of-mutated-array-closest-to-target/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1300.java) | |Medium||
395+
|1300|[Sum of Mutated Array Closest to Target](https://leetcode.com/problems/sum-of-mutated-array-closest-to-target/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1300.java) | |Medium|Binary Search, Sorting|
396396
|1299|[Replace Elements with Greatest Element on Right Side](https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1299.java) | |Easy||
397397
|1297|[Maximum Number of Occurrences of a Substring](https://leetcode.com/problems/maximum-number-of-occurrences-of-a-substring/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1297.java) | |Medium||
398398
|1296|[Divide Array in Sets of K Consecutive Numbers](https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1296.java) | |Medium||

src/main/java/com/fishercoder/solutions/_1300.java

+9-17
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ public class _1300 {
44
public static class Solution1 {
55
public int findBestValue(int[] arr, int target) {
66
int ave = target / arr.length;
7-
int min = findMin(arr);
8-
int max = findMax(arr);
7+
int max = arr[0];
8+
int min = arr[0];
9+
for (int i = 1; i < arr.length; i++) {
10+
min = Math.min(min, arr[i]);
11+
max = Math.max(max, arr[i]);
12+
}
13+
if (ave >= max) {
14+
return max;
15+
}
916
//if ave is the best value, what's the difference to target?
1017
int closetDiff = findClosestDiffIfReplaceWithVal(arr, ave, target);
1118
int bestValue = ave;
@@ -47,20 +54,5 @@ private int findClosestDiffIfReplaceWithVal(int[] arr, int replaceValue, int tar
4754
return Math.abs(sum - target);
4855
}
4956

50-
private int findMax(int[] arr) {
51-
int max = arr[0];
52-
for (int i = 1; i < arr.length; i++) {
53-
max = Math.max(max, arr[i]);
54-
}
55-
return max;
56-
}
57-
58-
private int findMin(int[] arr) {
59-
int min = arr[0];
60-
for (int i = 1; i < arr.length; i++) {
61-
min = Math.min(min, arr[i]);
62-
}
63-
return min;
64-
}
6557
}
6658
}

src/test/java/com/fishercoder/_1300Test.java

+37-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,40 @@ public void test3() {
3333
assertEquals(11361, solution1.findBestValue(arr, 56803));
3434
}
3535

36-
}
36+
@Test
37+
public void test4() {
38+
arr = new int[]{2, 3, 5};
39+
assertEquals(5, solution1.findBestValue(arr, 11));
40+
}
41+
42+
@Test
43+
public void test5() {
44+
arr = new int[]{60864, 25176, 27249, 21296, 20204};
45+
assertEquals(11361, solution1.findBestValue(arr, 56803));
46+
}
47+
48+
@Test
49+
public void test6() {
50+
arr = new int[]{48772, 52931, 14253, 32289, 75263};
51+
assertEquals(8175, solution1.findBestValue(arr, 40876));
52+
}
53+
54+
@Test
55+
public void test7() {
56+
arr = new int[]{1547, 83230, 57084, 93444, 70879};
57+
assertEquals(17422, solution1.findBestValue(arr, 71237));
58+
}
59+
60+
@Test
61+
public void test8() {
62+
arr = new int[]{1, 1, 2};
63+
assertEquals(2, solution1.findBestValue(arr, 10));
64+
}
65+
66+
@Test
67+
public void test9() {
68+
arr = new int[]{1, 1, 1};
69+
assertEquals(1, solution1.findBestValue(arr, 10));
70+
}
71+
72+
}

0 commit comments

Comments
 (0)