Next Closest Time in C++


Suppose we have a time represented in the format "HH: MM", we have to generate the next closest time by reusing the current digits. We can use the digit an unlimited number of times.

So, if the input is like "19:34", then the output will be "19:39" as the next closest time choosing from digits 1, 9, 3, 4, is 19:39. It is not 19:33, because this occurs 23 hours and 59 minutes later.

To solve this, we will follow these steps −

  • Define a function eval(), this will take x,

  • a := convert x[0] to string

  • a := a + x[1]

  • b := convert x[2] to string

  • b := b + x[3]

  • return a as integer * 60 + b as integer

  • From the main method do the following −

  • ret := blank string

  • temp := blank string

  • diff := inf

  • Define an array time

  • insert t[0] at the end of time

  • insert t[1] at the end of time

  • insert t[3] at the end of time

  • insert t[4] at the end of time

  • n := size of time

  • src := blank string

  • temp1 := blank string

  • temp2 := blank string

  • for initialize i := 0, when i < n, update (increase i by 1), do −

    • src := src + time[i]

  • for initialize i := 0, when i < n, update (increase i by 1), do −

    • for initialize j := 0, when j < n, update (increase j by 1), do −

      • for initialize k := 0, when k < n, update (increase k by 1), do −

        • for initialize l := 0, when l < n, update (increase l by 1), do −

        • temp1 := time[i]

        • temp1 := temp1 + time[j]

        • temp2 := time[k]

        • temp2 := temp2 + time[l]

        • if temp1 as number > 23 or temp2 as number > 59, then −

          • Ignore following part, skip to the next iteration

        • temp := temp1 + temp2

        • if temp is same as src, then −

          • Ignore following part, skip to the next iteration

        • newDiff := eval(temp - eval(src))

        • if newDiff < 0, then −

          • newDiff := newDiff + (60 * 24)

        • if newDiff < diff, then −

          • diff := newDiff

          • ret := temp1 + ":" + temp2

  • return (if size of ret is same as 0, then t, otherwise ret)

Example

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
   int eval(string x){
      string a = to_string(x[0]);
      a += x[1];
      string b = to_string(x[2]);
      b += x[3];
      return stoi(a) * 60 + stoi(b);
   }
   string nextClosestTime(string t) {
      string ret = "";
      string temp = "";
      int diff = INT_MAX;
      vector<char> time;
      time.push_back(t[0]);
      time.push_back(t[1]);
      time.push_back(t[3]);
      time.push_back(t[4]);
      int n = time.size();
      string src = "";
      string temp1 = "";
      string temp2 = "";
      for (int i = 0; i < n; i++)
         src += time[i];
      for (int i = 0; i < n; i++) {
         for (int j = 0; j < n; j++) {
            for (int k = 0; k < n; k++) {
               for (int l = 0; l < n; l++) {
                  temp1 = time[i];
                  temp1 += time[j];
                  temp2 = time[k];
                  temp2 += time[l];
                  if (stoi(temp1) > 23 || stoi(temp2) > 59)
                     continue;
                  temp = temp1 + temp2;
                  if (temp == src)
                     continue;
                  int newDiff = eval(temp) - eval(src);
                  if (newDiff < 0)
                     newDiff += (60 * 24);
                  if (newDiff < diff) {
                     diff = newDiff;
                     ret = temp1 + ":" + temp2;
                  }
               }
            }
         }
      }
      return ret.size() == 0 ? t : ret;
   }
};
main(){
   Solution ob;
   cout<<(ob.nextClosestTime("19:34"));
}

Input

"19:34"

Output

19:39

Updated on: 16-Nov-2020

225 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements