Sunday, April 17, 2016

HackerRank: Connected Cells in a grid (V) - C++ solution

April 17, 2016

problem statement:

https://www.hackerrank.com/challenges/connected-cell-in-a-grid


C++ solution:
spent time: 40 minutes

1. use pair<int, int>, make_pair, and queue; very short code - excellent!

https://gist.github.com/jianminchen/a1d7ac03bcaea4e1def43f61f41f53cb

pair, make_pair, understand the standard library function template - make_pair:
http://www.cplusplus.com/reference/utility/make_pair/

2. Another solution:
https://gist.github.com/jianminchen/93839295fb15ab1a7d2226213f15dcd0

3. use vector
https://gist.github.com/jianminchen/3e489592beaa267c10540302bf08745b

4. interesting solution, no queue, no recursive calls; just store the values:
4
4
1 1 0 0
0 1 1 0
0 0 1 0
1 0 0 0

{-5, 0, -1, -1, -1, 0, 0, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, ...}           int[111]

https://gist.github.com/jianminchen/be42129298ca4ec156f06ecca58e72a1

No comments:

Post a Comment