📌  相关文章
📜  可以由四位数字组成的最大可能时间 | (递归方法)

📅  最后修改于: 2022-05-13 01:56:09.919000             🧑  作者: Mango

可以由四位数字组成的最大可能时间 | (递归方法)

给定一个包含 4 个整数的数组arr[] 。任务是返回可以使用数组中的数字形成的最大24 小时时间。请注意,24 小时格式的最小时间是00:00最大时间是23:59 。如果无法形成有效时间,则返回空字符串。
例子:

HashMap 方法:-已经讨论了解决此问题的一种方法 https://www.geeksforgeeks.org/maximum-possible-time-that-can-be-formed-from-four-digits/

递归方法:该任务也可以使用递归来解决。尝试使用递归生成所有可能的排列,然后在对所有排列进行排序后丢弃无效排列并返回具有最大可能值的排列。如果不存在有效的排列,则返回一个字符串。有效排列的条件。

  • 小时一位数字必须在[0, 2]范围内。
  • 如果第一个数字被选为2 ,则小时第二个数字必须在[0, 3]范围内,否则为[0, 9]
  • 分钟一位数字必须在[0, 5]范围内,分钟第二位数字必须在[0, 9]范围内

下面是上述代码的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Stores all the permutations
vector res;
 
// Function to generate valid permutations
void getMax(int idx, vector& nums, string per)
{
    // All the digits are used
    if (idx == nums.size()) {
 
        // If first digit is
        // not 0, 1 or 2 then it is
        // invalid, return
        if (per[0] != '0'
            && per[0] != '1'
            && per[0] != '2')
            return;
 
        // Second digit must be less than 7
        if (per[2] >= '6')
            return;
 
        // If first digit is 2
        // then second digit must be
        // smaller than 5
        if (per[0] == '2'
            and per[1] >= '4')
            return;
 
        // Push the vali
        // d permutation in result
        res.push_back(per);
        return;
    }
 
    for (int i = idx; i < nums.size(); i++) {
 
        // add num to res
        per += nums[i] + '0';
 
        // swap and solve
        // for further permutation
        swap(nums[idx], nums[i]);
 
        // Recur
        getMax(idx + 1, nums, per);
 
        // Backtrack
        per.pop_back();
 
        swap(nums[i], nums[idx]);
    }
}
 
// Function to return maximum time
// possible in 24-hour format
string getMaxtime(vector& arr)
{
    string per = "";
    getMax(0, arr, per);
 
    // No valid permutation
    // can be generated
    if (res.empty())
        return "";
 
    // Sort all valid permutations
    sort(res.begin(), res.end());
 
    // Largest valid permutation
    string ans = res[res.size() - 1];
 
    // Resultant string
    string result
        = ans.substr(0, 2)
          + ":"
          + ans.substr(2, 2);
    return result;
}
 
// Driver Code
int main()
{
    vector arr = { 1, 2, 3, 4 };
    int n = sizeof(arr) / sizeof(int);
    cout << (getMaxtime(arr));
    return 0;
}


Java
// Java implementation of the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
  // Stores all the permutations
  public static ArrayList res = new ArrayList<>();
 
  // Function to generate valid permutations
  public static void getMax(int idx, int[] nums, String per)
  {
    // All the digits are used
    if (idx == nums.length) {
 
      // If first digit is
      // not 0, 1 or 2 then it is
      // invalid, return
      if (per.charAt(0) != '0'  &&
          per.charAt(0) != '1' &&
          per.charAt(0) != '2')
        return;
 
      // Second digit must be less than 7
      if (per.charAt(2) >= '6')
        return;
 
      // If first digit is 2
      // then second digit must be
      // smaller than 5
      if (per.charAt(0) == '2' && per.charAt(1) >= '4')
        return;
 
      // Push the vali
      // d permutation in result
      res.add(per);
 
      return;
    }
 
    for (int i = idx; i < nums.length; i++) {
 
      // add num to res
      per += String.valueOf(nums[i]);
 
      // swap and solve
      // for further permutation
      int temp = nums[idx];
      nums[idx] = nums[i];
      nums[i] = temp;
 
      // Recur
      getMax(idx + 1, nums, per);
 
      // Backtrack
      per = per.substring(0, per.length() - 1);
 
      temp = nums[i];
      nums[i] = nums[idx];
      nums[idx] = temp;
    }
  }
 
  // Function to return maximum time
  // possible in 24-hour format
  public static String getMaxtime(int[] arr)
  {
    String per = "";
    getMax(0, arr, per);
 
    // No valid permutation
    // can be generated
    if (res.isEmpty())
      return "";
 
    // Sort all valid permutations
    Collections.sort(res);
 
    // Largest valid permutation
    String ans = res.get(res.size() - 1);
 
 
    // Resultant String
    String result
      = ans.substring(0, 2)
      + ":"
      + ans.substring(2, 4);
    return result;
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    int[] arr = { 1, 2, 3, 4 };
    int n = arr.length;
    System.out.println(getMaxtime(arr));
  }
}
 
// This code is contributed by Shubham Singh


输出
23:41

时间复杂度 O(N! * log(N!))
辅助空间 O(N)