Skip to content

Commit 7deefad

Browse files
Merge pull request #151
add new problems 16.02
2 parents 4eb37c7 + 348f8a6 commit 7deefad

File tree

8 files changed

+429
-45
lines changed

8 files changed

+429
-45
lines changed

app/src/main/java/com/leetcode_kotlin/Executing.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.leetcode_kotlin
22

3+
import java.util.concurrent.ConcurrentHashMap
4+
35

46
/**
57
* Executing
@@ -8,11 +10,10 @@ package com.leetcode_kotlin
810

911
fun main() {
1012

13+
val concurrentHashMap = ConcurrentHashMap<Int, Int>()
14+
15+
concurrentHashMap.put(1, 5)
1116

12-
val arr = intArrayOf(1, 4, 6, 7, 3, 7, 7)
13-
arr.drop(1).forEachIndexed { index, num ->
14-
println("$index $num")
15-
}
1617

1718
}
1819

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,15 @@
11
package com.github.contest
22

3-
import com.github.contest.dp.countVowels
4-
import com.github.contest.graph.findJudge
5-
import com.github.contest.heap.customStructure.MinHeap
6-
73

84
/**
95
* Stand
106
*/
117

128
fun main() {
139

14-
findJudge(
15-
5,
16-
arrayOf(
17-
intArrayOf(1, 3),
18-
intArrayOf(2, 3),
19-
intArrayOf(4, 3),
20-
intArrayOf(4, 1),
21-
intArrayOf(5, 3),
22-
intArrayOf(5, 1),
23-
intArrayOf(5, 4)
24-
)
25-
)
2610

2711
}
2812

29-
fun vowels() {
30-
31-
/**
32-
* a a a
33-
* aa
34-
* aa
35-
* aaa
36-
* count - 10
37-
*/
38-
39-
countVowels("aaa")
40-
41-
}
4213

4314

