Coder Profile - Show off your skills, get a coder profile.
 
 
 
code pin board Dynamic Programming, Min blocks needed. Download Source Code
Author Details Code Information
Cinjection ( Oleksi Derkatch )

Pinned 13 Codes
Posted 8 Coding Articles

Send A Message
View Coders Profile
Language C++
Expires Never
Length 1,204 Characters (69 Lines)
Password no password
Description

Uses dynamic programming techniques to find the minimum number of blocks needed to make a stack H high, given a list of different blocks.
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int getMinBlocks(vector<int> blockList, int targetSum, int currentSum)
  7. {
  8.           static vector<int> MinBlocksNeeded(targetSum+1);
  9.  
  10.           //Initilize the ways to make stack to a large number
  11.           if (!currentSum)
  12.           {
  13.                     for (int i(0); i <= targetSum; i++)
  14.                     {
  15.                               MinBlocksNeeded[i] = 1000;
  16.                     }
  17.                     MinBlocksNeeded[0] = 0;
  18.           }
  19.  
  20.           for (int i(0); i < blockList.size(); i++)
  21.           {
  22.                     if (blockList[i] <= currentSum)
  23.                     {
  24.  
  25.                               if ( MinBlocksNeeded[ currentSum - blockList[i] ] < MinBlocksNeeded[currentSum] )
  26.                               {
  27.                                         MinBlocksNeeded[currentSum] = MinBlocksNeeded[ currentSum - blockList[i] ] + 1;
  28.                               }
  29.  
  30.                     }
  31.                     else
  32.                     {
  33.                               break;
  34.                     }
  35.           }
  36.  
  37.           if (targetSum == currentSum)
  38.           {
  39.                     return MinBlocksNeeded[targetSum];
  40.           }
  41.           else
  42.           {
  43.                     return getMinBlocks(blockList, targetSum, currentSum + 1);
  44.           }
  45.  
  46. }
  47.  
  48. int main()
  49. {
  50.  
  51.           int targetH(0);
  52.           int nBlocks(0);
  53.  
  54.           cin>>targetH;
  55.           cin>>nBlocks;
  56.  
  57.           vector<int> blockList(nBlocks);
  58.  
  59.           for (int i(0); i < nBlocks; i++)
  60.           {
  61.                     cin>>blockList[i];
  62.           }
  63.          //Assume the blocks are ordered from smallest to largest.
  64.  
  65.           cout<< getMinBlocks(blockList, targetH, 0) <<endl;
  66.  
  67.           cin.get();
  68.           return 0;
  69. }
code pin board Back To Code Pin Board Post New Code
Please login to post comments.
Page 1 of 1
 
 
Part of the MyPingle Network
Development Blog :: Make A Donation :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents
Version 1.44.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved