zoukankan      html  css  js  c++  java
  • fewest number of operations needed to get to 1

    package _interview_question
    
    import java.util.*
    
    /**
     * The question is to find the fewest number of operations needed to get to 1.
    available operations:
    - add 1
    - subtract 1
    - divide by 2
    
    Example 1:
    input: 15, output: 5,
    because: 15->16->8->4->2->1
    
    Example 2:
    input: 10, output: 4,
    because: 10->5->4->3->2->1
    
    Have any constraints?
     * */
    class HelpNode(var value: Double) {
        var step: Int = 0
    }
    
    class Solution10 {
        /*
          solution:bfs, Time complexity:O(3^n), Space complexity:O(n)
        * */
        fun getToOne(num: Int): Int {
            val queue = LinkedList<HelpNode>()
            val node = HelpNode(num.toDouble())
            queue.offer(node)
            while (queue.isNotEmpty()) {
                val cur = queue.poll()
                if (cur.value == 1.0) {
                    return cur.step
                }
                val add = cur.value + 1
                val subtract = cur.value - 1
                val divide = cur.value / 2//because divide, so we need double to keep correct
                //println("divide:${divide}")
    
                val addNode = HelpNode(add)
                addNode.step = cur.step + 1
                queue.offer(addNode)
    
                val subtractNode = HelpNode(subtract)
                subtractNode.step = cur.step + 1
                queue.offer(subtractNode)
    
                val divideNode = HelpNode(divide)
                divideNode.step = cur.step + 1
                queue.offer(divideNode)
            }
            return -1
        }
    }
  • 相关阅读:
    Spring Boot----整合SpringCloud
    python apscheduler学习
    Java8 Lambda
    Java Json操作
    python 环境相关
    大数据 Hadoop 单机版安装
    大数据 CDH 6.2 安装
    Java8 时间API
    python 字符串方法
    《JavaScript高级程序设计》笔记——第一章到第三章
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13311421.html
Copyright © 2011-2022 走看看