File tree Expand file tree Collapse file tree 3 files changed +90
-23
lines changed
contest/src/main/java/com/github/contest Expand file tree Collapse file tree 3 files changed +90
-23
lines changed Original file line number Diff line number Diff line change 1
1
package com.github.contest
2
2
3
+ import com.github.contest.dp.countVowels
4
+ import com.github.contest.graph.findJudge
3
5
import com.github.contest.heap.customStructure.MinHeap
4
- import java.util.PriorityQueue
5
6
6
7
7
8
/* *
@@ -10,35 +11,45 @@ import java.util.PriorityQueue
10
11
11
12
fun main () {
12
13
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
+ )
18
26
27
+ }
19
28
20
- println (heap.poll())
29
+ fun vowels () {
21
30
31
+ /* *
32
+ * a a a
33
+ * aa
34
+ * aa
35
+ * aaa
36
+ * count - 10
37
+ */
22
38
23
- // halveArrayTest(intArrayOf(3, 8, 20) )
39
+ countVowels( " aaa " )
24
40
25
41
}
26
42
27
43
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
+
32
52
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())
42
54
43
- return operations
44
- }
55
+ }
Original file line number Diff line number Diff line change @@ -321,4 +321,43 @@ fun countVowels(word: String): Long {
321
321
}
322
322
323
323
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
324
363
}
Original file line number Diff line number Diff line change 1
1
package com.github.contest.graph
2
2
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
+ }
You can’t perform that action at this time.
0 commit comments