Home
Posts
Categories
Series
Tags
About
High Score Kit: Brute Force#1
Fatigue
postedOn: 2024-4-11   updatedOn: 2024-4-11   includedIn: HighScoreKit
wordsCount: 487   readingTime: 3 mins   viewers:

Problem

In the XX game, there is a fatigue system represented by non-negative integers, allowing players to explore dungeons by using a certain amount of fatigue. Each dungeon requires a “minimum required fatigue” to start exploring and consumes a certain amount of “fatigue” upon completion. For example, to explore a dungeon with a “minimum required fatigue” of 80 and a “consumption fatigue” of 20, a player’s current remaining fatigue must be at least 80, and it will decrease by 20 after exploring the dungeon. The game features multiple dungeons that can be explored once a day, and a player aims to maximize the number of dungeons explored in a day. Given the player’s current fatigue k and a 2D array dungeons that contains the “minimum required fatigue” and “consumption fatigue” for each dungeon, implement a function solution to return the maximum number of dungeons a player can explore.

Constraints

  • k is a natural number between 1 and 5,000.
  • The length of the dungeons array (number of dungeons) is between 1 and 8.
  • Each row in the dungeons array has 2 elements representing the [“minimum required fatigue”, “consumption fatigue”] for each dungeon.
  • “Minimum required fatigue” is always equal to or greater than “consumption fatigue”.
  • Both “minimum required fatigue” and “consumption fatigue” range from 1 to 1,000.
  • Different dungeons can have the same [“minimum required fatigue”, “consumption fatigue”] values.

Example

k dungeons result
80 [[80,20],[50,40],[30,10]] 3

Explanation of Examples

In the example, the player starts with a fatigue of 80. If the player explores the dungeons in the sequence of the first → third → second, here’s how it goes:

  • Initial fatigue is 80, matching the first dungeon’s “minimum required fatigue” of 80. After exploring, the remaining fatigue is 60.
  • The remaining fatigue is 60, which is enough for the third dungeon’s “minimum required fatigue” of 30. After exploring, the remaining fatigue is 50.
  • The remaining fatigue is 50, matching the second dungeon’s “minimum required fatigue” of 50. After exploring, the remaining fatigue is 10. Thus, in this scenario, the player can explore all three dungeons, making the maximum number of dungeons explored 3.

How to Approach?

Step1: Initialize variables in loop

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.*;

class Solution {
    private int answer = 0;

    public int solution(int k, int[][] dungeons) {
        boolean[] visited = new boolean[dungeons.length];
        exploreDungeons(k, dungeons, visited, 0);
        return answer;
    }

    private void exploreDungeons(int fatigue, int[][] dungeons, boolean[] visited, int count){
        for(int i=0; i<dungeons.length; i++){
            if (!visited[i] && dungeons[i][0] <= fatigue) {
                // Mark the dungeon as visited
                visited[i] = true;
                // Recursively try the next dungeons with the updated fatigue and count
                exploreDungeons(fatigue - dungeons[i][1], dungeons, visited, count + 1);
                // Backtrack: unmark the dungeon as visited for the next iterations
                visited[i] = false;
        }
        answer = Math.max(answer, count);
        }
    }   
}
Aaron Oh
고려대코딩개발협동조합 창단 멤버
Table of Contents
Related Posts
High Score Kit: Brute Force#2
Problem In a power grid network composed of n transmission towers connected in a tree structure via wires, you are tasked with dividing the network into two parts by cutting one of the wires. The goal is to make the number of transmission towers in each part as equal as possible.
2024-4-11
High Score Kit: Greedy#1
Problem Given a number, your task is to remove k digits from the number in such a way that the remaining digits form the largest possible number. The number is provided as a string number and the integer k represents the number of digits to remove. Your function solution should return the resulting largest number as a string.
2024-4-11
High Score Kit: DFS&BFS#2
Problem In the ROR game, you play on a team that competes to destroy the opposing team’s base first, making it advantageous to reach the enemy base as quickly as possible. Suppose you’re a team member and are located at position (row: 1, column: 1) on a 5x5 map, while the enemy base is at (row: 5, column: 5).
2024-4-11
High Score Kit: DFS&BFS#1
Problem You are given an array of non-negative integers and a target number. Your task is to find how many ways you can add and subtract the given numbers to achieve the target number. The order of numbers in the array must be preserved. For example, with the array [1, 1, 1, 1, 1], there are five ways to reach the target number 3:
2024-4-11
High Score Kit: Stack&Queue#2
Problem The Programmers team is currently performing feature improvement tasks. Each feature can only be released when its progress reaches 100%. However, since the development speed of each feature varies, it is possible for features developed later to be completed earlier. In such cases, a feature that is developed later will be released together with the feature in front of it that is scheduled for earlier release.
2024-4-10
High Score Kit: Stack&Queue#1
Problem Given an array arr consisting of integers ranging from 0 to 9, the task is to remove consecutive duplicates while preserving the order of the remaining elements. For instance, if arr = [1, 1, 3, 3, 0, 1, 1], the result should be [1, 3, 0, 1]. Similarly, for arr = [4, 4, 4, 3, 3], the result should be [4, 3].
2024-4-10