C# - Simple logic but lengthy code!! With Explanation!!


  • 0
    J

    The idea is to start from the right and replace the rightmost number with the next big number in the set and check if it is still a valid time. If not move to the next number and do the same.
    Once we replace the number, scan the remaining numbers to its right and try replacing them with the smallest number from the set. Check if it is valid and return the smallest next time.
    For e.g. for "14:54" . Replace the rightmost digit 4 from the next big number that is 5. "14:55" is valid, return it.

    For " 23:23" Look at 3 , its next big number does not exist in the set. So move one position left to index 2 which is 2. Look for the next big number which is 3. Replace 3 at index 2 with 3. Check for its validity. And continue scanning to its right and replace the 3 with 2's , the smallest number in the set.

    Here is the code (not clean):

    public string NextClosestTime(string time)
            {
                char[] c = time.ToCharArray();
    
                var a = new int[4];
                var t = time.Split(':');
    
                a[0] = (int)Char.GetNumericValue(t[0][0]);
                a[1] = (int)Char.GetNumericValue(t[0][1]);
                a[2] = (int)Char.GetNumericValue(t[1][0]);
                a[3] = (int)Char.GetNumericValue(t[1][1]);
    
                Array.Sort(a);
    
                var result = new char[4];
    
                time = time.Replace(":", "");
                c = time.ToCharArray();
                int i = 3;
                bool iscopied = false;
    
                while (i >= 0)
                {
                    int index = Array.FindIndex(a, (x) => x == (int)Char.GetNumericValue(time[i]));
    
                    while (index < 3 && c[i] == Char.Parse(a[index + 1].ToString()))
                    {
                        index++;
                    }
    
                    if (index < 3)
                    {
                        c[i] = Char.Parse(a[index + 1].ToString());
    
                        if (isv(c))
                        {
                            iscopied = true;
                            Array.Copy(c, result, c.Length);
    
                            int j = i;
    
                            while (j < 3)
                            {
                                if (a[0] < (int)Char.GetNumericValue(c[j + 1]))
                                {
                                    c[j + 1] = Char.Parse(a[0].ToString());
    
                                    if (isv(c))
                                    {
                                        Array.Copy(c, result, c.Length);
                                    }
                                }
    
                                j++;
                            }
                            break;
                        }
                        else
                        {
                            if (iscopied)
                                Array.Copy(result, c, c.Length);
                            else c = time.Replace(":", "").ToCharArray();
                        }
                    }
    
                    i--;
                }
    
                if (!iscopied)
                {
                    char aaa = Char.Parse(a[0].ToString());
                    return new string(new char[] { aaa, aaa, ':', aaa, aaa
        });
                }
    
                var s = new string(result);
                return s.Insert(2, ":");
            }
    
            public bool isv(char[] c)
            {
                string s1 = ((int)Char.GetNumericValue(c[0])).ToString();
                s1 += Char.GetNumericValue(c[1]).ToString();
                int n1 = int.Parse(s1);
                if (n1 > 23)
                {
                    return false;
                }
    
                s1 = ((int)Char.GetNumericValue(c[2])).ToString();
                s1 += Char.GetNumericValue(c[3]).ToString();
                n1 = int.Parse(s1);
    
                if (n1 > 59)
                {
                    return false;
                }
    
                return true;
            }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.