44-
fun heapWork() {
4515

46-
val heap = MinHeap<Int>()
47-
heap.offer(3)
48-
heap.offer(1)
49-
heap.offer(2)
50-
heap.offer(5)
51-
52-
53-
println(heap.poll())
54-
55-
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,86 @@
11
package com.github.contest.backtracking
22

3+
/**
4+
* 2698. Find the Punishment Number of an Integer
5+
*/
6+
7+
8+
fun punishmentNumber(n: Int): Int {
9+
var sum = 0
10+
for (i in 1..n) {
11+
if (isPunishmentNumber(i)) {
12+
sum += i * i
13+
}
14+
}
15+
return sum
16+
}
17+
18+
19+
private fun isPunishmentNumber(n: Int): Boolean {
20+
val square = n * n
21+
val squareStr = square.toString()
22+
return canSplit(squareStr, 0, n, 0)
23+
}
24+
25+
26+
private fun canSplit(squareStr: String, index: Int, target: Int, currentSum: Int): Boolean {
27+
if (index == squareStr.length) {
28+
return currentSum == target
29+
}
30+
31+
var currentNum = 0
32+
for (i in index until squareStr.length) {
33+
currentNum = currentNum * 10 + (squareStr[i] - '0')
34+
if (canSplit(squareStr, i + 1, target, currentSum + currentNum)) {
35+
return true
36+
}
37+
}
38+
return false
39+
}
40+
41+
/**
42+
* 1718. Construct the Lexicographically Largest Valid Sequence
43+
*/
44+
45+
46+
fun constructDistancedSequence(n: Int): IntArray {
47+
val result = IntArray(2 * n - 1)
48+
val used = BooleanArray(n + 1)
49+
50+
fun backtrack(index: Int): Boolean {
51+
if (index == result.size) {
52+
return true
53+
}
54+
if (result[index] != 0) {
55+
return backtrack(index + 1)
56+
}
57+
58+
for (num in n downTo 1) {
59+
if (!used[num]) {
60+
if (num == 1) {
61+
result[index] = num
62+
used[num] = true
63+
if (backtrack(index + 1)) {
64+
return true
65+
}
66+
used[num] = false
67+
result[index] = 0
68+
} else if (index + num < result.size && result[index + num] == 0) {
69+
result[index] = num
70+
result[index + num] = num
71+
used[num] = true
72+
if (backtrack(index + 1)) {
73+
return true
74+
}
75+
used[num] = false
76+
result[index] = 0
77+
result[index + num] = 0
78+
}
79+
}
80+
}
81+
return false
82+
}
83+
84+
backtrack(0)
85+
return result
86+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.github.contest.backtracking
2+
3+
/**
4+
* 2698. Find the Punishment Number of an Integer
5+
* Prod Variant
6+
*/
7+
8+
9+
fun punishmentNumberProdVariant(n: Int): Int {
10+
return (1..n).filter { isPunishment(it) }.sumOf { it * it }
11+
}
12+
13+
private fun isPunishment(num: Int): Boolean {
14+
val squareStr = (num * num).toString()
15+
16+
fun backtrack(index: Int, currentSum: Int): Boolean = when {
17+
index == squareStr.length -> currentSum == num
18+
else -> (index until squareStr.length).any { i ->
19+
val subNum = squareStr.substring(index, i + 1).toInt()
20+
backtrack(i + 1, currentSum + subNum)
21+
}
22+
}
23+
24+
return backtrack(0, 0)
25+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.github.contest.hashTable.customStructure
2+
3+
import java.util.concurrent.locks.ReentrantLock
4+
import kotlin.concurrent.withLock
5+
6+
class CustomConcurrentHashMap<T> {
7+
8+
9+
}
10+
11+
12+
class MyConcurrentHashMap<K, V>(private val segmentCount: Int = 16) {
13+
14+
private class Segment<K, V> {
15+
val entries = mutableListOf<Entry<K, V>>()
16+
val lock = ReentrantLock()
17+
18+
fun get(key: K): V? = lock.withLock {
19+
entries.find { it.key == key }?.value
20+
}
21+
22+
fun put(key: K, value: V): V? = lock.withLock {
23+
entries.find { it.key == key }?.let {
24+
val oldValue = it.value
25+
it.value = value
26+
oldValue
27+
} ?: run {
28+
entries.add(Entry(key, value))
29+
null
30+
}
31+
}
32+
33+
fun remove(key: K): V? = lock.withLock {
34+
entries.find { it.key == key }?.let {
35+
entries.remove(it)
36+
it.value
37+
}
38+
}
39+
40+
fun size(): Int = lock.withLock { entries.size }
41+
42+
fun clear() = lock.withLock { entries.clear() }
43+
}
44+
45+
private data class Entry<K, V>(val key: K, var value: V)
46+
47+
private val segments: Array<Segment<K, V>> = Array(segmentCount) { Segment() }
48+
49+
private fun getSegmentIndex(key: K): Int = key.hashCode().let { Math.abs(it % segmentCount) }
50+
51+
fun get(key: K): V? = segments[getSegmentIndex(key)].get(key)
52+
53+
fun put(key: K, value: V): V? = segments[getSegmentIndex(key)].put(key, value)
54+
55+
fun remove(key: K): V? = segments[getSegmentIndex(key)].remove(key)
56+
57+
fun size(): Int = segments.sumOf { it.size() }
58+
59+
fun clear() {
60+
segments.forEach { it.clear() }
61+
}
62+
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,32 @@ private fun PriorityQueue<Long>.addNums(nums: IntArray) {
2828
val list = mutableListOf<Long>()
2929
nums.forEach { list.add(it.toLong()) }
3030
this.addAll(list)
31+
}
32+
33+
/**
34+
* 2500. Delete Greatest Value in Each Row
35+
* Alternative Solution
36+
*/
37+
38+
39+
fun deleteGreatestValueAltSolution(grid: Array<IntArray>): Int {
40+
val pqRows = Array(grid.size) { PriorityQueue<Int>(reverseOrder()) }
41+
42+
for (i in 0 until grid.size) {
43+
for (num in grid[i]) {
44+
pqRows[i].offer(num)
45+
}
46+
}
47+
48+
var sum = 0
49+
50+
while (pqRows[0].isNotEmpty()) {
51+
var maxVal = 0
52+
for (pq in pqRows) {
53+
maxVal = maxOf(maxVal, pq.poll())
54+
}
55+
sum += maxVal
56+
}
57+
58+
return sum
3159
}

0 commit comments

Comments
 (0)