← 返回 snowflake 的题目列表RecordCollection Sort Colors
类型:qbank
LeetCode 75 / Sort Colors variant: a `RecordCollection` holds a list of colors encoded as RGB / 0-1-2. The collection exposes `getColor(i)` and `swap(i, j)`; implement the in-place O(n) rearrangement and write tests against the class API.
Requirements
Input is a RecordCollection object, not a raw list.
Each record has a color value: red / green / blue, encoded as 0, 1, 2.
The collection provides getColor(i) and swap(i, j); those helpers are already implemented.
Reorder the collection in place so equal colors are grouped in sorted RGB / numeric order.
Required complexity: O(n) time and in-place swapping.
Write your own tests and run them manually.
Notes
The prompt is explicitly a LeetCode 75 variant, but the API wrapper matters: do not assume direct list indexing or assignment.
The common failure mode is accidentally using extra arrays because the object wrapper hides the underlying list. Keep the implementation expressed in terms of getColor and swap.
Clarify whether the collection exposes len() / size(); if not, ask how the number of records is provided.
Preparation
Implement the in-place three-color partition using only getColor(i) and swap(i, j).
Write tests for empty collection, one color only, already sorted, reverse sorted, and repeated middle color.
Practice narrating why the implementation remains O(n) even though some indices are rechecked after swaps.