Skip to content

Commit 33b0816

Browse files
Added tasks 2164, 2165.
1 parent c07f240 commit 33b0816

File tree

6 files changed

+193
-0
lines changed

6 files changed

+193
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g2101_2200.s2164_sort_even_and_odd_indices_independently;
2+
3+
// #Easy #Array #Sorting #2022_06_05_Time_2_ms_(97.22%)_Space_42.7_MB_(82.78%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int[] sortEvenOdd(int[] nums) {
9+
int[] odd = new int[nums.length / 2];
10+
int[] even = new int[(nums.length + 1) / 2];
11+
int o = 0;
12+
int e = 0;
13+
for (int i = 0; i < nums.length; i++) {
14+
if (i % 2 == 0) {
15+
even[e] = nums[i];
16+
++e;
17+
} else {
18+
odd[o] = nums[i];
19+
++o;
20+
}
21+
}
22+
Arrays.sort(odd);
23+
Arrays.sort(even);
24+
e = 0;
25+
o = odd.length - 1;
26+
for (int i = 0; i < nums.length; i++) {
27+
if (i % 2 == 0) {
28+
nums[i] = even[e];
29+
++e;
30+
} else {
31+
nums[i] = odd[o];
32+
--o;
33+
}
34+
}
35+
return nums;
36+
}
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2164\. Sort Even and Odd Indices Independently
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums`. Rearrange the values of `nums` according to the following rules:
6+
7+
1. Sort the values at **odd indices** of `nums` in **non-increasing** order.
8+
* For example, if <code>nums = [4,**1**,2,**3**]</code> before this step, it becomes <code>[4,**3**,2,**1**]</code> after. The values at odd indices `1` and `3` are sorted in non-increasing order.
9+
2. Sort the values at **even indices** of `nums` in **non-decreasing** order.
10+
* For example, if <code>nums = [**4**,1,**2**,3]</code> before this step, it becomes <code>[**2**,1,**4**,3]</code> after. The values at even indices `0` and `2` are sorted in non-decreasing order.
11+
12+
Return _the array formed after rearranging the values of_ `nums`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [4,1,2,3]
17+
18+
**Output:** [2,3,4,1]
19+
20+
**Explanation:**
21+
22+
First, we sort the values present at odd indices (1 and 3) in non-increasing order.
23+
24+
So, nums changes from [4,**1**,2,**3**] to [4,**3**,2,**1**].
25+
26+
Next, we sort the values present at even indices (0 and 2) in non-decreasing order. So, nums changes from [**4**,1,**2**,3] to [**2**,3,**4**,1].
27+
28+
Thus, the array formed after rearranging the values is [2,3,4,1].
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [2,1]
33+
34+
**Output:** [2,1]
35+
36+
**Explanation:** Since there is exactly one odd index and one even index, no rearrangement of values takes place.
37+
38+
The resultant array formed is [2,1], which is the same as the initial array.
39+
40+
**Constraints:**
41+
42+
* `1 <= nums.length <= 100`
43+
* `1 <= nums[i] <= 100`
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g2101_2200.s2165_smallest_value_of_the_rearranged_number;
2+
3+
// #Medium #Math #Sorting #2022_06_05_Time_1_ms_(100.00%)_Space_41.6_MB_(33.45%)
4+
5+
public class Solution {
6+
public long smallestNumber(long num) {
7+
int[] count = new int[10];
8+
long tempNum = 0;
9+
if (num > 0) {
10+
tempNum = num;
11+
} else {
12+
tempNum = num * -1;
13+
}
14+
int min = 10;
15+
while (tempNum > 0) {
16+
int rem = (int) (tempNum % 10);
17+
if (rem != 0) {
18+
min = Math.min(min, rem);
19+
}
20+
count[rem]++;
21+
tempNum = tempNum / 10;
22+
}
23+
long output = 0;
24+
if (num > 0) {
25+
output = output * 10 + min;
26+
count[min]--;
27+
for (int i = 0; i < 10; i++) {
28+
for (int j = 0; j < count[i]; j++) {
29+
output = output * 10 + i;
30+
}
31+
}
32+
} else {
33+
for (int i = 9; i >= 0; i--) {
34+
for (int j = 0; j < count[i]; j++) {
35+
output = output * 10 + i;
36+
}
37+
}
38+
output = output * -1;
39+
}
40+
return output;
41+
}
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2165\. Smallest Value of the Rearranged Number
2+
3+
Medium
4+
5+
You are given an integer `num.` **Rearrange** the digits of `num` such that its value is **minimized** and it does not contain **any** leading zeros.
6+
7+
Return _the rearranged number with minimal value_.
8+
9+
Note that the sign of the number does not change after rearranging the digits.
10+
11+
**Example 1:**
12+
13+
**Input:** num = 310
14+
15+
**Output:** 103
16+
17+
**Explanation:** The possible arrangements for the digits of 310 are 013, 031, 103, 130, 301, 310.
18+
19+
The arrangement with the smallest value that does not contain any leading zeros is 103.
20+
21+
**Example 2:**
22+
23+
**Input:** num = -7605
24+
25+
**Output:** -7650
26+
27+
**Explanation:** Some possible arrangements for the digits of -7605 are -7650, -6705, -5076, -0567.
28+
29+
The arrangement with the smallest value that does not contain any leading zeros is -7650.
30+
31+
**Constraints:**
32+
33+
* <code>-10<sup>15</sup> <= num <= 10<sup>15</sup></code>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2101_2200.s2164_sort_even_and_odd_indices_independently;
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 sortEvenOdd() {
11+
assertThat(
12+
new Solution().sortEvenOdd(new int[] {4, 1, 2, 3}),
13+
equalTo(new int[] {2, 3, 4, 1}));
14+
}
15+
16+
@Test
17+
void sortEvenOdd2() {
18+
assertThat(new Solution().sortEvenOdd(new int[] {2, 1}), equalTo(new int[] {2, 1}));
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2101_2200.s2165_smallest_value_of_the_rearranged_number;
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 smallestNumber() {
11+
assertThat(new Solution().smallestNumber(310L), equalTo(103L));
12+
}
13+
14+
@Test
15+
void smallestNumber2() {
16+
assertThat(new Solution().smallestNumber(-7605L), equalTo(-7650L));
17+
}
18+
}

0 commit comments

Comments
 (0)