compute Binomial Coefficient or combinations with dynamic programming
2014-08-23 12:17
459 查看
The Problem
Write a function that takes two parameters n and k and returns the value of Binomial Coefficient C(n, k). For example, your function should return 6 for n = 4 and k = 2, and it should return 10 for n = 5 and k = 2.
referece:
http://www.geeksforgeeks.org/dynamic-programming-set-9-binomial-coefficient/ http://en.wikipedia.org/wiki/Pascal%27s_triangle
Write a function that takes two parameters n and k and returns the value of Binomial Coefficient C(n, k). For example, your function should return 6 for n = 4 and k = 2, and it should return 10 for n = 5 and k = 2.
def ComputeBinomialCoefficients(n,k): # ComputeBinomialCoefficients(5,2) shoud return 10 table = [0] * (k+1) table[0] = 1 for i in range(1, n+1): for j in range(min(i,k), 0, -1): # why begin with min(i,k): 1) we do not need any results more than k. 2) Yang Hui (Pascal) Triangle: j must be less than i table[j] = table[j] + table[j - 1] # this comes from YANG Hui Triangle (or Pascal Trianle) of Binomial Coefficients return table[k]
referece:
http://www.geeksforgeeks.org/dynamic-programming-set-9-binomial-coefficient/ http://en.wikipedia.org/wiki/Pascal%27s_triangle
相关文章推荐
- spring.data.jpa.query :Cannot use native queries with dynamic sorting and/or pagination in method
- Charts with Dynamic Height or Width based on Categories/Data (SQL 2008, RDL)
- [3_3_shopping] Dynamic Programming or Shortest Path Model
- vertex cover problem with dynamic programming
- [4_1_nuggets] Dynamic Programming or Number Theory
- FRM-30425: Summarized database item must reside in a block with Query All Records or Precompute Summ
- Julia is a high-level, high-performance dynamic programming language for technical computing, with syntax that is familiar to users of other technical
- SpringData JPA @Query分页查询 Cannot use native queries with dynamic sorting and/or pagination in method
- Sqoop学习笔记 --- sqoop import with primary_key , or without primary_key
- crtbegin_dynamic.o: No such file: No such file or directory
- Introduction to Programming with C++ Comprehension Version
- Myhabits报错invalid types () or values ()解决方法with invalid types () or values (). Cause: java.lang.NoSu
- 14多校 B-A simple dynamic programming problem
- 【LeetCode with Python】 Combinations
- Shared-Memory Programming with Pthreads
- 2016 UESTC Training for Dynamic Programming C - 柱爷的下凡 预处理打表、背包问题
- 强化学习(三):动态规划求解MDP(Planning by Dynamic Programming)
- 2014 UESTC Training for Dynamic Programming I
- 基于引用的元编程(Metaprogramming with Quotations)
- These are useful functions when you are dealing with URL encoded URLs or POST data.