Skip to content

Commit 11a5c2c

Browse files
authored
Added tasks 1828, 1829, 1830, 1832, 1833.
1 parent cb2ec47 commit 11a5c2c

File tree

15 files changed

+463
-0
lines changed

15 files changed

+463
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g1801_1900.s1828_queries_on_number_of_points_inside_a_circle;
2+
3+
// #Medium #Array #Math #Geometry #2022_05_07_Time_23_ms_(75.03%)_Space_50.5_MB_(46.96%)
4+
5+
public class Solution {
6+
public int[] countPoints(int[][] points, int[][] queries) {
7+
int[] result = new int[queries.length];
8+
int i = 0;
9+
for (int[] query : queries) {
10+
int pts = 0;
11+
for (int[] point : points) {
12+
if ((point[0] - query[0]) * (point[0] - query[0])
13+
+ (point[1] - query[1]) * (point[1] - query[1])
14+
<= query[2] * query[2]) {
15+
pts++;
16+
}
17+
}
18+
result[i++] = pts;
19+
}
20+
return result;
21+
}
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
1828\. Queries on Number of Points Inside a Circle
2+
3+
Medium
4+
5+
You are given an array `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> is the coordinates of the <code>i<sup>th</sup></code> point on a 2D plane. Multiple points can have the **same** coordinates.
6+
7+
You are also given an array `queries` where <code>queries[j] = [x<sub>j</sub>, y<sub>j</sub>, r<sub>j</sub>]</code> describes a circle centered at <code>(x<sub>j</sub>, y<sub>j</sub>)</code> with a radius of <code>r<sub>j</sub></code>.
8+
9+
For each query `queries[j]`, compute the number of points **inside** the <code>j<sup>th</sup></code> circle. Points **on the border** of the circle are considered **inside**.
10+
11+
Return _an array_ `answer`_, where_ `answer[j]` _is the answer to the_ <code>j<sup>th</sup></code> _query_.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/03/25/chrome_2021-03-25_22-34-16.png)
16+
17+
**Input:** points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]]
18+
19+
**Output:** [3,2,2]
20+
21+
**Explanation:** The points and circles are shown above. queries[0] is the green circle, queries[1] is the red circle, and queries[2] is the blue circle.
22+
23+
**Example 2:**
24+
25+
![](https://assets.leetcode.com/uploads/2021/03/25/chrome_2021-03-25_22-42-07.png)
26+
27+
**Input:** points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]
28+
29+
**Output:** [2,3,2,4]
30+
31+
**Explanation:** The points and circles are shown above. queries[0] is green, queries[1] is red, queries[2] is blue, and queries[3] is purple.
32+
33+
**Constraints:**
34+
35+
* `1 <= points.length <= 500`
36+
* `points[i].length == 2`
37+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 500</code>
38+
* `1 <= queries.length <= 500`
39+
* `queries[j].length == 3`
40+
* <code>0 <= x<sub>j</sub>, y<sub>j</sub> <= 500</code>
41+
* <code>1 <= r<sub>j</sub> <= 500</code>
42+
* All coordinates are integers.
43+
44+
**Follow up:** Could you find the answer for each query in better complexity than `O(n)`?
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g1801_1900.s1829_maximum_xor_for_each_query;
2+
3+
// #Medium #Array #Bit_Manipulation #Prefix_Sum
4+
// #2022_05_07_Time_7_ms_(20.94%)_Space_152.5_MB_(6.86%)
5+
6+
public class Solution {
7+
public int[] getMaximumXor(int[] nums, int maximumBit) {
8+
int[] result = new int[nums.length];
9+
long[] xOr = new long[nums.length];
10+
xOr[0] = nums[0];
11+
for (int i = 1; i < nums.length; i++) {
12+
xOr[i] ^= xOr[i - 1] ^ nums[i];
13+
}
14+
long maxNum = (long) Math.pow(2, maximumBit) - 1;
15+
for (int i = 0; i < nums.length; i++) {
16+
result[nums.length - i - 1] = (int) (maxNum ^ xOr[i]);
17+
}
18+
return result;
19+
}
20+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
1829\. Maximum XOR for Each Query
2+
3+
Medium
4+
5+
You are given a **sorted** array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the following query `n` **times**:
6+
7+
1. Find a non-negative integer <code>k < 2<sup>maximumBit</sup></code> such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k` is **maximized**. `k` is the answer to the <code>i<sup>th</sup></code> query.
8+
2. Remove the **last** element from the current array `nums`.
9+
10+
Return _an array_ `answer`_, where_ `answer[i]` _is the answer to the_ <code>i<sup>th</sup></code> _query_.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [0,1,1,3], maximumBit = 2
15+
16+
**Output:** [0,3,2,3]
17+
18+
**Explanation:**: The queries are answered as follows:
19+
20+
1<sup>st</sup> query: nums = [0,1,1,3], k = 0 since 0 XOR 1 XOR 1 XOR 3 XOR 0 = 3.
21+
22+
2<sup>nd</sup> query: nums = [0,1,1], k = 3 since 0 XOR 1 XOR 1 XOR 3 = 3.
23+
24+
3<sup>rd</sup> query: nums = [0,1], k = 2 since 0 XOR 1 XOR 2 = 3.
25+
26+
4<sup>th</sup> query: nums = [0], k = 3 since 0 XOR 3 = 3.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [2,3,4,7], maximumBit = 3
31+
32+
**Output:** [5,2,6,5]
33+
34+
**Explanation:**: The queries are answered as follows:
35+
36+
1<sup>st</sup> query: nums = [2,3,4,7], k = 5 since 2 XOR 3 XOR 4 XOR 7 XOR 5 = 7.
37+
38+
2<sup>nd</sup> query: nums = [2,3,4], k = 2 since 2 XOR 3 XOR 4 XOR 2 = 7.
39+
40+
3<sup>rd</sup> query: nums = [2,3], k = 6 since 2 XOR 3 XOR 6 = 7.
41+
42+
4<sup>th</sup> query: nums = [2], k = 5 since 2 XOR 5 = 7.
43+
44+
**Example 3:**
45+
46+
**Input:** nums = [0,1,2,2,5,7], maximumBit = 3
47+
48+
**Output:** [4,3,6,4,6,7]
49+
50+
**Constraints:**
51+
52+
* `nums.length == n`
53+
* <code>1 <= n <= 10<sup>5</sup></code>
54+
* `1 <= maximumBit <= 20`
55+
* <code>0 <= nums[i] < 2<sup>maximumBit</sup></code>
56+
* `nums` is sorted in **ascending** order.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g1801_1900.s1830_minimum_number_of_operations_to_make_string_sorted;
2+
3+
// #Hard #String #Math #Combinatorics #2022_05_07_Time_125_ms_(94.12%)_Space_42.7_MB_(64.71%)
4+
5+
public class Solution {
6+
public int makeStringSorted(String s) {
7+
int n = s.length();
8+
int[] count = new int[26];
9+
for (int i = 0; i < n; i++) {
10+
count[s.charAt(i) - 'a']++;
11+
}
12+
13+
long[] fact = new long[n + 1];
14+
fact[0] = 1;
15+
int mod = 1000000007;
16+
for (int i = 1; i <= n; i++) {
17+
fact[i] = (fact[i - 1] * i) % mod;
18+
}
19+
int len = n;
20+
long ans = 0;
21+
for (int i = 0; i < n; i++) {
22+
len--;
23+
int bound = s.charAt(i) - 'a';
24+
int first = 0;
25+
long rev = 1;
26+
for (int k = 0; k < 26; k++) {
27+
if (k < bound) {
28+
first += count[k];
29+
}
30+
rev = (rev * fact[count[k]]) % mod;
31+
}
32+
ans =
33+
ans % mod
34+
+ (((first * fact[len]) % mod)
35+
* (modPow(rev, (long) mod - 2, mod))
36+
% mod)
37+
% mod;
38+
ans = ans % mod;
39+
count[bound]--;
40+
}
41+
return (int) ans;
42+
}
43+
44+
private long modPow(long x, long n, int m) {
45+
long result = 1;
46+
while (n > 0) {
47+
if ((n & 1) != 0) {
48+
result = (result * x) % m;
49+
}
50+
x = (x * x) % m;
51+
n = n >> 1;
52+
}
53+
return result;
54+
}
55+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
1830\. Minimum Number of Operations to Make String Sorted
2+
3+
Hard
4+
5+
You are given a string `s` (**0-indexed**). You are asked to perform the following operation on `s` until you get a sorted string:
6+
7+
1. Find **the largest index** `i` such that `1 <= i < s.length` and `s[i] < s[i - 1]`.
8+
2. Find **the largest index** `j` such that `i <= j < s.length` and `s[k] < s[i - 1]` for all the possible values of `k` in the range `[i, j]` inclusive.
9+
3. Swap the two characters at indices `i - 1` and `j`.
10+
4. Reverse the suffix starting at index `i`.
11+
12+
Return _the number of operations needed to make the string sorted._ Since the answer can be too large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "cba"
17+
18+
**Output:** 5
19+
20+
**Explanation:** The simulation goes as follows: Operation 1: i=2, j=2. Swap s[1] and s[2] to get s="cab", then reverse the suffix starting at 2. Now, s="cab".
21+
22+
Operation 2: i=1, j=2. Swap s[0] and s[2] to get s="bac", then reverse the suffix starting at 1. Now, s="bca".
23+
24+
Operation 3: i=2, j=2. Swap s[1] and s[2] to get s="bac", then reverse the suffix starting at 2. Now, s="bac".
25+
26+
Operation 4: i=1, j=1. Swap s[0] and s[1] to get s="abc", then reverse the suffix starting at 1. Now, s="acb".
27+
28+
Operation 5: i=2, j=2. Swap s[1] and s[2] to get s="abc", then reverse the suffix starting at 2. Now, s="abc".
29+
30+
**Example 2:**
31+
32+
**Input:** s = "aabaa"
33+
34+
**Output:** 2
35+
36+
**Explanation:** The simulation goes as follows:
37+
38+
Operation 1: i=3, j=4. Swap s[2] and s[4] to get s="aaaab", then reverse the substring starting at 3. Now, s="aaaba".
39+
40+
Operation 2: i=4, j=4. Swap s[3] and s[4] to get s="aaaab", then reverse the substring starting at 4. Now, s="aaaab".
41+
42+
**Constraints:**
43+
44+
* `1 <= s.length <= 3000`
45+
* `s` consists only of lowercase English letters.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g1801_1900.s1832_check_if_the_sentence_is_pangram;
2+
3+
// #Easy #String #Hash_Table #2022_05_07_Time_3_ms_(41.29%)_Space_42.1_MB_(54.65%)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class Solution {
9+
public boolean checkIfPangram(String sentence) {
10+
Set<Character> alphabet = new HashSet<>();
11+
for (char c : sentence.toCharArray()) {
12+
alphabet.add(c);
13+
}
14+
return alphabet.size() == 26;
15+
}
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
1832\. Check if the Sentence Is Pangram
2+
3+
Easy
4+
5+
A **pangram** is a sentence where every letter of the English alphabet appears at least once.
6+
7+
Given a string `sentence` containing only lowercase English letters, return `true` _if_ `sentence` _is a **pangram**, or_ `false` _otherwise._
8+
9+
**Example 1:**
10+
11+
**Input:** sentence = "thequickbrownfoxjumpsoverthelazydog"
12+
13+
**Output:** true
14+
15+
**Explanation:** sentence contains at least one of every letter of the English alphabet.
16+
17+
**Example 2:**
18+
19+
**Input:** sentence = "leetcode"
20+
21+
**Output:** false
22+
23+
**Constraints:**
24+
25+
* `1 <= sentence.length <= 1000`
26+
* `sentence` consists of lowercase English letters.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g1801_1900.s1833_maximum_ice_cream_bars;
2+
3+
// #Medium #Array #Sorting #Greedy #2022_05_07_Time_39_ms_(84.49%)_Space_56.7_MB_(78.12%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int maxIceCream(int[] costs, int coins) {
9+
Arrays.sort(costs);
10+
int i = 0;
11+
int ans = 0;
12+
while (i < costs.length && costs[i] <= coins) {
13+
coins -= costs[i];
14+
i++;
15+
ans++;
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
1833\. Maximum Ice Cream Bars
2+
3+
Medium
4+
5+
It is a sweltering summer day, and a boy wants to buy some ice cream bars.
6+
7+
At the store, there are `n` ice cream bars. You are given an array `costs` of length `n`, where `costs[i]` is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has `coins` coins to spend, and he wants to buy as many ice cream bars as possible.
8+
9+
Return _the **maximum** number of ice cream bars the boy can buy with_ `coins` _coins._
10+
11+
**Note:** The boy can buy the ice cream bars in any order.
12+
13+
**Example 1:**
14+
15+
**Input:** costs = [1,3,2,4,1], coins = 7
16+
17+
**Output:** 4
18+
19+
**Explanation:** The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7.
20+
21+
**Example 2:**
22+
23+
**Input:** costs = [10,6,8,7,7,8], coins = 5
24+
25+
**Output:** 0
26+
27+
**Explanation:** The boy cannot afford any of the ice cream bars.
28+
29+
**Example 3:**
30+
31+
**Input:** costs = [1,6,3,1,2,5], coins = 20
32+
33+
**Output:** 6
34+
35+
**Explanation:** The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18.
36+
37+
**Constraints:**
38+
39+
* `costs.length == n`
40+
* <code>1 <= n <= 10<sup>5</sup></code>
41+
* <code>1 <= costs[i] <= 10<sup>5</sup></code>
42+
* <code>1 <= coins <= 10<sup>8</sup></code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g1801_1900.s1828_queries_on_number_of_points_inside_a_circle;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void countPoints() {
11+
assertThat(
12+
new Solution()
13+
.countPoints(
14+
new int[][] {{1, 3}, {3, 3}, {5, 3}, {2, 2}},
15+
new int[][] {{2, 3, 1}, {4, 3, 1}, {1, 1, 2}}),
16+
equalTo(new int[] {3, 2, 2}));
17+
}
18+
19+
@Test
20+
void countPoints2() {
21+
assertThat(
22+
new Solution()
23+
.countPoints(
24+
new int[][] {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}},
25+
new int[][] {{1, 2, 2}, {2, 2, 2}, {4, 3, 2}, {4, 3, 3}}),
26+
equalTo(new int[] {2, 3, 2, 4}));
27+
}
28+
}

0 commit comments

Comments
 (0)