博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Leetcode]650. 2 Keys Keyboard
阅读量:7085 次
发布时间:2019-06-28

本文共 1162 字,大约阅读时间需要 3 分钟。


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: 3Output: 3Explanation: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'.
class Solution {public:    int minSteps(int n) {        if (n == 1)            return 0;        int dp[n]; dp[0] = 0; dp[1] = 2;        for (int i = 3; i <= n; ++i){            int index = -1;            for (int j = 2; j < i; ++j)                if (i % j == 0)                    index = j;            if (index == -1)                dp[i-1] = i;            else                 dp[i-1] = dp[index-1] + (i / index);        }        return dp[n-1];    }};

 

转载于:https://www.cnblogs.com/David-Lin/p/8359027.html

你可能感兴趣的文章
coredata数据库模型复制
查看>>
Django 信号处理
查看>>
Linux加载光驱优盘
查看>>
Wireshark抓包工具使用教程以及常用抓包规则
查看>>
MySQL案例分享之系统消息
查看>>
Spring Security简介
查看>>
PCRE配置共享库
查看>>
find使用方法(筆記)
查看>>
系统管理命令watch
查看>>
symantec运行报错及解决汇总
查看>>
uptime命令与系统负载
查看>>
将中文字符串分割为数组 解决str_split中文乱码php
查看>>
Discuz论坛 启动报错(1045) notconnect 解决方法
查看>>
ambari与ClouderaManager
查看>>
cdn加速
查看>>
为什么学习Linux系统?
查看>>
Windows Server2012上使用Nginx做文件服务器
查看>>
linux下的apache配置
查看>>
app测试和web端测试的区别
查看>>
一瓶汽水1元,两瓶汽水可换一瓶,现有20元,最多可喝多少瓶汽水
查看>>