Skip to content

Commit ca3d3d6

Browse files
Merge pull request #159
add new problems 28.02
2 parents f15bc3b + b9f22f6 commit ca3d3d6

File tree

5 files changed

+132
-21
lines changed

5 files changed

+132
-21
lines changed

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.contest
22

33

4-
import com.github.contest.heap.largestInteger
4+
import com.github.contest.array.sortArrayByParityIIAlternativeSolution
55
import java.util.TreeMap
66

77

@@ -11,7 +11,7 @@ import java.util.TreeMap
1111

1212
fun main() {
1313

14-
largestInteger(1234)
14+
sortArrayByParityIIAlternativeSolution(intArrayOf(4, 2, 5, 7)).also { it.printArray() }
1515

1616
}
1717

contest/src/main/java/com/github/contest/array/AlternativeSolutionArray.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,23 @@ fun getCommonAlternativeSolution(nums1: IntArray, nums2: IntArray): Int {
2020
}
2121

2222
return -1
23+
}
24+
25+
/**
26+
* 922. Sort Array By Parity II
27+
* Alternative Solution
28+
*/
29+
30+
fun sortArrayByParityIIAlternativeSolution(nums: IntArray): IntArray {
31+
var evenIndex = 0
32+
var oddIndex = 1
33+
while (evenIndex < nums.size && oddIndex < nums.size) {
34+
while (evenIndex < nums.size && nums[evenIndex] % 2 == 0) evenIndex += 2
35+
while (oddIndex < nums.size && nums[oddIndex] % 2 != 0) oddIndex += 2
36+
if (evenIndex < nums.size && oddIndex < nums.size) {
37+
nums[evenIndex] = nums[oddIndex].also { nums[oddIndex] = nums[evenIndex] }
38+
}
39+
}
40+
41+
return nums
2342
}

contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,22 @@ fun candy(ratings: IntArray): Int {
4444
}
4545

4646
/**
47-
*
47+
* 922. Sort Array By Parity II
4848
*/
4949

50-
fun firstMissingPositive(nums: IntArray): Int {
51-
val n = nums.size
52-
53-
// In-place rearrangement
54-
for (i in 0 until n) {
55-
while (nums[i] in 1..n && nums[nums[i] - 1] != nums[i]) {
56-
val temp = nums[i]
57-
nums[i] = nums[temp - 1]
58-
nums[temp - 1] = temp
59-
}
50+
fun sortArrayByParityII(nums: IntArray): IntArray {
51+
val even = mutableListOf<Int>()
52+
val odd = mutableListOf<Int>()
53+
for (num in nums) {
54+
if (num % 2 == 0) even.add(num)
55+
else odd.add(num)
6056
}
6157

62-
// Find the missing positive
63-
for (i in 0 until n) {
64-
if (nums[i] != i + 1) {
65-
return i + 1
66-
}
58+
for (i in nums.indices) {
59+
nums[i] = if (i % 2 == 0) even.removeLast() else odd.removeLast()
6760
}
6861

69-
// All positives from 1 to n are present
70-
return n + 1
71-
}
62+
return nums
63+
}
64+
65+

contest/src/main/java/com/github/contest/heap/HeapAlterantiveSolution.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,40 @@ fun findDiagonalOrderAlternativeSolution(nums: List<List<Int>>): IntArray {
8686
private fun <T> List<T>.hasSingle(): Boolean = when {
8787
this.size == 1 -> true
8888
else -> false
89+
}
90+
91+
/**
92+
* 2231. Largest Number After Digit Swaps by Parity
93+
* Alternative Solution
94+
*/
95+
96+
97+
fun largestIntegerAltSolution(num: Int): Int {
98+
if (num <= 99) return num
99+
val odd = PriorityQueue<Int>(reverseOrder())
100+
val even = PriorityQueue<Int>(reverseOrder())
101+
val str = num.toString()
102+
val len = str.length
103+
104+
for (i in 0 until len) {
105+
if (str[i].digitToInt() % 2 == 0) {
106+
even.offer(str[i].digitToInt())
107+
} else odd.offer(str[i].digitToInt())
108+
}
109+
110+
var res = 0
111+
for (i in 0 until len) {
112+
if (str[i].digitToInt() % 2 == 0) {
113+
val value = even.poll()
114+
if (value != 0) res += value
115+
116+
} else {
117+
val value = odd.poll()
118+
if (value != 0) res += value
119+
120+
}
121+
if (even.isNotEmpty() || odd.isNotEmpty()) res *= 10
122+
}
123+
124+
return res
89125
}

contest/src/main/java/com/github/contest/heap/HeapProdVariant.kt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,68 @@ fun maxSubsequenceProdVariantII(nums: IntArray, k: Int): IntArray =
9696
}
9797

9898

99+
/**
100+
* 2231. Largest Number After Digit Swaps by Parity
101+
* Prod Variant
102+
*/
103+
104+
fun largestIntegerProdVariant(num: Int): Int = when {
105+
num <= 99 -> num
106+
else -> {
107+
val odd = PriorityQueue<Int>(reverseOrder())
108+
val even = PriorityQueue<Int>(reverseOrder())
109+
val str = num.toString()
110+
str.forEach {
111+
when {
112+
it.digitToInt() % 2 == 0 -> even.offer(it.digitToInt())
113+
else -> odd.offer(it.digitToInt())
114+
}
115+
}
116+
var res = 0
117+
str.forEach {
118+
when {
119+
it.digitToInt() % 2 == 0 -> {
120+
val value = even.poll()
121+
if (value != 0) res += value
122+
}
123+
124+
else -> {
125+
val value = odd.poll()
126+
if (value != 0) res += value
127+
}
128+
}
129+
if (even.isNotEmpty() && odd.isNotEmpty()) res *= 10
130+
}
131+
132+
res
133+
}
134+
}
135+
136+
fun largestIntegerProdVariantII(num: Int): Int {
137+
val even = PriorityQueue<Int>(reverseOrder())
138+
val odd = PriorityQueue<Int>(reverseOrder())
139+
140+
141+
val parityMap = num.toString().map { it.digitToInt() }.map {
142+
if (it % 2 == 0) {
143+
even.offer(it)
144+
true
145+
} else {
146+
odd.offer(it)
147+
false
148+
}
149+
}
150+
151+
return buildString {
152+
parityMap.forEach {
153+
val number = when {
154+
it -> even.poll()
155+
else -> odd.poll()
156+
}
157+
append(number)
158+
}
159+
}.toInt()
160+
}
99161

100162

101163

0 commit comments

Comments
 (0)