面试18之猫狗队列

题目描述
        有家动物收容所只收留猫和狗,但有特殊的收养规则,收养人有两种收养方式,第一种为直接收养所有动物中最早进入收容所的,第二种为选择收养的动物类型(猫或狗),并收养该种动物中最早进入收容所的。
       给定一个操作序列int[][2] ope(C++中为vector<vector<int>>)代表所有事件。若第一个元素为1,则代表有动物进入收容所,第二个元素为动物的编号,正数代表狗,负数代表猫;若第一个元素为2,则代表有人收养动物,第二个元素若为0,则采取第一种收养方式,若为1,则指定收养狗,若为-1则指定收养猫。请按顺序返回收养的序列。若出现不合法的操作,即没有可以符合领养要求的动物,则将这次领养操作忽略。
测试样例:
[[1,1],[1,-1],[2,0],[2,-1]]

返回:[1,-1]


方法一:

用一个vector来表示收养所,数组的头部元素就是最早进入收留所的动物,遍历一遍,找到第一个大于0的编号就是狗,第一个小于0的编号就是猫,
这种方式简单,但是效率低,每出一个动物都得遍历一遍,时间复杂度为O(N*N)。

vector<int> Asylum(vector<vector<int> > ope)
{

	vector<int> A;  //表示收养所
	vector<int> B;  //表示被收养的动物。

	for(int i = 0; i < ope.size(); ++i)
	{
		if(ope[i][0] == 1) //表示动物进收留所。
		{
				A.push_back(ope[i][1]); //将动物的编号放入收养所。  
		}

		if(ope[i][0] == 2) //表示有人收养动物了。
		{
			if(A.empty()) //如果收养所没有动物能收养,自己结束这次操作。
				continue;
			if(ope[i][1] == 0)  //第一种方式收养。收养的是最早进入收留所的动物。
			{
				B.push_back(A[0]);
				//收留所 要清除最早的动物。
				A.erase(A.begin());  
			}
			if(ope[i][1] == 1) //收养最早的狗。狗的编号大于0
			{	
				for(int j = 0; j < A.size(); ++j)
				{
					if(A[j] > 0) //找到最早的狗
					{
						B.push_back(A[j]);
						A.erase(A.begin()+j);
						break;
					}
				}
			}
			if(ope[i][1] == -1) //收养最早的猫。
			{
				for(int j = 0; j < A.size(); ++j)
				{
					if(A[j] < 0) //找到最早的猫,猫的编号小于0.
					{
						B.push_back(A[j]);
						A.erase(A.begin()+j);
						break;
					}
				}
			
			}
		}
	}
	return B;
}


方式二:
因为先进先出,用两个队列来表示收养所,猫队列的头部就是最早进入收留所的猫,狗队列的头部就是最早进入收留所的狗,
但是这种形式我们不能确定第一个进入的猫和第一个进入的狗谁最早进入,所以我们想到当入队列的时候,把动物的时间也压入。用一个额外的int变量来表示时序。

vector<int> Asylum(vector<vector<int> > ope)
{
	queue<int> cat;
	queue<int> dog;
	vector<int> ret;
	int idx = 0;  //表示时间。
	for(int i = 0; i < ope.size(); ++i)
	{
		if(ope[i][0] == 1) //有动物要进收养所了。
		{
			if(ope[i][1] > 0) //编号大于0,表示狗进入
			{
				dog.push(idx++); //把动物的时序也一并压入。
				dog.push(ope[i][1]);
			}
			else
			{
				cat.push(idx++);
				cat.push(ope[i][1]);
			}
		}
		if(ope[i][0] == 2) //表示有人要收养动物了。
		{
			if(ope[i][1] == 0) //第一种方式收养,收养最早的动物。
			{
				int catidx = cat.empty() ? 10000000 :cat.front(); 
				int dogidx = dog.empty() ? 10000000 :dog.front();
				if(catidx < dogidx)
				{
					cat.pop(); //把时序弹出。
					ret.push_back(cat.front() );
					cat.pop();
				}
				else
				{
					dog.pop(); //把时序弹出。
					ret.push_back(dog.front() );
					dog.pop();
				}
			}
			else if(ope[i][1] == 1) //第二种方式收养,收养狗
			{
				if(!dog.empty())
				{
					dog.pop(); //把时序弹出。
					ret.push_back(dog.front() );
					dog.pop();
				}
			}
			else
			{
				if(!cat.empty() )
				{
					cat.pop(); //把时序弹出。
					ret.push_back(cat.front() );
					cat.pop();
				}
			}
			
		}
	}
	return ret;
}

测试代码:

void test()
{
	vector<int > v[4];
	v[0].push_back(1);
	v[0].push_back(1);
	v[1].push_back(1);
	v[1].push_back(-1);
	v[2].push_back(2);
	v[2].push_back(0);
	v[3].push_back(2);
	v[3].push_back(-1);
	
	vector<vector<int> > vv;
	for(int i = 0; i < 4; ++i)
	{
		vv.push_back(v[i]);
	}

	vector<int> ret = Asylum(vv);

	for(int i = 0; i < ret.size(); ++i)
	{
		cout << ret[i] << " ";
	}
	cout <<endl;
}


int main()
{

	test();

	cout << "hello..."<<endl;

	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值