Skip to content

Commit 8604837

Browse files
solves #2164: Sort Even and Odd Indices Independently in java
1 parent 5ac25c0 commit 8604837

3 files changed

+53
-2
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,8 @@
710710
| 2144 | [Minimum Cost of Buying Candies With Discount](https://leetcode.com/problems/minimum-cost-of-buying-candies-with-discount) | [![Java](assets/java.png)](src/MinimumCostOfBuyingCandiesWithDiscount.java) | |
711711
| 2148 | [Count Elements With Strictly Smaller and Greater Elements](https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements) | [![Java](assets/java.png)](src/CountElementsWithStrictlySmallerAndGreaterElements.java) | |
712712
| 2154 | [Keep Multiplying Found Values by Two](https://leetcode.com/problems/keep-multiplying-found-values-by-two) | [![Java](assets/java.png)](src/KeepMultiplyingFoundValuesByTwo.java) | |
713-
| 2160 | [Minimum Sum of Four Digit Number After Splitting Digits](https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits) | | |
714-
| 2164 | [Sort Even and Odd Indices Independently](https://leetcode.com/problems/sort-even-and-odd-indices-independently) | | |
713+
| 2160 | [Minimum Sum of Four Digit Number After Splitting Digits](https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits) | [![Java](assets/java.png)](src/MinimumSumOfFourDigitNumberAfterSplittingDigits.java) | |
714+
| 2164 | [Sort Even and Odd Indices Independently](https://leetcode.com/problems/sort-even-and-odd-indices-independently) | [![Java](assets/java.png)](src/SortEvenAndOddIndicesIndependently.java) | |
715715
| 2169 | [Count Operations to Obtain Zero](https://leetcode.com/problems/count-operations-to-obtain-zero) | | |
716716
| 2176 | [Count Equal and Divisible Pairs in an Array](https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array) | | |
717717
| 2180 | [Count Integers With Even Digit Sum](https://leetcode.com/problems/count-integers-with-even-digit-sum) | [![Java](assets/java.png)](src/CountIntegersWithEvenDigitSum.java) | |
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits
2+
// T: O(1)
3+
// S: O(1)
4+
5+
import java.util.Arrays;
6+
7+
public class MinimumSumOfFourDigitNumberAfterSplittingDigits {
8+
public int minimumSum(int num) {
9+
final int digit1 = num / 1000;
10+
final int digit2 = (num / 100) % 10;
11+
final int digit3 = (num / 10) % 10;
12+
final int digit4 = num % 10;
13+
14+
final int[] digits = { digit1, digit2, digit3, digit4 };
15+
Arrays.sort(digits);
16+
17+
return toNumber(digits[0], digits[2]) + toNumber(digits[1], digits[3]);
18+
}
19+
20+
private int toNumber(int a, int b) {
21+
return 10 * a + b;
22+
}
23+
}

Diff for: src/SortEvenAndOddIndicesIndependently.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://leetcode.com/problems/sort-even-and-odd-indices-independently
2+
// T: O(n log(n))
3+
// S: O(n)
4+
5+
import java.util.Arrays;
6+
import java.util.Comparator;
7+
8+
public class SortEvenAndOddIndicesIndependently {
9+
public int[] sortEvenOdd(int[] nums) {
10+
final int[] even = new int[(nums.length + 1) / 2];
11+
final Integer[] odd = new Integer[nums.length / 2];
12+
13+
for (int i = 0 ; i < nums.length ; i++) {
14+
if (i % 2 == 0) even[i / 2] = nums[i];
15+
else odd[i / 2] = nums[i];
16+
}
17+
18+
Arrays.sort(even);
19+
Arrays.sort(odd, Comparator.reverseOrder());
20+
21+
for (int i = 0 ; i < nums.length ; i++) {
22+
if (i % 2 == 0) nums[i] = even[i / 2];
23+
else nums[i] = odd[i / 2];
24+
}
25+
26+
return nums;
27+
}
28+
}

0 commit comments

Comments
 (0)