2013.12.22 04:39
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You are not suppose to use the library's sort function for this problem.
Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
Could you come up with an one-pass algorithm using only constant space?
Solution1:
First solution is the plain counting sort. Count the numbers first and rewrite the array next. This algorithm requires two passes of the array.
Time complexity is O(n), space compelxity is O(1).
Accepted code:
1 // 1WA, 1AC 2 class Solution { 3 public: 4 void sortColors(int A[], int n) { 5 // IMPORTANT: Please reset any member data you declared, as 6 // the same Solution instance will be reused for each test case. 7 if(n <= 0 || A == nullptr){ 8 return; 9 } 10 11 int n0, n1, n2; 12 int i, j; 13 14 // 1WA here, wrong sentence: n0 = n1 = n2; 15 n0 = n1 = n2 = 0; 16 for(i = 0; i < n; ++i){ 17 if(A[i] == 0){ 18 ++n0; 19 }else if(A[i] == 1){ 20 ++n1; 21 }else{ 22 ++n2; 23 } 24 } 25 26 j = 0; 27 for(i = 0; i < n0; ++i){ 28 A[j++] = 0; 29 } 30 for(i = 0; i < n1; ++i){ 31 A[j++] = 1; 32 } 33 for(i = 0; i < n2; ++i){ 34 A[j++] = 2; 35 } 36 } 37 };
Solution2:
The one-pass solution for this problem can be done with three iterators, corresponding to 0, 1 and 2. Put 0 on the front, 2 at the back and 1 in the middle.
It seems this algorithm is O(n) in time, but it is actually inefficient O(n^2). The process of inserting the "1"s into middle part requires a lot of swap operation. I don't think this is a good choice, because it's no faster than insertion sort.
Time complexity is O(n^2), space complexity is O(1).
Accepted code:
1 // 1TLE, 1WA, 1AC, one-pass but inefficient solution 2 class Solution { 3 public: 4 void sortColors(int A[], int n) { 5 if(n <= 0 || A == nullptr){ 6 return; 7 } 8 9 int i0, i1, i2; 10 int tmp; 11 12 i0 = 0; 13 i1 = n - 1; 14 i2 = n - 1; 15 // 1WA here, it's "<=" instead of "<". 16 while(i0 <= i1){ 17 if(A[i0] == 1){ 18 tmp = A[i0]; 19 A[i0] = A[i1]; 20 A[i1] = tmp; 21 --i1; 22 }else if(A[i0] == 2){ 23 tmp = A[i0]; 24 A[i0] = A[i2]; 25 A[i2] = tmp; 26 --i2; 27 if(i2 < i1){ 28 // 1TLE here, it's "i1 = i2" instead of "i2 = i1". 29 i1 = i2; 30 } 31 }else{ 32 ++i0; 33 } 34 } 35 } 36 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)