Skip to content

Commit efb74f3

Browse files
authored
Merge pull request #180 from sir-gon/feature/feature/new_year_chaos
[Hacker Rank] Interview Preparation Kit: Arrays: New Year Chaos. Solv…
2 parents 65ebfbf + 7cf0d68 commit efb74f3

File tree

5 files changed

+335
-0
lines changed

5 files changed

+335
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
2+
3+
using System.Diagnostics.CodeAnalysis;
4+
5+
6+
/**
7+
* New Year Chaos.
8+
*
9+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md]]
10+
*/
11+
public class NewYearChaos
12+
{
13+
[ExcludeFromCodeCoverage]
14+
protected NewYearChaos() { }
15+
16+
public const String TOO_CHAOTIC_ERROR = "Too chaotic";
17+
18+
/**
19+
* minimumBribesCalculate.
20+
*/
21+
public static int minimumBribesCalculate(List<int> q)
22+
{
23+
int bribes = 0;
24+
int i = 0;
25+
26+
foreach (int value in q)
27+
{
28+
int position = i + 1;
29+
30+
if (value - position > 2)
31+
{
32+
throw new InvalidOperationException(TOO_CHAOTIC_ERROR);
33+
}
34+
35+
List<int> fragment = q[Math.Min(Math.Max(value - 2, 0), i)..i];
36+
37+
foreach (int k in fragment)
38+
{
39+
if (k > value)
40+
{
41+
bribes += 1;
42+
}
43+
}
44+
i += 1;
45+
}
46+
47+
return bribes;
48+
}
49+
50+
/**
51+
* minimumBribes.
52+
*/
53+
public static String minimumBribesText(List<int> q)
54+
{
55+
try
56+
{
57+
int bribes = minimumBribesCalculate(q);
58+
return String.Format("{0}", bribes);
59+
}
60+
catch (InvalidOperationException e)
61+
{
62+
return String.Format(e.Message);
63+
}
64+
}
65+
66+
/**
67+
* minimumBribesText.
68+
*/
69+
public static void minimumBribes(List<int> q)
70+
{
71+
Console.WriteLine("{0}", minimumBribesText(q));
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[
2+
{
3+
"title": "Test Case 0-0",
4+
"input": [2, 1, 5, 3, 4],
5+
"expected": 3
6+
},
7+
{
8+
"title": "Test Case 0-1",
9+
"input": [2, 5, 1, 3, 4],
10+
"expected": "Too chaotic"
11+
},
12+
{
13+
"title": "Test Case 1-1",
14+
"input": [5, 1, 2, 3, 7, 8, 6, 4],
15+
"expected": "Too chaotic"
16+
},
17+
{
18+
"title": "Test Case 1-2",
19+
"input": [1, 2, 5, 3, 7, 8, 6, 4],
20+
"expected": 7
21+
},
22+
{
23+
"title": "Test Case 2",
24+
"input": [1, 2, 5, 3, 4, 7, 8, 6],
25+
"expected": 4
26+
}
27+
]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
2+
3+
[TestClass]
4+
public class NewYearChaosTest
5+
{
6+
public class NewYearChaosTestCase
7+
{
8+
public string title { get; set; } = default!;
9+
public List<int> input { get; set; } = default!;
10+
public string expected { get; set; } = default!;
11+
}
12+
13+
private List<NewYearChaosTestCase> testCases { get; set; } = default!;
14+
15+
[TestInitialize]
16+
public void testInitialize()
17+
{
18+
testCases = JsonLoader.resourceLoad<List<NewYearChaosTestCase>>(
19+
"hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json"
20+
) ?? [];
21+
}
22+
23+
[TestMethod]
24+
public void testMinimumBribesText()
25+
{
26+
string result;
27+
28+
foreach (NewYearChaosTestCase test in testCases)
29+
{
30+
result = NewYearChaos.minimumBribesText(test.input);
31+
NewYearChaos.minimumBribes(test.input);
32+
33+
Assert.AreEqual(test.expected, result);
34+
}
35+
}
36+
}
37+
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` `#arrays`
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` `#arrays`
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.

0 commit comments

Comments
 (0)