Skip to content

Commit 9e96ffb

Browse files
Merge pull request #155
add new problems 20.02
2 parents 83a0af5 + 96e9fc2 commit 9e96ffb

File tree

7 files changed

+322
-36
lines changed

7 files changed

+322
-36
lines changed

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

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

3-
import com.github.contest.heap.customStructure.MaxHeap
3+
4+
import com.github.contest.backtracking.findDifferentBinaryString
5+
import java.util.TreeMap
46

57

68
/**
@@ -9,15 +11,19 @@ import com.github.contest.heap.customStructure.MaxHeap
911

1012
fun main() {
1113

12-
val maxheap = MaxHeap<Int>()
14+
findDifferentBinaryString(arrayOf("00", "01"))
1315

14-
for (i in 1..39) maxheap.offer(i)
16+
}
1517

16-
while (maxheap.isNotEmpty()) {
17-
println(maxheap.poll())
18-
}
1918

19+
fun workWithTreeMap() {
20+
val treeMapOne = TreeMap<String, Int>()
21+
treeMapOne["apple"] = 1
22+
treeMapOne["banana"] = 2
23+
treeMapOne["cherry"] = 3
2024

25+
println("TreeMap with natural ordering:")
26+
treeMapOne.forEach { (key, value) -> println("$key: $value") }
2127
}
2228

2329

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ fun getCommonProdVariant(nums1: IntArray, nums2: IntArray): Int {
1515
}
1616
.filterNotNull()
1717
.firstOrNull() ?: -1
18-
}
18+
}
19+
20+
/**
21+
* 3151. Special Array I
22+
* Prod Variant
23+
*/
24+
25+
fun isArraySpecialProdVariant(nums: IntArray): Boolean =
26+
nums.isEmpty() || nums.toList().windowed(2).all { (a, b) -> a % 2 != b % 2 }

contest/src/main/java/com/github/contest/backtracking/BacktrackingLeetcode.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,32 @@ private fun generateHappyString(
141141
subset.deleteCharAt(subset.length - 1)
142142
}
143143
}
144+
}
145+
146+
/**
147+
* 1980. Find Unique Binary String
148+
*/
149+
150+
151+
fun findDifferentBinaryString(nums: Array<String>): String {
152+
val set = mutableSetOf<String>()
153+
for (num in nums) set.add(num)
154+
return generateBinaryString(set, 0, nums[0].length, StringBuilder())
155+
}
156+
157+
private fun generateBinaryString(set: Set<String>, index: Int, n: Int, str: StringBuilder): String {
158+
if (index == n) {
159+
return if (!set.contains(str.toString())) str.toString() else ""
160+
}
161+
repeat(2) {
162+
str.append(it.toString())
163+
val ans = generateBinaryString(set, index + 1, n, str)
164+
if (ans.isNotEmpty()) return ans
165+
str.deleteLast()
166+
}
167+
return ""
168+
}
169+
170+
private fun StringBuilder.deleteLast() {
171+
if (isNotEmpty()) this.deleteCharAt(this.length - 1)
144172
}

contest/src/main/java/com/github/contest/hashTable/customStructure/CustomConcurrentHashMap.kt

Lines changed: 176 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,207 @@ package com.github.contest.hashTable.customStructure
33
import java.util.concurrent.locks.ReentrantLock
44
import kotlin.concurrent.withLock
55

6-
class CustomConcurrentHashMap<T> {
7-
8-
9-
}
10-
116

