Skip to content

Commit f0bbe50

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Arrays: New Year Chaos. Solved ✅.
1 parent 32837ef commit f0bbe50

File tree

4 files changed

+272
-0
lines changed

4 files changed

+272
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# [Arrays: New Year Chaos](https://www.hackerrank.com/challenges/new-year-chaos)
2+
3+
Determine how many bribes took place to get a queue into its current state.
4+
5+
- Difficulty: ` #medium `
6+
- Category: ` #ProblemSolvingBasic `
7+
8+
## Solution sources
9+
10+
- This solution focuses on "who were bribed" and counts his displacements with
11+
respect to those who bribed him
12+
(they have original position numbers greater than this one in front of him):
13+
[Solution to HackerRank's New Year Chaos in Python](https://csanim.com/tutorials/hackerrank-solution-new-year-chaos)
14+
15+
- This solution focuses on the expected positions and compares whether the "briber"
16+
is ahead in line: <https://www.youtube.com/watch?v=LgszjFykAbE>
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# [Arrays: New Year Chaos](https://www.hackerrank.com/challenges/new-year-chaos)
2+
3+
Determine how many bribes took place to get a queue into its current state.
4+
5+
- Difficulty: ` #medium `
6+
- Category: ` #ProblemSolvingBasic `
7+
8+
It is New Year's Day and people are in line for the Wonderland rollercoaster ride.
9+
Each person wears a sticker indicating their initial position in
10+
the queue from ` 1 ` to ` n `.
11+
12+
Any person can bribe the person directly in front of them to swap positions,
13+
but they still wear their original sticker. One person can bribe at most two others.
14+
15+
Determine the minimum number of bribes that took place to get
16+
to a given queue order.
17+
Print the number of bribes, or, if anyone has bribed more than two people,
18+
print ` Too chaotic `.
19+
20+
## Example
21+
22+
$ q = [1, 2, 3, 4, 5, 6, 7, 8] $
23+
24+
If person ` 5 ` bribes person ` 4 `, the queue will look like this:
25+
$ [1, 2, 3, 5, 4, 6, 7, 8] $. Only ` 1 ` bribe is required. Print ` 1 `.
26+
27+
$ q = [4, 1, 2, 3] $
28+
29+
Person ` 4 ` had to bribe ` 3 ` people to get to the current position.
30+
Print `Too chaotic`.
31+
32+
## Function Description
33+
34+
Complete the function minimumBribes in the editor below.
35+
36+
minimumBribes has the following parameter(s):
37+
38+
- `int q[n]`: the positions of the people after all bribes
39+
40+
## Returns
41+
42+
- No value is returned. Print the minimum number of bribes necessary or
43+
` Too chaotic ` if someone has bribed more than people.
44+
45+
## Input Format
46+
47+
The first line contains an integer ` t `, the number of test cases.
48+
49+
Each of the next ` t ` pairs of lines are as follows:
50+
51+
- The first line contains an integer ` t `, the number of people in the queue
52+
- The second line has `n` space-separated integers describing the
53+
final state of the queue.
54+
55+
## Constraints
56+
57+
- $ 1 \leq t \leq 10 $
58+
- $ 1 \leq n \leq 10^5 $
59+
60+
## Subtasks
61+
62+
For `60%` score $ 1 \leq t \leq 10^3 $
63+
For `100%` score $ 1 \leq t \leq 10^5 $
64+
65+
## Sample Input
66+
67+
```text
68+
STDIN Function
69+
----- --------
70+
2 t = 2
71+
5 n = 5
72+
2 1 5 3 4 q = [2, 1, 5, 3, 4]
73+
5 n = 5
74+
2 5 1 3 4 q = [2, 5, 1, 3, 4]
75+
```
76+
77+
## Sample Output
78+
79+
```text
80+
3
81+
Too chaotic
82+
```
83+
84+
## Explanation
85+
86+
### Test Case 1
87+
88+
The initial state:
89+
90+
```mermaid
91+
flowchart LR
92+
93+
A[Ride!]:::first
94+
1([1])
95+
2([2])
96+
3([3])
97+
4([4])
98+
5([5])
99+
100+
A ~~~ 1
101+
1 -.- 2
102+
2 -.- 3
103+
3 -.- 4
104+
4 -.- 5
105+
106+
classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px;
107+
classDef first fill:#9FBCE8
108+
classDef emphasys fill:#FEB130
109+
```
110+
111+
After person `5` moves one position ahead by bribing person `4`:
112+
113+
```mermaid
114+
flowchart LR
115+
A[Ride!]:::first
116+
1([1])
117+
2([2])
118+
3([3])
119+
4([4]):::emphasys
120+
5([5]):::emphasys
121+
122+
A ~~~ 1
123+
1 -.- 2
124+
2 -.- 3
125+
3 -.- 5
126+
5 -.- 4
127+
128+
classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px;
129+
classDef first fill:#9FBCE8
130+
classDef emphasys fill:#FEB130
131+
```
132+
133+
Now person `5` moves another position ahead by bribing person `3`:
134+
135+
```mermaid
136+
flowchart LR
137+
A[Ride!]:::first
138+
1([1])
139+
2([2])
140+
3([3]):::emphasys
141+
4([4])
142+
5([5]):::emphasys
143+
144+
A ~~~ 1
145+
1 -.- 2
146+
2 -.- 5
147+
5 -.- 3
148+
3 -.- 4
149+
150+
classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px;
151+
classDef first fill:#9FBCE8
152+
classDef emphasys fill:#FEB130
153+
```
154+
155+
And person `2` moves one position ahead by bribing person `3`:
156+
157+
```mermaid
158+
flowchart LR
159+
A[Ride!]:::first
160+
1([1]):::emphasys
161+
2([2]):::emphasys
162+
3([3])
163+
4([4])
164+
5([5])
165+
166+
A ~~~ 2
167+
2 -.- 1
168+
1 -.- 5
169+
5 -.- 3
170+
3 -.- 4
171+
172+
classDef default color:#000,fill:#FFE195,stroke:#333,stroke-width:4px;
173+
classDef first fill:#9FBCE8
174+
classDef emphasys fill:#FEB130
175+
```
176+
177+
So the final state is `2, 1, 5, 3, 4` after three bribing operations.
178+
179+
### Test Case 2
180+
181+
No person can bribe more than two people, yet it appears person `5` has done so.
182+
It is not possible to achieve the input state.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger';
3+
4+
import { minimumBribesTransform, TOO_CHAOTIC_ERROR } from './new_year_chaos';
5+
6+
const TEST_CASES = [
7+
{ title: 'Test Case 0-0', input: [2, 1, 5, 3, 4], expected: 3 },
8+
{
9+
title: 'Test Case 0-1',
10+
input: [2, 5, 1, 3, 4],
11+
expected: TOO_CHAOTIC_ERROR
12+
},
13+
{
14+
title: 'Test Case 1-1',
15+
input: [5, 1, 2, 3, 7, 8, 6, 4],
16+
expected: TOO_CHAOTIC_ERROR
17+
},
18+
{ title: 'Test Case 1-2', input: [1, 2, 5, 3, 7, 8, 6, 4], expected: 7 },
19+
{ title: 'Test Case 2', input: [1, 2, 5, 3, 4, 7, 8, 6], expected: 4 }
20+
];
21+
22+
describe('new_year_chaos', () => {
23+
it('minimumBribes Test Cases', () => {
24+
expect.assertions(5);
25+
26+
TEST_CASES.forEach((value) => {
27+
const answer = minimumBribesTransform(value.input);
28+
29+
console.debug(
30+
`minimumBribesTransform(${value.input}) solution found: ${answer}`
31+
);
32+
33+
expect(answer).toStrictEqual(value.expected);
34+
});
35+
});
36+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md]]
3+
*/
4+
5+
export const TOO_CHAOTIC_ERROR = 'Too chaotic';
6+
7+
export function minimumBribes(q: number[]): number {
8+
let bribes = 0;
9+
let i = 0;
10+
11+
q.forEach((value) => {
12+
const position = i + 1;
13+
if (value - position > 2) {
14+
throw new Error(TOO_CHAOTIC_ERROR);
15+
}
16+
17+
const fragment = q.slice(Math.max(value - 2, 0), i);
18+
19+
fragment.forEach((k) => {
20+
if (k > value) {
21+
bribes += 1;
22+
}
23+
});
24+
i += 1;
25+
});
26+
27+
return bribes;
28+
}
29+
30+
export function minimumBribesTransform(queue: number[]): number | string {
31+
try {
32+
return minimumBribes(queue);
33+
} catch (e) {
34+
return TOO_CHAOTIC_ERROR;
35+
}
36+
}
37+
38+
export default { minimumBribes, minimumBribesTransform, TOO_CHAOTIC_ERROR };

0 commit comments

Comments
 (0)