8000 add 2300 · githubniraj/Leetcode@570a5b4 · GitHub
[go: up one dir, main page]

Skip to content
< 8000 header class="HeaderMktg header-logged-out js-details-container js-header Details f4 py-3" role="banner" data-is-top="true" data-color-mode=light data-light-theme=light data-dark-theme=dark>

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

8000
Appearance settings

Commit 570a5b4

Browse files
add 2300
1 parent ff815fc commit 570a5b4

File tree

3 files changed

+67
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+67
-0
lines changed

paginated_contents/algorithms/3rd_thousand/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
| 2315 | [Count Asterisks](https://leetcode.com/problems/count-asterisks/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2315.java) || Easy ||
8181
| 2309 | [Greatest English Letter in Upper and Lower Case](https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2309.java) || Easy ||
8282
| 2303 | [Calculate Amount Paid in Taxes](https://leetcode.com/problems/calculate-amount-paid-in-taxes/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2303.java) || Easy ||
83+
| 2300 | [Successful Pairs of Spells and Potions](https://leetcode.com/problems/successful-pairs-of-spells-and-potions/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2300.java) || Medium |Binary Search
8384
| 2299 | [Strong Password Checker II](https://leetcode.com/problems/strong-password-checker-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2299.java) || Easy ||
8485
| 2293 | [Min Max Game](https://leetcode.com/problems/min-max-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2293.java) || Easy ||
8586
| 2288 | [Apply Discount to Prices](https://leetcode.com/problems/apply-discount-to-prices/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2288.java) || Medium ||
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import java.util.Arrays;
4+
5+
public class _2300 {
6+
public static class Solution1 {
7+
public int[] successfulPairs(int[] spells, int[] potions, long success) {
8+
int[] result = new int[spells.length];
9+
Arrays.sort(potions);
10+
for (int i = 0; i < spells.length; i++) {
11+
int j = binarySearch(potions, success, spells[i]);
12+
result[i] = potions.length - j;
13+
}
14+
return result;
15+
}
16+
17+
private int binarySearch(int[] potions, long success, int spell) {
18+
int left = 0;
19+
int right = potions.length - 1;
20+
while (left < right) {
21+
int mid = left + (right - left) / 2;
22+
if ((long) potions[mid] * spell >= success) {
23+
right = mid;
24+
} else {
25+
left = mid + 1;
26+
}
27+
}
28+
if (left == right && left == potions.length - 1 && (long) spell * potions[left] < success) {
29+
return potions.length;
30+
}
31+
return right;
32+
}
33+
}
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2300;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
8+
9+
public class _2300Test {
10+
private static _2300.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _2300.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[]{4, 0, 3}, solution1.successfulPairs(new int[]{5, 1, 3}, new int[]{1, 2, 3, 4, 5}, 7));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertArrayEquals(new int[]{2, 0, 2}, solution1.successfulPairs(new int[]{3, 1, 2}, new int[]{8, 5, 8}, 16));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertArrayEquals(new int[]{0, 0, 0, 1, 3, 3, 4}, solution1.successfulPairs(new int[]{1, 2, 3, 4, 5, 6, 7}, new int[]{1, 2, 3, 4, 5, 6, 7}, 25));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)
0