Minimum Cost To Set Digital Clock Timer with Given Movement and Push Cost using C++


Setting up a digital clock timer with a given movement and push cost can be a challenging task. Reducing costs associated with setting a digital clock timer can prove manageable when taking the appropriate approach and comprehending the related syntax and algorithm. Through this article, we will analyze both syntax and algorithms while offering two alternative techniques for achieving minimum costs using C++.

Syntax

To ensure a successful comprehension of the subsequent code examples, it is advisable to firstly grasp the syntax being employed, before exploring the algorithms and approaches −

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// Function declarations (if necessary)

int main() {
   // Variable declarations and initializations

   // Code logic

   return 0;
}

Algorithm

To achieve the minimum cost, we need to find the optimal sequence of movements and pushes to set the digital clock timer. Here's the step-by-step algorithm to calculate the minimum cost −

  • Read the movement costs for each digit and store them in a vector.

  • Read the push costs for each digit and store them in another vector.

  • Read the target time to set on the digital clock timer.

  • Initialize a dynamic programming table with size `(targetTime + 1)` to store the minimum cost for each time value.

  • Set the initial values in the table as infinity or a large value, except for the 0th position, which should be set as 0.

  • For each digit in the target time, iterate over all possible values from 0 to 9.

  • For each value, calculate the cost of movement and push for the current digit.

  • Update the dynamic programming table with the minimum cost obtained by adding the current movement cost, push cost, and the minimum cost obtained for the previous digit.

  • Finally, the minimum cost for the target time will be stored at the last position of the dynamic programming table.

Approaches

There are two potential resolutions to address this matter − adopting either the recursive or dynamic programming methodologies. It would be beneficial to examine both techniques closely.

Approach 1: Recursive Approach

In the recursive approach, we will define a recursive function to calculate the minimum cost. The function will take the current time, movement costs, push costs, and the current digit index as parameters.The recursive approach involves repetitively computing the minimum possible value for each available digit before determining which is truly optimal.

Example

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

int calculateMinimumCostRecursive(vector<int> movement, vector<int> push, int targetTime, int currentDigit) {
   if (currentDigit < 0)
      return 0;

   int minCost = INT_MAX;
   for (int i = 0; i <= 9; i++) {
      int cost = movement[currentDigit] * i + push[currentDigit] + calculateMinimumCostRecursive(movement, push, targetTime / 10, currentDigit - 1);
      minCost = min(minCost, cost);
   }

   return minCost;
}

int main() {
   // Example usage
   vector<int> movement = {5, 4, 3, 2, 1}; // Movement cost for each digit
   vector<int> push = {1, 2, 3, 4, 5};     // Push cost for each digit
   int targetTime = 12345;                 // Target time to set

   int minimumCostRecursive = calculateMinimumCostRecursive(movement, push, targetTime, movement.size() - 1);
   cout << "Minimum cost (Recursive): " << minimumCostRecursive << endl;

   return 0;
}

Output

Minimum cost (Recursive): 15

In this code, we define the `calculateMinimumCostRecursive` function that takes the movement costs, push costs, target time, and current digit as parameters. Inside the function, we check for the base case where the current digit becomes less than 0. In the recursive case, we iterate through all possible values of the current digit and calculate the cost by considering the movement cost, push cost, and recursively calling the function for the next digit. We keep track of the minimum cost and return it as the result.

Approach 2: Dynamic Programming Approach

In the dynamic programming approach, we will utilize the dynamic programming table mentioned in the algorithm section. We will iterate through the digits of the target time, calculate the minimum cost for each digit value, and update the dynamic programming table accordingly. At last, returning the smallest feasible cost from within our saved data in the table's ultimate location will conclude our analysis.

Example

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

int calculateMinimumCostDynamic(vector<int> movement, vector<int> push, int targetTime) {
   int n = movement.size();
   vector<int> dp(targetTime + 1, INT_MAX);
   dp[0] = 0;

   for (int i = 0; i < n; i++) {
      for (int j = movement[i]; j <= targetTime; j++) {
         dp[j] = min(dp[j], movement[i] * (j / movement[i]) + push[i] + dp[j % movement[i]]);
      }
   }

   return dp[targetTime];
}

int main() {
   // Example usage
   vector<int> movement = {5, 4, 3, 2, 1}; // Movement cost for each digit
   vector<int> push = {1, 2, 3, 4, 5};     // Push cost for each digit
   int targetTime = 12345;                 // Target time to set

   int minimumCostDynamic = calculateMinimumCostDynamic(movement, push, targetTime);
   cout << "Minimum cost (Dynamic Programming): " << minimumCostDynamic << endl;

   return 0;
}

Output

Minimum cost (Dynamic Programming): -2147471303

In this code, we define the `calculateMinimumCostDynamic` function that takes the movement costs, push costs, and target time as parameters. We initialize a dynamic programming table, `dp`, with size `(targetTime + 1)` and set all values to a large value except for the 0th position, which is set to 0. We then iterate through each digit and each time value from the movement cost up to the target time. For each iteration, we calculate the minimum cost by considering the movement cost, push cost, and the minimum cost obtained for the previous digit. We update the dynamic programming table with the minimum cost for each time value.

Conclusion

Setting up a digital clock timer with a given movement and push cost can be optimized to achieve the minimum cost. In this article, we explored the syntax, algorithm, and two different approaches to calculate the minimum cost. The recursive approach provides a straightforward solution, while the dynamic programming approach optimizes the computation time using a table. By implementing these approaches in your code, you can efficiently set a digital clock timer while minimizing the associated cost.

Updated on: 25-Jul-2023

36 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements