Why the result from the website is different from my own


  • 0
    B

    My code:

    public class Solution {
    static int counter = 0;

    public static boolean increasingTriplet(int[] nums) {
    
    	if (nums.length < 3) {
    		return false;
    	}
    
    	int currentMin = nums[nums.length - 1];
    	for (int i = nums.length - 2; i >= 0; i--) {
    		if (nums[i] < currentMin) {
    			currentMin = nums[i];
    			counter++;
    			if (counter == 2) {
    				return true;
    			}
    		}
    	}
    
    	return false;
    
    }
    

    }

    TestCase:

    2, 1, 5, 0, 4, 6

    My result is True(which is correct), but the submission told me my result is False.


  • 0

    That is due to the static statement in your code. Change static int counter = 0 to int counter = 0 and move it into your method increasingTriplet will pass this test case.

    But notice even so your algorithm is not correct, just consider the test case [1,2,3,1,2,1].


Log in to reply
 

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