From beebc54755608c01d5b2322ead1ad902fbd06378 Mon Sep 17 00:00:00 2001 From: Kagura Date: Thu, 8 Aug 2024 14:48:10 +0800 Subject: [PATCH] 0807 --- src/Main.kt | 2 +- src/Test121.kt | 27 +++++++++++++++++++++++++++ src/Test45.kt | 42 ++++++++++++++++++++++++++++++++++++++++++ src/Test46.kt | 8 ++++++++ src/Test55.kt | 38 ++++++++++++++++++++++++++++++++++++++ src/Test763.kt | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 src/Test121.kt create mode 100644 src/Test45.kt create mode 100644 src/Test46.kt create mode 100644 src/Test55.kt create mode 100644 src/Test763.kt diff --git a/src/Main.kt b/src/Main.kt index 8dc2b2c..d199a1d 100644 --- a/src/Main.kt +++ b/src/Main.kt @@ -1,4 +1,4 @@ fun main() { - val t = Test75() + val t = Test763() t.test() } \ No newline at end of file diff --git a/src/Test121.kt b/src/Test121.kt new file mode 100644 index 0000000..375f022 --- /dev/null +++ b/src/Test121.kt @@ -0,0 +1,27 @@ +import kotlin.math.max + +class Test121 { + class Solution { + fun maxProfit(prices: IntArray): Int { + if (prices.size == 1){ + return 0 + } + + var minIdx = 0 + var maxProf = 0 + for (i in prices.indices){ + maxProf = max(maxProf,prices[i] - prices[minIdx]) + if (prices[minIdx] > prices[i]){ + minIdx = i + } + + } + return maxProf + } + } + + fun test(){ + println(Solution().maxProfit(intArrayOf(7,1,5,3,6,4))) + println(Solution().maxProfit(intArrayOf(7,6,4,3,1))) + } +} \ No newline at end of file diff --git a/src/Test45.kt b/src/Test45.kt new file mode 100644 index 0000000..2c35a9c --- /dev/null +++ b/src/Test45.kt @@ -0,0 +1,42 @@ +import kotlin.math.max +import kotlin.math.min + +class Test45 { + class Solution { + fun jump(nums: IntArray): Int { + if (nums.size == 1){ + return 0 + } + var i = 0 + var jump = 0 + while (i < nums.size -1){ + if (i+nums[i] >= nums.size-1){ // Jump to self's max is enough + return jump +1 + } + // Perform next jump + jump++ + // Find next max + var maxJumpTo = 0 // Jump score = index+value + var nextIdx = 0 + for (j in i+1 .. min(i+nums[i],nums.size-1)){ + if (maxJumpTo < j+nums[j]){ + maxJumpTo = j+nums[j] + nextIdx = j //Jump to idx at j + } + } + i = nextIdx + } + return jump + } + } + + fun test(): Unit { + println(Solution().jump(intArrayOf(2,3,1,1,4))) + println(Solution().jump(intArrayOf(2,3,0,1,4))) + println(Solution().jump(intArrayOf(0))) + println(Solution().jump(intArrayOf(3,4,3,2,5,4,3))) + println(Solution().jump(intArrayOf(1,2))) + println(Solution().jump(intArrayOf(1,1,1,1))) + + } +} \ No newline at end of file diff --git a/src/Test46.kt b/src/Test46.kt new file mode 100644 index 0000000..c7e37f1 --- /dev/null +++ b/src/Test46.kt @@ -0,0 +1,8 @@ +class Test46 { + class Solution { + fun permute(nums: IntArray): List> { + val result = arrayOf(intArrayOf()) + for (i in) + } + } +} \ No newline at end of file diff --git a/src/Test55.kt b/src/Test55.kt new file mode 100644 index 0000000..21e5146 --- /dev/null +++ b/src/Test55.kt @@ -0,0 +1,38 @@ +class Test55 { + class Solution { + fun canJump(nums: IntArray): Boolean { + if (nums.size == 1){ + return true + } + for (i in nums.indices){ + if (nums[i] == 0){ // Look back + var j = i-1 + var isOk = false + while (j >= 0){ + if (nums[j] + j >= nums.size -1){ + return true + } + if (nums[j] > i-j){ + isOk = true + break + } + j-- + } + if (!isOk) { + return false + } + } + } + return true + } + } + + fun test(): Unit { + println(Solution().canJump(intArrayOf(2,3,1,1,4))) + println(Solution().canJump(intArrayOf(3,2,1,0,4))) + println(Solution().canJump(intArrayOf(0))) + println(Solution().canJump(intArrayOf(2,0))) + println(Solution().canJump(intArrayOf(2,0,0))) + + } +} \ No newline at end of file diff --git a/src/Test763.kt b/src/Test763.kt new file mode 100644 index 0000000..3a06192 --- /dev/null +++ b/src/Test763.kt @@ -0,0 +1,50 @@ +import kotlin.math.max + +class Test763 { + class Solution { + fun partitionLabels(s: String): List { + val lastOccurIndex = Array(26){-1} + fun getLastOccur(index: Int) : Int { + if (lastOccurIndex[s[index].code - 'a'.code] == -1){ // If not calculated, perform now + var charLastOccur = s.length-1 + while (charLastOccur >= index){ // Find last time this character occurs + if (s[charLastOccur] != s[index]){ + charLastOccur -- + }else{ + lastOccurIndex[s[index].code-'a'.code] = charLastOccur + break + } + } + } + return lastOccurIndex[s[index].code - 'a'.code] + } + + var result = intArrayOf() + var left = 0 + var right = getLastOccur(left) + while (left <= s.lastIndex){ + var i = left+1 + while (i < right) { + if (lastOccurIndex[s[i].code - 'a'.code] == -1){ + getLastOccur(i) + } + right = max(right, lastOccurIndex[s[i].code - 'a'.code]) + i++ + } + result += right - left + 1 + if (right == s.lastIndex){ + return result.toList() + } + left = right + 1 + right = getLastOccur(left) + } + return listOf(0) + } + } + fun test(){ + println(Solution().partitionLabels("ababcbacadefegdehijhklij")) + println(Solution().partitionLabels("eccbbbbdec")) + println(Solution().partitionLabels("qiejxqfnqceocmy")) + + } +} \ No newline at end of file