Skip to content

Instantly share code, notes, and snippets.

@jones
Created April 5, 2015 18:16
Show Gist options
  • Save jones/a153972b91487e97336e to your computer and use it in GitHub Desktop.
Save jones/a153972b91487e97336e to your computer and use it in GitHub Desktop.
"Incoming! Fire the defense turrets at these coordinates! Go go go!"
#include <cmath>
#include <vector>
class TurretDefense {
public:
int firstMiss(std::vector <int>xs, std::vector <int>ys, std::vector <int> times) {
int curx = 0;
int cury = 0;
int curtime = 0;
for(int i = 0; i < xs.size(); i++) {
if (curtime + manhattan(curx, cury, xs[i], ys[i]) <= times[i]) {
curtime = times[i];
curx = xs[i];
cury = ys[i];
} else {
return i;
}
}
return -1;
}
int manhattan(int xt, int yt, int xg, int yg) {
return abs(xt-xg) + abs(yt-yg);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment