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

Commitffbd96a

Browse files
add 1891
1 parentad74745 commitffbd96a

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ _If you like this project, please leave me a star._ ★
112112
|1903|[Largest Odd Number in String](https://leetcode.com/problems/largest-odd-number-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1903.java)|[:tv:](https://youtu.be/IIt_ARZzclY)|Easy|Greedy|
113113
|1897|[Redistribute Characters to Make All Strings Equal](https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1897.java)||Easy|String, Greedy|
114114
|1893|[Check if All the Integers in a Range Are Covered](https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1893.java)||Easy|Array, HashTable, Prefix Sum|
115+
|1891|[Cutting Ribbons](https://leetcode.com/problems/cutting-ribbons/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1891.java)||Medium|Array, Binary Search|
115116
|1886|[Determine Whether Matrix Can Be Obtained By Rotation](https://leetcode.com/problems/determine-whether-matrix-can-be-obtained-by-rotation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1886.java)||Easy|Array|
116117
|1880|[Check if Word Equals Summation of Two Words](https://leetcode.com/problems/check-if-word-equals-summation-of-two-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1880.java)||Easy|String|
117118
|1877|[Minimize Maximum Pair Sum in Array](https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1877.java)||Medium|Greedy, Sort|
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Arrays;
4+
5+
publicclass_1891 {
6+
publicstaticclassSolution1 {
7+
/**
8+
* My completely original solution on 1/27/2022.
9+
*/
10+
publicintmaxLength(int[]ribbons,intk) {
11+
longsum =0l;
12+
intmax =ribbons[0];
13+
for (intnum :ribbons) {
14+
sum +=num;
15+
max =Math.max(max,num);
16+
}
17+
if (sum <k) {
18+
return0;
19+
}elseif (sum ==k) {
20+
return1;
21+
}else {
22+
Arrays.sort(ribbons);
23+
intleft =1;
24+
intright =max;
25+
intans =1;
26+
while (left <right) {
27+
intmid =left + (right -left) /2;
28+
intcount =0;
29+
for (inti =ribbons.length -1;i >=0;i--) {
30+
count +=ribbons[i] /mid;
31+
if (count >=k) {
32+
ans =Math.max(ans,mid);
33+
break;
34+
}
35+
}
36+
if (count <k) {
37+
right =mid -1;
38+
}else {
39+
left =mid +1;
40+
}
41+
}
42+
intcount =0;
43+
for (inti =ribbons.length -1;i >=0;i--) {
44+
count +=ribbons[i] /left;
45+
if (count >=k) {
46+
ans =Math.max(ans,left);
47+
returnans;
48+
}
49+
}
50+
returnans;
51+
}
52+
}
53+
}
54+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1891;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1891Test {
10+
privatestatic_1891.Solution1solution1;
11+
privatestaticint[]ribbons;
12+
privatestaticintk;
13+
privatestaticintexpected;
14+
15+
@BeforeClass
16+
publicstaticvoidsetup() {
17+
solution1 =new_1891.Solution1();
18+
}
19+
20+
@Test
21+
publicvoidtest1() {
22+
ribbons =newint[]{9,7,5};
23+
k =3;
24+
expected =5;
25+
assertEquals(expected,solution1.maxLength(ribbons,k));
26+
}
27+
28+
@Test
29+
publicvoidtest2() {
30+
ribbons =newint[]{7,5,9};
31+
k =4;
32+
expected =4;
33+
assertEquals(expected,solution1.maxLength(ribbons,k));
34+
}
35+
36+
@Test
37+
publicvoidtest3() {
38+
ribbons =newint[]{5,7,9};
39+
k =22;
40+
expected =0;
41+
assertEquals(expected,solution1.maxLength(ribbons,k));
42+
}
43+
44+
@Test
45+
publicvoidtest4() {
46+
ribbons =newint[]{100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,1,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000};
47+
k =49;
48+
expected =100000;
49+
assertEquals(expected,solution1.maxLength(ribbons,k));
50+
}
51+
52+
53+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp