88
99import Foundation
1010
11- class CoinChangeProblem {
11+ public class CoinChangeProblem {
12+ public init ( ) { }
13+
1214 func solution( ) {
1315 // read in N, M
1416 let metrics = getLineToArray ( ) . map { Int ( $0) !}
@@ -26,7 +28,7 @@ class CoinChangeProblem {
2628 // 1. dont use the mth coin
2729 // 2. use at least 1 mth coin
2830 // By constructing a table we could avoid overlapping subproblems
29- func solveChanges( coins: [ Int ] , coinsCounts: Int , total: Int ) {
31+ public func solveChanges( coins: [ Int ] , coinsCounts: Int , total: Int ) {
3032 // 1. construct matrix to save result
3133 // - row represent total count from 0 to total
3234 // - col represent mth coin to use
@@ -49,7 +51,10 @@ class CoinChangeProblem {
4951 }
5052}
5153
52- class MaximumSubArray {
54+ public class MaximumSubArray {
55+
56+ public init ( ) { }
57+
5358 func solution( ) {
5459 let T = Int ( getLine ( ) ) !
5560 for _ in 0 ..< T {
@@ -59,7 +64,7 @@ class MaximumSubArray {
5964 }
6065 }
6166
62- func solve( inputs: [ Int ] ) {
67+ public func solve( inputs: [ Int ] ) {
6368 let maxElement = inputs. maxElement ( )
6469 if let maxE = maxElement where maxE < 0 {
6570 print ( " \( maxE) \( maxE) " )
@@ -101,7 +106,10 @@ func ==(lhs: KnapSackPair, rhs: KnapSackPair) -> Bool {
101106 return lhs. itemIndex == rhs. itemIndex && lhs. capacity == rhs. capacity
102107}
103108
104- class Knapsack {
109+ public class Knapsack {
110+
111+ public init ( ) { }
112+
105113 func solution( ) {
106114 let T = getInt ( )
107115 for _ in 0 ..< T {
@@ -114,7 +122,7 @@ class Knapsack {
114122 /**
115123 * This solution used memoization approach, which is a fantistic feature from Swift
116124 */
117- func solveWithMemo( N: Int , K: Int , array: [ Int ] ) {
125+ public func solveWithMemo( N: Int , K: Int , array: [ Int ] ) {
118126 if K == 0 {
119127 print ( " 0 " )
120128 return
@@ -143,7 +151,10 @@ class Knapsack {
143151 }
144152}
145153
146- class LongestIncreasingSubsequence {
154+ public class LongestIncreasingSubsequence {
155+
156+ public init ( ) { }
157+
147158 func solution( ) {
148159 let N = getInt ( )
149160 let array = getLinesToArray ( N) . map { Int ( $0) ! }
@@ -155,7 +166,7 @@ class LongestIncreasingSubsequence {
155166 * LIS[i] = max(LIS)
156167 * time efficiency O(n^2)
157168 */
158- func solve( array: [ Int ] ) {
169+ public func solve( array: [ Int ] ) {
159170 let N = array. count
160171 var LIS = Array ( count: N, repeatedValue: 0 )
161172 LIS [ 0 ] = 1
@@ -203,7 +214,9 @@ class LongestIncreasingSubsequence {
203214 }
204215}
205216
206- class SherlockAndCost {
217+ public class SherlockAndCost {
218+
219+ public init ( ) { }
207220
208221 func solution( ) {
209222 let T = getInt ( )
@@ -241,9 +254,12 @@ class SherlockAndCost {
241254 }
242255}
243256
244- class HexagonalGrid {
257+ public class HexagonalGrid {
258+
245259 static let nextPos = [ ( 1 , 0 ) , ( 1 , - 1 ) , ( 0 , 1 ) ]
246260
261+ public init ( ) { }
262+
247263 func solution( ) {
248264 let T = getInt ( )
249265 for _ in 0 ..< T {
@@ -263,7 +279,7 @@ class HexagonalGrid {
263279 }
264280 }
265281
266- func solve( grid: [ [ Int ] ] ) {
282+ public func solve( grid: [ [ Int ] ] ) {
267283 var currentGrid = grid
268284 if rec ( & currentGrid) {
269285 print ( " YES " )
@@ -305,15 +321,18 @@ class HexagonalGrid {
305321 }
306322}
307323
308- class SamAndSubString {
324+ public class SamAndSubString {
325+
326+ public init ( ) { }
327+
309328 static let MOD = Int ( 1e9 + 7 )
310329
311330 func solution( ) {
312331 let input = getLine ( )
313332 solve ( input)
314333 }
315334
316- func solve( input: String ) -> Int {
335+ public func solve( input: String ) -> Int {
317336 let N = input. characters. count
318337 var preSum = Array ( count: N, repeatedValue: 0 )
319338 preSum [ 0 ] = Int ( String ( input. characters. first!) ) !
@@ -336,7 +355,10 @@ class SamAndSubString {
336355 }
337356}
338357
339- class TravelAroundTheWorld {
358+ public class TravelAroundTheWorld {
359+
360+ public init ( ) { }
361+
340362 func solution( ) {
341363 let metrics = getLineToArray ( ) . map { Int ( $0) ! }
342364 let a = getLineToArray ( ) . map ( ) { Int ( $0) ! }
@@ -357,7 +379,7 @@ class TravelAroundTheWorld {
357379 * all of the city with need[i] == 0 is suitable start point
358380 *
359381 */
360- func solve( c: Int , a: [ Int ] , b: [ Int ] ) -> Int {
382+ public func solve( c: Int , a: [ Int ] , b: [ Int ] ) -> Int {
361383 let N = a. count
362384 var earthA = Array ( count: 2 * N, repeatedValue: 0 )
363385 var earthB = Array ( count: 2 * N, repeatedValue: 0 )
@@ -412,7 +434,10 @@ class TravelAroundTheWorld {
412434 }
413435}
414436
415- class RedJohnisBack {
437+ public class RedJohnisBack {
438+
439+ public init ( ) { }
440+
416441 func solution( ) {
417442 let T = getInt ( )
418443 for _ in 0 ..< T {
@@ -422,7 +447,7 @@ class RedJohnisBack {
422447 }
423448
424449 // Use a way similar to fibonacci sequence
425- func solve( n: Int ) -> Int {
450+ public func solve( n: Int ) -> Int {
426451 let fibOneAndFour : ( Int ) -> Int = memoize { fibOneAndFour, n in
427452 if n>= 1 && n<= 3 {
428453 return 1
@@ -444,30 +469,33 @@ class RedJohnisBack {
444469 * Struct Passenger to store information like offer value and accept weight of gold
445470 * Using struct here is for sorting
446471 */
447- struct Passenger {
472+ public struct Passenger {
448473 let offer : Int
449474 let weight : Int
450475}
451476
452477extension Passenger : Hashable {
453- var hashValue : Int {
478+ public var hashValue : Int {
454479 get {
455480 return offer. hashValue + weight. hashValue
456481 }
457482 }
458483}
459484
460485extension Passenger : CustomStringConvertible {
461- var description : String { return " Passenger offer \( offer) for \( weight) grams " }
486+ public var description : String { return " Passenger offer \( offer) for \( weight) grams " }
462487}
463488
464- func == ( lhs: Passenger , rhs: Passenger ) -> Bool {
489+ public func == ( lhs: Passenger , rhs: Passenger ) -> Bool {
465490 return lhs. offer == rhs. offer && lhs. weight == rhs. weight
466491}
467492
468493let LOWESTNUMBER = - 1 * NSIntegerMax
469494
470- class DorseyThief {
495+ public class DorseyThief {
496+
497+ public init ( ) { }
498+
471499 func solution( ) {
472500 let metrics = getLineToArray ( ) . map { Int ( $0) ! }
473501 var array : [ Passenger ] = [ ]
@@ -479,7 +507,7 @@ class DorseyThief {
479507 solve ( array, c: metrics [ 1 ] )
480508 }
481509
482- func solve( array: [ Passenger ] , c: Int ) -> Int {
510+ public func solve( array: [ Passenger ] , c: Int ) -> Int {
483511 // sort the input array
484512 let sortedArray = array. sort { p1, p2 in
485513 if p1. offer == p2. offer {
0 commit comments