diff --git a/.idea/gradle.xml b/.idea/gradle.xml
index bfe173f1..96ccfae7 100644
--- a/.idea/gradle.xml
+++ b/.idea/gradle.xml
@@ -4,6 +4,7 @@
-
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 0ad17cbd..9f71c83d 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,7 +1,7 @@
-
+
diff --git a/.idea/other.xml b/.idea/other.xml
deleted file mode 100644
index 22069ff9..00000000
--- a/.idea/other.xml
+++ /dev/null
@@ -1,549 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt
index 101ed5a5..4190f537 100644
--- a/contest/src/main/java/com/github/contest/Execute.kt
+++ b/contest/src/main/java/com/github/contest/Execute.kt
@@ -1,7 +1,7 @@
package com.github.contest
-import com.github.contest.design.WordDictionary
+import com.github.contest.graph.mostProfitablePath
import java.util.TreeMap
@@ -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() {
diff --git a/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt b/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
index 767dfb54..761e796b 100644
--- a/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
+++ b/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
@@ -140,6 +140,23 @@ fun mergeArrays(nums1: Array, nums2: Array): Array
}.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
+}
+
diff --git a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt
index e13fab9f..75368cf4 100644
--- a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt
+++ b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt
@@ -541,6 +541,47 @@ fun longestSubsequence(arr: IntArray, difference: Int): Int {
return maxLen
}
+/**
+ * 2140. Solving Questions With Brainpower
+ */
+
+fun mostPoints(questions: Array): Long {
+ return solve(questions, 0)
+}
+
+private fun solve(questions: Array, 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): 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]
+}
+
diff --git a/contest/src/main/java/com/github/contest/graph/GraphLeetcode.kt b/contest/src/main/java/com/github/contest/graph/GraphLeetcode.kt
index 4b541800..b8f05927 100644
--- a/contest/src/main/java/com/github/contest/graph/GraphLeetcode.kt
+++ b/contest/src/main/java/com/github/contest/graph/GraphLeetcode.kt
@@ -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
@@ -64,4 +67,85 @@ fun findCenter(edges: Array): Int {
a == c || a == d -> a
else -> b
}
-}
\ No newline at end of file
+}
+
+/**
+ * 2467. Most Profitable Path in a Tree
+ */
+
+
+fun mostProfitablePath(edges: Array, bob: Int, amount: IntArray): Int {
+ val graph = Array(amount.size) { mutableListOf() }
+ 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()
+ 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,
+ graph: Array>
+): 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>,
+ 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
+}
+
+
+