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.
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ A : inout [Int]) -> Int {
int index = 0;
int daysCount = 0;
int finalAmount = 0;
int seventh = 0;
int totalCount = 0;
boolean isJLoopFinish = false;
if (A.length >= 25) { return 25; }
if (A.length <= 3) { return (A.length * 2); }
for (int i = 0; i < A.length; i++) {
if (isJLoopFinish == true) {
seventh++;
break;
}
daysCount = 0;
index = A[i] + 7;
totalCount++;
for (int j = i+1; j < A.length ; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.length-1))) { isJLoopFinish = true; }
if (A[j] < index) {
daysCount++;
} else {
if (daysCount > 3) {
seventh++;
daysCount = 0;
i = j-1;
} else { daysCount = 0; }
break;
}
}
}
totalCount = totalCount - seventh;
finalAmount = (totalCount * 2) + (seventh * 7);
if (finalAmount > 25) { finalAmount = 25; }
return finalAmount;
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
int index = 0;
int daysCount = 0;
int finalAmount = 0;
int seventh = 0;
int totalCount = 0;
boolean isJLoopFinish = false;
if (A.length >= 25) { return 25; }
if (A.length <= 3) { return (A.length * 2); }
for (int i = 0; i < A.length; i++) {
if (isJLoopFinish == true) {
seventh++;
break;
}
daysCount = 0;
index = A[i] + 7;
totalCount++;
for (int j = i+1; j < A.length ; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.length-1))) { isJLoopFinish = true; }
if (A[j] < index) {
daysCount++;
} else {
if (daysCount > 3) {
seventh++;
daysCount = 0;
i = j-1;
} else { daysCount = 0; }
break;
}
}
}
totalCount = totalCount - seventh;
finalAmount = (totalCount * 2) + (seventh * 7);
if (finalAmount > 25) { finalAmount = 25; }
return finalAmount;
}
The submission is being evaluated.
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
int index = 0;
int daysCount = 0;
int finalAmount = 0;
int seventh = 0;
int totalCount = 0;
bool isJLoopFinish = false;
if (A.length >= 25) { return 25; }
if (A.length <= 3) { return (A.length * 2); }
for (int i = 0; i < A.length; i++) {
if (isJLoopFinish == true) {
seventh++;
break;
}
daysCount = 0;
index = A[i] + 7;
totalCount++;
for (int j = i+1; j < A.length ; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.length-1))) { isJLoopFinish = true; }
if (A[j] < index) {
daysCount++;
} else {
if (daysCount > 3) {
seventh++;
daysCount = 0;
i = j-1;
} else { daysCount = 0; }
break;
}
}
}
totalCount = totalCount - seventh;
finalAmount = (totalCount * 2) + (seventh * 7);
if (finalAmount > 25) { finalAmount = 25; }
return finalAmount;
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
int index = 0;
int daysCount = 0;
int finalAmount = 0;
int seventh = 0;
int totalCount = 0;
bool isJLoopFinish = false;
if (A.length >= 25) { return 25; }
if (A.length <= 3) { return (A.length * 2); }
for (int i = 0; i < A.length; i++) {
if (isJLoopFinish == true) {
seventh++;
break;
}
daysCount = 0;
index = A[i] + 7;
totalCount++;
for (int j = i+1; j < A.length ; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.length-1))) { isJLoopFinish = true; }
if (A[j] < index) {
daysCount++;
} else {
if (daysCount > 3) {
seventh++;
daysCount = 0;
i = j-1;
} else { daysCount = 0; }
break;
}
}
}
totalCount = totalCount - seventh;
finalAmount = (totalCount * 2) + (seventh * 7);
if (finalAmount > 25) { finalAmount = 25; }
return finalAmount;
}
The submission is being evaluated.
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0;
var daysCount = 0;
var finalAmount = 0;
var seventh = 0;
var totalCount = 0;
bool isJLoopFinish = false;
if (A.length >= 25) { return 25; }
if (A.length <= 3) { return (A.length * 2); }
for (int i = 0; i < A.length; i++) {
if (isJLoopFinish == true) {
seventh++;
break;
}
daysCount = 0;
index = A[i] + 7;
totalCount++;
for (int j = i+1; j < A.length ; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.length-1))) { isJLoopFinish = true; }
if (A[j] < index) {
daysCount++;
} else {
if (daysCount > 3) {
seventh++;
daysCount = 0;
i = j-1;
} else { daysCount = 0; }
break;
}
}
}
totalCount = totalCount - seventh;
finalAmount = (totalCount * 2) + (seventh * 7);
if (finalAmount > 25) { finalAmount = 25; }
return finalAmount;
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0;
var daysCount = 0;
var finalAmount = 0;
var seventh = 0;
var totalCount = 0;
bool isJLoopFinish = false;
if (A.length >= 25) { return 25; }
if (A.length <= 3) { return (A.length * 2); }
for (int i = 0; i < A.length; i++) {
if (isJLoopFinish == true) {
seventh++;
break;
}
daysCount = 0;
index = A[i] + 7;
totalCount++;
for (int j = i+1; j < A.length ; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.length-1))) { isJLoopFinish = true; }
if (A[j] < index) {
daysCount++;
} else {
if (daysCount > 3) {
seventh++;
daysCount = 0;
i = j-1;
} else { daysCount = 0; }
break;
}
}
}
totalCount = totalCount - seventh;
finalAmount = (totalCount * 2) + (seventh * 7);
if (finalAmount > 25) { finalAmount = 25; }
return finalAmount;
}
The submission is being evaluated.
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.length >= 25) { return 25; }
if (A.length <= 3) { return (A.length * 2); }
for (int i = 0; i < A.length; i++) {
if (isJLoopFinish == true) {
seventh++;
break;
}
daysCount = 0;
index = A[i] + 7;
totalCount++;
for (int j = i+1; j < A.length ; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.length-1))) { isJLoopFinish = true; }
if (A[j] < index) {
daysCount++;
} else {
if (daysCount > 3) {
seventh++;
daysCount = 0;
i = j-1;
} else { daysCount = 0; }
break;
}
}
}
totalCount = totalCount - seventh;
finalAmount = (totalCount * 2) + (seventh * 7);
if (finalAmount > 25) { finalAmount = 25; }
return finalAmount;
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.length <= 3) { return (A.count * 2) }
for (int i = 0; i < A.length; i++) {
if (isJLoopFinish == true) {
seventh++;
break;
}
daysCount = 0;
index = A[i] + 7;
totalCount++;
for (int j = i+1; j < A.length ; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.length-1))) { isJLoopFinish = true; }
if (A[j] < index) {
daysCount++;
} else {
if (daysCount > 3) {
seventh++;
daysCount = 0;
i = j-1;
} else { daysCount = 0; }
break;
}
}
}
totalCount = totalCount - seventh;
finalAmount = (totalCount * 2) + (seventh * 7);
if (finalAmount > 25) { finalAmount = 25; }
return finalAmount;
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for (int i = 0; i < A.length; i++) {
if (isJLoopFinish == true) {
seventh++;
break;
}
daysCount = 0;
index = A[i] + 7;
totalCount++;
for (int j = i+1; j < A.length ; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.length-1))) { isJLoopFinish = true; }
if (A[j] < index) {
daysCount++;
} else {
if (daysCount > 3) {
seventh++;
daysCount = 0;
i = j-1;
} else { daysCount = 0; }
break;
}
}
}
totalCount = totalCount - seventh;
finalAmount = (totalCount * 2) + (seventh * 7);
if (finalAmount > 25) { finalAmount = 25; }
return finalAmount;
}
The submission is being evaluated.
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for (let i = 0; i < A.count; i++) {
if (isJLoopFinish == true) {
seventh++;
break;
}
daysCount = 0;
index = A[i] + 7;
totalCount++;
for (int j = i+1; j < A.length ; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.length-1))) { isJLoopFinish = true; }
if (A[j] < index) {
daysCount++;
} else {
if (daysCount > 3) {
seventh++;
daysCount = 0;
i = j-1;
} else { daysCount = 0; }
break;
}
}
}
totalCount = totalCount - seventh;
finalAmount = (totalCount * 2) + (seventh * 7);
if (finalAmount > 25) { finalAmount = 25; }
return finalAmount;
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for (let i = 0; i < A.count; i++) {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for (let j = i+1; j < A.length ; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.length-1))) { isJLoopFinish = true; }
if (A[j] < index) {
daysCount++;
} else {
if (daysCount > 3) {
seventh++;
daysCount = 0;
i = j-1;
} else { daysCount = 0; }
break;
}
}
}
totalCount = totalCount - seventh;
finalAmount = (totalCount * 2) + (seventh * 7);
if (finalAmount > 25) { finalAmount = 25; }
return finalAmount;
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for (let i = 0; i < A.count; i++) {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for (let j = i + 1; j < A.count; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.length-1))) { isJLoopFinish = true; }
if (A[j] < index) {
daysCount++;
} else {
if (daysCount > 3) {
seventh++;
daysCount = 0;
i = j-1;
} else { daysCount = 0; }
break;
}
}
}
totalCount = totalCount - seventh;
finalAmount = (totalCount * 2) + (seventh * 7);
if (finalAmount > 25) { finalAmount = 25; }
return finalAmount;
}
The submission is being evaluated.
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for (let i = 0; i < A.count; i++) {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for (let j = i + 1; j < A.count; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.length-1))) { isJLoopFinish = true; }
if (A[j] < index) {
daysCount++;
} else {
if (daysCount > 3) {
seventh++
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh;
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount;
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for (let i = 0; i < A.count; i++) {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for (let j = i + 1; j < A.count; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.length-1))) { isJLoopFinish = true; }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for (let i = 0; i < A.count; i++) {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for (let j = i + 1; j < A.count; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for let i in 0.. (let i = 0; i < A.count; i++) {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for (let j = i + 1; j < A.count; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for let i in 0 .. A.count {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for (let j = i + 1; j < A.count; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for i in 0 .. A.count {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for (let j = i + 1; j < A.count; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
The submission is being evaluated.
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for i in 0..A.count {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for (let j = i + 1; j < A.count; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
The submission is being evaluated.
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for let i in 0 .. A.count {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for (let j = i + 1; j < A.count; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
The submission is being evaluated.
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for i in 0 .. A.count {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for (let j = i + 1; j < A.count; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for i in 0 ... A.count {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for (let j = i + 1; j < A.count; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for i in 0 ... A.count {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for (let j = i + 1; j < A.count; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
The submission is being evaluated.
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for i in 0 ... A.count {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for j in i + 1 ... A.count - 1(let j = i + 1; j < A.count; j++) {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for i in 0 ... A.count - 1 {
if (isJLoopFinish == true) {
seventh++
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for j in i + 1 ... A.count - 1 {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
The submission is being evaluated.
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for i in 0 ... A.count - 1 {
if (isJLoopFinish == true) {
seventh += 1
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for j in i + 1 ... A.count - 1 {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for i in 0 ... A.count - 1 {
if (isJLoopFinish == true) {
seventh += 1
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for j in i + 1 ... A.count - 1 {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
The submission is being evaluated.
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
for i in 0 ... A.count - 1 {
if (isJLoopFinish == true) {
seventh += 1
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for j in i + 1 ... A.count - 1 {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
var i = 0
while i < A.count {
if (isJLoopFinish == true) {
seventh += 1
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for j in i + 1 ... A.count - 1 {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
i += 1
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
var i = 0
while i < A.count {
if (isJLoopFinish == true) {
seventh += 1
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for j in i + 1 ... A.count - 1 {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
i += 1
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
The submission is being evaluated.
import Foundation
import Glibc
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
var i = 0
while i < A.count {
if (isJLoopFinish == true) {
seventh += 1
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for j in i + 1 ... A.count - 1 {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
i += 1
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
var index = 0
var daysCount = 0
var finalAmount = 0
var seventh = 0
var totalCount = 0
var isJLoopFinish = false
if (A.count >= 25) { return 25 }
if (A.count <= 3) { return (A.count * 2) }
var i = 0
while i < A.count {
if (isJLoopFinish == true) {
seventh += 1
break
}
daysCount = 0
index = A[i] + 7
totalCount += 1
for j in i + 1 ... A.count - 1 {
if ((daysCount >= 3) && (A[j] < index) && (j == (A.count - 1))) { isJLoopFinish = true }
if (A[j] < index) {
daysCount += 1
} else {
if (daysCount > 3) {
seventh += 1
daysCount = 0
i = j - 1
} else { daysCount = 0 }
break;
}
}
i += 1
}
totalCount = totalCount - seventh
finalAmount = (totalCount * 2) + (seventh * 7)
if (finalAmount > 25) { finalAmount = 25 }
return finalAmount
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
mincostTickets()
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
mincostTickets(A, A)
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
The submission is being evaluated.
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
_ = mincostTickets(A, A)
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
_ = mincostTickets(A, A)
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
The submission is being evaluated.
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
return mincostTickets(A, A)
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
The submission is being evaluated.
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
return mincostTickets(A, )
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
return mincostTickets(A, [2, 7])
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
return mincostTickets(A, [2, 7, 25])
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
The submission is being evaluated.
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
return mincostTickets(A, [2, 7, 25])
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
return mincostTickets(A, [2, 7, 25])
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
return mincostTickets(A, [2, 7, 25])
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
The submission is being evaluated.
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
return mincostTickets(A, [2, 7, 25])
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
The submission is being evaluated.
import Foundation
import Glibc
// - Complexity:
// - time: O(n), where n is the number of the days in the travel plan.
// - space: O(n), where n is the number of the days in the travel plan.
public func solution(_ A : inout [Int]) -> Int {
return mincostTickets(A, [2, 7, 25])
}
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
var memo = [Int](repeating: -1, count: days.count)
let durations = [1, 7, 30]
return dp(0, days, costs, &memo, durations)
}
private func dp(_ i: Int, _ days: [Int], _ costs: [Int], _ memo: inout [Int], _ durations: [Int]) -> Int {
guard i < days.count else { return 0 }
guard memo[i] == -1 else { return memo[i] }
var ans = Int.max
var j = i
for k in 0..<3 {
while j < days.count, days[j] < days[i] + durations[k] { j += 1 }
ans = min(ans, dp(j, days, costs, &memo, durations) + costs[k])
}
memo[i] = ans
return ans
}
The submission is being evaluated.