package offer.middle

import java.util.*

fun main() {
    // 剑指 Offer 31. 栈的压入、弹出序列
    // https://leetcode.cn/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof/

    println(validateStackSequences1(intArrayOf(2,1,0), intArrayOf(1,2,0)))
}

fun validateStackSequences(pushed: IntArray, popped: IntArray): Boolean {

    val stack = Stack<Int>()
    var popIndex = 0
    for (i in pushed) {
        stack.push(i)
        // 循环判断与出栈
        while (stack.isNotEmpty() && stack.peek() == popped[popIndex]) {
            stack.pop()
            popIndex++
        }
    }
    return stack.isEmpty()
}

fun validateStackSequences1(pushed: IntArray, popped: IntArray): Boolean {
    val stack = Stack<Int>()
    var poppedIndex = 0
    for (i in pushed) {
        if (i == popped[poppedIndex]) {
            poppedIndex++
            while (stack.isNotEmpty() && poppedIndex < popped.size && stack.peek() == popped[poppedIndex]) {
                poppedIndex++
                stack.pop()
            }
        }else {
            stack.push(i)
        }
    }

    return stack.isEmpty()
}

class Test {
    fun higherFunction(age: Int, param: (unit:String, weight:Float)->String) {
        val weightUnit = param("kg", 20.0f)
        println(weightUnit)
    }
    fun testLambda() {
        higherFunction(1) {unit, weight ->
            "$weight $unit"
        }
        val listOf = listOf(1, 2, 3)
        listOf.forEach {
            println(it)
        }
        listOf.find {
            it % 2 == 0
        }
    }

    fun normalFunc(block: () -> Unit) {
        block()
        println("call normal func")
    }

    inline fun inlineFunc(block: () -> Unit) {
        block()
        println("call inline func")
    }

    fun callInline() {
        val normal = normalFunc{

        }

        val in_line = inlineFunc {

        }
    }
}