Skip to content

Instantly share code, notes, and snippets.

View M15t's full-sized avatar

M15t

  • VIetnam
View GitHub Profile
@M15t
M15t / solution.java
Created April 4, 2024 14:37
Non-empty array
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)) {
@M15t
M15t / solution.java
Created April 4, 2024 14:35
Buy tickets
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