8000 add 2231 · githubniraj/Leetcode@267371d · GitHub
[go: up one dir, main page]

Skip to content

Commit 267371d

Browse files
add 2231
1 parent 0a4b323 commit 267371d

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ _If you like this project, please leave me a star._ ★
8686
| 2239 |[Find Closest Number to Zero](https://leetcode.com/problems/find-closest-number-to-zero/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2239.java) || Easy ||
8787
| 2236 |[Root Equals Sum of Children](https://leetcode.com/problems/root-equals-sum-of-children/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2236.java) || Easy ||
8888
| 2235 |[Add Two Integers](https://leetcode.com/problems/add-two-integers/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2235.java) | [:tv:](https://youtu.be/Qubhoqoks6I) | Easy ||
89+
| 2231 |[Largest Number After Digit Swaps by Parity](https://leetcode.com/problems/largest-number-after-digit-swaps-by-parity/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2231.java) || Easy ||
8990
| 2229 |[Check if an Array Is Consecutive](https://leetcode.com/problems/check-if-an-array-is-consecutive/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2229.java) || Easy ||
9091
| 2224 |[Minimum Number of Operations to Convert Time](https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2224.java) || Easy ||
9192
| 2220 |[Minimum Bit Flips to Convert Number](https://leetcode.com/problems/minimum-bit-flips-to-convert-number/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2220.java) || Easy ||
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.*;
4+
5+
public class _2231 {
6+
public static class Solution1 {
7+
public int largestInteger(int num) {
8+
List<Integer> odds = new ArrayList<>();
9+
List<Integer> evens = new ArrayList<>();
10+
PriorityQueue<Integer> oddTimes = new PriorityQueue<>();
11+
PriorityQueue<Integer> evenTimes = new PriorityQueue<>();
12+
int times = 1;
13+
while (num != 0) {
14+
int digit = num % 10;
15+
num /= 10;
16+
if (digit % 2 == 0) {
17+
evens.add(digit);
18+
evenTimes.offer(times);
19+
} else {
20+
odds.add(digit);
21+
oddTimes.offer(times);
22+
}
23+
times *= 10;
24+
}
25+
Collections.sort(evens);
26+
Collections.sort(odds);
27+
int composite = 0;
28+
for (int i = 0; i < odds.size(); i++) {
29+
composite += odds.get(i) * oddTimes.poll();
30+
}
31+
for (int i = 0; i < evens.size(); i++) {
32+
composite += evens.get(i) * evenTimes.poll();
33+
}
34+
return composite;
35+
}
36+
}
37+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._2231;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _2231Test {
10+
private static _2231.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _2231.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3412, solution1.largestInteger(1234));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(87655, solution1.largestInteger(65875));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(427, solution1.largestInteger(247));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(472, solution1.largestInteger(274));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(75856, solution1.largestInteger(55678));
40+
}
41+
}

0 commit comments

Comments
 (0)
0