You want to buy public transport tickets for some upcoming days. You know exactly the days on which you will be traveling. There are three types of ticket:
- 1-day ticket, costs 2, valid for one day;
- 7-day ticket, costs 7, valid for seven consecutive days (e.g. if the first valid day is X, then the last valid day is X+6);
- 30-day ticket, costs 25, valid for thirty consecutive days.
You want to pay as little as possible having valid tickets on your travelling days.
You are given a sorted (in increasing order) array A of days when you will be traveling. For example, given:
A[0] = 1 A[1] = 2 A[2] = 4 A[3] = 5 A[4] = 7 A[5] = 29 A[6] = 30you can buy one 7-day ticket and two 1-day tickets. The two 1-day tickets should be used on days 29 and 30. The 7-day ticket should be used on the first seven days. The total cost is 11 and there is no possible way of paying less.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A consisting of N integers that specifies days on which you will be traveling, returns the minimum amount of money that you have to spend on tickets.
For example, given the above data, the function should return 11, as explained above.
Write an efficient algorithm for the following assumptions:
- M is an integer within the range [1..100,000];
- each element of array A is an integer within the range [1..M];
- N is an integer within the range [1..M];
- array A is sorted in increasing order;
- the elements of A are all distinct.
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
for(int i = 0; i)
return 3;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
for(int i = 0; i)
return 3;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
for(int i = 0; i < )
return 3;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
for(int i = 0; i < A.Length; i++)
return 3;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0; // technically redundant
for (int d = 1; d <= 30; ++d) {
if (! isDayWithTrip[d]) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
// Possibility #1: one-day pass on day d:
minCost = minCostUpThroughDay[d-1] + 2;
// Possibility #2: seven-day pass ending on or after day d:
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
// Possibility #3: 30-day pass for the whole period:
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
for(int i )
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= 30; ++d) {
if (!isDayWithTrip[d]) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
for(int i )
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= 30; ++d) {
if (!isDayWithTrip[d]) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
for(int i )
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= 30; ++d) {
if (!isDayWithTrip[d]) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
Dictionary<int, int>
for(int i )
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= 30; ++d) {
if (!isDayWithTrip[d]) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
Dictionary<int, int> tripDays = new Dictionary<int, int>();
for(int i=0; i < A.Length; i++)
{
}
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= 30; ++d) {
if (!isDayWithTrip[d]) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
Dictionary<int, int> tripDays = new Dictionary<int, int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add()
}
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= 30; ++d) {
if (!isDayWithTrip[d]) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new Dictionary<int, int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add()
}
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= 30; ++d) {
if (!isDayWithTrip[d]) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add()
}
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= 30; ++d) {
if (!isDayWithTrip[d]) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
{
HashSet.Add()
}
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= 30; ++d) {
if (!isDayWithTrip[d]) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add(A[i]);
}
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= 30; ++d) {
if (!isDayWithTrip[d]) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add(A[i]);
}
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= 30; ++d) {
if (!isDayWithTrip[d]) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add(A[i]);
}
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
int daysOfTravel = A[A.Length-1];
for (int d = 1; d <= 30; ++d) {
if (!isDayWithTrip[d]) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add(A[i]);
}
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
int daysOfTravel = A[A.Length-1];
for (int d = 1; d <= daysOfTravel; ++d) {
if (!isDayWithTrip[d]) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add(A[i]);
}
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
int daysOfTravel = A[A.Length-1];
for (int d = 1; d <= daysOfTravel; ++d) {
if (!tripDays.TryGetValue(d, out _)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add(A[i]);
}
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
int daysOfTravel = A[A.Length-1];
for (int d = 1; d <= daysOfTravel; ++d) {
if (!tripDays.TryGetValue(d, out _)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add(A[i]);
}
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
int daysOfTravel = A[A.Length-1];
for (int d = 1; d <= daysOfTravel; ++d) {
if (!tripDays.Contains(d, out _)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add(A[i]);
}
int[] minCostUpThroughDay = new int[31];
minCostUpThroughDay[0] = 0;
int daysOfTravel = A[A.Length-1];
for (int d = 1; d <= daysOfTravel; ++d) {
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add(A[i]);
}
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d) {
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost =
Math.min(minCost, minCostUpThroughDay[Math.max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add(A[i]);
}
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d) {
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add(A[i]);
}
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d) {
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
{
tripDays.Add(A[i]);
}
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel-1];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel-1];
}
}
Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range. at Solution.solution (System.Int32[] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.IndexOutOfRangeException: Array index is out of range. at Solution.solution (System.Int32[] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel-1];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
Co
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel-1];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
Console.WriteLine(minCost);
}
return minCostUpThroughDay[daysOfTravel-1];
}
}
Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range. at Solution.solution (System.Int32[] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.IndexOutOfRangeException: Array index is out of range. at Solution.solution (System.Int32[] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0stdout:
2 4 6 7 7 9
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
C
int[] minCostUpThroughDay = new int[daysOfTravel];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
Console.WriteLine(minCost);
}
return minCostUpThroughDay[daysOfTravel-1];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
Console.WriteLine
int[] minCostUpThroughDay = new int[daysOfTravel];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
Console.WriteLine(minCost);
}
return minCostUpThroughDay[daysOfTravel-1];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
Console.WriteLine(minCost);
}
return minCostUpThroughDay[daysOfTravel-1];
}
}
2 4 6 7 7 9 11
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
Console.WriteLine(minCost);
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it be best to buy a ticket
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it havebe best to buy a ticket
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
[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, 26, 27, 28, 29, 30]
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
[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, 26, 27, 28, 29, 30]
[1, 2, 3, 8, 9, 15, 30]
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
function result: 25
function result: 14
function result: 15
[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, 26, 27, 28, 29, 30]
[1, 2, 3, 8, 9, 15, 30]
[1, 2, 3, 4, 8, 9, 15, 30]
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)]);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-25)] + 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-25)] + 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
function result: 32
function result: 14
function result: 15
[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, 26, 27, 28, 29, 30]
[1, 2, 3, 8, 9, 15, 30]
[1, 2, 3, 4, 8, 9, 15, 30]
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-30)] + 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
function result: 25
function result: 14
function result: 15
[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, 26, 27, 28, 29, 30]
[1, 2, 3, 8, 9, 15, 30]
[1, 2, 3, 4, 8, 9, 15, 30]
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-30)] + 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-30)] + 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
function result: 32
function result: 14
function result: 15
[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, 26, 27, 28, 29, 30, 31, 32, 33, 34]
[1, 2, 3, 8, 9, 15, 30]
[1, 2, 3, 4, 8, 9, 15, 30]
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-30)] + 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
function result: 32
function result: 27
function result: 14
function result: 15
[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, 26, 27, 28, 29, 30, 31, 32, 33, 34]
[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, 26, 27, 28, 29, 30, 31]
[1, 2, 3, 8, 9, 15, 30]
[1, 2, 3, 4, 8, 9, 15, 30]
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-30)] + 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-30)] + 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
[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, 26, 27, 28, 29, 30, 31, 32, 33, 34]
[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, 26, 27, 28, 29, 30, 31]
[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, 26, 27, 28, 29, 30]
[1, 2, 3, 8, 9, 15, 30]
[1, 2, 3, 4, 8, 9, 15, 30]
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one (30 days ago) since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-30)] + 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one (30 days ago) since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-30)] + 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
[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, 26, 27, 28, 29, 30, 31, 32, 33, 34]
[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, 26, 27, 28, 29, 30, 31]
[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, 26, 27, 28, 29, 30]
[1, 2, 3, 8, 9, 15, 30]
[1, 2, 3, 4, 8, 9, 15, 30]
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one (30 days ago) since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-30)] + 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
[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, 26, 27, 28, 29, 30, 31, 32, 33, 34]
[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, 26, 27, 28, 29, 30, 31]
[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, 26, 27, 28, 29, 30]
[1, 2, 3, 8, 9, 15, 30]
[1, 2, 3, 4, 8, 9, 15, 30]
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
HashSet<int> tripDays = new HashSet<int>();
for(int i=0; i < A.Length; i++)
tripDays.Add(A[i]);
int daysOfTravel = A[A.Length-1];
int[] minCostUpThroughDay = new int[daysOfTravel+1];
minCostUpThroughDay[0] = 0;
for (int d = 1; d <= daysOfTravel; ++d)
{
if (!tripDays.Contains(d)) {
minCostUpThroughDay[d] = minCostUpThroughDay[d-1];
continue;
}
int minCost;
//just buying a regular ticket
minCost = minCostUpThroughDay[d-1] + 2;
//would it have been better to buy a ticket for 7 days...7 days ago?
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-7)] + 7);
//just buy the 25 one (30 days ago) since we actually got a higher cost by buying 1-day and 7-day tickets
minCost = Math.Min(minCost, minCostUpThroughDay[Math.Max(0, d-30)] + 25);
minCostUpThroughDay[d] = minCost;
}
return minCostUpThroughDay[daysOfTravel];
}
}
The solution obtained perfect score.
M - the biggest element of of the array A