4位数的最长有效时间

时间:2017-07-31 05:41:03

标签: java

我试图从给定的4位数找到最大有效时间。我用过数字(2,4,0,0)。代码返回20:42,而它应该返回20:40。 关于如何处理此事的任何建议?

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

public class MaxTimeCombination {
public static void main(String[] args) {

    System.out.println(solution(2, 4, 0, 0));
    System.out.println(solution(3, 0, 7, 0));
}

public static String solution(int A, int B, int C, int D) {
    // brute force permutation
    int[] temp = new int[] {A, B, C, D};

    List<List<Integer>> permutation = permute(temp);
    int h = Integer.MIN_VALUE;
    int m = Integer.MIN_VALUE;
    boolean exists = false;

    /*      System.out.println("Permutations:" + permutation);
    for (int i = 0; i < permutation.size(); i++) {
    if (permutation.get(i).get(0) > 0  && permutation.get(i).get(0) < 3 ){
        List <Integer> output = permutation.get(i);
        System.out.println(output);     
    }

    }*/


    for (int i = 0; i < permutation.size(); i++) {
        //if (permutation.get(i).get(0) > 0  && permutation.get(i).get(0) < 3 ){
        List<Integer> k = permutation.get(i);
        //System.out.println("Sorted :" + k);
        int hh = k.get(0)*10 + k.get(1);
        if (hh < 24) {
            exists = true;
            if (hh > h) {
                h = hh;
            } 
        } 
        int mm = k.get(2)*10 + k.get(3);

        if ( mm < 60) {
            exists = true;
            if (mm > m) {
                m = mm;
            } 
        } 
    }

    return (exists ? String.format("%02d:%02d", h, m) : "NOT POSSIBLE");
}

public static List<List<Integer>> permute(int[] num) {
    List<List<Integer>> result = new ArrayList<>();

    //start from an empty list
    result.add(new ArrayList<>());

    for (int i = 0; i < num.length; i++) {
        //list of list in current iteration of the array num
        List<List<Integer>> current = new ArrayList<>();

        for (List<Integer> l : result) {
            // # of locations to insert is largest index + 1
            for (int j = 0; j < l.size()+1; j++) {
                // + add num[i] to different locations
                l.add(j, num[i]);

                List<Integer> temp = new ArrayList<>(l);
                current.add(temp);

                //System.out.print(temp + " ");

                //l.remove(num[i]); 
                l.remove(j);
            }

        }

        result = new ArrayList<>(current);
    }

    return result;
}
}

2 个答案:

答案 0 :(得分:0)

您需要重新构建h和m max的测试。您当前的代码会独立查找每个代码的最大值。你得到的是最大小时和最大分钟,即使它们没有在排列中一起出现,如20:42。

这是测试的工作版本。

...
(<tf.Tensor 'pool_3:0' shape=(?, ?, ?, 2048) dtype=float32>,)
(<tf.Tensor 'pool_3/_reshape/shape:0' shape=(2,) dtype=int32>,)
(<tf.Tensor 'pool_3/_reshape:0' shape=(1, 2048) dtype=float32>,)
(<tf.Tensor 'softmax/weights_quint8_const:0' shape=(2048, 1008) dtype=quint8>,)
(<tf.Tensor 'softmax/weights_min:0' shape=() dtype=float32>,)
(<tf.Tensor 'softmax/weights_max:0' shape=() dtype=float32>,)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_reshape_dims:0' shape=(1,) dtype=int32>,)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_reduction_dims:0' shape=(1,) dtype=int32>,)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_reshape_pool_3/_reshape:0' shape=(2048,) dtype=float32>,)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_min_pool_3/_reshape:0' shape=() dtype=float32>,)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_max_pool_3/_reshape:0' shape=() dtype=float32>,)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_quantize_pool_3/_reshape:0' shape=(1, 2048) dtype=quint8>, <tf.Tensor 'softmax/logits/MatMul_eightbit_quantize_pool_3/_reshape:1' shape=() dtype=float32>, <tf.Tensor 'softmax/logits/MatMul_eightbit_quantize_pool_3/_reshape:2' shape=() dtype=float32>)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_quantized_bias_add:0' shape=(1, 1008) dtype=qint32>, <tf.Tensor 'softmax/logits/MatMul_eightbit_quantized_bias_add:1' shape=() dtype=float32>, <tf.Tensor 'softmax/logits/MatMul_eightbit_quantized_bias_add:2' shape=() dtype=float32>)
(<tf.Tensor 'softmax/logits/MatMul_eightbit_quantize_down:0' shape=(1, 1008) dtype=quint8>, <tf.Tensor 'softmax/logits/MatMul_eightbit_quantize_down:1' shape=() dtype=float32>, <tf.Tensor 'softmax/logits/MatMul_eightbit_quantize_down:2' shape=() dtype=float32>)
(<tf.Tensor 'softmax/biases_quint8_const:0' shape=(1008,) dtype=quint8>,)
(<tf.Tensor 'softmax/biases_min:0' shape=() dtype=float32>,)
(<tf.Tensor 'softmax/biases_max:0' shape=() dtype=float32>,)
(<tf.Tensor 'softmax/logits_eightbit_quantized_bias_add:0' shape=(1, 1008) dtype=qint32>, <tf.Tensor 'softmax/logits_eightbit_quantized_bias_add:1' shape=() dtype=float32>, <tf.Tensor 'softmax/logits_eightbit_quantized_bias_add:2' shape=() dtype=float32>)
(<tf.Tensor 'softmax/logits_eightbit_quantize_down:0' shape=(1, 1008) dtype=quint8>, <tf.Tensor 'softmax/logits_eightbit_quantize_down:1' shape=() dtype=float32>, <tf.Tensor 'softmax/logits_eightbit_quantize_down:2' shape=() dtype=float32>)
(<tf.Tensor 'softmax/logits:0' shape=(1, 1008) dtype=float32>,)
(<tf.Tensor [] shape=(1, 1008) dtype=float32>,)

请注意,int hh = k.get(0) * 10 + k.get(1); if (hh < 24) { if (hh >= h) { int mm = k.get(2) * 10 + k.get(3); if (mm < 60) { exists = true; if (hh > h || mm > m) { m = mm; } h = hh; } } } 已成为hh>h。即使小时等于我们之前看到的小时,我们也需要寻找最大分钟。检查最大分钟的代码已在小时测试的if子句中移动。我们需要确保我们考虑的那一分钟与最​​大小时相关联。最后,我们需要在hh>=h时更新最长分钟,或者我们有一个新的最长时间mm>m

通过此更改,您的代码会提供预期值:hh>h

答案 1 :(得分:0)

我认为你对这个问题过分思考。请找到以下工作解决方案:

import java.util.ArrayList;
import java.util.Collections;

public class TestClass{

    public static void main(String[] args)
    {
        int maxLimits[] = {2, 3, 5, 9};

        ArrayList<Integer> list = new ArrayList<>();    
        list.add(3);
        list.add(2);
        list.add(9);
        list.add(2);

        Collections.sort(list);
        int time[] = new int[4];

        for(int i = 0; i<4; i++)
        {
            int index = 0;
            for(int j=0; j<list.size(); j++)
            {

                if (list.get(j) <= maxLimits[i]) 
                {
                    time[i] = list.get(j);
                    index = j;
                }
            }

            list.remove(index);
        }
    }
}

希望这会有所帮助。 : - )