Skip to content

Commit 670c2d7

Browse files
add first problems of graph 997
1 parent b0f06ec commit 670c2d7

File tree

3 files changed

+90
-23
lines changed

3 files changed

+90
-23
lines changed
Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.github.contest
22

3+
import com.github.contest.dp.countVowels
4+
import com.github.contest.graph.findJudge
35
import com.github.contest.heap.customStructure.MinHeap
4-
import java.util.PriorityQueue
56

67

78
/**
@@ -10,35 +11,45 @@ import java.util.PriorityQueue
1011

1112
fun main() {
1213

13-
val heap = MinHeap<Int>()
14-
heap.offer(3)
15-
heap.offer(1)
16-
heap.offer(2)
17-
heap.offer(5)
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+
)
1826

27+
}
1928

20-
println(heap.poll())
29+
fun vowels() {
2130

31+
/**
32+
* a a a
33+
* aa
34+
* aa
35+
* aaa
36+
* count - 10
37+
*/
2238

23-
// halveArrayTest(intArrayOf(3, 8, 20))
39+
countVowels("aaa")
2440

2541
}
2642

2743

28-
fun halveArrayTest(nums: IntArray): Int {
29-
val heap = PriorityQueue<Double>(reverseOrder())
30-
nums.forEach { heap.offer(it.toDouble()) }
31-
val sum = nums.sum().toDouble()
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+
3252

33-
val halve = sum / 2
34-
var halvest = 0.0
35-
var operations = 0
36-
while (halvest < halve) {
37-
val element = heap.poll() / 2
38-
heap.offer(element)
39-
halvest += element
40-
operations++
41-
}
53+
println(heap.poll())
4254

43-
return operations
44-
}
55+
}

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+
}

0 commit comments

Comments
 (0)