Skip to content

Commit 7c2b785

Browse files
add 2091
1 parent 5e8f1e5 commit 7c2b785

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|2091|[Removing Minimum and Maximum From Array](https://leetcode.com/problems/removing-minimum-and-maximum-from-array/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2091.java) ||Medium||
1112
|2090|[K Radius Subarray Averages](https://leetcode.com/problems/k-radius-subarray-averages/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2090.java) ||Medium||
1213
|2089|[Find Target Indices After Sorting Array](https://leetcode.com/problems/find-target-indices-after-sorting-array/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2089.java) ||Easy||
1314
|2086|[Minimum Number of Buckets Required to Collect Rainwater from Houses](https://leetcode.com/problems/minimum-number-of-buckets-required-to-collect-rainwater-from-houses/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2086.java) ||Medium||
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2091 {
4+
public static class Solution1 {
5+
public int minimumDeletions(int[] nums) {
6+
int minIndex = 0;
7+
int maxIndex = 0;
8+
int min = nums[0];
9+
int max = nums[0];
10+
for (int i = 1; i < nums.length; i++) {
11+
if (nums[i] > max) {
12+
max = nums[i];
13+
maxIndex = i;
14+
}
15+
if (nums[i] < min) {
16+
min = nums[i];
17+
minIndex = i;
18+
}
19+
}
20+
int minDeletions = 0;
21+
if (nums.length == 1) {
22+
return 1;
23+
}
24+
int len = nums.length - 1;
25+
int firstIndex = maxIndex > minIndex ? minIndex : maxIndex;
26+
int secondIndex = maxIndex == firstIndex ? minIndex : maxIndex;
27+
int firstDistance = firstIndex;
28+
int lastDistance = len - secondIndex;
29+
int betweenDistance = secondIndex - firstIndex;
30+
if (firstDistance < lastDistance) {
31+
minDeletions += firstDistance;
32+
minDeletions++;
33+
if (betweenDistance == 1 || lastDistance == 0) {
34+
minDeletions++;
35+
return minDeletions;
36+
}
37+
if (betweenDistance <= lastDistance) {
38+
minDeletions += betweenDistance;
39+
} else {
40+
minDeletions += lastDistance;
41+
minDeletions++;
42+
}
43+
} else {
44+
minDeletions += lastDistance;
45+
minDeletions++;
46+
if (betweenDistance == 1 || firstDistance == 0) {
47+
minDeletions++;
48+
return minDeletions;
49+
}
50+
if (betweenDistance <= firstDistance) {
51+
minDeletions += betweenDistance;
52+
} else {
53+
minDeletions += firstDistance;
54+
minDeletions++;
55+
}
56+
}
57+
return minDeletions;
58+
}
59+
}
60+
}

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