127
class MyConcurrentHashMap<K, V>(private val segmentCount: Int = 16) {
138

9+
private class HashEntry<K, V>(val key: K, var value: V, var next: HashEntry<K, V>?)
10+
1411
private class Segment<K, V> {
15-
val entries = mutableListOf<Entry<K, V>>()
1612
val lock = ReentrantLock()
13+
var table: Array<HashEntry<K, V>?> = arrayOfNulls(16)
14+
var count = 0
1715

1816
fun get(key: K): V? = lock.withLock {
19-
entries.find { it.key == key }?.value
17+
val hash = key.hashCode()
18+
val index = hash % table.size
19+
var entry = table[index]
20+
while (entry != null) {
21+
if (entry.key == key) {
22+
return entry.value
23+
}
24+
entry = entry.next
25+
}
26+
return null
2027
}
2128

2229
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+
val hash = key.hashCode()
31+
val index = hash % table.size
32+
var entry = table[index]
33+
while (entry != null) {
34+
if (entry.key == key) {
35+
val oldValue = entry.value
36+
entry.value = value
37+
return oldValue
38+
}
39+
entry = entry.next
40+
}
41+
val newEntry = HashEntry(key, value, table[index])
42+
table[index] = newEntry
43+
count++
44+
if (count >= table.size * 0.75) {
45+
rehash()
3046
}
47+
return null
3148
}
3249

3350
fun remove(key: K): V? = lock.withLock {
34-
entries.find { it.key == key }?.let {
35-
entries.remove(it)
36-
it.value
51+
val hash = key.hashCode()
52+
val index = hash % table.size
53+
var entry = table[index]
54+
var prev: HashEntry<K, V>? = null
55+
while (entry != null) {
56+
if (entry.key == key) {
57+
if (prev == null) {
58+
table[index] = entry.next
59+
} else {
60+
prev.next = entry.next
61+
}
62+
count--
63+
return entry.value
64+
}
65+
prev = entry
66+
entry = entry.next
3767
}
68+
return null
3869
}
3970

40-
fun size(): Int = lock.withLock { entries.size }
71+
private fun rehash() {
72+
val oldTable = table
73+
val newTableSize = table.size * 2
74+
table = arrayOfNulls(newTableSize)
75+
count = 0
76+
for (entry in oldTable) {
77+
var current = entry
78+
while (current != null) {
79+
put(current.key, current.value)
80+
current = current.next
81+
}
82+
}
83+
}
84+
}
4185

42-
fun clear() = lock.withLock { entries.clear() }
86+
private val segments: Array<Segment<K, V>> = Array(segmentCount) { Segment() }
87+
88+
private fun getSegment(key: K): Segment<K, V> {
89+
val hash = key.hashCode()
90+
val index = (hash ushr 16) % segmentCount
91+
return segments[index]
4392
}
4493

45-
private data class Entry<K, V>(val key: K, var value: V)
94+
fun get(key: K): V? = getSegment(key).get(key)
4695

47-
private val segments: Array<Segment<K, V>> = Array(segmentCount) { Segment() }
96+
fun put(key: K, value: V): V? = getSegment(key).put(key, value)
97+
98+
fun remove(key: K): V? = getSegment(key).remove(key)
99+
}
100+
101+
102+
class CustomConcurrentHashMap<K, V>(private val capacitySegments: Int = 16) {
103+
104+
private data class HashEntry<K, V>(val key: K, var value: V, var next: HashEntry<K, V>?)
105+
106+
107+
private class Segment<K, V> {
108+
109+
private val lock = ReentrantLock()
110+
private var table: Array<HashEntry<K, V>?> = arrayOfNulls(16)
111+
private var count = 0
112+
113+
fun put(key: K, value: V): V? = lock.withLock {
114+
val hash = key.hashCode()
115+
val index = hash % table.size
116+
var entry = table[index]
117+
while (entry != null) {
118+
if (entry.key == key) {
119+
val oldValue = entry.value
120+
entry.value = value
121+
return oldValue
122+
}
123+
entry = entry.next
124+
}
125+
entry = table[index]
126+
val newEntry = HashEntry(key, value, table[index])
127+
table[index] = newEntry
128+
count++
129+
if (count >= table.size * 0.75) rehash()
130+
return null
131+
}
132+
133+
134+
fun get(key: K): V? {
135+
val hash = key.hashCode()
136+
val index = hash % table.size
137+
var entry = table[index]
138+
while (entry != null) {
139+
if (entry.key == key) return entry.value
140+
entry = entry.next
141+
}
142+
143+
return null
144+
}
145+
146+
fun remove(key: K): V? = lock.withLock {
147+
val hash = key.hashCode()
148+
val index = hash % table.size
149+
var entry = table[index]
150+
var prev: HashEntry<K, V>? = null
151+
while (entry != null) {
152+
if (entry.key == key) {
153+
if (prev == null) {
154+
table[index] = entry.next
155+
} else prev.next = entry.next
156+
count--
157+
return entry.value
158+
}
159+
prev = entry
160+
entry = entry.next
161+
}
162+
163+
return null
164+
}
48165

49-
private fun getSegmentIndex(key: K): Int = key.hashCode().let { Math.abs(it % segmentCount) }
50166

51-
fun get(key: K): V? = segments[getSegmentIndex(key)].get(key)
167+
private fun rehash() {
168+
val oldTable = table
169+
val newSize = table.size * 2
170+
table = arrayOfNulls(newSize)
171+
count = 0
172+
for (entry in oldTable) {
173+
var current = entry
174+
while (current != null) {
175+
put(current.key, current.value)
176+
current = current.next
177+
}
178+
}
179+
}
52180

53-
fun put(key: K, value: V): V? = segments[getSegmentIndex(key)].put(key, value)
181+
}
54182

55-
fun remove(key: K): V? = segments[getSegmentIndex(key)].remove(key)
183+
private val segments = Array<Segment<K, V>>(capacitySegments) { Segment() }
56184

57-
fun size(): Int = segments.sumOf { it.size() }
185+
fun put(key: K, value: V) = getSegment(key).let { it.put(key, value) }
58186

59-
fun clear() {
60-
segments.forEach { it.clear() }
187+
fun get(key: K): V? = getSegment(key).let { it.get(key) }
188+
189+
private fun getSegment(key: K): Segment<K, V> {
190+
val hash = key.hashCode()
191+
val index = (hash ushr 16) % capacitySegments
192+
return segments[index]
61193
}
62-
}
194+
195+
196+
}
197+
198+
199+
200+
201+
202+
203+
204+
205+
206+
207+
208+
209+

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,34 @@ fun deleteGreatestValueAltSolution(grid: Array<IntArray>): Int {
5656
}
5757

5858
return sum
59+
}
60+
61+
/**
62+
* 1424. Diagonal Traverse II
63+
* Alternative Solution
64+
*/
65+
66+
67+
fun findDiagonalOrderAlternativeSolution(nums: List<List<Int>>): IntArray {
68+
if (nums.hasSingle()) nums[0].toIntArray()
69+
val map = mutableMapOf<Int, MutableList<Int>>()
70+
for (i in nums.indices) {
71+
for (j in 0 until nums[i].size) {
72+
map.getOrPut(i + j) { mutableListOf() }.let { it.add(nums[i][j]) }
73+
}
74+
}
75+
var diagonal = 0
76+
val result = mutableListOf<Int>()
77+
while (map.contains(diagonal)) {
78+
val values = map.getOrDefault(diagonal, mutableListOf())
79+
for (i in values.size - 1 downTo 0) result.add(values[i])
80+
diagonal++
81+
}
82+
83+
return result.toIntArray()
84+
}
85+
86+
private fun <T> List<T>.hasSingle(): Boolean = when {
87+
this.size == 1 -> true
88+
else -> false
5989
}

0 commit comments

Comments
 (0)