Skip to content

Commit 4eb37c7

Browse files
Merge pull request #150
add new problems 14.02
2 parents 015ee96 + 670c2d7 commit 4eb37c7

File tree

11 files changed

+411
-3
lines changed

11 files changed

+411
-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.
Lines changed: 43 additions & 3 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.halveArray
3+
import com.github.contest.dp.countVowels
4+
import com.github.contest.graph.findJudge
5+
import com.github.contest.heap.customStructure.MinHeap
46

57

68
/**
@@ -9,7 +11,45 @@ import com.github.contest.heap.halveArray
911

1012
fun main() {
1113

12-
halveArray(intArrayOf(7, 7, 7, 31, 2)).also { println(it) }
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+
)
1326

27+
}
1428

15-
}
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+
}
42+
43+
44+
fun heapWork() {
45+
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.github.contest.design
2+
3+
/**
4+
* 1352. Product of the Last K Numbers
5+
*/
6+
7+
class ProductOfNumbers() {
8+
9+
private val numbers = mutableListOf<Int>()
10+
11+
fun add(num: Int) {
12+
numbers.add(num)
13+
}
14+
15+
fun getProduct(k: Int): Int {
16+
return when {
17+
k == 1 -> numbers.last()
18+
else -> calculateProduct(k)
19+
}
20+
}
21+
22+
private fun calculateProduct(k: Int): Int {
23+
var res = 1
24+
var k = k
25+
while (k != 0) {
26+
res *= numbers[numbers.size - k]
27+
k--
28+
}
29+
return res
30+
}
31+
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.contest.design
2+
3+
/**
4+
* 1352. Product of the Last K Numbers
5+
* Prod Variant
6+
*/
7+
8+
class ProductOfNumbersProdVariant() {
9+
10+
private val products = mutableListOf<Int>()
11+
12+
fun add(num: Int) {
13+
if (num == 0) {
14+
products.clear()
15+
} else {
16+
if (products.isEmpty()) {
17+
products.add(num)
18+
} else {
19+
products.add(products.last() * num)
20+
}
21+
}
22+
}
23+
24+
fun getProduct(k: Int): Int {
25+
if (k > products.size) {
26+
return 0
27+
}
28+
if (k == products.size) {
29+
return products.last()
30+
}
31+
return products.last() / products[products.size - k - 1]
32+
}
33+
}

contest/src/main/java/com/github/contest/dp/DpLeetcode.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,43 @@ fun countVowels(word: String): Long {
321321
}
322322

323323
return totalVowels
324+
}
325+
326+
/**
327+
*
328+
*/
329+
330+
fun maxTwoEvents(events: Array<IntArray>): Int {
331+
val n = events.size
332+
events.sortBy { it[0] } // Sort by start time
333+
val dp = IntArray(n)
334+
var maxVal = 0
335+
for (i in n - 1 downTo 0) {
336+
maxVal = maxOf(maxVal, events[i][2])
337+
dp[i] = maxVal
338+
}
339+
var res = 0
340+
for (i in 0 until n) {
341+
val start = events[i][0]
342+
val end = events[i][1]
343+
val value = events[i][2]
344+
val nextIndex = findNextNonOverlapping(events, end)
345+
val nextValue = if (nextIndex == n) 0 else dp[nextIndex]
346+
res = maxOf(res, value + nextValue)
347+
}
348+
return res
349+
}
350+
351+
private fun findNextNonOverlapping(events: Array<IntArray>, end: Int): Int {
352+
var left = 0
353+
var right = events.size
354+
while (left < right) {
355+
val mid = left + (right - left) / 2
356+
if (events[mid][0] <= end) {
357+
left = mid + 1
358+
} else {
359+
right = mid
360+
}
361+
}
362+
return left
324363
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
package com.github.contest.graph
22

3+
/**
4+
* 997. Find the Town Judge
5+
*/
6+
7+
fun findJudge(n: Int, trust: Array<IntArray>): Int {
8+
if (trust.isEmpty() && n == 1) return 1
9+
val inEdges = IntArray(n + 1)
10+
for (person in trust) {
11+
inEdges[person[0]]--
12+
inEdges[person[1]]++
13+
}
14+
15+
for (person in inEdges.indices) {
16+
if (inEdges[person] == n - 1) return person
17+
}
18+
return -1
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.contest.heap
2+
3+
import java.util.PriorityQueue
4+
5+
/**
6+
* 3066. Minimum Operations to Exceed Threshold Value II
7+
* Alternative Solution
8+
*/
9+
10+
11+
fun minOperationsAlternativeSolution(nums: IntArray, k: Int): Int {
12+
var operations = 0
13+
val pq = PriorityQueue<Long>()
14+
pq.addNums(nums)
15+
16+
while (pq.size > 1 && pq.peek() < k) {
17+
val x = pq.poll()
18+
val y = pq.poll()
19+
val new = minOf(x, y) * 2 + maxOf(x, y)
20+
pq.offer(new)
21+
operations++
22+
}
23+
24+
return operations
25+
}
26+
27+
private fun PriorityQueue<Long>.addNums(nums: IntArray) {
28+
val list = mutableListOf<Long>()
29+
nums.forEach { list.add(it.toLong()) }
30+
this.addAll(list)
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.contest.heap
2+
3+
import java.util.PriorityQueue
4+
5+
/**
6+
* 3066. Minimum Operations to Exceed Threshold Value II
7+
* Prod Variant
8+
*/
9+
10+
fun minOperationProdVariant(nums: IntArray, k: Int): Int {
11+
var operation = 0
12+
val pq = PriorityQueue<Long>().apply { nums.forEach { offer(it.toLong()) } }
13+
14+
while (pq.size > 1 && pq.peek() < k) {
15+
val (x, y) = listOf(pq.poll(), pq.poll()).sorted()
16+
pq.offer(x * 2 + y)
17+
operation++
18+
}
19+
20+
return operation
21+
}
22+
23+
24+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.github.contest.heap.customStructure
2+
3+
class MinHeap<T : Comparable<T>> {
4+
5+
private val heap: MutableList<T> = mutableListOf()
6+
7+
val size: Int
8+
get() = heap.size
9+
10+
fun isEmpty(): Boolean = heap.isEmpty()
11+
12+
fun peek(): T? = heap.firstOrNull()
13+
14+
fun offer(element: T) {
15+
heap.add(element)
16+
heapifyUp(heap.size - 1)
17+
}
18+
19+
fun poll(): T? {
20+
if (isEmpty()) throw NoSuchElementException("No Elements")
21+
swap(0, heap.size - 1)
22+
val element = heap.removeLast()
23+
heapifyDown(0)
24+
return element
25+
}
26+
27+
28+
private fun heapifyUp(index: Int) {
29+
var currentIndex = index
30+
var parentIndex = getParentIndex(currentIndex)
31+
while (currentIndex > 0 && heap[currentIndex] < heap[parentIndex]) {
32+
swap(currentIndex, parentIndex)
33+
currentIndex = parentIndex
34+
parentIndex = getParentIndex(currentIndex)
35+
}
36+
}
37+
38+
private fun heapifyDown(index: Int) {
39+
var currentIndex = index
40+
while (hasLeftChild(currentIndex)) {
41+
var smallerChildIndex = getLeftChildIndex(currentIndex)
42+
if (hasRightChild(currentIndex) && heap[getRightChildIndex(currentIndex)] < heap[smallerChildIndex]) {
43+
smallerChildIndex = getRightChildIndex(currentIndex)
44+
}
45+
if (heap[currentIndex] < heap[smallerChildIndex]) {
46+
break
47+
} else {
48+
swap(currentIndex, smallerChildIndex)
49+
}
50+
currentIndex = smallerChildIndex
51+
}
52+
}
53+
54+
55+
private fun getParentIndex(index: Int): Int = (index - 1) / 2
56+
private fun getLeftChildIndex(index: Int): Int = 2 * index + 1
57+
private fun getRightChildIndex(index: Int): Int = 2 * index + 2
58+
private fun hasLeftChild(index: Int): Boolean = getLeftChildIndex(index) < heap.size
59+
private fun hasRightChild(index: Int): Boolean = getRightChildIndex(index) < heap.size
60+
61+
private fun swap(index1: Int, index2: Int) {
62+
val temp = heap[index1]
63+
heap[index1] = heap[index2]
64+
heap[index2] = temp
65+
}
66+
}
67+
68+
69+
70+

0 commit comments

Comments
 (0)