Skip to content

Commit 30828d9

Browse files
authored
Merge pull request #221 from sir-gon/feature/luck-balance
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Greedy Algorithms…
2 parents 2228a3d + b359f1c commit 30828d9

File tree

4 files changed

+238
-0
lines changed

4 files changed

+238
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package ae.hackerrank.interview_preparation_kit.greedy_algorithms;
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* LuckBalance.
10+
*
11+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/greedy_algorithms/luck-balance.md]]
12+
*/
13+
public class LuckBalance {
14+
15+
private class Competition {
16+
private int luck;
17+
private int important;
18+
19+
Competition(int luck, int important) {
20+
this.luck = luck;
21+
this.important = important;
22+
}
23+
24+
public int getLuck() {
25+
return this.luck;
26+
}
27+
28+
public int getImportant() {
29+
return this.important;
30+
}
31+
}
32+
33+
private LuckBalance() { }
34+
35+
/**
36+
* luckBalance.
37+
*
38+
* @link https://www.baeldung.com/java-sort-collection-multiple-fields#use-comparatorcomparing-and-lambda-expression
39+
*/
40+
public static int luckBalance(int k, List<List<Integer>> contests) {
41+
List<Competition> importantCompetitions = new ArrayList<>();
42+
List<Competition> nonimportantCompetitions = new ArrayList<>();
43+
44+
for (var x : contests) {
45+
Integer luck = x.get(0);
46+
Integer important = x.get(1);
47+
48+
if (important == 1) {
49+
importantCompetitions.add(new LuckBalance().new Competition(luck, important));
50+
} else {
51+
nonimportantCompetitions.add(new LuckBalance().new Competition(luck, important));
52+
}
53+
}
54+
55+
importantCompetitions = importantCompetitions
56+
.stream()
57+
.sorted(
58+
Comparator
59+
.comparing(Competition::getImportant).reversed()
60+
.thenComparing(Competition::getLuck).reversed()
61+
)
62+
.collect(Collectors.toList());
63+
64+
int total = 0;
65+
int size = importantCompetitions.size();
66+
67+
for (int i = 0; i < Math.min(k, size); i++) {
68+
total += importantCompetitions.get(i).getLuck();
69+
}
70+
71+
for (int i = Math.min(k, size); i < size; i++) {
72+
total -= importantCompetitions.get(i).getLuck();
73+
}
74+
75+
for (Competition x : nonimportantCompetitions) {
76+
total += x.luck;
77+
}
78+
79+
return total;
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ae.hackerrank.interview_preparation_kit.greedy_algorithms;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestInstance;
10+
import org.junit.jupiter.api.TestInstance.Lifecycle;
11+
import util.JsonLoader;
12+
13+
/**
14+
* LuckBalanceTest.
15+
*/
16+
@TestInstance(Lifecycle.PER_CLASS)
17+
class LuckBalanceTest {
18+
public static class LuckBalanceTestCase {
19+
public String title;
20+
public Integer k;
21+
public List<List<Integer>> contests;
22+
public Integer expected;
23+
}
24+
25+
private List<LuckBalanceTestCase> testCases;
26+
27+
@BeforeAll
28+
public void setup() throws IOException {
29+
String path = String.join("/",
30+
"hackerrank",
31+
"interview_preparation_kit",
32+
"greedy_algorithms",
33+
"luck_balance.testcases.json");
34+
35+
this.testCases = JsonLoader.loadJson(path, LuckBalanceTestCase.class);
36+
}
37+
38+
@Test void testLuckBalance() {
39+
for (LuckBalanceTestCase test : testCases) {
40+
Integer result = LuckBalance.luckBalance(test.k, test.contests);
41+
42+
assertEquals(test.expected, result,
43+
"%s(%d, %s) => must be: %s".formatted(
44+
"LuckBalance.luckBalance",
45+
test.k,
46+
test.contests,
47+
test.expected
48+
)
49+
);
50+
}
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"title": "Sample Test case 0",
4+
"k": 3,
5+
"contests": [[5, 1], [2, 1], [1, 1], [8, 1], [10, 0], [5, 0]],
6+
"expected": 29
7+
}
8+
]
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# [Greedy Algorithms: Luck Balance](https://www.hackerrank.com/challenges/luck-balance)
2+
3+
- Difficulty: `#easy`
4+
- Category: `#ProblemSolvingBasic`
5+
6+
Lena is preparing for an important coding competition that is preceded
7+
by a number of sequential preliminary contests.
8+
Initially, her luck balance is 0.
9+
She believes in "saving luck", and wants to check her theory.
10+
Each contest is described by two integers, `L[i]` and `T[i]`:
11+
12+
- `L[i]` is the amount of luck associated with a contest.
13+
If Lena wins the contest, her luck balance will decrease by `L[i]`;
14+
if she loses it, her luck balance will increase by `L[i]`.
15+
16+
- `T[i]` denotes the contest's importance rating.
17+
It's equal to `1` if the contest is important, and it's equal to `0` if it's unimportant.
18+
19+
If Lena loses no more than `k` important contests, what is the maximum amount
20+
of luck she can have after competing in all the preliminary contests?
21+
This value may be negative.
22+
23+
## Example
24+
25+
```text
26+
Contest L[i] T[i]
27+
1 5 1
28+
2 1 1
29+
3 4 0
30+
```
31+
32+
If Lena loses all of the contests, her will be `5 + 1 +4 = 10`.
33+
Since she is allowed to lose important contests,
34+
and there are only `2` important contests,
35+
she can lose all three contests to maximize her luck at `10`.
36+
37+
If `k = 1`, she has to win at least of the important contests.
38+
She would choose to win the lowest value important contest worth `1`.
39+
Her final luck will be `5 + 4 - 1 = 8`.
40+
41+
## Function Description
42+
43+
Complete the luckBalance function in the editor below.
44+
45+
luckBalance has the following parameter(s):
46+
47+
- `int k`: the number of important contests Lena can lose
48+
- `int contests[n][2]`: a 2D array of integers where each `contests[i]`
49+
contains two integers that represent the luck balance and importance of the contest
50+
51+
## Returns
52+
53+
- `int`: the maximum luck balance achievable
54+
55+
## Input Format
56+
57+
The first line contains two space-separated integers `n` and `k`,
58+
the number of preliminary contests and the maximum number
59+
of important contests Lena can lose.
60+
61+
Each of the next lines contains two space-separated integers,
62+
`L[i]` and `T[i]`, the contest's luck balance and its importance rating.
63+
64+
## Constraints
65+
66+
- $ 1 \leq n \leq 100 $
67+
- $ 0 \leq k \leq N $
68+
- $ 1 \leq L[i] \leq 10^4 $
69+
- $ T[i] \isin \{0,1\} $
70+
71+
## Sample Input
72+
73+
```text
74+
STDIN Function
75+
----- --------
76+
6 3 n = 6, k = 3
77+
5 1 contests = [[5, 1], [2, 1], [1, 1], [8, 1], [10, 0], [5, 0]]
78+
2 1
79+
1 1
80+
8 1
81+
10 0
82+
5 0
83+
```
84+
85+
## Sample Output
86+
87+
```text
88+
29
89+
```
90+
91+
## Explanation
92+
93+
There are `n = 6` contests. Of these contests, `4` are important
94+
and she cannot lose more than of them.
95+
Lena maximizes her luck if she wins the $ 3^{rd} $ important contest
96+
(where `L[i] = 1`) and loses all of the other five contests for a total
97+
luck balance of `5 + 2 + 8 + 10 + 5 - 1 = 29`.

0 commit comments

Comments
 (0)