Chp9: Recursion and Dynamic Programming
2013-03-28 05:18
459 查看
A goog hint that a problem is recursive is that it can be build off sub-problems.
Bottom-Up Recursion: most intuitive, we start with knowing how to solve the problem for a simple case.
Up-Bottom Recursion: more complex.
Dynamic programming is little more than recursion where you cache the results. A good way to approach such a problem is often to implement it as a normal recursive solution, and then to add the caching part.
9.1 A child is running up a staircase with n steps, and can hop either 1, 2, 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs.(DP)
9.3 A magic index in an array A[0 ... n-1] is defined to be an index such that A[i] = i. Given a sorted array, write a method to find a magic index.
Follow Up: what if the elements are not distinct?
cannot conclude which side the magic index is on.
The general pattern is that we compare midIndex and midValue for equality first. Then, if they are not equal, we recursively search the left and right sides as follows:
Left side: search indices start through Math.min(minIndex - 1, minValue)
Right side: search indices Math.max(midIndex + 1, midValue) through end.
But this can only return one of the results, if there exists many results.
9.4 Write a method to return all subsets of a set.
Bottom-Up Recursion: most intuitive, we start with knowing how to solve the problem for a simple case.
Up-Bottom Recursion: more complex.
Dynamic programming is little more than recursion where you cache the results. A good way to approach such a problem is often to implement it as a normal recursive solution, and then to add the caching part.
9.1 A child is running up a staircase with n steps, and can hop either 1, 2, 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs.(DP)
static int[] buf; public int steps_need(int hight){ if(buf[hight - 1] > 0){ return buf[hight - 1]; } else if(buf[hight - 1] < 0){ System.out.println("overflow"); return -1; } else{ if(hight == 1) buf[hight - 1] = 1; else if(hight == 2) buf[hight - 1] = 2; else if(hight == 3) buf[hight - 1] = 4; else buf[hight - 1] = steps_need(hight - 1) + steps_need(hight - 2) + steps_need(hight - 3); return buf[hight - 1]; } } public void pro7(){ int hight = 30; if(hight < 1){ System.out.println("input error"); return; } buf = new int[hight]; int ways = steps_need(hight); System.out.println("ways: "+ ways); }
9.3 A magic index in an array A[0 ... n-1] is defined to be an index such that A[i] = i. Given a sorted array, write a method to find a magic index.
public int magicFast(int[] array, int start, int end){ if(end < start || start < 0 || end >= array.length) return -1; int mid = (start + end) / 2; if(array[mid] == mid) return mid; else if(array[mid] > mid) return magicFast(array, start, mid - 1); else return magicFast(array, mid + 1, end); } public int magicFast(int[] array){ return magicFast(array, 0, array.length - 1); }
Follow Up: what if the elements are not distinct?
cannot conclude which side the magic index is on.
The general pattern is that we compare midIndex and midValue for equality first. Then, if they are not equal, we recursively search the left and right sides as follows:
Left side: search indices start through Math.min(minIndex - 1, minValue)
Right side: search indices Math.max(midIndex + 1, midValue) through end.
But this can only return one of the results, if there exists many results.
public int magicFast(int[] array, int start, int end){ if(end < start || start < 0 || end > array.length) return -1; int midIndex = (start + end) / 2; int midValue = array[midIndex]; if(midValue == midIndex) return midIndex; //search left int leftIndex = Math.min(midIndex - 1, midValue); int left = magicFast(array, start, leftIndex); if(left >= 0) return left; //search right int rightIndex = Math.max(midIndex + 1, midValue); int right = magicFast(array, rightIndex, end); return right; } public int magicFast(int[] array){ return magicFast(array, 0, array.length - 1); }
9.4 Write a method to return all subsets of a set.
ArrayList<ArrayList<Integer>> getSubset(ArrayList<Integer> set){ ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>(); int max = 1 << set.size(); for(int k = 0; k < max; k ++){ ArrayList<Integer> subset = convert(k, set); results.add(subset); } return results; } ArrayList<Integer> convert(int x, ArrayList<Integer> set){ ArrayList<Integer> result = new ArrayList<Integer>(); int index = 0; for(int k = x; k > 0; k >>= 1){ if((k & 1) == 1) result.add(set.get(index)); index ++; } return subset; }
相关文章推荐
- Cranking the Coding Interview: Recursion and Dynamic Programming
- a problem solved by dynamic programming and some ideals
- 菜鸟看论文——Stochastic Occupancy Grids and Dynamic Programming(一)
- Study notes "Basic algorithms of dynamic programming and backtracking
- Programming clojure – Recursion and Lazy-seq
- Polymorphism, Dynamic Typing, and Dynamic Binding(Chapter 9 of Programming in Objective-C 2.0)
- WEEK2-Recursion and invariant programming
- MITx - 6.00.2x 笔记(Unit1 Lecture 2 Decision Trees and Dynamic Programming)
- dynamic programming in Date Structures and Algorithm Analysis in C
- Files and Strings(Chapter 10 of Python 3 Object Oriented Programming)
- "Dynamic programming" + "Veido" + "Google"
- UESTC_菲波拉契数制升级版 2015 UESTC Training for Dynamic Programming<Problem L>
- iOS7—Day by day—Day12:Dynamic Type and Font Descriptors
- Build Static and Dynamic Libary in Linux
- CXF ws client, dynamic endpoint and loading WSDL from the classpath
- MIT 6.00 Introduction to Computer Science and Programming, Fall 2008
- System and Device Programming------questions solved about device
- Access Database Design and Programming
- Understanding and Configuring Dynamic ARP Inspection
- System and Device Programming------device