Skip to content

Commit

Permalink
Merge branch 'master' into camelcase
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanFehr authored Jun 5, 2018
2 parents 3180e0d + bfa99d5 commit e180b32
Show file tree
Hide file tree
Showing 29 changed files with 1,447 additions and 42 deletions.
57 changes: 57 additions & 0 deletions Algorithms/Implementation/Cavity Map/Solution.cs
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 Algorithms/Implementation/Day Of The Programmer/Solution.cs
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);
}
}
56 changes: 56 additions & 0 deletions Algorithms/Implementation/Fair Rations/Solution.cs
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);
}
}
25 changes: 12 additions & 13 deletions Algorithms/Implementation/Fair Rations/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
[1 1 1 1 1 1 1 1 1 1 1 1 1] unsolvable
O(n) inplace or O(n) space
Time: O(n)
Space: O(1)
*/
Expand All @@ -55,18 +55,16 @@ public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int B[] = new int[N];
for(int B_i=0; B_i < N; B_i++){
B[B_i] = in.nextInt();
}


int bread = 0;
int currentBread = 0;

for(int i = 0; i < B.length; i++)
for(int i = 0; i < N; i++)
{
if(i == B.length-1)//We have reached the last person
currentBread += in.nextInt();
if(i == N-1)//We have reached the last person
{
if(B[i] % 2 == 1) //If last person ended with odd bread it is not possible
if(currentBread % 2 == 1) //If last person ended with odd bread it is not possible
{
System.out.println("NO");
System.exit(0);
Expand All @@ -79,12 +77,13 @@ public static void main(String[] args) {
}


if(B[i] % 2 == 1) //The current person has odd bread give them and the person behind them bread
if(currentBread % 2 == 1) //The current person has odd bread give them and the next person bread
{
B[i] = B[i] + 1;
B[i+1] = B[i+1] +1;
currentBread = 1;
bread += 2;
continue;
}
currentBread = 0; // No extra bread was given out
}

}
Expand Down
Loading

0 comments on commit e180b32

Please sign in to comment.