Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
My Solution with explanation

Mohammed Dh Abbas

Jun 24, 2024

class Solution: def sort(self, arr): b, m, e = 0, 0, len(arr) - 1 while m <= e: if arr[e] == 2: e -= 1 elif arr[b] == 0: # here is the tricky part if the middle pointer at same position as the position as the beginning pointer # and the beginning pointer = 0 it meas we are done with that part of the array so we can safely # move the middle pointer too together with the beginning pointer if m == b: m += 1 b += 1 elif arr[m] == 1: m += 1 elif arr[e] < arr[m]: # swap arr[e], arr[m] = arr[m], arr[e] elif arr[m] < arr[b]: # swap arr[m], arr[b] = arr[b], arr[m] return arr

0

0

Comments
Comments

On this page

Problem Statement

Examples

Example 2

Try it yourself