This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashSet; | |
class Solution { | |
public int solution(int[] A) { | |
int currentPosition = 0; | |
int jumps = 0; | |
HashSet<Integer> visited = new HashSet<>(); | |
while (currentPosition >= 0 && currentPosition < A.length) { | |
if (visited.contains(currentPosition)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int solution(int[] A) { | |
int[] dp = new int[31]; // dp[i] stores the minimum cost to travel up to day i | |
int n = A.length; | |
int travelIndex = 0; // pointer to the current travel date | |
for (int i = 1; i <= 30; i++) { | |
if (travelIndex < n && i == A[travelIndex]) { // if i is a travel date | |
int oneDayCost = dp[i - 1] + 2; // cost if using a 1-day ticket on day i | |
int sevenDayCost = i > 7 ? dp[i - 7] + 7 : 7; // cost if using a 7-day ticket on day i |