您的位置:首页 > 其它

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)

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: