package Classify.DP.Medium;

import org.junit.jupiter.api.Test;

/**

*

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

 Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).
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:
The n will be in the range [1, 1000].

*/

public class KeysKeyboard {

/**

* 这题的思路就是 dp 代表的就是当前的 A 的数量,然后我们优先考虑上一步复制过的情况,因为这样能够最快的填满 n 个 A ,但是使用这种情况要注意的就是

* 我们可能上部赋值我们最后收不了尾,所以必须有一个判断就是我们能用上一步的复制的内容填满剩下的 A ,也就是 n % dp[i - 1] == 0

* 里面的内容的意思就是我们使用了上一步的复制的内容那么我们的字符串就直接翻倍,而且我们进行了一次复制,那么我们 count 就得加2

* @param n

* @return

*/

public int minSteps(int n) {

if (n == 1) {

return 0;

}

int[] dp = new int[1000];

dp[0] = 1;

int paste = 1;

int i = 0;

int count = 1;

while (dp[i] <= n) {

++i;

if (n % dp[i - 1] == 0) {

paste = dp[i - 1];

dp[i] = dp[i - 1] * 2;

++count;

if (paste != 1) {

++count;

}

} else {

dp[i] = dp[i - 1] + paste;

++count;

}

if (dp[i] == n) {

return count;

}

}

return count;

}

@Test
public void fun() {
System.out.println(minSteps(10));
}

}

LeetCode-2 Keys Keyboard的更多相关文章

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

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

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

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

  3. LeetCode 4 Keys Keyboard

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

  4. Leetcode 之 Keys Keyboard

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

  5. 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 ...

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

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

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

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

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

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

  9. LC 650. 2 Keys Keyboard

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

  10. LeetCode 650 - 2 Keys Keyboard

    LeetCode 第650题 Initially on a notepad only one character 'A' is present. You can perform two operati ...

随机推荐

  1. Entity Framework Core 2.0 数据库迁移

    看见过几篇其他大神写的关于EFCore2.0的文章.本人有点小白,一开始看文档的时候除了一些基本操作外其他部分几乎没有读懂,我估计会有一部分人跟我一样,因为人家读懂了的早就懂了. 在这里我写一下我自己 ...

  2. vi/vim键盘图-

    vi/vim键盘图-----又一张桌面背景好图 也许还是有很多人不能愿意用CLI的vi/Vim来写东西,不过,当你真的习惯了,它的高效性就是不可估量了.下面的这张图,一看就明白了,从此,学习变的不再艰 ...

  3. springmvc注解

    简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...

  4. 团队作业八——第二次团队冲刺(Beta版本)第5天

    团队作业八--第二次团队冲刺(Beta版本)第5天 一.每个人的工作 (1) 昨天已完成的工作 完成界面跳转界面. (2) 今天计划完成的工作 简单模式逻辑代码涉及与相关功能的具体实现 (3) 工作中 ...

  5. java继承中的初始化顺序

    初始化顺序:父类的静态变量-->父类的静态代码块-->子类的静态变量-->子类的静态代码快-->父类的非静态变量(父类的非静态代码块)-->父类的构造函数-->子类 ...

  6. Swing-布局管理器之FlowLayout(流式布局)-入门

    FlowLayout应该是Swing布局管理器学习中最简单.最基础的一个.所谓流式,就是内部控件像水流一样,从前到后按顺序水平排列,直到达到容器的宽度时跳转到第二行.既然是水平排列,那么就存在三种基本 ...

  7. 关于在git添加远程地址的过程中遇到的问题

    问题产生的过程 我根据菜鸟教程的步骤,做了如下操作: 1.打开安装文件夹中的git-bash程序 2.设置username和email 3.添加远程地址 结果如下: 之后通过百度知道要先git ini ...

  8. 201521123099 《Java程序设计》第八周学习总结

    1. 本周学习总结 2. 书面作业 本次作业题集集合 List中指定元素的删除(题目4-1) 1.1 实验总结 老师上课解释的还有私下问同学大概能懂.主要理解到一点在删除List中元素时要考虑元素删除 ...

  9. 201521123077 《Java程序设计》第3周学习总结

    1. 本周学习总结 2. 书面作业 Q1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; ...

  10. 201521123051《Java程序设计》第十一周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. Java多线程同步的方法: (1)同步方法:即有synchronized关键字修饰的方法. 由于java的每个对象 ...