这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。
第二次参加,第一题再也没发生上次的悲剧了。
250: 5分钟看题,5分钟干掉。水题不多说:
class CarrotBoxesEasy{
public:
int theIndex(vector carrots, int K)
{
int _max = -1;
int idx = -1;
for(int i=1; i<=K; ++i)
{
_max = -1;
idx = -1;
for(int j=0; j _max)
{
_max = carrots[j];
idx = j;
}
--carrots[idx];
}
return idx;
}
};
575: 其实也是水题,但是不知道为何被我想复杂了。 今天再做一次,发现很简单,可以用两个vector A和B,一个存储从最小开始的成立的队列,一个存储从最后开始的成立的队列,然后比较A,B相应元素,若相等,则等于这个值,否则当前位置等于-1.
#include
#include
#include
#include
using namespace std;
class ColorfulCards{
public:
bool Is_prime(int n)
{
if(n==1)
return 0;
else if(n==2)
return 1;
else if(n==3)
return 1;
for(int i=2; i*i<=n; ++i)
if(n%i==0)
return 0;
return 1;
}
vector theCards(int N, string colors)
{
//cout << "N=" << N << " ; " << colors << endl;
vector beg, end, ans;
int cnt = 1;
for(int i=0; i=0; --i)
{
while( !( (colors[i]=='R'&&Is;_prime(cnt)) || (colors[i]=='B'&&!Is_prime(cnt)) ) )
--cnt;
end.push_back(cnt);
--cnt;
}
//for(int i=0; i<end.size(); ++i)
// cout << end[i] << endl;
//cout << endl;
for(int i=0; i<colors.size(); ++i)
if(beg[i] == end[colors.size()-1-i])
ans.push_back(beg[i]);
else
ans.push_back(-1);
//for(int i=0; i<ans.size(); ++i)
//cout << ans[i] << endl;
return ans;
}
};
这次增加了10几分。。。现在是953分了,下次争取干掉2题。。