C# Timeout


  • 0

    I have seen in many questions that the C# code times out, but the very same code in Java (and probably other languages) executes successfully.

    Beside of that, I run a code in Java which runs in less than 5ms but the same code would take more than 100ms in C# (if doesn't time out!).

    This issue makes me have to check my code over and over again and finally have to give up. Is there any way we can match the time out between C# and other languages, or increase C# time out?

    The last time I have run into this issue is on Q298.

    Java code copied from LeetCode Solution, executes in 3ms:

        private int maxLength = 0;
        public int longestConsecutive(TreeNode root) 
        {
            dfs(root, null, 0);
            return maxLength;
        }
    
        private void dfs(TreeNode p, TreeNode parent, int length) 
        {
            if (p == null) return;
            length = (parent != null && p.val == parent.val + 1) ? length + 1 : 1;
            maxLength = Math.max(maxLength, length);
            dfs(p.left, p, length);
            dfs(p.right, p, length);
        }
    

    Literally the same code in C# times out. Just had to change 'longestConsecutive' to 'LongestConsecutive' and 'Math.max' to 'Math.Max'.

    private int maxLength = 0;
    public int LongestConsecutive(TreeNode root) 
    {
        dfs(root, null, 0);
        return maxLength;
    }
    
    private void dfs(TreeNode p, TreeNode parent, int length) 
    {
        if (p == null) return;
        length = (parent != null && p.val == parent.val + 1) ? length + 1 : 1;
        maxLength = Math.Max(maxLength, length);
        dfs(p.left, p, length);
        dfs(p.right, p, length);
    }
    

    In addition, I would like to report another strange issue that I have encountered in one of my submissions.
    This is regarding Q482.
    The code fails on the last test case 35/35.
    Input= "-", K=2
    Even tough I had to hard code the condition to check "-" in the first line, it still throws Memory Limit Exceeded error.

        public string LicenseKeyFormatting(string S, int K)
        {
            if (S == "-") return "";
            string withOutDashes = RemoveDashes(S);
            int d = withOutDashes.Length % K;
            int counter = d;
            StringBuilder res = new StringBuilder();
            for (int i = 0; i < withOutDashes.Length; i++)
            {
                if (i == counter)
                {
                    if (i != 0)
                        res.Append("-");
                    counter += K;
                }
                res.Append(withOutDashes[i]);
            }
            return res.ToString().ToUpper();
        }
        private string RemoveDashes(string s)
        {
            string result = "";
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] != '-') result += s[i];
            }
            return result;
        }
    

  • 0

    @1337c0d3r @love_Fawn @XIDUI et al, hope this post finds you well. @administrators


  • 0

    Mentioning @contributors and @Global-Moderators since I didn't receive any response from @administrators.


  • 1

    @yashar I just tried submitting your first C# code for Q298 and it gets Accepted. Submitted multiple times and all got Accepted. I didn't notice the Time Limit Exceeded issue you're talking about?


  • 0

    @yashar As for Q482, that is your code's problem, because you should use StringBuilder in your RemoveDashes method. I changed your RemoveDashes method to the following and it gets Accepted:

    private string RemoveDashes(string s)
    {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] != '-') result.Append(s[i]);
        }
        return result.ToString();
    }

  • 0

    @1337c0d3r Thanks for checking. I confirm that the code for the first question runs properly now. I don't know what was going wrong few days ago.

    For the second question, using StringBuilder fixes the issue. However I'm not sure how my old code passes 34 test cases but only fails at the last case "-", even though it doesn't get to the line which calls RemoveDashes.


Log in to reply
 

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