0

Having an array arrs of 6 digits, let's set a latest time (24-hour time) which can be form by putting each digit just one time.

The times (24-hour) are set by"HH:MM:SS", whichHH is from00 to23,MM is from00 to59, and SS is from00 to59.The start time is 00:00:00, and the end time is 23:59:59.

Let's return the latest time with"HH:MM:SS" style. If no time should be valid, return an empty string.

class ChallengeClass { string[] v = new string[720];

int l = 0;

void p(int[] s, int n)
{

    int i = n;

    if (n != 5) while (i < 6)
        {
            (s[i], s[n]) = (s[n], s[i]);

            p(s, n + 1);

            (s[i], s[n]) = (s[n], s[i++]);

        }else if (s[0] * 10 + s[1] < 24 & s[2] * 10 + s[3] < 60 & s[4] * 10 + s[5] < 60) v[l++] = string.Concat(s);

        }

    public string LastedTime(int[] a)

    {

        p(a, 0);

        System.Array.Sort(v);

        return l == 0 ? "" : v[^1].Insert(2, ":").Insert(5, ":");

    }
}

That's my code but it so long, can anyone make it shorter

  • consider use type `DateTime`? – Lei Yang Mar 17 '22 at 06:05
  • 5
    Please do avoid writing such code - there is TimeSpan.ParseExact just for that - https://stackoverflow.com/questions/11719055/why-does-timespan-parseexact-not-work. If that is homework (so you have to actually write that) *and* the code is working consider asking for code review on [codereview.se]. – Alexei Levenkov Mar 17 '22 at 06:08
  • 2
    In addition to the comment of Alexei Levenkov, try to use names that make sense. `l`, `p`, `s`, `n`, `a`, `v` make no sense at all. This and the fact that it is poorly formatted are two reasons why your code is barely readable. – Markus Safar Mar 17 '22 at 06:11
  • Irrespective of whether you think you have problem with the code and it should stay on SO or code is working and you need just code review please update the code to use reasonable names for variables. In particular never use l for variable name unless you need to troll someone. – Alexei Levenkov Mar 17 '22 at 06:12
  • Sorry for the inconvenience, but this contest requires the minimum number of characters possible so I have to name the variable as above. – Babynghe2003 Mar 17 '22 at 06:56
  • C# is not the best language for [code golf](https://codegolf.stackexchange.com/) – Hans Kesting Mar 21 '22 at 09:34

1 Answers1

0
public string Time(int[] x)
{
    int m = 86399;
    while (m --> 0)
    {
        var t = new System.TimeSpan(0, 0, m) + "";
        if (a(x) + "::" == a(t.ToCharArray())) return t;
    }
    return "";
}
string a<T>(T[] t)
{
    System.Array.Sort(t);
    return string.Concat(t);
}

This is the best solution

KenDzz
  • 39
  • 2
  • 7
  • 1
    And can you [edit] your post to explain why this is the best solution ? – Elikill58 Mar 21 '22 at 11:10
  • The author would like to shorten the maximum code according to the requirements of the topic. I've shortened it to the minimum possible while making sure it matches the previously requested thread's output. If you have a shorter idea, please share it with everyone. Thanks – KenDzz Mar 24 '22 at 06:30