Skip to content

Commit 83a0af5

Browse files
Merge pull request #154
add new problem and max heap 19.02
2 parents 168da7a + 4615c45 commit 83a0af5

File tree

6 files changed

+176
-13
lines changed

6 files changed

+176
-13
lines changed

.idea/other.xml

Lines changed: 0 additions & 11 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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.contest
22

3-
import com.github.contest.heap.numberGame
3+
import com.github.contest.heap.customStructure.MaxHeap
44

55

66
/**
@@ -9,7 +9,13 @@ import com.github.contest.heap.numberGame
99

1010
fun main() {
1111

12-
numberGame(intArrayOf(5, 4, 2, 3))
12+
val maxheap = MaxHeap<Int>()
13+
14+
for (i in 1..39) maxheap.offer(i)
15+
16+
while (maxheap.isNotEmpty()) {
17+
println(maxheap.poll())
18+
}
1319

1420

1521
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,38 @@ private fun isValid(num: Int, pos: Int, arr: IntArray): Boolean {
5858
if (pos + num + 1 >= arr.size) return false
5959
if (arr[pos + num + 1] != 0) return false
6060
return true
61+
}
62+
63+
/**
64+
* 1415. The k-th Lexicographical String of All Happy Strings of Length n
65+
* Alternative Solution
66+
*/
67+
68+
fun getHappyStringAltSolution(n: Int, k: Int): String {
69+
val count = intArrayOf(0)
70+
val result = StringBuilder()
71+
val chars = charArrayOf('a', 'b', 'c')
72+
73+
fun backtrack(current: StringBuilder) {
74+
if (current.length == n) {
75+
count[0]++
76+
if (count[0] == k) {
77+
result.append(current.toString())
78+
}
79+
return
80+
}
81+
82+
if (count[0] >= k) return
83+
84+
for (char in chars) {
85+
if (current.isEmpty() || current.last() != char) {
86+
current.append(char)
87+
backtrack(current)
88+
current.deleteCharAt(current.length - 1)
89+
}
90+
}
91+
}
92+
93+
backtrack(StringBuilder())
94+
return result.toString()
6195
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,37 @@ private fun dfs(count: IntArray): Int {
108108
}
109109
return sum
110110
}
111+
112+
/**
113+
* 1415. The k-th Lexicographical String of All Happy Strings of Length n
114+
*/
115+
116+
117+
fun getHappyString(n: Int, k: Int): String {
118+
val res = mutableListOf<String>()
119+
val set = "abc"
120+
val subset = StringBuilder()
121+
generateHappyString(0, n, set, subset, res)
122+
return if (k > res.size) "" else res[k - 1]
123+
}
124+
125+
private fun generateHappyString(
126+
index: Int,
127+
n: Int,
128+
set: String,
129+
subset: StringBuilder,
130+
res: MutableList<String>
131+
) {
132+
if (index == n) {
133+
res.add(subset.toString())
134+
return
135+
}
136+
137+
for (s in set) {
138+
if (subset.isEmpty() || subset[subset.length - 1] != s) {
139+
subset.append(s)
140+
generateHappyString(index + 1, n, set, subset, res)
141+
subset.deleteCharAt(subset.length - 1)
142+
}
143+
}
144+
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,38 @@ private fun isPunishment(num: Int): Boolean {
2222
}
2323

2424
return backtrack(0, 0)
25+
}
26+
27+
/**
28+
* 1415. The k-th Lexicographical String of All Happy Strings of Length n
29+
* Prod Variant
30+
*/
31+
32+
33+
fun getHappyStringProdVariant(n: Int, k: Int): String {
34+
val result = StringBuilder()
35+
val chars = setOf('a', 'b', 'c')
36+
var count = 0
37+
fun generateHappyString(current: StringBuilder) {
38+
if (current.length == n) {
39+
count++
40+
if (count == k) result.append(current.toString())
41+
return
42+
}
43+
44+
if (count >= k) return
45+
46+
chars.filter { current.isEmpty() || current.last() != it }.forEach {
47+
current.append(it)
48+
generateHappyString(current)
49+
current.deleteLast()
50+
}
51+
}
52+
53+
generateHappyString(StringBuilder())
54+
return result.toString()
55+
}
56+
57+
private fun StringBuilder.deleteLast() {
58+
if (isNotEmpty()) deleteCharAt(this.length - 1)
2559
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.github.contest.heap.customStructure
2+
3+
class MaxHeap<T : Comparable<T>> {
4+
5+
private val heap = mutableListOf<T>()
6+
val size get() = heap.size
7+
8+
fun isEmpty() = heap.isEmpty()
9+
10+
fun isNotEmpty() = heap.isNotEmpty()
11+
12+
fun offer(item: T) {
13+
heap.add(item)
14+
siftUp(heap.size - 1)
15+
}
16+
17+
fun poll(): T? {
18+
if (isEmpty()) throw IndexOutOfBoundsException()
19+
heap.swap(0, heap.size - 1)
20+
val element = heap.removeLast()
21+
siftDown(0)
22+
return element
23+
}
24+
25+
26+
private fun siftUp(index: Int) {
27+
var currentIndex = index
28+
var parentIndex = parentIndex(currentIndex)
29+
while (currentIndex > 0 && heap[currentIndex] > heap[parentIndex]) {
30+
heap.swap(currentIndex, parentIndex)
31+
currentIndex = parentIndex
32+
parentIndex = parentIndex(currentIndex)
33+
}
34+
}
35+
36+
37+
private fun siftDown(index: Int) {
38+
var currentIndex = index
39+
while (hasLeftChild(currentIndex)) {
40+
var smallestIndex = leftChildIndex(currentIndex)
41+
if (hasRightChild(currentIndex) && heap[rightChildIndex(currentIndex)] > heap[smallestIndex]) {
42+
smallestIndex = rightChildIndex(currentIndex)
43+
}
44+
if (heap[currentIndex] > heap[smallestIndex]) break
45+
heap.swap(currentIndex, smallestIndex)
46+
currentIndex = smallestIndex
47+
}
48+
}
49+
50+
private fun MutableList<T>.swap(from: Int, to: Int) {
51+
val temp = this[from]
52+
this[from] = this[to]
53+
this[to] = temp
54+
}
55+
56+
private fun hasLeftChild(index: Int): Boolean = leftChildIndex(index) < heap.size
57+
58+
private fun hasRightChild(index: Int): Boolean = rightChildIndex(index) < heap.size
59+
60+
private fun leftChildIndex(index: Int) = (index * 2) + 1
61+
62+
private fun rightChildIndex(index: Int) = (index * 2) + 2
63+
64+
private fun parentIndex(index: Int) = (index - 1) / 2
65+
66+
}

0 commit comments

Comments
 (0)