I came back from vacation. I started a new job. I'm exhausted mentally and physically from so many activities and no time to recover. I'm a week behind. It's past midnight.
Let's code.
I suppose one nice aspect of being so behind is I can sort of take these at my own pace, since I'm way far behind any sort of race with friends or anyone else doing this. But on the other hand... I am so far behind.
Anyway. A tree house. A 2d grid of values, and a need to check if a "tree" is "hidden" - basically if all trees directly between it and any cardinal edge of the grid are shorter, it's visible. And for the first part of the problem, it seems pretty simple, just a count of the number of 'visible' trees. A naieve (but good enough) solution is just to loop through every single tree. Since the input it square, it checking it would be O(n^3/2). Which... isn't too bad?
(note: an alternate solution might be to look in from the edges, which would be computationally faster, but perfect is the enemy of good enough, and I don't know how I would implement that as easily)
bool isTreeVisible(vector<vector<int>> grid, int x, int y)
{
// Edge checks: edge trees are always visible
if (x == 0 || y == 0 || x == (grid.size()-1) || y == (grid[x].size()-1))
return true;
int tree = grid[x][y], maxCheck;
// Left of row check
maxCheck = 0;
for (int i = 0; i < x; i++) maxCheck = max(maxCheck, grid[i][y]);
if (maxCheck < tree) return true;
/*
Repeat for all directions..
*/
return false;
}
That part took... a few seconds. The first problem this year to really do so with the initial approach. I didn't think it was that inefficient. But it does have a lot of loops.
Ooookay, a totally different problem roughly within the same space. A bit more... intense of a problem to think about, but not too bad overall. Just need to change up the checking function a bit. It's a bit simpler than it could be; it could have some complex line-of-sight thing. But for now, just check outwards until it finds the edge or first non-blocking tree.
A tip: All edge trees have a score of 0
, as you will always multiply by 0
for at least one factor.
int getSenicScore (vector<vector<int>> grid, int x, int y)
{
int tree = grid[x][y], dist, score;
// Calculate left score
dist = 0;
for (int i = x-1; i >= 0; i--)
{
dist++;
if (grid[i][y] >= tree) break;
}
score = dist;
/*
Repeat, *= dist to score for each direction
*/
return score;
}
Took about as long to run, but worked on the first try. Woo!