|
1 |
| -public func eggDrop(numberOfEggs: Int, numberOfFloors: Int) -> Int { |
2 |
| - if numberOfEggs == 0 || numberOfFloors == 0{ //edge case: When either number of eggs or number of floors is 0, answer is 0 |
3 |
| - return 0 |
4 |
| - } |
5 |
| - if numberOfEggs == 1 || numberOfFloors == 1{ //edge case: When either number of eggs or number of floors is 1, answer is 1 |
6 |
| - return 1 |
7 |
| - } |
8 |
| - |
9 |
| - var eggFloor = [[Int]](repeating: [Int](repeating: 0, count: numberOfFloors+1), count: numberOfEggs+1) //egg(rows) floor(cols) array to store the solutions |
10 |
| - var attempts: Int = 0 |
11 |
| - |
12 |
| - for var floorNumber in (0..<(numberOfFloors+1)){ |
13 |
| - eggFloor[1][floorNumber] = floorNumber //base case: if there's only one egg, it takes 'numberOfFloors' attempts |
14 |
| - } |
15 |
| - eggFloor[2][1] = 1 //base case: if there are two eggs and one floor, it takes one attempt |
16 |
| - |
17 |
| - for var eggNumber in (2..<(numberOfEggs+1)){ |
18 |
| - for var floorNumber in (2..<(numberOfFloors+1)){ |
19 |
| - eggFloor[eggNumber][floorNumber] = Int.max //setting the final result a high number to find out minimum |
20 |
| - for var visitingFloor in (1..<(floorNumber+1)){ |
21 |
| - //there are two cases |
22 |
| - //case 1: egg breaks. meaning we'll have one less egg, and we'll have to go downstairs -> visitingFloor-1 |
23 |
| - //case 2: egg doesn't break. meaning we'll still have 'eggs' number of eggs, and we'll go upstairs -> floorNumber-visitingFloor |
24 |
| - attempts = 1 + max(eggFloor[eggNumber-1][visitingFloor-1], eggFloor[eggNumber][floorNumber-visitingFloor])//we add one taking into account the attempt we're taking at the moment |
25 |
| - |
26 |
| - if attempts < eggFloor[eggNumber][floorNumber]{ //finding the min |
27 |
| - eggFloor[eggNumber][floorNumber] = attempts; |
28 |
| - } |
29 |
| - } |
| 1 | +public func drop(numberOfEggs: Int, numberOfFloors: Int) -> Int { |
| 2 | + guard numberOfEggs != 0 && numberOfFloors != 0 else { return 0 } |
| 3 | + guard numberOfEggs != 1 && numberOfFloors != 1 else { return 1 } |
| 4 | + |
| 5 | + var eggFloor: [[Int]] = .init(repeating: .init(repeating: 0, count: numberOfFloors + 1), count: numberOfEggs + 1) |
| 6 | + var attempts = 0 |
| 7 | + |
| 8 | + for floorNumber in stride(from: 0, through: numberOfFloors, by: 1) { |
| 9 | + eggFloor[1][floorNumber] = floorNumber |
| 10 | + } |
| 11 | + eggFloor[2][1] = 1 |
| 12 | + |
| 13 | + for eggNumber in stride(from: 2, through: numberOfEggs, by: 1) { |
| 14 | + for floorNumber in stride(from: 2, through: numberOfFloors, by: 1) { |
| 15 | + eggFloor[eggNumber][floorNumber] = Int.max |
| 16 | + for visitingFloor in stride(from: 1, through: floorNumber, by: 1) { |
| 17 | + attempts = 1 + max(eggFloor[eggNumber - 1][visitingFloor - 1], eggFloor[eggNumber][floorNumber - visitingFloor]) |
| 18 | + |
| 19 | + if attempts < eggFloor[eggNumber][floorNumber] { |
| 20 | + eggFloor[eggNumber][floorNumber] = attempts |
30 | 21 | }
|
| 22 | + } |
31 | 23 | }
|
32 |
| - |
33 |
| - return eggFloor[numberOfEggs][numberOfFloors] |
34 |
| -} |
35 |
| - |
36 |
| -//Helper function to find max of two integers |
37 |
| -public func max(_ x1: Int, _ x2: Int) -> Int{ |
38 |
| - return x1 > x2 ? x1 : x2 |
| 24 | + } |
| 25 | + |
| 26 | + return eggFloor[numberOfEggs][numberOfFloors] |
39 | 27 | }
|
0 commit comments