OJ bug?


  • 0
    B

    I'm quite confused that following code couldn't pass OJ but the failing case is correct in playground:

    public class Solution {
        public int CalPoints(string[] ops) {
            int sum=0,pre=0,pre1=0, pre2=0;
            for(int i = 0; i < ops.Length; i++) {
                string op = ops[i];
                if(op == "+") {
                    int tmp = pre + pre1;
                    sum += tmp;
                    pre2 = pre1;
                    pre1 = pre;
                    pre = tmp;
                } else if (op == "D") {
                    int tmp = pre * 2;
                    sum += tmp;
                    pre2 = pre1;
                    pre1 = pre;
                    pre = tmp;
                } else if (op == "C") {
                    sum -= pre;
                    pre = pre1;
                    pre1 = pre2;
                } else {
                    int tmp = Int32.Parse(op);
                    sum += tmp;
                    pre2 = pre1;
                    pre1 = pre;
                    pre = tmp;
                }
            }
            return sum;
        }
    }
    

    Does someone know what might be wrong?


  • 2
    R

    @Bigcat Was it the following case?

    Input: ["-60","D","-36","30","13","C","C","-33","53","79"]
    Output: 227
    Expected: -117

    OJ accepted the same code after the contest end. Absolutely ridiculous. Totally ruined my contest.


  • 0
    B

    @rajincse Nope, my code was failing for ["15483","-20523","C","C","9061","26083","+","C","4702","+","D","10260","-525","C","+","15023","-20005","-1647","C","-12853","20706","D","-21983","24892","10570","D","1215","D","D","+","3854","20505","C","D","-18850","-2170","27914","-26175","+","3188","+","+","21804","D","+","-847","D","26184","14945","C","D","+","+","13795","+","1839","15755","2627","-2090","C","C","29743","24319","D","-22624","20374","+","D","2631","+","7296","-5109","9454","-10466","D","C","+","17555","+","12144","D","16710","27969","22863","D","8521","+","D","C","-25486","-1137","9782","25633","-12031","-11248","+","C","-13559","D","D"]; Output: 20245, Expected: 1143440

    However, it is correct when I run in some other place. I've also tried Javascript with same algorithm, also failed in OJ but correct in other place (like in browser console).


  • 2
    L

    @rajincse I have the same problem just like you.
    Also destroy my contest


  • 0
    L

    @rajincse Have you handled it yet?


  • 0

    I think the problem lies in the case when there are 2 or more continuous "C", which will make the actual previous two round's points "vanished".

    Take these lines for instance,

                } else if (op == "C") {
                    sum -= pre;
                    pre = pre1;
                    pre1 = pre2;
                }
    

    let's say we have 2 continuous "C", at first let's say pre2, pre1, pre's values are

    pre2 : 2
    pre1 : 1
    pre : 0
    

    after the first "C", we will have

    pre2 : 2
    pre1 : 2
    pre : 1
    

    after the second "C", we will have

    pre2 : 2
    pre1 : 2
    pre : 2
    

    at this point we have only one actual previous round's points 2, but the actual previous second round's points "vanished". Then we can't correctly do the operations which require the second round's points of "+".

    So my point is that using fixed number of variable pre, pre1, pre2 has the problem when the number of continuous "C" is greater or equal to the number of variable to store previous round's points - 1.

    Hope this will help.


  • 0
    B

    @BryanBo.Cao Thanks. You are right about the flaw in this implementation. And I know the correct implementation should be keeping track of all valid scores. What I'm trying to understand is why OJ gives result that is different than other places. Still use above code, it fails at:

    ["15483","-20523","C","C","9061","26083","+","C","4702","+","D","10260","-525","C","+","15023","-20005","-1647","C","-12853","20706","D","-21983","24892","10570","D","1215","D","D","+","3854","20505","C","D","-18850","-2170","27914","-26175","+","3188","+","+","21804","D","+","-847","D","26184","14945","C","D","+","+","13795","+","1839","15755","2627","-2090","C","C","29743","24319","D","-22624","20374","+","D","2631","+","7296","-5109","9454","-10466","D","C","+","17555","+","12144","D","16710","27969","22863","D","8521","+","D","C","-25486","-1137","9782","25633","-12031","-11248","+","C","-13559","D","D"]
    

    It doesn't have continuous "C"s followed by "+" or "D". So it's not failing because of the flaw you pointed out.


  • 1

    @rajincse said in OJ bug?:

    OJ accepted the same code after the contest end.

    Which two submissions? (I mean the not-accepted one during the contest and the accepted one after the contest)


  • 0

    @Bigcat Yes you are right. That was for the code in general but not for this test case :)


  • 0
    R

    @linzihan I didn't have to change a single line of code. It got accepted after the contest was over. #FAIL


  • 0
    R

    @StefanPochmann The code was not accepted during the contest. However, the same code got accepted when the contest was over.


  • 0

    @rajincse Would've been helpful to know which of your eight submissions to look at, but ok, I checked them all. And... you're wrong. You did change the code. You changed a backUpBuffer.remove() to backUpBuffer.removeLast(). During the contest you used remove, after the contest you used removeLast.


  • 0
    A

    @linzihan I too have the exact same problem. Were you able to fix it?


  • 0
    R

    @StefanPochmann Thanks. I didn't notice. However, changing back to backUpBuffer.remove() gave the correct answer -117 for the aforementioned test case. I would appreciate if you can check it.


  • 0

    @rajincse said in OJ bug?:

    @StefanPochmann However, changing back to backUpBuffer.remove() gave the correct answer -117 for the aforementioned test case.

    Not sure what you mean. Are you in this situation? https://discuss.leetcode.com/topic/104782/


Log in to reply
 

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