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:

  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.
  3. 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'.
  4. Example 1:
  5. Input: 3
  6. Output: 3
  7. Explanation:
  8. Intitally, we have one character 'A'.
  9. In step 1, we use Copy All operation.
  10. In step 2, we use Paste operation to get 'AA'.
  11. In step 3, we use Paste operation to get 'AAA'.
  12. Note:
  13. 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;

}

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

}

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. Google的SPDY协议成为HTTP 2.0的基础

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt384 据TNW援引 IFTF HTTP 工作组主席 Mark Notting ...

  2. AJAX 处理xml 数据

    //这个方式返回的得是 xml标准的对象,可以返回 xml字符串,前端js 使用转为xml   function createXml(str){ if(document.all){//IE浏览器 va ...

  3. 结对作业(1)----基于GUI的四则运算

    小伙伴:201421123031 余洋 201421123044  潘志坚  题目要求: 我们在个人作业1中,用各种语言实现了一个命令行的四则运算小程序.进一步,本次要求把这个程序做成GUI(可以是W ...

  4. IT之光

    作为一个IT界的新新人才,现在拥有第一个博客,可以在这里学习和分享IT方面的知识和技术.

  5. 201521123107 《Java程序设计》第3周学习总结

    第3周作业-面向对象基本概念 1.本周学习总结 2.书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static ...

  6. 201521123102 《Java程序设计》第6周学习总结

    1. 本周学习总结 2. 书面作业 Q1.clone方法 1.1 Object对象中的clone方法是被protected修饰,在自定义的类中覆盖clone方法时需要注意什么? 子类要实现Clonea ...

  7. 201521123070 《JAVA程序设计》第6周学习总结

    1. 本章学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...

  8. 201521123095《java程序设计》第4周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结上课内容. 对于一个系统中,对于名词大多为类或属性,对于动词大多为方法. 1.3 注释的应用 使用类的注释与 ...

  9. java201521123118《java程序设计》第3周总结

    1. 本周学习 总结初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用纸笔或者下面的工具画出本周学习到的知识点.截图或者拍照上传. 2. 书面作 ...

  10. 201521123045 《Java程序设计》第9周学习总结

    201521123045 <Java程序设计>第9周学习总结 1. 本章学习总结 2. 书面作业 本次PTA作业题集异常 1.常用异常题目5-11.1 截图你的提交结果(出现学号) 1.2 ...