zoukankan      html  css  js  c++  java
  • 650. 2 Keys Keyboard

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

    题目含义:求获取n个A的最小操作步骤的数目minStep
    1. Copy All: 只能全部复制,不能部分复制.
    2. Paste: 拷贝留在剪贴板上的字符
     1     public int minSteps(int n) {
     2 //        先把dp的最小步骤都设置为无穷大, 初始化条件为:dp[0]=dp[1]=0 ,状态转移方程为:
     3 //        dp[i]=min(dp[i],dp[j]+i/j),i>1,j<i且i是j的整数倍
     4 //        上述状态转移方程表示:如果i是j的倍数,那么i可以通过粘贴(i/j-1)次j 得到, 再加上一次复制操作,
     5         int[] dp = new int[n+1];
     6         if (n<2) return 0;
     7         Arrays.fill(dp,Integer.MAX_VALUE);
     8         dp[0]=0;
     9         dp[1]=0;
    10         for (int i=2;i<n+1;i++)
    11         {
    12             for (int j=1;j<i;j++)
    13             {
    14                 if (i%j==0) dp[i]=Math.min(dp[i],dp[j] + i/j);
    15             }
    16         }
    17         return dp[n];        
    18     }
  • 相关阅读:
    equals比较对象
    解决打包时出现的Failed to verify bitcode
    苹果的MDM简介
    unable to boot the simulator,无法启动模拟器已解决
    利用NSCalendar类实现日期的比较
    服务器开启https协议
    解决NSTimer存在的内存泄漏的问题
    ipad和iphone的适配
    集成shareSDK错误总结(新浪微博)
    AFN解析器里的坑
  • 原文地址:https://www.cnblogs.com/wzj4858/p/7698558.html
Copyright © 2011-2022 走看看