1. Topic Requirements
You is a professional robber planning to rob houses along a street. Each house have a certain amount of money stashed, the only constraint stopping all from robbing each of the them are that Adjac ENT houses have security system connected and it would automatically contact the police if the adjacent houses were broken Into on the same night.
Given a list of non-negative integers representing the amount of money in each house, determine the maximum amount of mone Y you can rob tonight without alerting the police.
2. Analysis
The title requirement is the maximum value of money, so, at the beginning can be thought of greedy algorithm, not a house of money and house number package as a struct, according to the size of money value, and then from large to small pick meet the requirements of the room, when traversing this struct array, It is necessary to determine whether the adjacent rooms of the current room number I i+1 and i-1 have been selected, the code is as follows:
1 structMyhouse2 {3 intvalue;4 intNumber ;5 BOOL operator< (Myhouse &a)6 {7 returnValue >A.value;8 }9 };Ten One //BOOL CMP (myhouse &a,myhouse &b) A //{ - //return A.value < B.value; - //} the - classSolution { - Public: - intRob (vector<int> &num) { +Vector<myhouse>Myvector; - inti,j,k; + intLen =num.size (); A Myhouse temp; at intres =0; - intRes1 =0; - if(0==len) - { - returnRes; - } in for(i=0; i<len;i++) - { toTemp.value =Num[i]; +Temp.number =i; - Myvector.push_back (temp); the } * $ sort (Myvector.begin (), Myvector.end ());Panax Notoginseng - /*For (i=0;i<len;i++) the { + cout << myvector[i].value << "" <<myVector[i].number<<endl; A }*/ the + - Set<int> MySet;//Save the room number I've stolen. $ myset.clear (); $ inttempnum1,tempnum2; - for(i=0; i<len;i++) - { theTEMPNUM1 = myvector[i].number+1; -tempNum2 = myvector[i].number-1;Wuyi if(Myset.find (TEMPNUM1)! = Myset.end () | | myset.find (TEMPNUM2)! =myset.end ()) the { -res1+=Myvector[i].value; Wu Continue; - } About $res+=Myvector[i].value; - Myset.insert (myvector[i].number); - } - returnRes>res1?res:res1; A + } the};
Submit the code, found that the greedy algorithm can not pass, such as the instance {2,3,2}, will get the wrong answer.
So this greedy algorithm does not work, you have to think of other methods.
For the extremum problem, you can also consider the use of dynamic planning for the dynamics programming to solve the problem, we maintain an array DP, where Dp[i] represents to the I position when the number of nonadjacent can form the largest and, after analysis, we can get a recursive formula Dp[i] = max ( Num[i] + dp[i-2], dp[i-1]), the code is as follows:
1 classSolution {2 Public:3 intRob (vector<int> &num) {4 5 6 intLen =num.size ();7 if(Num.size () <=1) 8 returnNum.empty ()?0: num[0];9vector<int> dp={num[0],max (num[0],num[1])};Ten One A for(intI=2; i<len;i++) - { -Dp.push_back (Max (dp[i-1],dp[i-2]+num[i])); the } - - returnDp.back (); - + } -};
Submit accepted.
Leetcode 198 House Robber