total number of hops

If you can hop 1, 2, or 3 steps at a time, calculate the total number of possible combinations for `n` steps.

 I thought of it as follows, let S(n) be the # of combinations
to go n steps
a) to go n+3 is the S(n+2) + the hop with distance 1 
b) to go n+3 is the S(n+1) + the hop with distance 2
c) to go n+3 is the S(n) + the hop with distance 3
d) S(n+3) = S(n) + S(n+1) + S(n+2) 
<=> S(n) = S(n-3) + S(n-2) + S(n-1)

it can be computed recursively if you memoize in O(n), otherwise
big-O of S < big-O of T(n) = 3*T(n-1) = O(3^n) (closer bound 
is possible with the master theorem or a more complex prove)

iterative computation will be something like Fibonacci: 

int combinations(int n)
{
  if(n<=1) return 1;
  if(n==2) return 2;
  if(n==3) return 3;
  int sn_3 = 1;
  int sn_2 = 2;
  int sn_1 = 3;
  int s = 0;
  for(int i=3; i<=n; i++) {
    s = sn_3 + sn_2 + sn_1;
    sn_3 = sn_2;
    sn_2 = sn_1;
    sn_1 = s;
  }
}
return s;

  

时间: 2024-08-01 07:17:10

total number of hops的相关文章

CareerCup Facebook Total number of substring palindrome

Write a function for retrieving the total number of substring palindromes. For example the input is 'abba' then the possible palindromes= a, b, b, a, bb, abba So the result is 6. Updated at 11/11/2013: After the interview I got know that the O(n^3) s

Total Number of Unicorn Companies: 188

https://www.cbinsights.com/research-unicorn-companies Total Number of Unicorn Companies: 188 Total Cumulative Valuation: $647B tions) The Unicorn Company List Company Valuation ($B) Date Joined Country Industry Select Investors Decolar $1 3/10/2015 A

【MySQL笔记】mysql报错"ERROR 1206 (HY000): The total number of locks exceeds the lock table size"的解决方法

step1:查看 1.1 Mysql命令行里输入"show engines:"查看innoddb数据引擎状态, 1.2 show variables "%_buffer%"里查看innodb_buffer_pool_size的数值,默认是8M(太小,需要改大一点!) step2:找配置文件,修改innodb_buffer_pool_size=64M 2.1 在linux里配置文件是my.cnf,windows里是my.ini(注:不是my-default.ini).

Mysql_解决The total number of locks exceeds the lock table size错误

在操作mysql数据库表时出现以下错误. 网上google搜索相关问题,发现一位外国牛人这么解释: If you're running an operation on a large number of rows within a table that uses the InnoDB storage engine, you might see this error: ERROR 1206 (HY000): The total number of locks exceeds the lock ta

[Daily Coding Problem] Find the total number of solutions of a linear equation of n variables

Given a linear equation of n variables, find the total number of non-negative integer solutions of it. All coefficients are positive. Example: input: x + 2 * y = 5 output: 3,  the 3 possible integer solutions are x = 1, y = 2; x = 3, y = 1; x = 5, y

MYSQL碰到The total number of locks exceeds the lock table size 问题解决记录

解决记录如下: 在mysql里面进行修改操作时提示:The total number of locks exceeds the lock table size ,通过百度搜到innodb_buffer_pool_size过小: 打开mysql 命令框 输入 show variables like "%tmp%"; 查看innodb_buffer_pool_size,输入SET GLOBAL innodb_buffer_pool_size=67108864; 完成之后再次使用show v

The total number of locks exceeds the lock table s

InnoDB表执行大批量数据的更新,插入,删除操作时会出现这个问题,需要调整InnoDB全局的innodb_buffer_pool_size的值来解决这个问题 SHOW GLOBAL VARIABLES LIKE "%buffer_pool%" 可以通过编辑/etc/my.cnf,添加下面的语句来修改缓存大小: innodb_buffer_pool_size = 2G

[Coding Made Simple] Coin Changes Number of ways to get a total

Given coins of certain denominations and a total, how many ways these coins can be combined to get the total. Dynamic Programming solution State: T[i][j]: given the first i coins, the total number of ways these coins can be combined to get the total

LeetCode 233. Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 本来想凑一个关于阶乘的题目,做个笔记,不枉我昨天A八个