Skip to content

Commit 5fed8c8

Browse files
Merge pull request #164
add new problem 7.03
2 parents c06716a + 5750aa4 commit 5fed8c8

File tree

5 files changed

+139
-3
lines changed

5 files changed

+139
-3
lines changed

.idea/other.xml

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.dp.longestMountain
4+
import com.github.contest.math.closestPrimes
55
import java.util.TreeMap
66

77

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

1212
fun main() {
1313

14-
longestMountain(intArrayOf(2, 1, 4, 7, 3, 2, 5))
14+
closestPrimes(19, 31).also { it.printArray() }
1515
}
1616

1717
fun generateTesting() {

contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,5 +226,34 @@ private fun sumOfDigit(num: Int): Int {
226226
num /= 10
227227
}
228228

229+
return res
230+
}
231+
232+
/**
233+
* 2965. Find Missing and Repeated Values
234+
*/
235+
236+
fun findMissingAndRepeatedValues(grid: Array<IntArray>): IntArray {
237+
val map = mutableMapOf<Int, Int>()
238+
val res = IntArray(2)
239+
val n = grid.size
240+
for (arr in grid) {
241+
for (num in arr) {
242+
map[num] = map.getOrDefault(num, 0) + 1
243+
}
244+
}
245+
for (key in map.keys) {
246+
if (map[key] == 2) {
247+
res[0] = key
248+
break
249+
}
250+
}
251+
for (i in 1..(n * n)) {
252+
if (!map.contains(i)) {
253+
res[1] = i
254+
break
255+
}
256+
}
257+
229258
return res
230259
}

contest/src/main/java/com/github/contest/hashTable/HashTableProdVariant.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,34 @@ fun areAlmostEqualProdVariant(s1: String, s2: String): Boolean =
1717
}
1818
}
1919

20+
/**
21+
* 2965. Find Missing and Repeated Values
22+
* Prod Variant
23+
*/
24+
25+
fun findMissingAndRepeatedValuesProdVariant(grid: Array<IntArray>): IntArray {
26+
val map = mutableMapOf<Int, Int>()
27+
val res = IntArray(2)
28+
val n = grid.size
29+
30+
grid.forEach { row ->
31+
row.forEach {
32+
map[it] = map.getOrDefault(it, 0) + 1
33+
}
34+
}
35+
36+
map.forEach { (num, count) ->
37+
if (count == 2) {
38+
res[0] = num
39+
return@forEach
40+
}
41+
}
42+
43+
(1..(n * n)).forEach {
44+
if (!map.contains(it)) res[1] = it
45+
}
46+
47+
return res
48+
}
49+
50+

contest/src/main/java/com/github/contest/math/MathLeetcode.kt

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.contest.math
22

3+
import kotlin.math.sqrt
4+
35
/**
46
* 1025. Divisor Game
57
*/
@@ -19,4 +21,56 @@ fun checkPowersOfThree(n: Int): Boolean {
1921
num /= 3
2022
}
2123
return true
22-
}
24+
}
25+
26+
/**
27+
* 2579. Count Total Number of Colored Cells
28+
*/
29+
30+
31+
fun coloredCells(n: Int): Long = when {
32+
n == 1 -> 1L
33+
else -> coloredCells(n - 1) + 4L * (n - 1).toLong()
34+
}
35+
36+
/**
37+
* 2523. Closest Prime Numbers in Range
38+
*/
39+
40+
41+
fun closestPrimes(left: Int, right: Int): IntArray {
42+
val primes = mutableListOf<Int>()
43+
val isPrime = BooleanArray(right + 1) { true }
44+
isPrime[0] = false
45+
isPrime[1] = false
46+
47+
for (p in 2..sqrt(right.toDouble()).toInt()) {
48+
if (isPrime[p]) {
49+
for (i in p * p..right step p) {
50+
isPrime[i] = false
51+
}
52+
}
53+
}
54+
55+
for (p in left..right) {
56+
if (isPrime[p]) {
57+
primes.add(p)
58+
}
59+
}
60+
61+
if (primes.size < 2) return intArrayOf(-1, -1)
62+
63+
var minDiff = Int.MAX_VALUE
64+
var result = intArrayOf(-1, -1)
65+
for (i in 0 until primes.size - 1) {
66+
val diff = primes[i + 1] - primes[i]
67+
if (diff < minDiff) {
68+
minDiff = diff
69+
result[0] = primes[i]
70+
result[1] = primes[i + 1]
71+
}
72+
}
73+
74+
return result
75+
}
76+

0 commit comments

Comments
 (0)