Skip to content

Commit ffbd96a

Browse files
add 1891
1 parent ad74745 commit ffbd96a

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+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
public class _1891 {
6+
public static class Solution1 {
7+
/**
8+
* My completely original solution on 1/27/2022.
9+
*/
10+
public int maxLength(int[] ribbons, int k) {
11+
long sum = 0l;
12+
int max = ribbons[0];
13+
for (int num : ribbons) {
14+
sum += num;
15+
max = Math.max(max, num);
16+
}
17+
if (sum < k) {
18+
return 0;
19+
} else if (sum == k) {
20+
return 1;
21+
} else {
22+
Arrays.sort(ribbons);
23+
int left = 1;
24+
int right = max;
25+
int ans = 1;
26+
while (left < right) {
27+
int mid = left + (right - left) / 2;
28+
int count = 0;
29+
for (int i = 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+
int count = 0;
43+
for (int i = ribbons.length - 1; i >= 0; i--) {
44+
count += ribbons[i] / left;
45+
if (count >= k) {
46+
ans = Math.max(ans, left);
47+
return ans;
48+
}
49+
}
50+
return ans;
51+
}
52+
}
53+
}
54+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1891;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1891Test {
10+
private static _1891.Solution1 solution1;
11+
private static int[] ribbons;
12+
private static int k;
13+
private static int expected;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _1891.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
ribbons = new int[]{9, 7, 5};
23+
k = 3;
24+
expected = 5;
25+
assertEquals(expected, solution1.maxLength(ribbons, k));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
ribbons = new int[]{7, 5, 9};
31+
k = 4;
32+
expected = 4;
33+
assertEquals(expected, solution1.maxLength(ribbons, k));
34+
}
35+
36+
@Test
37+
public void test3() {
38+
ribbons = new int[]{5, 7, 9};
39+
k = 22;
40+
expected = 0;
41+
assertEquals(expected, solution1.maxLength(ribbons, k));
42+
}
43+
44+
@Test
45+
public void test4() {
46+
ribbons = new int[]{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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy