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

Commit6671c52

Browse files
authored
Added tasks 2591-2595
1 parentda6ac41 commit6671c52

File tree

15 files changed

+447
-0
lines changed

15 files changed

+447
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packageg2501_2600.s2591_distribute_money_to_maximum_children;
2+
3+
// #Easy #Math #Greedy #2023_08_23_Time_1_ms_(100.00%)_Space_40.7_MB_(71.47%)
4+
5+
publicclassSolution {
6+
publicintdistMoney(intmoney,intchildren) {
7+
if (money <children) {
8+
return -1;
9+
}
10+
if (money <children +7) {
11+
return0;
12+
}
13+
intnumEighters =0;
14+
money -=children;
15+
intpossibleEighters =children;
16+
while (money >=7 &&possibleEighters >0) {
17+
money -=7;
18+
++numEighters;
19+
--possibleEighters;
20+
}
21+
if ((money >0 &&possibleEighters ==0) || (money ==3 &&possibleEighters ==1)) {
22+
--numEighters;
23+
}
24+
returnnumEighters;
25+
}
26+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2591\. Distribute Money to Maximum Children
2+
3+
Easy
4+
5+
You are given an integer`money` denoting the amount of money (in dollars) that you have and another integer`children` denoting the number of children that you must distribute the money to.
6+
7+
You have to distribute the money according to the following rules:
8+
9+
* All money must be distributed.
10+
* Everyone must receive at least`1` dollar.
11+
* Nobody receives`4` dollars.
12+
13+
Return_the**maximum** number of children who may receive**exactly**_`8`_dollars if you distribute the money according to the aforementioned rules_. If there is no way to distribute the money, return`-1`.
14+
15+
**Example 1:**
16+
17+
**Input:** money = 20, children = 3
18+
19+
**Output:** 1
20+
21+
**Explanation:** The maximum number of children with 8 dollars will be 1. One of the ways to distribute the money is:
22+
- 8 dollars to the first child.
23+
- 9 dollars to the second child.
24+
- 3 dollars to the third child.
25+
26+
It can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1.
27+
28+
**Example 2:**
29+
30+
**Input:** money = 16, children = 2
31+
32+
**Output:** 2
33+
34+
**Explanation:** Each child can be given 8 dollars.
35+
36+
**Constraints:**
37+
38+
*`1 <= money <= 200`
39+
*`2 <= children <= 30`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
packageg2501_2600.s2592_maximize_greatness_of_an_array;
2+
3+
// #Medium #Array #Sorting #Greedy #Two_Pointers
4+
// #2023_08_23_Time_8_ms_(100.00%)_Space_57.2_MB_(41.49%)
5+
6+
importjava.util.Arrays;
7+
8+
publicclassSolution {
9+
publicintmaximizeGreatness(int[]nums) {
10+
Arrays.sort(nums);
11+
intperm =0;
12+
for (intnum :nums) {
13+
if (nums[perm] <num) {
14+
perm++;
15+
}
16+
}
17+
returnperm;
18+
}
19+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2592\. Maximize Greatness of an Array
2+
3+
Medium
4+
5+
You are given a 0-indexed integer array`nums`. You are allowed to permute`nums` into a new array`perm` of your choosing.
6+
7+
We define the**greatness** of`nums` be the number of indices`0 <= i < nums.length` for which`perm[i] > nums[i]`.
8+
9+
Return_the**maximum** possible greatness you can achieve after permuting_`nums`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums =[1,3,5,2,1,3,1]
14+
15+
**Output:** 4
16+
17+
**Explanation:** One of the optimal rearrangements is perm =[2,5,1,3,3,1,1]. At indices = 0, 1, 3, and 4, perm[i] > nums[i]. Hence, we return 4.
18+
19+
**Example 2:**
20+
21+
**Input:** nums =[1,2,3,4]
22+
23+
**Output:** 3
24+
25+
**Explanation:** We can prove the optimal perm is[2,3,4,1]. At indices = 0, 1, and 2, perm[i] > nums[i]. Hence, we return 3.
26+
27+
**Constraints:**
28+
29+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
30+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
packageg2501_2600.s2593_find_score_of_an_array_after_marking_all_elements;
2+
3+
// #Medium #Array #Sorting #Heap_Priority_Queue #Simulation
4+
// #2023_08_23_Time_159_ms_(96.76%)_Space_56.2_MB_(68.11%)
5+
6+
importjava.util.PriorityQueue;
7+
8+
publicclassSolution {
9+
privatestaticclassPoint {
10+
intidx;
11+
intval;
12+
13+
Point(intidx,intval) {
14+
this.idx =idx;
15+
this.val =val;
16+
}
17+
}
18+
19+
publiclongfindScore(int[]arr) {
20+
PriorityQueue<Point>pq =
21+
newPriorityQueue<>((a,b) ->b.val ==a.val ?a.idx -b.idx :a.val -b.val);
22+
intn =arr.length;
23+
boolean[]visited =newboolean[n];
24+
for (inti =0;i <n;i++) {
25+
pq.add(newPoint(i,arr[i]));
26+
}
27+
longans =0;
28+
while (!pq.isEmpty()) {
29+
Pointp =pq.remove();
30+
intidx =p.idx;
31+
if (!visited[idx]) {
32+
ans +=p.val;
33+
visited[idx] =true;
34+
if (idx -1 >=0 && !visited[idx -1]) {
35+
visited[idx -1] =true;
36+
}
37+
if (idx +1 <n && !visited[idx +1]) {
38+
visited[idx +1] =true;
39+
}
40+
}
41+
}
42+
returnans;
43+
}
44+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2593\. Find Score of an Array After Marking All Elements
2+
3+
Medium
4+
5+
You are given an array`nums` consisting of positive integers.
6+
7+
Starting with`score = 0`, apply the following algorithm:
8+
9+
* Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.
10+
* Add the value of the chosen integer to`score`.
11+
* Mark**the chosen element and its two adjacent elements if they exist**.
12+
* Repeat until all the array elements are marked.
13+
14+
Return_the score you get after applying the above algorithm_.
15+
16+
**Example 1:**
17+
18+
**Input:** nums =[2,1,3,4,5,2]
19+
20+
**Output:** 7
21+
22+
**Explanation:** We mark the elements as follows:
23+
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements:[<ins>2</ins>,<ins>1</ins>,<ins>3</ins>,4,5,2].
24+
- 2 is the smallest unmarked element, so we mark it and its left adjacent element:[<ins>2</ins>,<ins>1</ins>,<ins>3</ins>,4,<ins>5</ins>,<ins>2</ins>].
25+
- 4 is the only remaining unmarked element, so we mark it:[<ins>2</ins>,<ins>1</ins>,<ins>3</ins>,<ins>4</ins>,<ins>5</ins>,<ins>2</ins>]. Our score is 1 + 2 + 4 = 7.
26+
27+
**Example 2:**
28+
29+
**Input:** nums =[2,3,5,1,3,2]
30+
31+
**Output:** 5
32+
33+
**Explanation:** We mark the elements as follows:
34+
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements:[2,3,<ins>5</ins>,<ins>1</ins>,<ins>3</ins>,2].
35+
- 2 is the smallest unmarked element, since there are two of them, we choose the left-most one, so we mark the one at index 0 and its right adjacent element:[<ins>2</ins>,<ins>3</ins>,<ins>5</ins>,<ins>1</ins>,<ins>3</ins>,2].
36+
- 2 is the only remaining unmarked element, so we mark it:[<ins>2</ins>,<ins>3</ins>,<ins>5</ins>,<ins>1</ins>,<ins>3</ins>,<ins>2</ins>]. Our score is 1 + 2 + 2 = 5.
37+
38+
**Constraints:**
39+
40+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
41+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
packageg2501_2600.s2594_minimum_time_to_repair_cars;
2+
3+
// #Medium #Array #Binary_Search #2023_08_23_Time_15_ms_(97.28%)_Space_53.8_MB_(85.07%)
4+
5+
publicclassSolution {
6+
publiclongrepairCars(int[]ranks,intcars) {
7+
longlow =0;
8+
longhigh =100000000000000L;
9+
longpivot;
10+
while (low <=high) {
11+
pivot =low + (high -low) /2;
12+
if (canRepairAllCars(ranks,cars,pivot)) {
13+
high =pivot -1;
14+
}else {
15+
low =pivot +1;
16+
}
17+
}
18+
returnlow;
19+
}
20+
21+
privatebooleancanRepairAllCars(int[]ranks,intcars,longtotalMinutes) {
22+
intrepairedCars =0;
23+
for (inti =0;i <ranks.length &&repairedCars <cars;i++) {
24+
repairedCars += (int)Math.sqrt((double)totalMinutes /ranks[i]);
25+
}
26+
returnrepairedCars >=cars;
27+
}
28+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2594\. Minimum Time to Repair Cars
2+
3+
Medium
4+
5+
You are given an integer array`ranks` representing the**ranks** of some mechanics. ranks<sub>i</sub> is the rank of the i<sup>th</sup> mechanic. A mechanic with a rank`r` can repair n cars in <code>r * n<sup>2</sup></code> minutes.
6+
7+
You are also given an integer`cars` representing the total number of cars waiting in the garage to be repaired.
8+
9+
Return_the**minimum** time taken to repair all the cars._
10+
11+
**Note:** All the mechanics can repair the cars simultaneously.
12+
13+
**Example 1:**
14+
15+
**Input:** ranks =[4,2,3,1], cars = 10
16+
17+
**Output:** 16
18+
19+
**Explanation:**
20+
21+
- The first mechanic will repair two cars. The time required is 4\* 2\* 2 = 16 minutes.
22+
23+
- The second mechanic will repair two cars. The time required is 2\* 2\* 2 = 8 minutes.
24+
25+
- The third mechanic will repair two cars. The time required is 3\* 2\* 2 = 12 minutes.
26+
27+
- The fourth mechanic will repair four cars. The time required is 1\* 4\* 4 = 16 minutes.
28+
29+
It can be proved that the cars cannot be repaired in less than 16 minutes.
30+
31+
**Example 2:**
32+
33+
**Input:** ranks =[5,1,8], cars = 6
34+
35+
**Output:** 16
36+
37+
**Explanation:**
38+
39+
- The first mechanic will repair one car. The time required is 5\* 1\* 1 = 5 minutes.
40+
41+
- The second mechanic will repair four cars. The time required is 1\* 4\* 4 = 16 minutes.
42+
43+
- The third mechanic will repair one car. The time required is 8\* 1\* 1 = 8 minutes.
44+
45+
It can be proved that the cars cannot be repaired in less than 16 minutes.
46+
47+
**Constraints:**
48+
49+
* <code>1 <= ranks.length <= 10<sup>5</sup></code>
50+
*`1 <= ranks[i] <= 100`
51+
* <code>1 <= cars <= 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.s2595_number_of_even_and_odd_bits;
2+
3+
// #Easy #Bit_Manipulation #2023_08_23_Time_1_ms_(100.00%)_Space_41.3_MB_(82.54%)
4+
5+
publicclassSolution {
6+
publicint[]evenOddBit(intn) {
7+
int[]res =newint[2];
8+
inteven =0;
9+
intodd =0;
10+
for (inti =0;i <32;i++) {
11+
if (i %2 ==0) {
12+
if ((n &1) ==1) {
13+
even++;
14+
}
15+
}else {
16+
if ((n &1) ==1) {
17+
odd++;
18+
}
19+
}
20+
n >>=1;
21+
}
22+
res[0] =even;
23+
res[1] =odd;
24+
returnres;
25+
}
26+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2595\. Number of Even and Odd Bits
2+
3+
Easy
4+
5+
You are given a**positive** integer`n`.
6+
7+
Let`even` denote the number of even indices in the binary representation of`n` (**0-indexed**) with value`1`.
8+
9+
Let`odd` denote the number of odd indices in the binary representation of`n` (**0-indexed**) with value`1`.
10+
11+
Return_an integer array_`answer`_where_`answer = [even, odd]`.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 17
16+
17+
**Output:**[2,0]
18+
19+
**Explanation:**
20+
21+
The binary representation of 17 is 10001.
22+
23+
It contains 1 on the 0<sup>th</sup> and 4<sup>th</sup> indices.
24+
25+
There are 2 even and 0 odd indices.
26+
27+
**Example 2:**
28+
29+
**Input:** n = 2
30+
31+
**Output:**[0,1]
32+
33+
**Explanation:**
34+
35+
The binary representation of 2 is 10.
36+
37+
It contains 1 on the 1<sup>st</sup> index.
38+
39+
There are 0 even and 1 odd indices.
40+
41+
**Constraints:**
42+
43+
*`1 <= n <= 1000`
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
packageg2501_2600.s2591_distribute_money_to_maximum_children;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voiddistMoney() {
11+
assertThat(newSolution().distMoney(20,3),equalTo(1));
12+
}
13+
14+
@Test
15+
voiddistMoney2() {
16+
assertThat(newSolution().distMoney(16,2),equalTo(2));
17+
}
18+
19+
@Test
20+
voiddistMoney3() {
21+
assertThat(newSolution().distMoney(1,2),equalTo(-1));
22+
}
23+
24+
@Test
25+
voiddistMoney4() {
26+
assertThat(newSolution().distMoney(2,1),equalTo(0));
27+
}
28+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp