Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

549 changes: 0 additions & 549 deletions .idea/other.xml

This file was deleted.

22 changes: 13 additions & 9 deletions contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.contest


import com.github.contest.design.WordDictionary
import com.github.contest.graph.mostProfitablePath
import java.util.TreeMap


Expand All @@ -11,15 +11,19 @@ import java.util.TreeMap

fun main() {

// val edges = arrayOf(intArrayOf(0, 1), intArrayOf(1, 2), intArrayOf(1, 3), intArrayOf(3, 4))
// mostProfitablePath(edges, 3, intArrayOf(-2, 4, 2, -4, 6)).also { println(it) }
//
testing()

val wordDictionary = WordDictionary()
wordDictionary.addWord("bad")
wordDictionary.addWord("dad")
wordDictionary.addWord("mad")
wordDictionary.search("pad").also { println(it) } // return False
wordDictionary.search("bad").also { println(it) } // return True
wordDictionary.search(".ad").also { println(it) } // return True
wordDictionary.search("b..").also { println(it) }
}

fun testing() {
val list = listOf(1, 2, 3)
list.reduceRight { i, acc ->
println("$i, $acc")
acc - i
}.also { println(it) }
}

fun generateTesting() {
Expand Down
17 changes: 17 additions & 0 deletions contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ fun mergeArrays(nums1: Array<IntArray>, nums2: Array<IntArray>): Array<IntArray>
}.toTypedArray()
}

/**
* 2873. Maximum Value of an Ordered Triplet I
*/

fun maximumTripletValue(nums: IntArray): Long {
var max = 0L
for (i in nums.indices) {
for (j in i + 1 until nums.size) {
for (k in j + 1 until nums.size) {
max = maxOf(max, ((nums[i] - nums[j]).toLong() * nums[k].toLong()))
}
}
}

return max
}




Expand Down
41 changes: 41 additions & 0 deletions contest/src/main/java/com/github/contest/dp/DpLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,47 @@ fun longestSubsequence(arr: IntArray, difference: Int): Int {
return maxLen
}

/**
* 2140. Solving Questions With Brainpower
*/

fun mostPoints(questions: Array<IntArray>): Long {
return solve(questions, 0)
}

private fun solve(questions: Array<IntArray>, index: Int): Long {
if (index >= questions.size) {
return 0
}

val points = questions[index][0]
val brainpower = questions[index][1]
val take = points.toLong() + solve(questions, index + brainpower + 1)


val skip = solve(questions, index + 1)

return maxOf(take, skip)
}

fun mostPointsDp(questions: Array<IntArray>): Long {
val n = questions.size
val dp = LongArray(n + 1) { 0L }

for (i in n - 1 downTo 0) {
val points = questions[i][0]
val brainpower = questions[i][1]
val nextQuestion = i + brainpower + 1

val take = points.toLong() + (if (nextQuestion < n) dp[nextQuestion] else 0L)
val skip = dp[i + 1]

dp[i] = maxOf(take, skip)
}

return dp[0]
}




Expand Down
86 changes: 85 additions & 1 deletion contest/src/main/java/com/github/contest/graph/GraphLeetcode.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.github.contest.graph

import java.util.Arrays
import java.util.LinkedList
import kotlin.math.max


/**
* 997. Find the Town Judge
Expand Down Expand Up @@ -64,4 +67,85 @@ fun findCenter(edges: Array<IntArray>): Int {
a == c || a == d -> a
else -> b
}
}
}

/**
* 2467. Most Profitable Path in a Tree
*/


fun mostProfitablePath(edges: Array<IntArray>, bob: Int, amount: IntArray): Int {
val graph = Array(amount.size) { mutableListOf<Int>() }
for (edge in edges) {
graph[edge[0]].add(edge[1])
graph[edge[1]].add(edge[0])
}

val bobPath = IntArray(amount.size)
Arrays.fill(bobPath, -1)
val path = ArrayList<Int>()
fillBobPath(bob, -1, path, graph)

for (i in path.indices) {
bobPath[path[i]] = i
}

return getAliceMaxScore(0, -1, 0, bobPath, graph, 0, amount)
}

private fun fillBobPath(
node: Int,
parentNode: Int,
path: ArrayList<Int>,
graph: Array<MutableList<Int>>
): Boolean {
if (node == 0) return true
for (neighborNode in graph[node]) {
if (neighborNode != parentNode) {
path.add(node)
if (fillBobPath(neighborNode, node, path, graph)) return true
path.removeAt(path.size - 1)
}
}
return false
}

private fun getAliceMaxScore(
node: Int,
parentNode: Int,
currScore: Int,
bobPath: IntArray,
graph: Array<MutableList<Int>>,
timestamp: Int,
amount: IntArray
): Int {
var currScore = currScore
if (bobPath[node] == -1 || bobPath[node] > timestamp) {
currScore += amount[node]
} else if (bobPath[node] == timestamp) {
currScore += amount[node] / 2
}
if (graph[node].size == 1 && node != 0) return currScore
var maxScore = Int.MIN_VALUE
for (neighborNode in graph[node]) {
if (neighborNode != parentNode) {
maxScore = max(
maxScore.toDouble(),
getAliceMaxScore(
neighborNode,
node,
currScore,
bobPath,
graph,
timestamp + 1,
amount
).toDouble()
)
.toInt()
}
}
return maxScore
}