简体   繁体   English

以24小时格式最大化数字。

[英]Maximize digits in 24 hour format.

Came across this question in a Glassdoor review and thought it was interesting. 在Glassdoor评论中遇到了这个问题,并认为它很有趣。

Given an integer consisting of 4 digits, we need to maximize it in 24 hour format. 给定一个由4位数组成的整数,我们需要以24小时格式最大化它。 For example, 4372 should return a String of the form 23:47, which is the maximum 24 hour value that can be obtained from the given integer. 例如,4372应该返回23:47形式的字符串,这是可以从给定整数获得的最大24小时值。 Assume the given integer always contains exactly 4 digits. 假设给定的整数总是包含4位数。

Here's an incomplete method I wrote trying to solve it: 这是我写的一个不完整的方法试图解决它:

private static String maximize24Hour(int digits) {
    if (digits == 0)
        return "00:00";
    if (digits < 0)
        return null;

    ArrayList<Integer> list = new ArrayList<>();
    do {
        list.add(digits % 10);
        digits /= 10;
    } while (digits != 0);

    // Extra processing needs to be done here. Haven't figured it out. 

    StringBuilder buf = new StringBuilder();
    for (Integer d : list) {
        buf.append(d);
    }

    String result = buf.toString();
    String hours = result.substring(0, 2);
    String minutes = result.substring(2, result.length()); 

    if (Integer.parseInt(result) > 2359 
            || Integer.parseInt(hours) > 23 
            || Integer.parseInt(minutes) > 59
            || result.length() != 4)
        return null;

    return hours.concat(":").concat(minutes);
}

Am I even approaching it correctly? 我是否正确接近它? If this was just any number, it would be trivial. 如果这只是任何数字,那将是微不足道的。 But it's asking for it to be in 24 hour format which is what I find tricky. 但它要求它是24小时格式,这是我觉得棘手。

I'd be interested to see if anyone has solutions/ideas for this challenge. 我有兴趣看看是否有人有这个挑战的解决方案/想法。

  1. Separate the integer into 4 digits. 将整数分成4位数。
  2. If you don't have a 0, 1, or 2 then there is no answer. 如果你没有0,1或2,则没有答案。
  3. Put the largest number that is <= 2 in the first spot in the time. 在时间的第一个位置放置<= 2的最大数字。
  4. If the first digit was a 2, then put the largest remaining number that is <=3 in the second place. 如果第一个数字是2,那么在第二个位置放置<= 3的最大剩余数字。 (If there isn't one, then there is no answer.) If the first digit was 1 or 0, then place the largest remaining number in the second place. (如果没有,则没有答案。)如果第一个数字是1或0,则将最大的剩余数字放在第二个位置。
  5. Put the largest remaining number that is <= 5 in the third place. 将剩余的最大数字<= 5放在第三位。
  6. Put the only remaining number in the fourth place. 将剩下的唯一数字放在第四位。

I think you don't need to back-track at any point because the bounds in steps 3, 4, and 5 are strictly increasing. 我认为您不需要在任何时候回溯,因为步骤3,4和5中的界限严格增加。 You certainly don't need to consider all possible permutations of the numbers since we know that certain places are bounded. 你当然不需要考虑数字的所有可能的排列,因为我们知道某些地方是有界限的。

Here is my method. 这是我的方法。 May not be the most efficient one, but it does the job. 可能不是最有效的,但它确实起作用。

  1. Break input into individual digits 将输入分为单个数字
  2. Get all permutations of all digits 获取所有数字的所有排列
  3. Check each output is valid, and keep track of the biggest. 检查每个输出是否有效,并跟踪最大值。

High level looks like this: 高级看起来像这样:

import java.util.List;
import java.util.ArrayList;

public class MaxDigit {
    // ... helper functions here

    // maximize function
    private static String maximize24Hour(int digits) {
        if (digits < 1000 || digits >= 10000) {
            return "invalid input";
        }

        // get all possibles and find the biggest
        int max = -1;
        List<String> singleDigits = getDigits(digits);
        List<String> allPossibles = getPermutations(singleDigits);
        for (String timeStr : allPossibles) {
            int timeInt = Integer.parseInt(timeStr);
            if (isValidTime(timeInt) && timeInt > max) {
                max = timeInt;
            }
        }

        // If none is valid
        if (max < 0) {
            return "cannot find any valid time";
        }
        // Convert int to time
        return max/100 + ":" + max%100;
    }

    public static void main(String[] args) {
        System.out.println(maximize24Hour(4372));
    }
}

And here are the helpers: 以下是帮手:

/**
 * Check specified time is valid
 * @param time Time in hhmm format
 * @return true if input time is valid
 */
private static boolean isValidTime(int time) {
    int hour = time / 100;
    int min = time % 100;
    return hour <= 23 && min <= 59;
}

/**
 * Generate all possible numbers from input
 *
 * For example: inputs {1, 2} gives {12, 21}
 * For example: inputs {1, 2, 3} gives {123, 132, 213, 231, 312, 321}
 *
 * @param inputs Input digits
 * @return all possibles
 */
private static List<String> getPermutations(List<String> inputs) {
    if (inputs.size() <= 1) {
        return inputs;
    }

    List<String> ret = new ArrayList<>();
    for (int i = 0; i < inputs.size(); ++i) {
        List<String> copy = new ArrayList<>(inputs);
        copy.remove(i);
        List<String> recusive = getPermutations(copy);
        for (String values : recusive) {
            ret.add(inputs.get(i) + values);
        }
    }
    return ret;
}

private static List<String> getDigits(int digits) {
    List<String> singleDigits = new ArrayList<>();
    while (digits > 0) {
        singleDigits.add(Integer.toString(digits%10));
        digits /= 10;
    }
    return singleDigits;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM