975. 奇偶跳
解决方案:
- 第一种方法完全按照题目要求从头开始进行奇偶跳跃,来判断数组A中的每个索引是不是起始索引,这样的暴力做法肯定超时;
- 第二种想到的是用这样的map<int,vector>容器去求解,但还是和第一种解法一样,需要在map容器里找到进行奇偶跳跃后相应的位置,仍然在第59个例子超时;
- 通过万能的Google,让我们想起了map中两个相应的函数,upper_bound、lower_bound,其中lower_bound是找到(大于等于key=val的值),upper_bound是找到(大于key=val的值)
通过他们可以在map中与当前val1大的值val2,但是没有办法一次确定查找到的值在数组A中的位置是不是符合要求;
根据类似题目的解法,我们要确定map(自动默认按照key的大小,从小到大排序)容器中最key和value都是递增排序,所以我们从终点向起点递推,因为无论最后一步是奇跳跃还是偶跳跃在A.size()-1这个位置都是true
class Solution {
public:
int oddEvenJumps(vector<int>& A) {
//维护两个bool数组判断每个点进行奇偶跳跃后能否到达终点
vector<bool>odd(A.size()),even(A.size());
//终点都能进行奇偶跳跃
odd[A.size()-1]=true,even[A.size()-1]=true;
//从后往前记录数组A中出现的每一个值对应的位置,实时更新相同数值的位置
map<int,int>positons;
positons[A.back()]=A.size()-1;//将终点添加进map中
for (int i = A.size()-2; i >=0 ; --i) {
int val=A[i];
auto pos=positons.find(val);
//如果map中val已经存在,奇偶跳跃分别继承对应的状态
if(pos!=positons.end()){
odd[i]=even[pos->second];
even[i]=odd[pos->second];
}
//如果不存在我们来判断能否进行奇偶跳跃以及跳跃到什么位置
else{
auto pos1=positons.lower_bound(val);
auto pos2=positons.upper_bound(val);
//判断能否进行奇跳跃;因为存在大于等于val的值,val能在数组A中进行奇跳跃到相应的位置;
if(pos1!=positons.end()){
odd[i]=even[pos1->second];
}
//判断能否进行偶跳跃;因为map中大于val的值不在首位,val能在数组A中进行偶跳跃到相应的位置;
if(pos2!=positons.begin()){
--pos2;
even[i]=odd[pos2->second];
}
}
positons[val]=i;
}
int res=0;
for (int j = 0; j <A.size() ; ++j) {
if(odd[j])
res++;
}
return res;
}
};
简单的使用容器类的方法去解决,到最后几个压力测试的时候,总会时间超限,需要想方法加快奇偶跳跃的速度;
现在可能的想法就是使用一个结构树来解决问题
时间超限
class Solution {
public:
int oddEvenJumps(vector<int>& A) {
int ans = 0;
for (int i = 0; i < A.size(); ++i) {
if (check(i, A))
{
cout << A[i] << endl;
ans++;
}
}
return ans;
}
bool check(int begin, vector<int>A) {
int step = 1;
int initial_val = A[begin];
while (begin < A.size() - 1) {
switch (step)
{
case 1:
{
auto pos_max = max_element(A.begin() + begin + 1, A.end());
if (*pos_max < A[begin])
return false;
else
{
vector<int>tmp_num;
tmp_num.assign(A.begin() + begin + 1, A.end());
sort(tmp_num.begin(), tmp_num.end());
for (int i = 0; i < tmp_num.size(); ++i) {
if (tmp_num[i] >= initial_val)
{
initial_val = tmp_num[i];
break;
}
}
begin = distance(A.begin(), find(A.begin() + begin + 1, A.end(), initial_val));
}
break;
}
case 0:
{
auto pos_min = min_element(A.begin() + begin + 1, A.end());
if (*pos_min > A[begin])
return false;
else
{
vector<int>tmp_num;
tmp_num.assign(A.begin() + begin + 1, A.end());
sort(tmp_num.begin(), tmp_num.end(), [](int a, int b) { return a > b; });
for (int i = 0; i < tmp_num.size(); ++i) {
if (tmp_num[i] <= initial_val)
{
initial_val = tmp_num[i];
break;
}
}
begin = distance(A.begin(), find(A.begin() + begin + 1, A.end(), initial_val));
}
break;
}
}
step = (step + 1) % 2;
}
return true;
}
};
更换了map容器仍然是时间超时
class Solution {
public:
vector<int>odd_step;
vector<int>even_step;
int oddEvenJumps(vector<int>& A) {
int ans = 0;
int limit = A.size();
map<int, vector<int>>tmp1;
for (int j = 0; j < A.size(); ++j) {
tmp1[A[j]].push_back(j);
}
for (int i = 0; i < A.size(); ++i) {
vector<int>odd_tmp, even_tmp;
if (find(odd_step.begin(), odd_step.end(), i) != odd_step.end()) {
ans++;
continue;
}
else if (check(i, A[i], limit, tmp1, odd_tmp, even_tmp))
{
//cout << i << endl;
odd_step.insert(odd_step.end(), odd_tmp.begin(), odd_tmp.end());
odd_step.erase(unique(odd_step.begin(), odd_step.end()), odd_step.end());
sort(odd_step.begin(), odd_step.end());
even_step.insert(even_step.end(), even_tmp.begin(), even_tmp.end());
ans++;
}
}
return ans;
}
bool check(int location, int val, int limit, map<int, vector<int>>val_count, vector<int>&odd_tmp, vector<int>&even_tmp) {
int step = 1;
while (location < limit - 1) {
switch (step)
{
case 1:
{
odd_tmp.push_back(location);
if (find(odd_step.begin(), odd_step.end(), location) != odd_step.end())
return true;
int old_location = location;
auto origin = val_count.find(val);
for (auto i = origin; i != val_count.end(); ++i) {
pair<int, vector<int>>tmp_pair = *i;
if (tmp_pair.second.empty() || location >= tmp_pair.second.back()) {
val_count[tmp_pair.first].clear();
continue;
}
else {
val = tmp_pair.first;
for (int j = 0; j < tmp_pair.second.size(); ++j) {
if (location < tmp_pair.second[j])
{
location = tmp_pair.second[j];
break;
}
}
}
break;
}
if (old_location == location)
return false;
break;
}
case 0: {
even_tmp.push_back(location);
if (find(even_step.begin(), even_step.end(), location) != even_step.end())
return true;
int old_location = location;
auto origin = val_count.find(val);
for (auto i = origin; i != val_count.begin(); --i) {
pair<int, vector<int>>tmp_pair = *i;
if (tmp_pair.second.empty() || location >= tmp_pair.second.back()) {
val_count[tmp_pair.first].clear();
continue;
}
else {
val = tmp_pair.first;
for (int j = 0; j < tmp_pair.second.size(); ++j) {
if (location < tmp_pair.second[j])
{
location = tmp_pair.second[j];
break;
}
}
break;
}
}
if (location == old_location)//进入到最小值去寻找
{
pair<int, vector<int>>tmp_pair = *val_count.begin();
for (int i = 0; i < tmp_pair.second.size(); ++i) {
if (location < tmp_pair.second[i])
{
location = tmp_pair.second[i];
break;
}
}
if (location == old_location)
return false;
}
break;
}
}
step = (step + 1) % 2;
}
return true;
}
};