-
Notifications
You must be signed in to change notification settings - Fork 693
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from rasik210/sparsearrays
Sparse arrays problem: C# solution
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Problem: https://www.hackerrank.com/challenges/sparse-arrays/problem | ||
C# Language Version: 6.0 | ||
.NET Framework Version: 4.7 | ||
Tool Version : Visual Studio Community 2017 | ||
Thoughts (Key points in algorithm) : | ||
- Going via sparse matrix route the complexity comes out to be O(n * q * 20) so I solved it with help of hash map which gives better complexity | ||
- Scan each input string and stores its count in a hash map. | ||
- For each query string, check its count in the hash map and print it on console. | ||
Gotchas: | ||
<None> | ||
Time Complexity: O(n + q) //n is number of input strings and q is number of queries. | ||
Space Complexity: O(n + q) //a hash map (for storing frequency) and an array (for storing output counts) is addtional space requirement. | ||
*/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
class Solution | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var inputCount = int.Parse(Console.ReadLine()); | ||
var stringMap = new Dictionary<string, int>(); | ||
|
||
for (var i = 0; i < inputCount; i++) | ||
{ | ||
var input = Console.ReadLine(); | ||
if (stringMap.ContainsKey(input)) | ||
stringMap[input]++; | ||
else | ||
stringMap.Add(input, 1); | ||
} | ||
|
||
var queryCount = int.Parse(Console.ReadLine()); | ||
var output = new int[queryCount]; | ||
for (var i = 0; i < queryCount; i++) | ||
{ | ||
var queryString = Console.ReadLine(); | ||
if (stringMap.ContainsKey(queryString)) | ||
output[i] = stringMap[queryString]; | ||
} | ||
|
||
foreach (var item in output) | ||
Console.WriteLine(item); | ||
} | ||
} |
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