Initially on a notepad only one character 'A' is present. You can perform two operations on this notepad for each step:

  1. Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).

  2. Paste: You can paste the characters which are copied last time.

Given a number n. You have to get exactly n 'A' on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n 'A'.

Example 1:

Input: 3
Output: 3
Explanation:
Intitally, we have one character 'A'.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get 'AA'.
In step 3, we use Paste operation to get 'AAA'.

Note:

  1. The n will be in the range [1, 1000].

想法:DP方式,状态转移表达时候result[i] = min(result[i],result[j]+i/j)

class Solution {
public:
    int minSteps(int n) {
        vector<);
        result[] = ;
         ; i <= n ; i++){
            result[i] = i;
             ; j < i/ ; j++){
                ){
                    result[i] = min(result[i],result[j] + i / j);
                }
            }
        }
        return result[n];
    }
};

leetcode650—2 Keys Keyboard的更多相关文章

  1. leetcode650 2 Keys Keyboard

    思路: 把给定的数分解质因子之后,对于每一个质因子x,都需要x次操作(一次copy all操作和x-1次paste),所以答案就是对分解后的所有质因子求和. 实现: class Solution { ...

  2. [LeetCode] 4 Keys Keyboard 四键的键盘

    Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...

  3. [LeetCode] 2 Keys Keyboard 两键的键盘

    Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...

  4. LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion

    1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

  5. Leetcode 之 Keys Keyboard

    1. 2 Keys Keyboard 先把dp的最小不走都设置为无穷大(Integer.MAX_VALUE),初始化条件:dp[0] = dp[1] = 0,状态转移方程为dp[i] = Math.m ...

  6. LeetCode 4 Keys Keyboard

    原题链接在这里:https://leetcode.com/problems/4-keys-keyboard/description/ 题目: Imagine you have a special ke ...

  7. [leetcode] 650. 2 Keys Keyboard (Medium)

    解法一: 暴力DFS搜索,对每一步进行复制还是粘贴的状态进行遍历. 注意剪枝的地方: 1.当前A数量大于目标数量,停止搜索 2.当前剪贴板数字大于等于A数量时,只搜索下一步为粘贴的状态. Runtim ...

  8. [LeetCode] 650. 2 Keys Keyboard 两键的键盘

    Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...

  9. [LeetCode] 651. 4 Keys Keyboard 四键的键盘

    Imagine you have a special keyboard with the following keys: Key 1: (A): Print one 'A' on screen. Ke ...

随机推荐

  1. mysql5.7.13.zip安装(windows)

    按照我之前安装5.6的安装方法(MySQL免安装版下载与配置)安装5.7的时候出现问题: mysql服务无法启动,服务没有报任何错误

  2. ssh免密登陆及时间设置

    1.ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa // 在自己主机生成私钥和公钥 2.scp id_rsa.pub centos@s201:/home/centos ...

  3. windows 公司内部搭建禅道(项目管控)

    禅道的搭建异常爽快,非常方便,一般情况下我们使用开源版就可以了.下面是搭建流程,这里主要记录一些前期的注意事项 使用一键安装版就可以,很快,禅道安装主机安装好所需的Apache容器和mysql数据库, ...

  4. 排序算法(1)--Insert Sorting--插入排序[1]--straight insertion sort--直接插入排序

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 1.基本思想 将一个记录插入到已排序好的有序表中,从而得到一个新,记录数增1的有序表.即:先将序列的第1个记录看成 ...

  5. 【代码笔记】iOS-UIActionSheet动态添加按钮

    一,效果图. 二,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIVi ...

  6. 【读书笔记】iOS-网络-HTTP-请求内容

    一,GET方法. 从服务器获取一段内容,用HTTP术语来说就是实体.GET请求通常不包含请求体,不过也是可以包含的.有些网络缓存设施只会缓存GET响应.GET请求通常不会导致服务器端的数据变化. 二, ...

  7. iOS设备上出现的click,live,on点击失去效果

    iOS设备上出现的点击事件失效,但是在Android上可以正常使用, 1.iOS设备对标签点击限制,不认为是可点击的标签,需要给要绑定点击事件的标签加上一个样式,cursor:pointer:这样就可 ...

  8. 网络基础 HTTP协议之缓存简介

    HTTP协议之缓存简介 by:授客 QQ:1033553122 用浏览器查看缓存 IE为例,Tools->Internet options -> View files,如图 点击图示的Vi ...

  9. vue与原生混合开发

    前段时间,做了一个混合开发的项目,主要是以vue框架开发h5页面,使用cordova作为中间沟通桥梁,实现了h5与安卓.iOS的混合开发,由于从事iOS开发,h5也是刚接触不久,很多深入原理还不太清楚 ...

  10. 开源免费的文档协作系统 onlyoffice平台轻松部署

    请移步至此,更详细:http://blog.csdn.net/hotqin888/article/details/79337881 ONLYOFFICE是一个免费的.开源的企业办公套件,用于在线组织团 ...