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. 最小公倍数(BNUOJ30195)

    最小公倍数 Time Limit: 0 ms Case Time Limit: 0 ms Memory Limit: 0 KBSubmit: 17 Accepted: 1 This problem w ...

  2. First Date (hnoj12952)日期计算

    First Date Time Limit: 3000ms, Special Time Limit:7500ms, Memory Limit:65536KB Total submit users: 7 ...

  3. 应用rbac组件 动态生成一级菜单

    动态生成一级菜单 改表结构 需要知道是否是菜单\icon\名称权限表 +字段: is_menu = models.BooleanField(max_length=32,verbose_name='是否 ...

  4. 说说对npm的开发模式和生产模式的理解

    nodejs这些年的发展非常快,相信没有哪个前端不知道的了,npm也成为了前端开发中经常用到了的一个命令.那么npm不是只用一个 "npm install xxx"命令就够了吗?实 ...

  5. JS里面的懒加载(lazyload)

    懒加载技术(简称lazyload)并不是新技术, 它是js程序员对网页性能优化的一种方案.lazyload的核心是按需加载 涉及到图片,falsh资源 , iframe, 网页编辑器(类似FCK)等占 ...

  6. js将对象数组按照自定义规则排序

    javascript对一个对象数组进行自定义规则排序,对象中有两个字段. 按照对象中一个字段a的值从小到大规则排序, 效果如下: 排序前: [0]:a=9,b=3 [1]:a=33,b=7 [2]:a ...

  7. C# 如何防止重放攻击

    重放攻击 重放攻击是指黑客通过抓包的方式,得到客户端的请求数据及请求连接,重复的向服务器发送请求的行为. 比如你有一个 “购买” 的操作,当你点击购买按钮时,向服务器发送购买的请求.而这时黑客对你的请 ...

  8. VMWare虚拟机安装Linux系统时安装界面显示不全的解决、Linux分区

    1.只有最下面的按钮不全(可以显示字的一部分):可以用Tab键操控进行下一步 2.只有一半左右的界面:用Alt+鼠标左键移动安装界面 3.关于Linux分区:如果想用Linux进行深入操作,尽量不要选 ...

  9. python2编码的问题

    1,python2的默认编码是ascii码. 2,python2中有2中数据模型来支持字符串这种数据类型,分别为str和unicode. 3,uncode转换为其他编码是encode,其他编码转换成u ...

  10. Huawei 常用基本配置命令一

    华为交换机的三种视图: 用户视图, 系统视图, 接口视图 用户视图: 刚开始登入交换机时的视图,一般看到的是尖括号<> . save // 配置完交换机后保存当前配置的命令 system- ...