-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into camelcase
- Loading branch information
Showing
29 changed files
with
1,447 additions
and
42 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,57 @@ | ||
/* | ||
Problem: https://www.hackerrank.com/challenges/cavity-map/problem | ||
C# Language Version: 7.0 | ||
.Net Framework Version: 4.7 | ||
Tool Version : Visual Studio Community 2017 | ||
Key notes: | ||
- Basic idea is that we've to check all the positions which are not at boundary. | ||
a b c d | ||
e f g h | ||
i j k l | ||
m n o p | ||
So we've to check f,g,j,k positions only. | ||
- For any position being checked just compare it depth with all its adjacent positions sharing an edge | ||
and replace it with 'X' if it is deepest in its neighborhood. | ||
Time Complexity: O(n^2) //there is a nested loops where we've to iterate entire array except the ones on the boundary. | ||
Space Complexity: O(n^2) //we've to save n lines of input as a square matrix map for managing depths. | ||
*/ | ||
|
||
using System; | ||
|
||
class Solution | ||
{ | ||
static string[] CavityMap(string[] grid) | ||
{ | ||
for (var row = 1; row < grid.Length - 1; row++) | ||
{ | ||
for (var column = 1; column < grid.Length - 1; column++) | ||
{ | ||
if (grid[row][column] > grid[row - 1][column] | ||
&& grid[row][column] > grid[row + 1][column] | ||
&& grid[row][column] > grid[row][column - 1] | ||
&& grid[row][column] > grid[row][column + 1]) | ||
{ | ||
var removeCavity = grid[row].Remove(column, 1).Insert(column, "X"); | ||
|
||
grid[row] = removeCavity; | ||
} | ||
} | ||
} | ||
return grid; | ||
} | ||
|
||
static void Main(String[] args) | ||
{ | ||
var n = int.Parse(Console.ReadLine()); | ||
//grid has n rows. | ||
var grid = new string[n]; | ||
for (var grid_i = 0; grid_i < n; grid_i++) | ||
grid[grid_i] = Console.ReadLine(); | ||
var result = CavityMap(grid); | ||
Console.WriteLine(string.Join("\n", result)); | ||
|
||
|
||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
Algorithms/Implementation/Day Of The Programmer/Solution.cs
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,92 @@ | ||
/* | ||
Problem: https://www.hackerrank.com/challenges/day-of-the-programmer/problem | ||
C# Language Version: 6.0 | ||
.Net Framework Version: 4.7 | ||
Tool Version : Visual Studio Community 2017 | ||
Thoughts : | ||
We need special handling for Gregorian calendar and Julian calendar based on input year. | ||
It is pretty straight forward to reach to correct answer if we follow the information given in the problem. | ||
Since it is a O(1) complexity problem so not much optimization is possible. | ||
Time Complexity: O(1) //There are no loops. | ||
Space Complexity: O(1) //number of dynamically allocated variables remain constant for any input. | ||
*/ | ||
|
||
using System; | ||
|
||
class Solution | ||
{ | ||
static string Solve(int year) | ||
{ | ||
//256th Day | ||
var programmerDate = ""; | ||
if (year >= 1919) | ||
programmerDate = GetProgrammerDateForGregorianCalendar(year); | ||
else if (year <= 1917) | ||
{ | ||
programmerDate = GetProgrammerDateForJulianCalendar(year); | ||
} | ||
else | ||
{ | ||
//gregorian switch year | ||
programmerDate = "26.09.1918";// GetProgrammerDateForCalendarSwitchYear1918(year); | ||
} | ||
|
||
return programmerDate; | ||
} | ||
|
||
private static string GetProgrammerDateForCalendarSwitchYear1918(int year) | ||
{ | ||
//1918 was a gregorian calendar after 1918 (it wasn't a leap year) | ||
var daysTillAug = 230;// 31 + 15 + 31 + 30 + 31 + 30 + 31 + 31 | ||
var programmerDateInSeptember = 0; | ||
programmerDateInSeptember = 256 - daysTillAug; | ||
var dateWithFormat = programmerDateInSeptember + ".09." + year.ToString(); | ||
return dateWithFormat; | ||
} | ||
|
||
private static string GetProgrammerDateForJulianCalendar(int year) | ||
{ | ||
var daysTillAugInLeapYear = 244; //31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 | ||
var daysTillAugInNonLeapYear = 243; //31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 | ||
var programmerDateInSeptember = 0; | ||
programmerDateInSeptember = IsJulianLeapYear(year) ? 256 - daysTillAugInLeapYear : 256 - daysTillAugInNonLeapYear; | ||
var dateWithFormat = programmerDateInSeptember + ".09." + year.ToString(); | ||
return dateWithFormat; | ||
} | ||
|
||
private static bool IsJulianLeapYear(int year) | ||
{ | ||
if (year % 4 == 0) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
private static string GetProgrammerDateForGregorianCalendar(int year) | ||
{ | ||
var daysTillAugInLeapYear = 244; //31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 | ||
var daysTillAugInNonLeapYear = 243; //31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 | ||
var programmerDateInSeptember = 0; | ||
|
||
programmerDateInSeptember = IsGregorianLeapYear(year) ? 256 - daysTillAugInLeapYear : 256 - daysTillAugInNonLeapYear; | ||
var dateWithFormat = programmerDateInSeptember + ".09." + year.ToString(); | ||
return dateWithFormat; | ||
} | ||
|
||
private static bool IsGregorianLeapYear(int year) | ||
{ | ||
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
static void Main(String[] args) | ||
{ | ||
var year = int.Parse(Console.ReadLine()); | ||
var result = Solve(year); | ||
Console.WriteLine(result); | ||
} | ||
} |
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,56 @@ | ||
/* | ||
Problem: https://www.hackerrank.com/challenges/fair-rations/problem | ||
C# Language Version: 6.0 | ||
.Net Framework Version: 4.7 | ||
Tool Version : Visual Studio Community 2017 | ||
Thoughts : | ||
- Keep a count of distributed breads. | ||
- Iterate the entire array except the last element | ||
- check if the current bread count is odd then add 1 to it also add 1 to subsequent bread count. increment count of distributed breads by 2. | ||
- If last element of the array is even then print the count of distributed breads. | ||
- If last element of the array is odd then print "NO". | ||
Time Complexity: O(n) //Single iteration of entire array might be required in worst case. | ||
Space Complexity: O(1) //optimal solution | ||
O(n) //We're storing the initial bread distribution in an array. Space complexity can't match the optimal O(1) solution as in C# you have to read the entire console line at a time (size n). | ||
There is no way to iteratively read space delimited input. If there had been a Scanner like class which exists in Java then it would have been possible to accomplish the same algorithm in O(1) space complexity. | ||
*/ | ||
using System; | ||
|
||
class Solution | ||
{ | ||
static void FairRations(int[] B) | ||
{ | ||
var count = 0; | ||
for (var i = 0; i < B.Length - 1; i++) | ||
{ | ||
if (B[i] % 2 == 1) | ||
{ | ||
B[i] += 1; | ||
B[i + 1] += 1; | ||
count += 2; | ||
} | ||
|
||
//improvisation: Keep skipping the next elements if they are even | ||
while (i < B.Length - 1 && B[i + 1] % 2 == 0) | ||
i++; | ||
} | ||
|
||
if (B[B.Length - 1] % 2 == 1) | ||
Console.WriteLine("NO"); | ||
else | ||
Console.WriteLine(count.ToString()); | ||
} | ||
|
||
static void Main(String[] args) | ||
{ | ||
//No need to capture the count. We can use array's length property instead. | ||
Console.ReadLine(); | ||
var tempArray = Console.ReadLine().Split(' '); | ||
var breadLovesDistribution = Array.ConvertAll(tempArray, int.Parse); | ||
FairRations(breadLovesDistribution); | ||
} | ||
} |
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
Oops, something went wrong.