Skip to content

Commit 32f5c00

Browse files
Merge pull request #156
add new problems 21.02
2 parents 9e96ffb + 56583bb commit 32f5c00

File tree

6 files changed

+205
-3
lines changed

6 files changed

+205
-3
lines changed

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

Lines changed: 33 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.backtracking.findDifferentBinaryString
4+
import com.github.contest.heap.maxSubsequence
55
import java.util.TreeMap
66

77

@@ -11,8 +11,39 @@ import java.util.TreeMap
1111

1212
fun main() {
1313

14-
findDifferentBinaryString(arrayOf("00", "01"))
14+
maxSubsequence(intArrayOf(-1, -2, 3, 4), 3).apply { this.printArray() }
15+
}
16+
17+
fun generateTesting() {
18+
val sequence = sequenceOf(3, 5, 6, 7, 7, 8, 8, 8, 9, 3)
19+
sequence.map { it * 2 }
20+
.filter { it > 3 }
21+
.filter { it > 2 }
22+
.constrainOnce()
23+
24+
25+
}
26+
27+
fun generateSequence() {
28+
var counter = 0
29+
val numbers = generateSequence {
30+
if (counter < 5) {
31+
counter++
32+
counter
33+
} else null
34+
}
35+
36+
println(numbers.toList())
37+
}
38+
39+
fun doing() {
40+
val collection = mutableListOf(listOf(5), listOf(2), listOf(4))
41+
val other = mutableListOf(3, 5, 10)
42+
val res = collection.flatMap {
43+
it.asReversed()
44+
}
1545

46+
println(res)
1647
}
1748

1849

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.contest.binaryTree
2+
3+
/**
4+
* 1261. Find Elements in a Contaminated Binary Tree
5+
* Alternative Solution
6+
* DFS + SET
7+
*/
8+
9+
class FindElementsAlternativeSolution(root: TreeNode?) {
10+
11+
private val values: MutableSet<Int> = mutableSetOf()
12+
13+
init {
14+
recover(root, 0)
15+
}
16+
17+
18+
private fun recover(node: TreeNode?, value: Int) {
19+
if (node == null) return
20+
21+
node.`val` = value
22+
values.add(value)
23+
24+
recover(node.left, 2 * value + 1)
25+
recover(node.right, 2 * value + 2)
26+
}
27+
28+
fun find(target: Int): Boolean {
29+
return values.contains(target)
30+
}
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
11
package com.github.contest.binaryTree
22

3+
import java.util.LinkedList
4+
5+
/**
6+
* 1261. Find Elements in a Contaminated Binary Tree
7+
*/
8+
9+
class TreeNode(var `val`: Int) {
10+
var left: TreeNode? = null
11+
var right: TreeNode? = null
12+
}
13+
14+
class FindElements(private val root: TreeNode?) {
15+
16+
fun find(target: Int): Boolean = bfs(target)
17+
18+
private fun bfs(target: Int): Boolean {
19+
if (target == 0 && root != null) return true
20+
val queue = LinkedList<Pair<Int, TreeNode?>>()
21+
queue.offer(0 to root)
22+
while (queue.isNotEmpty()) {
23+
val size = queue.size
24+
for (i in 0 until size) {
25+
val (index, node) = queue.poll()
26+
if (index == target) return true
27+
if (index > target) return false
28+
node?.left?.let { queue.offer((index * 2 + 1) to it) }
29+
node?.right?.let { queue.offer((index * 2 + 2) to it) }
30+
}
31+
}
32+
33+
return false
34+
}
35+
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.github.contest.binaryTree
2+
3+
/**
4+
* 1261. Find Elements in a Contaminated Binary Tree
5+
* Prod Variant
6+
*/
7+
8+
class FindElementProdVariant(root: TreeNode?) {
9+
10+
private val values = buildSet {
11+
fun dfs(root: TreeNode?, value: Int) {
12+
if (root == null) return
13+
14+
root.`val` = value
15+
add(value)
16+
17+
dfs(root?.left, value * 2 + 1)
18+
dfs(root?.right, value * 2 + 2)
19+
}
20+
dfs(root, 0)
21+
}
22+
23+
fun find(target: Int): Boolean = values.contains(target)
24+
25+
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,27 @@ fun findDiagonalOrder(nums: List<List<Int>>): IntArray {
286286
}
287287

288288
return result
289-
}
289+
}
290+
291+
/**
292+
* 2099. Find Subsequence of Length K With the Largest Sum
293+
*/
294+
295+
fun maxSubsequence(nums: IntArray, k: Int): IntArray {
296+
val heap = PriorityQueue<Pair<Int, Int>>(compareBy { it.first })
297+
for (i in nums.indices) {
298+
heap.offer(Pair(nums[i], i))
299+
if (heap.size > k) {
300+
heap.poll()
301+
}
302+
}
303+
304+
val indices = mutableListOf<Pair<Int, Int>>()
305+
while (heap.isNotEmpty()) {
306+
indices.add(heap.poll())
307+
}
308+
309+
indices.sortBy { it.second }
310+
311+
return indices.map { it.first }.toIntArray()
312+
}

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,63 @@ fun findDiagonalOrderProdVariant(nums: List<List<Int>>): IntArray {
4141
}
4242

4343

44+
fun findDiagonalOrderProdVariantII(nums: List<List<Int>>): IntArray {
45+
PriorityQueue { a: Pair<Int, Pair<Int, Int>>, b: Pair<Int, Pair<Int, Int>> ->
46+
if (a.second.first != b.second.first) a.second.first - b.second.first
47+
else b.first - a.first
48+
}.apply {
49+
nums.forEachIndexed { row, _ ->
50+
nums[row].forEachIndexed { col, num ->
51+
offer(row to (row + col to num))
52+
}
53+
}
54+
}.apply {
55+
return mutableListOf<Int>().also {
56+
while (this.isNotEmpty()) it.add(this.poll().second.second)
57+
}.toIntArray()
58+
}
59+
}
60+
61+
/**
62+
* 2099. Find Subsequence of Length K With the Largest Sum
63+
* Prod Variant
64+
*/
65+
66+
fun maxSubsequenceProdVariant(nums: IntArray, k: Int): IntArray {
67+
val heap = PriorityQueue<Pair<Int, Int>>(compareBy { it.first })
68+
69+
nums.forEachIndexed { index, num ->
70+
heap.offer(index to num)
71+
if (heap.size > k) heap.poll()
72+
}
73+
74+
return mutableListOf<Pair<Int, Int>>()
75+
.apply { heap.forEach { add(it) } }
76+
.sortedBy { it.second }
77+
.map { it.first }
78+
.toIntArray()
79+
}
80+
81+
fun maxSubsequenceProdVariantII(nums: IntArray, k: Int): IntArray =
82+
nums.mapIndexed { index, num -> num to index }
83+
.let { pairs ->
84+
PriorityQueue<Pair<Int, Int>>(compareBy { it.first })
85+
.apply {
86+
pairs.forEach {
87+
offer(it)
88+
if (size > k) poll()
89+
}
90+
}.let { heap ->
91+
List(heap.size) { heap.poll() }
92+
.sortedBy { it.second }
93+
.map { it.first }
94+
.toIntArray()
95+
}
96+
}
97+
98+
99+
100+
101+
44102

45103

0 commit comments

Comments
 (0)