Skip to content

[Hacker Rank] Interview Preparation Kit: Greedy Algorithms: Luck Bala… #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# [Greedy Algorithms: Luck Balance](https://www.hackerrank.com/challenges/luck-balance)

- Difficulty: `#easy`
- Category: `#ProblemSolvingBasic` `#greedyalgorithms`

Lena is preparing for an important coding competition that is preceded
by a number of sequential preliminary contests.
Initially, her luck balance is 0.
She believes in "saving luck", and wants to check her theory.
Each contest is described by two integers, `L[i]` and `T[i]`:

- `L[i]` is the amount of luck associated with a contest.
If Lena wins the contest, her luck balance will decrease by `L[i]`;
if she loses it, her luck balance will increase by `L[i]`.

- `T[i]` denotes the contest's importance rating.
It's equal to `1` if the contest is important, and it's equal to `0` if it's unimportant.

If Lena loses no more than `k` important contests, what is the maximum amount
of luck she can have after competing in all the preliminary contests?
This value may be negative.

## Example

```text
Contest L[i] T[i]
1 5 1
2 1 1
3 4 0
```

If Lena loses all of the contests, her will be `5 + 1 +4 = 10`.
Since she is allowed to lose important contests,
and there are only `2` important contests,
she can lose all three contests to maximize her luck at `10`.

If `k = 1`, she has to win at least of the important contests.
She would choose to win the lowest value important contest worth `1`.
Her final luck will be `5 + 4 - 1 = 8`.

## Function Description

Complete the luckBalance function in the editor below.

luckBalance has the following parameter(s):

- `int k`: the number of important contests Lena can lose
- `int contests[n][2]`: a 2D array of integers where each `contests[i]`
contains two integers that represent the luck balance and importance of the contest

## Returns

- `int`: the maximum luck balance achievable

## Input Format

The first line contains two space-separated integers `n` and `k`,
the number of preliminary contests and the maximum number
of important contests Lena can lose.

Each of the next lines contains two space-separated integers,
`L[i]` and `T[i]`, the contest's luck balance and its importance rating.

## Constraints

- $ 1 \leq n \leq 100 $
- $ 0 \leq k \leq N $
- $ 1 \leq L[i] \leq 10^4 $
- $ T[i] \isin \{0,1\} $

## Sample Input

```text
STDIN Function
----- --------
6 3 n = 6, k = 3
5 1 contests = [[5, 1], [2, 1], [1, 1], [8, 1], [10, 0], [5, 0]]
2 1
1 1
8 1
10 0
5 0
```

## Sample Output

```text
29
```

## Explanation

There are `n = 6` contests. Of these contests, `4` are important
and she cannot lose more than of them.
Lena maximizes her luck if she wins the $ 3^{rd} $ important contest
(where `L[i] = 1`) and loses all of the other five contests for a total
luck balance of `5 + 2 + 8 + 10 + 5 - 1 = 29`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/greedy_algorithms/luck-balance.md]] // noqa

package hackerrank

import (
"cmp"
"slices"
)

type Contest struct {
luck int32
important int32
}

func luckBalance(k int32, contests [][]int32) int32 {
var important_contests = []Contest{}
var nonimportant_contests = []Contest{}

for _, contest := range contests {
var contest = Contest{
luck: contest[0],
important: contest[1],
}

if contest.important == 1 {

important_contests = append(important_contests, contest)
} else {
nonimportant_contests = append(nonimportant_contests, contest)
}
}

slices.SortFunc(
important_contests,
func(a, b Contest) int {
return cmp.Or(
-cmp.Compare(a.important, b.important),
-cmp.Compare(a.luck, b.luck),
)
})

var total int32 = 0
var size = int32(len(important_contests))
var cut = min(k, int32(size))

for i := 0; int32(i) < cut; i++ {
total += important_contests[i].luck
}

for i := cut; i < size; i++ {
total -= important_contests[i].luck
}

for _, contest := range nonimportant_contests {
total += contest.luck
}

return total
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"title": "Sample Test case 0",
"k": 3,
"contests": [[5, 1], [2, 1], [1, 1], [8, 1], [10, 0], [5, 0]],
"expected": 29
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package hackerrank

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
"gon.cl/algorithms/utils"
)

type LuckBalanceTestCase struct {
Contests [][]int32 `json:"contests"`
K int32 `json:"k"`
Expected int32 `json:"expected"`
}

var LuckBalanceTestCases []LuckBalanceTestCase

// You can use testing.T, if you want to test the code without benchmarking
func LuckBalanceSetupSuite(t testing.TB) {
wd, _ := os.Getwd()
filepath := wd + "/luck_balance.testcases.json"
t.Log("Setup test cases from JSON: ", filepath)

var _, err = utils.LoadJSON(filepath, &LuckBalanceTestCases)
if err != nil {
t.Log(err)
}
}

func TestLuckBalance(t *testing.T) {

LuckBalanceSetupSuite(t)

for _, tt := range LuckBalanceTestCases {
testname := fmt.Sprintf("luckBalance(%v, %v) => %v \n", tt.K, tt.Contests, tt.Expected)
t.Run(testname, func(t *testing.T) {
ans := luckBalance(tt.K, tt.Contests)
assert.Equal(t, tt.Expected, ans)
})
}
}