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;
}
solution.swift:5:6: error: consecutive statements on a line must be separated by ';' int index = 0; ^ ; solution.swift:6:6: error: consecutive statements on a line must be separated by ';' int daysCount = 0; ^ ; solution.swift:7:6: error: consecutive statements on a line must be separated by ';' int finalAmount = 0; ^ ; solution.swift:8:6: error: consecutive statements on a line must be separated by ';' int seventh = 0; ^ ; solution.swift:9:6: error: consecutive statements on a line must be separated by ';' int totalCount = 0; ^ ; solution.swift:10:10: error: consecutive statements on a line must be separated by ';' boolean isJLoopFinish = false; ^ ; solution.swift:15:3: error: C-style for statement has been removed in Swift 3 for (int i = 0; i < A.length; i++) { ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.swift:25:4: error: C-style for statement has been removed in Swift 3 for (int j = i+1; j < A.length ; j++) { ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.swift:5:3: error: use of unresolved identifier 'int' int index = 0; ^~~ solution.swift:5:13: error: cannot assign to value: 'index' is a function int index = 0; ~~~~~ ^ solution.swift:6:3: error: use of unresolved identifier 'int' int daysCount = 0; ^~~ solution.swift:6:7: error: use of unresolved identifier 'daysCount' int daysCount = 0; ^~~~~~~~~ solution.swift:7:3: error: use of unresolved identifier 'int' int finalAmount = 0; ^~~ solution.swift:7:7: error: use of unresolved identifier
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;
}
solution.swift:5:6: error: consecutive statements on a line must be separated by ';' int index = 0; ^ ; solution.swift:6:6: error: consecutive statements on a line must be separated by ';' int daysCount = 0; ^ ; solution.swift:7:6: error: consecutive statements on a line must be separated by ';' int finalAmount = 0; ^ ; solution.swift:8:6: error: consecutive statements on a line must be separated by ';' int seventh = 0; ^ ; solution.swift:9:6: error: consecutive statements on a line must be separated by ';' int totalCount = 0; ^ ; solution.swift:10:7: error: consecutive statements on a line must be separated by ';' bool isJLoopFinish = false; ^ ; solution.swift:15:3: error: C-style for statement has been removed in Swift 3 for (int i = 0; i < A.length; i++) { ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.swift:25:4: error: C-style for statement has been removed in Swift 3 for (int j = i+1; j < A.length ; j++) { ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.swift:5:3: error: use of unresolved identifier 'int' int index = 0; ^~~ solution.swift:5:13: error: cannot assign to value: 'index' is a function int index = 0; ~~~~~ ^ solution.swift:6:3: error: use of unresolved identifier 'int' int daysCount = 0; ^~~ solution.swift:6:7: error: use of unresolved identifier 'daysCount' int daysCount = 0; ^~~~~~~~~ solution.swift:7:3: error: use of unresolved identifier 'int' int finalAmount = 0; ^~~ solution.swift:7:7: error: use of unresolved identifier 'finalAmou
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;
}
solution.swift:10:7: error: consecutive statements on a line must be separated by ';' bool isJLoopFinish = false; ^ ; solution.swift:15:3: error: C-style for statement has been removed in Swift 3 for (int i = 0; i < A.length; i++) { ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.swift:25:4: error: C-style for statement has been removed in Swift 3 for (int j = i+1; j < A.length ; j++) { ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.swift:10:3: error: use of unresolved identifier 'bool'; did you mean 'Bool'? bool isJLoopFinish = false; ^~~~ Bool Swift.Bool:1:15: note: 'Bool' declared here public struct Bool { ^ solution.swift:10:8: error: use of unresolved identifier 'isJLoopFinish' bool isJLoopFinish = false; ^~~~~~~~~~~~~ solution.swift:12:13: error: value of type '[Int]' has no member 'length' if (A.length >= 25) { return 25; } ^ ~~~~~~ solution.swift:13:7: error: value of type '[Int]' has no member 'length' if (A.length <= 3) { return (A.length * 2); } ^ ~~~~~~ solution.swift:13:32: error: value of type '[Int]' has no member 'length' if (A.length <= 3) { return (A.length * 2); } ^ ~~~~~~
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;
}
solution.swift:15:3: error: C-style for statement has been removed in Swift 3 for (int i = 0; i < A.length; i++) { ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.swift:25:4: error: C-style for statement has been removed in Swift 3 for (int j = i+1; j < A.length ; j++) { ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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;
}
solution.swift:15:3: error: C-style for statement has been removed in Swift 3 for (let i = 0; i < A.count; i++) { ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.swift:25:4: error: C-style for statement has been removed in Swift 3 for (let j = i + 1; j < A.count; j++) { ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
}
solution.swift:25:4: error: C-style for statement has been removed in Swift 3 for (let j = i + 1; j < A.count; j++) { ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.swift:15:14: error: use of unresolved operator '..' for i in 0 .. A.count { ^~
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
}
solution.swift:25:4: error: C-style for statement has been removed in Swift 3 for (let j = i + 1; j < A.count; j++) { ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.swift:15:13: error: use of unresolved operator '..' for i in 0..A.count { ^~
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
}
solution.swift:15:7: error: 'let' pattern cannot appear nested in an already immutable context for let i in 0 .. A.count { ^ solution.swift:25:4: error: C-style for statement has been removed in Swift 3 for (let j = i + 1; j < A.count; j++) { ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.swift:15:18: error: use of unresolved operator '..' for let i in 0 .. A.count { ^~
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
}
solution.swift:25:4: error: C-style for statement has been removed in Swift 3 for (let j = i + 1; j < A.count; j++) { ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ solution.swift:17:5: error: unary operator '++' cannot be applied to an operand of type '@lvalue Int' seventh++ ^ solution.swift:17:5: note: overloads for '++' exist with these partially matching parameter lists: (inout Float), (inout Double), (inout Float80), (inout CGFloat) seventh++ ^
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
}
solution.swift:17:5: error: unary operator '++' cannot be applied to an operand of type '@lvalue Int' seventh++ ^ solution.swift:17:5: note: overloads for '++' exist with these partially matching parameter lists: (inout Float), (inout Double), (inout Float80), (inout CGFloat) seventh++ ^ solution.swift:34:9: error: cannot assign to value: 'i' is a 'let' constant i = j - 1 ~ ^
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
}
solution.swift:34:9: error: cannot assign to value: 'i' is a 'let' constant i = j - 1 ~ ^
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
}
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
}
solution.swift:9:5: warning: result of call to 'mincostTickets' is unused mincostTickets(A, A) ^ ~~~~~~ solution.swift:10:1: error: missing return in a function expected to return 'Int' } ^
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
}
solution.swift:10:1: error: missing return in a function expected to return 'Int' } ^
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
}
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
}
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
}
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 solution obtained perfect score.
M - the biggest element of of the array A