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     }
  • 相关阅读:
    NYOJ--703
    CDOJ--1369
    NYOJ--205
    NYOJ--520
    NYOJ--69
    CDOJ--1237
    [gist]Android SHA-1
    ConnectionAbortedError: [WinError 10053] 你的主机中的软件中止了一个已建立的连接
    SyntaxError:unexpected EOF while parsing(<string,line 0>)
    Django之get请求url的参数
  • 原文地址:https://www.cnblogs.com/wzj4858/p/7698558.html
Copyright © 2011-2022 走看看