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. diff --git a/exercises/hackerrank/interview_preparation_kit/arrays/new_year_chaos.go b/exercises/hackerrank/interview_preparation_kit/arrays/new_year_chaos.go new file mode 100644 index 0000000..0026506 --- /dev/null +++ b/exercises/hackerrank/interview_preparation_kit/arrays/new_year_chaos.go @@ -0,0 +1,69 @@ +/** + * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md]] + */ + +package hackerrank + +import ( + "errors" + "fmt" +) + +//nolint:stylecheck // error strings should not be capitalized (ST1005) +const tooChaoticError = "Too chaotic" + +func minimumBribesCalculate(q []int32) (int32, error) { + + var bribes int32 + var i int32 = 0 + var value int32 + + for _, value = range q { + position := i + 1 + + if value-int32(position) > 2 { + //nolint:stylecheck // error strings should not be capitalized (ST1005) + return 0, errors.New(tooChaoticError) + } + + var fragment []int32 = q[min(max(value-2, 0), int32(i)):i] + + for _, k := range fragment { + if k > value { + bribes += 1 + } + } + i += 1 + } + + return bribes, nil + +} + +func minimumBribes(q []int32) { + result, err := minimumBribesCalculate(q) + + if err != nil { + fmt.Println(err) + + return + } + + fmt.Println(result) +} + +func MinimumBribes(q []int32) { + minimumBribes(q) +} + +func MinimumBribesText(q []int32) string { + result, err := minimumBribesCalculate(q) + + if err != nil { + fmt.Println(err) + + return fmt.Sprint(err) + } + + return fmt.Sprintf("%d", result) +} diff --git a/exercises/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json b/exercises/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json new file mode 100644 index 0000000..90d3bcc --- /dev/null +++ b/exercises/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/exercises/hackerrank/interview_preparation_kit/arrays/new_year_chaos_test.go b/exercises/hackerrank/interview_preparation_kit/arrays/new_year_chaos_test.go new file mode 100644 index 0000000..5d70e2e --- /dev/null +++ b/exercises/hackerrank/interview_preparation_kit/arrays/new_year_chaos_test.go @@ -0,0 +1,46 @@ +package hackerrank + +import ( + "fmt" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "gon.cl/algorithms/utils" +) + +type TestNewYearChaosTestCase struct { + Input []int32 `json:"input"` + Expected string `json:"expected"` +} + +var TestNewYearChaostestCases []TestNewYearChaosTestCase + +// You can use testing.T, if you want to test the code without benchmarking +func testNewYearChaosSetupSuite(t testing.TB) { + wd, _ := os.Getwd() + filepath := wd + "/new_year_chaos.testcases.json" + t.Log("Setup test cases from JSON: ", filepath) + + var _, err = utils.LoadJSON(filepath, &TestNewYearChaostestCases) + if err != nil { + t.Log(err) + } +} + +func TestNewYearChaos(t *testing.T) { + + testNewYearChaosSetupSuite(t) + + for _, tt := range TestNewYearChaostestCases { + testname := fmt.Sprintf("MinimumBribes(%v) => %v \n", tt.Input, tt.Expected) + t.Run(testname, func(t *testing.T) { + + MinimumBribes(tt.Input) + + ans := MinimumBribesText(tt.Input) + assert.Equal(t, tt.Expected, ans) + }) + + } +}