Sort Colors (Leetcode 75)

This is the Dutch National Flag Algorithm

Problem Link: https://leetcode.com/problems/sort-colors/

class Solution:
    def sortColors(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n=len(nums)
        i=0
        j=0
        k=n-1
        
        while(j<=k):
            if(nums[j]==0):
                nums[i],nums[j]=nums[j],nums[i]
                i+=1
                j+=1
            elif(nums[j]==1):
                j+=1
            else:
                nums[j],nums[k]=nums[k],nums[j]
                k-=1

Last updated