0807
This commit is contained in:
parent
3939fe4557
commit
beebc54755
6 changed files with 166 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
|||
fun main() {
|
||||
val t = Test75()
|
||||
val t = Test763()
|
||||
t.test()
|
||||
}
|
27
src/Test121.kt
Normal file
27
src/Test121.kt
Normal file
|
@ -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)))
|
||||
}
|
||||
}
|
42
src/Test45.kt
Normal file
42
src/Test45.kt
Normal file
|
@ -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)))
|
||||
|
||||
}
|
||||
}
|
8
src/Test46.kt
Normal file
8
src/Test46.kt
Normal file
|
@ -0,0 +1,8 @@
|
|||
class Test46 {
|
||||
class Solution {
|
||||
fun permute(nums: IntArray): List<List<Int>> {
|
||||
val result = arrayOf(intArrayOf())
|
||||
for (i in)
|
||||
}
|
||||
}
|
||||
}
|
38
src/Test55.kt
Normal file
38
src/Test55.kt
Normal file
|
@ -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)))
|
||||
|
||||
}
|
||||
}
|
50
src/Test763.kt
Normal file
50
src/Test763.kt
Normal file
|
@ -0,0 +1,50 @@
|
|||
import kotlin.math.max
|
||||
|
||||
class Test763 {
|
||||
class Solution {
|
||||
fun partitionLabels(s: String): List<Int> {
|
||||
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"))
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue