Created
March 3, 2021 07:12
-
-
Save ricanteja/59500e74debc7fa68a59ec73a0321b2e to your computer and use it in GitHub Desktop.
My solution to https://www.youtube.com/watch?v=rw4s4M3hFfs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ricardo Tejada (c) 2021 | |
using System; | |
using System.Collections.Generic; | |
namespace InterviewQuestions | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var Blocks = new List<Dictionary<string, bool>>() | |
{ | |
new Dictionary<string, bool>() | |
{ | |
{ "gym", false }, { "school", true }, { "store", false } | |
}, | |
new Dictionary<string, bool>() | |
{ | |
{ "gym", true }, { "school", false }, { "store", false } | |
}, | |
new Dictionary<string, bool>() | |
{ | |
{ "gym", true }, { "school", true }, { "store", false } | |
}, | |
new Dictionary<string, bool>() | |
{ | |
{ "gym", false }, { "school", true }, { "store", false } | |
}, | |
new Dictionary<string, bool>() | |
{ | |
{ "gym", false }, { "school", true }, { "store", true } | |
} | |
}; | |
var Reqs = new string[] { "gym", "school", "store" }; | |
int lastScore = 0, highScore = 0, bestBlock = 0; | |
for (int i = 0; i < Blocks.Count; i++) | |
{ | |
int currScore = 0; | |
for (int j = 0; j < Reqs.Length; j++) | |
currScore = (currScore << 1) | (Blocks[i][Reqs[j]] ? 1 : 0); | |
if ((currScore | lastScore) >= highScore) | |
{ | |
bestBlock = i; | |
highScore = currScore | lastScore; | |
} | |
lastScore = currScore; | |
} | |
Console.WriteLine($"Best block to live on is #{bestBlock}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ricanteja
If we expand the input data, in order to allow for more testing combinations, your code doesn't appear to work. I added one more location item type,
park
, as well as several moreblocks
. Given the following, the answer should be block index 5. But your code is still outputting 3.