scala solution


  • 0
    object Solution {
        def lastRemaining(n: Int): Int = {
            def process(left: Boolean, head: Int, step: Int, remaining: Int): Int = {
                if (remaining > 1) {
                    if (left || remaining % 2 == 1) {
                        process(!left, head + step, step * 2, remaining / 2)
                    } else {
                        process(!left, head, step * 2, remaining / 2)
                    }
                } else head
            }
            process(true, 1, 1, n)
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.