From 7cf0d685492c802cb6fe66d9815744c47c1186d7 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Sun, 16 Feb 2025 16:35:22 -0300 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation=20Kit?= =?UTF-8?q?:=20Arrays:=20New=20Year=20Chaos.=20Solved=20=E2=9C=93.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../arrays/NewYearChaos.cs | 73 +++++++ .../arrays/new_year_chaos.testcases.json | 27 +++ .../arrays/NewYearChaos.Test.cs | 37 ++++ .../arrays/new_year_chaos-solution-notes.md | 16 ++ .../arrays/new_year_chaos.md | 182 ++++++++++++++++++ 5 files changed, 335 insertions(+) create mode 100644 algorithm_exercises_csharp/src/hackerrank/interview_preparation_kit/arrays/NewYearChaos.cs create mode 100644 algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json create mode 100644 algorithm_exercises_csharp_test/src/hackerrank/interview_preparation_kit/arrays/NewYearChaos.Test.cs create mode 100644 docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos-solution-notes.md create mode 100644 docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md diff --git a/algorithm_exercises_csharp/src/hackerrank/interview_preparation_kit/arrays/NewYearChaos.cs b/algorithm_exercises_csharp/src/hackerrank/interview_preparation_kit/arrays/NewYearChaos.cs new file mode 100644 index 0000000..8a48320 --- /dev/null +++ b/algorithm_exercises_csharp/src/hackerrank/interview_preparation_kit/arrays/NewYearChaos.cs @@ -0,0 +1,73 @@ +namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit; + +using System.Diagnostics.CodeAnalysis; + + +/** + * New Year Chaos. + * + * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md]] + */ +public class NewYearChaos +{ + [ExcludeFromCodeCoverage] + protected NewYearChaos() { } + + public const String TOO_CHAOTIC_ERROR = "Too chaotic"; + + /** + * minimumBribesCalculate. + */ + public static int minimumBribesCalculate(List q) + { + int bribes = 0; + int i = 0; + + foreach (int value in q) + { + int position = i + 1; + + if (value - position > 2) + { + throw new InvalidOperationException(TOO_CHAOTIC_ERROR); + } + + List fragment = q[Math.Min(Math.Max(value - 2, 0), i)..i]; + + foreach (int k in fragment) + { + if (k > value) + { + bribes += 1; + } + } + i += 1; + } + + return bribes; + } + + /** + * minimumBribes. + */ + public static String minimumBribesText(List q) + { + try + { + int bribes = minimumBribesCalculate(q); + return String.Format("{0}", bribes); + } + catch (InvalidOperationException e) + { + return String.Format(e.Message); + } + } + + /** + * minimumBribesText. + */ + public static void minimumBribes(List q) + { + Console.WriteLine("{0}", minimumBribesText(q)); + } +} diff --git a/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json b/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json new file mode 100644 index 0000000..5527c31 --- /dev/null +++ b/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json @@ -0,0 +1,27 @@ +[ + { + "title": "Test Case 0-0", + "input": [2, 1, 5, 3, 4], + "expected": 3 + }, + { + "title": "Test Case 0-1", + "input": [2, 5, 1, 3, 4], + "expected": "Too chaotic" + }, + { + "title": "Test Case 1-1", + "input": [5, 1, 2, 3, 7, 8, 6, 4], + "expected": "Too chaotic" + }, + { + "title": "Test Case 1-2", + "input": [1, 2, 5, 3, 7, 8, 6, 4], + "expected": 7 + }, + { + "title": "Test Case 2", + "input": [1, 2, 5, 3, 4, 7, 8, 6], + "expected": 4 + } + ] diff --git a/algorithm_exercises_csharp_test/src/hackerrank/interview_preparation_kit/arrays/NewYearChaos.Test.cs b/algorithm_exercises_csharp_test/src/hackerrank/interview_preparation_kit/arrays/NewYearChaos.Test.cs new file mode 100644 index 0000000..5aee377 --- /dev/null +++ b/algorithm_exercises_csharp_test/src/hackerrank/interview_preparation_kit/arrays/NewYearChaos.Test.cs @@ -0,0 +1,37 @@ +namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit; + +[TestClass] +public class NewYearChaosTest +{ + public class NewYearChaosTestCase + { + public string title { get; set; } = default!; + public List input { get; set; } = default!; + public string expected { get; set; } = default!; + } + + private List testCases { get; set; } = default!; + + [TestInitialize] + public void testInitialize() + { + testCases = JsonLoader.resourceLoad>( + "hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json" + ) ?? []; + } + + [TestMethod] + public void testMinimumBribesText() + { + string result; + + foreach (NewYearChaosTestCase test in testCases) + { + result = NewYearChaos.minimumBribesText(test.input); + NewYearChaos.minimumBribes(test.input); + + Assert.AreEqual(test.expected, result); + } + } +} + diff --git a/docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos-solution-notes.md b/docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos-solution-notes.md new file mode 100644 index 0000000..e67bd4d --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos-solution-notes.md @@ -0,0 +1,16 @@ +# [Arrays: New Year Chaos](https://www.hackerrank.com/challenges/new-year-chaos) + +Determine how many bribes took place to get a queue into its current state. + +- Difficulty: `#medium` +- Category: `#ProblemSolvingBasic` `#arrays` + +## Solution sources + +- This solution focuses on "who were bribed" and counts his displacements with +respect to those who bribed him +(they have original position numbers greater than this one in front of him): +[Solution to HackerRank's New Year Chaos in Python](https://csanim.com/tutorials/hackerrank-solution-new-year-chaos) + +- This solution focuses on the expected positions and compares whether the "briber" + is ahead in line: diff --git a/docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md b/docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md new file mode 100644 index 0000000..aa96b3b --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md @@ -0,0 +1,182 @@ +# [Arrays: New Year Chaos](https://www.hackerrank.com/challenges/new-year-chaos) + +Determine how many bribes took place to get a queue into its current state. + +- Difficulty: `#medium` +- Category: `#ProblemSolvingBasic` `#arrays` + +It is New Year's Day and people are in line for the Wonderland rollercoaster ride. +Each person wears a sticker indicating their initial position in +the queue from ` 1 ` to ` n `. + +Any person can bribe the person directly in front of them to swap positions, +but they still wear their original sticker. One person can bribe at most two others. + +Determine the minimum number of bribes that took place to get +to a given queue order. +Print the number of bribes, or, if anyone has bribed more than two people, +print ` Too chaotic `. + +## Example + +$ q = [1, 2, 3, 4, 5, 6, 7, 8] $ + +If person ` 5 ` bribes person ` 4 `, the queue will look like this: +$ [1, 2, 3, 5, 4, 6, 7, 8] $. Only ` 1 ` bribe is required. Print ` 1 `. + +$ q = [4, 1, 2, 3] $ + +Person ` 4 ` had to bribe ` 3 ` people to get to the current position. +Print `Too chaotic`. + +## Function Description + +Complete the function minimumBribes in the editor below. + +minimumBribes has the following parameter(s): + +- `int q[n]`: the positions of the people after all bribes + +## Returns + +- No value is returned. Print the minimum number of bribes necessary or + ` Too chaotic ` if someone has bribed more than people. + +## Input Format + +The first line contains an integer ` t `, the number of test cases. + +Each of the next ` t ` pairs of lines are as follows: + +- The first line contains an integer ` t `, the number of people in the queue +- The second line has `n` space-separated integers describing the +final state of the queue. + +## Constraints + +- $ 1 \leq t \leq 10 $ +- $ 1 \leq n \leq 10^5 $ + +## Subtasks + +For `60%` score $ 1 \leq t \leq 10^3 $ +For `100%` score $ 1 \leq t \leq 10^5 $ + +## Sample Input + +```text +STDIN Function +----- -------- +2 t = 2 +5 n = 5 +2 1 5 3 4 q = [2, 1, 5, 3, 4] +5 n = 5 +2 5 1 3 4 q = [2, 5, 1, 3, 4] +``` + +## Sample Output + +```text +3 +Too chaotic +``` + +## Explanation + +### Test Case 1 + +The initial state: + +```mermaid +flowchart LR + + A[Ride!]:::first + 1([1]) + 2([2]) + 3([3]) + 4([4]) + 5([5]) + + A ~~~ 1 + 1 -.- 2 + 2 -.- 3 + 3 -.- 4 + 4 -.- 5 + + classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px; + classDef first fill:#9FBCE8 + classDef emphasys fill:#FEB130 +``` + +After person `5` moves one position ahead by bribing person `4`: + +```mermaid +flowchart LR + A[Ride!]:::first + 1([1]) + 2([2]) + 3([3]) + 4([4]):::emphasys + 5([5]):::emphasys + + A ~~~ 1 + 1 -.- 2 + 2 -.- 3 + 3 -.- 5 + 5 -.- 4 + + classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px; + classDef first fill:#9FBCE8 + classDef emphasys fill:#FEB130 +``` + +Now person `5` moves another position ahead by bribing person `3`: + +```mermaid +flowchart LR + A[Ride!]:::first + 1([1]) + 2([2]) + 3([3]):::emphasys + 4([4]) + 5([5]):::emphasys + + A ~~~ 1 + 1 -.- 2 + 2 -.- 5 + 5 -.- 3 + 3 -.- 4 + + classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px; + classDef first fill:#9FBCE8 + classDef emphasys fill:#FEB130 +``` + +And person `2` moves one position ahead by bribing person `3`: + +```mermaid +flowchart LR + A[Ride!]:::first + 1([1]):::emphasys + 2([2]):::emphasys + 3([3]) + 4([4]) + 5([5]) + + A ~~~ 2 + 2 -.- 1 + 1 -.- 5 + 5 -.- 3 + 3 -.- 4 + + classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px; + classDef first fill:#9FBCE8 + classDef emphasys fill:#FEB130 +``` + +So the final state is `2, 1, 5, 3, 4` after three bribing operations. + +### Test Case 2 + +No person can bribe more than two people, yet it appears person `5` has done so. +It is not possible to achieve the input state.