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

Commit0207b47

Browse files
authored
Added tasks 2523, 2525, 2526, 2527, 2528
1 parent09bfbe3 commit0207b47

File tree

16 files changed

+513
-1
lines changed

16 files changed

+513
-1
lines changed

‎README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,11 @@ implementation 'com.github.javadev:leetcode-in-java:1.20'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2528 |[Maximize the Minimum Powered City](src/main/java/g2501_2600/s2528_maximize_the_minimum_powered_city/Solution.java)| Hard | Array, Greedy, Binary_Search, Prefix_Sum, Sliding_Window, Queue | 51 | 77.59
1852+
| 2527 |[Find Xor-Beauty of Array](src/main/java/g2501_2600/s2527_find_xor_beauty_of_array/Solution.java)| Medium | Array, Math, Bit_Manipulation | 1 | 100.00
1853+
| 2526 |[Find Consecutive Integers from a Data Stream](src/main/java/g2501_2600/s2526_find_consecutive_integers_from_a_data_stream/DataStream.java)| Medium | Hash_Table, Design, Counting, Queue, Data_Stream | 32 | 94.65
1854+
| 2525 |[Categorize Box According to Criteria](src/main/java/g2501_2600/s2525_categorize_box_according_to_criteria/Solution.java)| Easy | Math | 0 | 100.00
1855+
| 2523 |[Closest Prime Numbers in Range](src/main/java/g2501_2600/s2523_closest_prime_numbers_in_range/Solution.java)| Medium | Math, Number_Theory | 1 | 100.00
18511856
| 2522 |[Partition String Into Substrings With Values at Most K](src/main/java/g2501_2600/s2522_partition_string_into_substrings_with_values_at_most_k/Solution.java)| Medium | String, Dynamic_Programming, Greedy | 6 | 84.66
18521857
| 2521 |[Distinct Prime Factors of Product of Array](src/main/java/g2501_2600/s2521_distinct_prime_factors_of_product_of_array/Solution.java)| Medium | Array, Hash_Table, Math, Number_Theory | 2 | 100.00
18531858
| 2520 |[Count the Digits That Divide a Number](src/main/java/g2501_2600/s2520_count_the_digits_that_divide_a_number/Solution.java)| Easy | Math | 0 | 100.00
@@ -1860,7 +1865,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.20'
18601865
| 2512 |[Reward Top K Students](src/main/java/g2501_2600/s2512_reward_top_k_students/Solution.java)| Medium | Array, String, Hash_Table, Sorting, Heap_Priority_Queue | 72 | 94.76
18611866
| 2511 |[Maximum Enemy Forts That Can Be Captured](src/main/java/g2501_2600/s2511_maximum_enemy_forts_that_can_be_captured/Solution.java)| Easy | Array, Two_Pointers | 0 | 100.00
18621867
| 2509 |[Cycle Length Queries in a Tree](src/main/java/g2501_2600/s2509_cycle_length_queries_in_a_tree/Solution.java)| Hard | Tree, Binary_Tree | 12 | 99.26
1863-
| 2508 |[Add Edges to Make Degrees of All Nodes Even](src/main/java/g2501_2600/s2508_add_edges_to_make_degrees_of_all_nodes_even/Solution.java)| Hard | Hash_Table, Graph |33 |100.00
1868+
| 2508 |[Add Edges to Make Degrees of All Nodes Even](src/main/java/g2501_2600/s2508_add_edges_to_make_degrees_of_all_nodes_even/Solution.java)| Hard | Hash_Table, Graph |36 |95.00
18641869
| 2507 |[Smallest Value After Replacing With Sum of Prime Factors](src/main/java/g2501_2600/s2507_smallest_value_after_replacing_with_sum_of_prime_factors/Solution.java)| Medium | Math, Number_Theory | 1 | 84.27
18651870
| 2506 |[Count Pairs Of Similar Strings](src/main/java/g2501_2600/s2506_count_pairs_of_similar_strings/Solution.java)| Easy | Array, String, Hash_Table | 6 | 85.15
18661871
| 2503 |[Maximum Number of Points From Grid Queries](src/main/java/g2501_2600/s2503_maximum_number_of_points_from_grid_queries/Solution.java)| Hard | Array, Sorting, Breadth_First_Search, Heap_Priority_Queue, Union_Find | 47 | 96.38
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
packageg2501_2600.s2523_closest_prime_numbers_in_range;
2+
3+
// #Medium #Math #Number_Theory #2023_04_19_Time_1_ms_(100.00%)_Space_39.9_MB_(97.74%)
4+
5+
publicclassSolution {
6+
publicint[]closestPrimes(intleft,intright) {
7+
intdiff = -1;
8+
intx = -1;
9+
inty = -1;
10+
intprev = -1;
11+
for (inti =left;i <=right;i++) {
12+
booleanisPrime =isPrime(i);
13+
if (isPrime) {
14+
if (prev != -1) {
15+
intd =i -prev;
16+
if (diff == -1 ||d <diff) {
17+
diff =d;
18+
x =prev;
19+
y =i;
20+
if (diff <=2) {
21+
returnnewint[] {x,y};
22+
}
23+
}
24+
}
25+
prev =i;
26+
}
27+
}
28+
returnnewint[] {x,y};
29+
}
30+
31+
privatebooleanisPrime(intx) {
32+
if (x ==1) {
33+
returnfalse;
34+
}
35+
doublesqrt =Math.sqrt(x);
36+
for (inti =2;i <=sqrt;i++) {
37+
if (x %i ==0) {
38+
returnfalse;
39+
}
40+
}
41+
returntrue;
42+
}
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2523\. Closest Prime Numbers in Range
2+
3+
Medium
4+
5+
Given two positive integers`left` and`right`, find the two integers`num1` and`num2` such that:
6+
7+
*`left <= nums1 < nums2 <= right` .
8+
*`nums1` and`nums2` are both**prime** numbers.
9+
*`nums2 - nums1` is the**minimum** amongst all other pairs satisfying the above conditions.
10+
11+
Return_the positive integer array_`ans = [nums1, nums2]`._If there are multiple pairs satisfying these conditions, return the one with the minimum_`nums1`_value or_`[-1, -1]`_if such numbers do not exist._
12+
13+
A number greater than`1` is called**prime** if it is only divisible by`1` and itself.
14+
15+
**Example 1:**
16+
17+
**Input:** left = 10, right = 19
18+
19+
**Output:**[11,13]
20+
21+
**Explanation:** The prime numbers between 10 and 19 are 11, 13, 17, and 19.
22+
23+
The closest gap between any pair is 2, which can be achieved by[11,13] or[17,19].
24+
25+
Since 11 is smaller than 17, we return the first pair.
26+
27+
**Example 2:**
28+
29+
**Input:** left = 4, right = 6
30+
31+
**Output:**[-1,-1]
32+
33+
**Explanation:** There exists only one prime number in the given range, so the conditions cannot be satisfied.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= left <= right <= 10<sup>6</sup></code>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packageg2501_2600.s2525_categorize_box_according_to_criteria;
2+
3+
// #Easy #Math #2023_04_19_Time_0_ms_(100.00%)_Space_40.1_MB_(36.65%)
4+
5+
publicclassSolution {
6+
publicStringcategorizeBox(intlength,intwidth,intheight,intmass) {
7+
longvol = (long)length *width *height;
8+
booleanb =false;
9+
booleanh =false;
10+
if (length >=10000 ||width >=10000 ||height >=10000 ||vol >=1000000000) {
11+
b =true;
12+
}
13+
if (mass >=100) {
14+
h =true;
15+
}
16+
if (b &&h) {
17+
return"Both";
18+
}elseif (!b && !h) {
19+
return"Neither";
20+
}elseif (b) {
21+
return"Bulky";
22+
}else {
23+
return"Heavy";
24+
}
25+
}
26+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2525\. Categorize Box According to Criteria
2+
3+
Easy
4+
5+
Given four integers`length`,`width`,`height`, and`mass`, representing the dimensions and mass of a box, respectively, return_a string representing the**category** of the box_.
6+
7+
* The box is`"Bulky"` if:
8+
***Any** of the dimensions of the box is greater or equal to <code>10<sup>4</sup></code>.
9+
* Or, the**volume** of the box is greater or equal to <code>10<sup>9</sup></code>.
10+
* If the mass of the box is greater or equal to`100`, it is`"Heavy".`
11+
* If the box is both`"Bulky"` and`"Heavy"`, then its category is`"Both"`.
12+
* If the box is neither`"Bulky"` nor`"Heavy"`, then its category is`"Neither"`.
13+
* If the box is`"Bulky"` but not`"Heavy"`, then its category is`"Bulky"`.
14+
* If the box is`"Heavy"` but not`"Bulky"`, then its category is`"Heavy"`.
15+
16+
**Note** that the volume of the box is the product of its length, width and height.
17+
18+
**Example 1:**
19+
20+
**Input:** length = 1000, width = 35, height = 700, mass = 300
21+
22+
**Output:** "Heavy"
23+
24+
**Explanation:**
25+
26+
None of the dimensions of the box is greater or equal to 10<sup>4</sup>.
27+
28+
Its volume = 24500000 <= 10<sup>9</sup>. So it cannot be categorized as "Bulky".
29+
30+
However mass >= 100, so the box is "Heavy".
31+
32+
Since the box is not "Bulky" but "Heavy", we return "Heavy".
33+
34+
**Example 2:**
35+
36+
**Input:** length = 200, width = 50, height = 800, mass = 50
37+
38+
**Output:** "Neither"
39+
40+
**Explanation:**
41+
42+
None of the dimensions of the box is greater or equal to 10<sup>4</sup>.
43+
44+
Its volume = 8\* 10<sup>6</sup> <= 10<sup>9</sup>. So it cannot be categorized as "Bulky".
45+
46+
Its mass is also less than 100, so it cannot be categorized as "Heavy" either.
47+
48+
Since its neither of the two above categories, we return "Neither".
49+
50+
**Constraints:**
51+
52+
* <code>1 <= length, width, height <= 10<sup>5</sup></code>
53+
* <code>1 <= mass <= 10<sup>3</sup></code>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packageg2501_2600.s2526_find_consecutive_integers_from_a_data_stream;
2+
3+
// #Medium #Hash_Table #Design #Counting #Queue #Data_Stream
4+
// #2023_04_19_Time_32_ms_(94.65%)_Space_80.2_MB_(39.13%)
5+
6+
publicclassDataStream {
7+
privatefinalintvalue;
8+
privatefinalintk;
9+
privateintcount;
10+
11+
publicDataStream(intvalue,intk) {
12+
this.value =value;
13+
this.k =k;
14+
}
15+
16+
publicbooleanconsec(intnum) {
17+
count =num ==value ?count +1 :0;
18+
returncount >=k;
19+
}
20+
}
21+
22+
/*
23+
* Your DataStream object will be instantiated and called as such:
24+
* DataStream obj = new DataStream(value, k);
25+
* boolean param_1 = obj.consec(num);
26+
*/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2526\. Find Consecutive Integers from a Data Stream
2+
3+
Medium
4+
5+
For a stream of integers, implement a data structure that checks if the last`k` integers parsed in the stream are**equal** to`value`.
6+
7+
Implement the**DataStream** class:
8+
9+
*`DataStream(int value, int k)` Initializes the object with an empty integer stream and the two integers`value` and`k`.
10+
*`boolean consec(int num)` Adds`num` to the stream of integers. Returns`true` if the last`k` integers are equal to`value`, and`false` otherwise. If there are less than`k` integers, the condition does not hold true, so returns`false`.
11+
12+
**Example 1:**
13+
14+
**Input**["DataStream", "consec", "consec", "consec", "consec"]
15+
16+
[[4, 3],[4],[4],[4],[3]]
17+
18+
**Output:**[null, false, false, true, false]
19+
20+
**Explanation:**
21+
22+
DataStream dataStream = new DataStream(4, 3); //value = 4, k = 3
23+
dataStream.consec(4); // Only 1 integer is parsed, so returns False.
24+
dataStream.consec(4); // Only 2 integers are parsed.
25+
// Since 2 is less than k, returns False.
26+
dataStream.consec(4); // The 3 integers parsed are all equal to value, so returns True.
27+
dataStream.consec(3); // The last k integers parsed in the stream are [4,4,3].
28+
// Since 3 is not equal to value, it returns False.
29+
30+
**Constraints:**
31+
32+
* <code>1 <= value, num <= 10<sup>9</sup></code>
33+
* <code>1 <= k <= 10<sup>5</sup></code>
34+
* At most <code>10<sup>5</sup></code> calls will be made to`consec`.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
packageg2501_2600.s2527_find_xor_beauty_of_array;
2+
3+
// #Medium #Array #Math #Bit_Manipulation #2023_04_19_Time_1_ms_(100.00%)_Space_59.1_MB_(62.11%)
4+
5+
publicclassSolution {
6+
publicintxorBeauty(int[]arr) {
7+
inti =1;
8+
while (i <arr.length) {
9+
arr[0] ^=arr[i];
10+
i++;
11+
}
12+
returnarr[0];
13+
}
14+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2527\. Find Xor-Beauty of Array
2+
3+
Medium
4+
5+
You are given a**0-indexed** integer array`nums`.
6+
7+
The**effective value** of three indices`i`,`j`, and`k` is defined as`((nums[i] | nums[j]) & nums[k])`.
8+
9+
The**xor-beauty** of the array is the XORing of**the effective values of all the possible triplets** of indices`(i, j, k)` where`0 <= i, j, k < n`.
10+
11+
Return_the xor-beauty of_`nums`.
12+
13+
**Note** that:
14+
15+
*`val1 | val2` is bitwise OR of`val1` and`val2`.
16+
*`val1 & val2` is bitwise AND of`val1` and`val2`.
17+
18+
**Example 1:**
19+
20+
**Input:** nums =[1,4]
21+
22+
**Output:** 5
23+
24+
**Explanation:**
25+
26+
The triplets and their corresponding effective values are listed below:
27+
28+
- (0,0,0) with effective value ((1 | 1) & 1) = 1
29+
30+
- (0,0,1) with effective value ((1 | 1) & 4) = 0
31+
32+
- (0,1,0) with effective value ((1 | 4) & 1) = 1
33+
34+
- (0,1,1) with effective value ((1 | 4) & 4) = 4
35+
36+
- (1,0,0) with effective value ((4 | 1) & 1) = 1
37+
38+
- (1,0,1) with effective value ((4 | 1) & 4) = 4
39+
40+
- (1,1,0) with effective value ((4 | 4) & 1) = 0
41+
42+
- (1,1,1) with effective value ((4 | 4) & 4) = 4
43+
44+
Xor-beauty of array will be bitwise XOR of all beauties = 1 ^ 0 ^ 1 ^ 4 ^ 1 ^ 4 ^ 0 ^ 4 = 5.
45+
46+
**Example 2:**
47+
48+
**Input:** nums =[15,45,20,2,34,35,5,44,32,30]
49+
50+
**Output:** 34
51+
52+
**Explanation:**`The xor-beauty of the given array is 34.`
53+
54+
**Constraints:**
55+
56+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
57+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
packageg2501_2600.s2528_maximize_the_minimum_powered_city;
2+
3+
// #Hard #Array #Greedy #Binary_Search #Prefix_Sum #Sliding_Window #Queue
4+
// #2023_04_19_Time_51_ms_(77.59%)_Space_60_MB_(5.17%)
5+
6+
publicclassSolution {
7+
privatebooleancanIBeTheMinimum(long[]power,longminimum,intk,intr) {
8+
intn =power.length;
9+
long[]extraPower =newlong[n];
10+
for (inti =0;i <n;i++) {
11+
if (i >0) {
12+
extraPower[i] +=extraPower[i -1];
13+
}
14+
longcurPower =power[i] +extraPower[i];
15+
longreq =minimum -curPower;
16+
if (req <=0) {
17+
continue;
18+
}
19+
if (req >k) {
20+
returnfalse;
21+
}
22+
k -=req;
23+
extraPower[i] += (req);
24+
if (i +2 *r +1 <n) {
25+
extraPower[i +2 *r +1] -= (req);
26+
}
27+
}
28+
returntrue;
29+
}
30+
31+
privatelong[]calculatePowerArray(int[]stations,intr) {
32+
intn =stations.length;
33+
long[]preSum =newlong[n];
34+
for (inti =0;i <n;i++) {
35+
intst =i -r;
36+
intlast =i +r +1;
37+
if (st <0) {
38+
st =0;
39+
}
40+
preSum[st] +=stations[i];
41+
if (last <n) {
42+
preSum[last] -=stations[i];
43+
}
44+
}
45+
for (inti =1;i <n;i++) {
46+
preSum[i] +=preSum[i -1];
47+
}
48+
returnpreSum;
49+
}
50+
51+
publiclongmaxPower(int[]stations,intr,intk) {
52+
longmin =0;
53+
longsum = (long)Math.pow(10,10) + (long)Math.pow(10,9);
54+
long[]power =calculatePowerArray(stations,r);
55+
longans = -1;
56+
while (min <=sum) {
57+
longmid = (min +sum) >>1;
58+
if (canIBeTheMinimum(power,mid,k,r)) {
59+
ans =mid;
60+
min =mid +1;
61+
}else {
62+
sum =mid -1;
63+
}
64+
}
65+
returnans;
66+
}
67+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp