“Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at e ffi ciency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small e ffi ciencies, say about 97% of the time: premature optimization is the root of all evil . Yet we should not pass up our opportunities in that critical 3%." —Don Knuth
Write the tabulation template for fj b Name Th. 11/8 (your response)
make-change, in fj nite coins Given a value and a set of coins, what is the minimum number of coins required to sum to the value, assuming we have an infinite number of each coin?
longest-common substring (LCS) How similar are these strings? The longest-common substring of s1 and s2 is the longest string that is a non-consecutive substring of both s1 and s2 . lcs('x', 'y') == 0 lcs(' ca r', ' ca t') == 2 lcs('x', '') == 0 lcs(' h u man ', 'c h i m p an zee') == 4 lcs('', 'x') == 0
Ti eoretical tools: code → math How many times does the platypus quack? for (int i=0; i<N; i++) { for (int j=0; j<N; j++) { platypus.quack() } }
Ti eoretical tools: code → math How many times does the platypus quack? for (int i=0; i<N; i++) { for j in range(N): platypus.quack() } }
Ti eoretical tools: code → math summations upper-bound (inclusive) (implicit increment) lower-bound index (inclusive)
Ti eoretical tools: code → math summations N − 1 for (int i=0; i<N; i++) { X for j in range(N): 1 platypus.quack() } j =0 }
Ti eoretical tools: code → math summations upper-bound (inclusive) N − 1 j cost 0 1 X 1 1 1 2 1 (implicit increment) 3 1 … … N-2 1 j =0 N-1 1 N ∈ O(N) index lower-bound (inclusive)
Ti eoretical tools: code → math summations i cost 1 1 2 1 3 1 4 1 … … N-1 1 N 1 N ∈ O(N)
Ti eoretical tools: code → math summations i cost 1 N 2 N 3 N 4 N … … N-1 N N N N 2 ∈ O(N 2 )
Ti eoretical tools: code → math summations i cost 1 1 2 2 3 3 4 4 … … N-1 N-1 N N ∈ O(N 2 )
Ti eoretical tools: code → math How many times does the platypus quack? for j in range(N): code platypus.quack() N − 1 X math 1 j =0 Wolfram Alpha sum 1,j=0 to N-1 closed form N asymptotic notation O ( N )
Ti eoretical tools: code → math How many times does the platypus quack? for i in range(N): for j in range(N): platypus.quack() } }
Ti eoretical tools: code → math How many times does the platypus quack? for i in range(N): code for j in range(N): platypus.quack() } N − 1 N − 1 } X X math 1 i =0 j =0 Wolfram Alpha sum (sum 1,j=0 to N-1),i=0 to N-1 closed form N 2 asymptotic notation O ( N 2 )
Ti eoretical tools: code → math How many times does the platypus quack? for i in range(N): for j in range(i, N): platypus.quack() } }
Ti eoretical tools: code → math How many times does the platypus quack? for i in range(N): for j in range(i, N): code platypus.quack() } } N − 1 N − 1 X X math 1 i =0 j = i Wolfram Alpha sum (sum 1,j=i to N-1),i=0 to N-1 N ( N + 1) closed form 2 asymptotic notation O ( N 2 )
Recommend
More recommend