debug they have same value but still get index out of range

80 Views Asked by At

When I debug my code, the value of the variables shows that they are equal to my limit condition but my while loop still works

I'm trying to stop my while loop by this limitation:

while (i + j) != len(fruits):

Even though (i+j) is equal to len(fruits), the loop still works, it doesn't break. It should be broken when it meets my limitation. My code is below:

from typing import List
class Solution:
    def totalFruit(self, fruits: List[int]) -> int:
        pointed = [0 for i in range(max(fruits) + 1)]
        maX, count = 0, 0
        count_type = 0
        i, j = 0, 0
        while (i + j) != len(fruits):
            while count_type < 3:
                if pointed[fruits[i + j]] == 0:
                    count_type += 1
                    if count_type < 3:
                        count += 1
                        pointed[fruits[i + j]] += 1
                        j += 1

                elif count_type < 3 and pointed[fruits[i + j]] != 0:
                    pointed[fruits[i + j]] += 1
                    count += 1
                    j += 1
                
                if count > maX: maX = count

            if pointed[fruits[i]] == 1: count_type -= 2

            pointed[fruits[i]] -= 1
            count -= 1
            i += 1 
            j-= 1

        return maX
            
fruits = input()
fruits = [int(i) for i in fruits.split()]
obj = Solution()
print(obj.totalFruit(fruits))

when I debug: enter image description here

0

There are 0 best solutions below