Max Number of A's given four keys

39 篇文章 0 订阅
9 篇文章 0 订阅
/*
  Imagine you have a special keyboard with following four keys: Key 1: Prints 'A' on screenKey 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to bufferKey 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. If you can only press the keyboard for N times (with the above four keys), write a program to produce maximum numbers of A's.
*/
// attention, Ctrl-A and Ctrl-C can copy the text into buffer but not print out.
// Ctrl-V is to print out.
// Ctrl-A + Ctrl-C + Ctrl-V can be sperate and coherant.
#include <iostream>
#include <vector>
using namespace std;

int findMax(int n, vector<int>& maxSolution) {
  if(n < 0) return -1;
  if(n <= 6) return 6;
  int max_so_far = 0;
  int max_with_this_i = 0;
  int multiplier = 2;
  // why start at n - 3?
  // Suppose we have input n.

/*
  Suppose we can the following numbes.
 n - 9
 n - 8
 n - 7
 n - 6            f(n-6), ctrA, ctrlc, ctrlV, ctrlV, ctrlV, ctrV
 n - 5                    f(n-5) ctrA, ctrlC, ctrlV, ctrlV, ctrV
 n - 4                          f(n-4) ctrlA, ctrlC, ctrlV, ctrV
 n - 3                                f(n-3), ctrlA, ctrlC, ctrV
 n - 2                                        f(n-2)
 n - 1                                               f(n-1)
 n                                                           f(n)
*/
  for(int i = n - 3; i >= 0; --i) {
    if(maxSolution[i] == -1) maxSolution[i] = findMax(i, maxSolution);
      max_as_with_this_i = multiplier * maxSolution[i];
    if(max_as_with_this_i > max_so_far) {
      max_so_far = max_as_with_this_i;
    }
    multiplier += 1;
  }
  return max_so_far;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值