Question about test case!


  • 0
    P

    The question says that It doesn't matter what you leave beyond the new length.

    But in the test case [1,1,1], the output [1,1,1] and return 2 are rejected, and it says [1,1] is expected

    Is this conflict with the question?

    Thanks!!


  • 0
    J

    Yeah this problem is extremely unclear about what it actually expects as output. Would be nice to revise it.

    Oh well, I have something working locally, not worth time trying to figure out what leetcode wants here.


  • 0
    P

    @jamesfm Yeah, you are right, not worthy. Thanks!


  • 0

    The problem is fine, you're probably just doing something wrong. Can't tell without seeing your code, of course.


  • 0
    P

    @StefanPochmann Sorry for not posting codes before~~

    class Solution(object):
        def removeDuplicates(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """ 
            n = len(nums)
            if n<=2:
            	return n
            curr = nums[-1]
            count = 1
            dupLen = 0
            for i in range( -2, -n-1, -1 ):
            	if nums[i]==curr:
            		count += 1
            		if count > 3:
            			dupLen += 1
            			nums.pop(i)
            			nums.append(curr)
            	else:
            		curr = nums[i]
            		count = 1
            return n - dupLen
    

    Please let me know if there are any stupid errors in my code. Thanks Stefan!


  • 0

    @Phillf Well you actually return 3, not 2.


  • 0
    P

    @StefanPochmann That's true, the code I ran locally is count >= 3, and that's the reason why I got the correct answer on my own tests but failed on leetcode. I should be more careful when copying and pasting my code. Thanks again!


  • 0
    J

    Maybe it's just the Ruby test case that is screwed up. Seems to be trying to compare against an array instead of a size.

    0_1473723980344_Screen Shot 2016-09-12 at 6.44.47 PM.png
    0_1473723992467_Screen Shot 2016-09-12 at 6.45.15 PM.png

    Problem is return an array results in a run time error.

    Here is some code that I quickly converted from an array to a number,

    # @param {Integer[]} nums
    # @return {Integer}
    def remove_duplicates(nums)
      freq = {}
      nums.each do |n|
        if freq.include? n
          freq[n] += 1
        else
          freq[n] = 1
        end
      end
    
      freq.map do |k, v|
        stop = [2, v].min
        (0...stop).map do |i|
          k
        end
      end.compact.flatten.size
    end
    

  • 0

    @jamesfm Well you're not changing nums at all. You'd better read the problem again.


  • 0

    @jamesfm For input [1,1,1,2] you return 3 and since you don't change nums, its first three elements are still [1,1,1], exactly as shown by LeetCode. Not [1,1,2] like they should be.


  • 1
    J

    @StefanPochmann Oh yup you are right. The wording in this question is bizarre, I didn't pick up the fact that they wanted you to mutate nums. (Most of the leetcode questions I have done so far don't do parameter mutation)

    Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.

    Would be better worded as:

    Your function should return length = 5, and modify nums to start with [1, 1, 2, 2, 3]. It doesn't matter what you leave beyond the new length.


  • 0

    @jamesfm How does a problem called "Remove Duplicates from Sorted Array II" not clearly want you to mutate the array? And if that weren't enough, it's obvious that you need to modify the array in order to go from [1,1,1,2,2,3] to starting with [1,1,2,2,3]. Your rewording is unnecessary, the problem is fine as it is.


  • 0

    @jamesfm That said, your wording makes it even clearer than it already is, plus it's shorter, so maybe that would be better. Might also help to link to the original problem instead of just saying this one is a follow up, as the original does make it clearer. On the other hand, that original is easy to look up anyway, so that's yet another reason the problem already is clear :-P


Log in to reply
 

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