Skip to content

Commit ea4b208

Browse files
committed
Updates code to Swift 4. Refactored a bit of the readme.
1 parent 4ace4c5 commit ea4b208

File tree

4 files changed

+43
-46
lines changed

4 files changed

+43
-46
lines changed
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
//: Playground - noun: a place where people can play
1+
#if swift(>=4.0)
2+
print("Hello, Swift 4!")
3+
#endif
24

3-
import Cocoa
5+
drop(numberOfEggs: 2, numberOfFloors: 2) //expected answer: 2
6+
drop(numberOfEggs: 2, numberOfFloors: 4) //expected answer: 3
7+
drop(numberOfEggs: 2, numberOfFloors: 6) //expected answer: 3
8+
drop(numberOfEggs: 5, numberOfFloors: 5) //expected answer: 2
9+
drop(numberOfEggs: 5, numberOfFloors: 20) //expected answer: 4
410

5-
eggDrop(numberOfEggs: 2, numberOfFloors: 2) //expected answer: 2
6-
eggDrop(numberOfEggs: 2, numberOfFloors: 4) //expected answer: 3
7-
eggDrop(numberOfEggs: 2, numberOfFloors: 6) //expected answer: 3
8-
eggDrop(numberOfEggs: 5, numberOfFloors: 5) //expected answer: 2
9-
eggDrop(numberOfEggs: 5, numberOfFloors: 20) //expected answer: 4
11+
drop(numberOfEggs: 2, numberOfFloors: 36)
12+
drop(numberOfEggs: 2, numberOfFloors: 10)
Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,27 @@
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
3021
}
22+
}
3123
}
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]
3927
}

Egg Drop Problem/EggDrop.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Egg Drop Problem/README.markdown

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ If the object is incredibly resilient, and you may need to do the testing on the
1212

1313
## Description
1414

15-
You're in a building with **m** floors and you are given **n** eggs. What is the minimum number attempts it will take to find out the lowest floor that breaks the egg?
15+
You're in a building with **m** floors and you are given **n** eggs. What is the maximum number of attempts it will take to find out the floor that breaks the egg?
1616

1717
For convenience, here are a few rules to keep in mind:
1818

@@ -22,9 +22,8 @@ For convenience, here are a few rules to keep in mind:
2222
- If an egg breaks, then it would break if dropped from a higher floor.
2323
- If an egg survives, then it would survive a shorter fall.
2424

25-
The problem is not actually to find the critical floor, but merely to decide floors from which eggs should be dropped so that total number of trials are minimized.
26-
2725
## Solution
26+
2827
- eggNumber -> Number of eggs at the moment
2928
- floorNumber -> Floor number at the moment
3029
- visitingFloor -> Floor being visited at the moment

0 commit comments

Comments
 (0)