Skip to content

Commit dea3165

Browse files
authored
Merge pull request kodecocodes#400 from aarkalyk/master
Egg Drop Dynamic Problem
2 parents cd7b83d + 260948a commit dea3165

File tree

6 files changed

+168
-0
lines changed

6 files changed

+168
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//: Playground - noun: a place where people can play
2+
3+
import Cocoa
4+
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
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}
30+
}
31+
}
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
39+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Egg Drop Problem/EggDrop.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}
30+
}
31+
}
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
39+
}

Egg Drop Problem/README.markdown

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Egg Drop Dynamic Problem
2+
3+
## Problem Description
4+
5+
Given some number of floors and some number of eggs, what is the minimum number of attempts it will take to find out from which floor egg will break.
6+
7+
Suppose that we wish to know which stories in a 36-story building are safe to drop eggs from, and which will cause the eggs to break on landing. We make a few assumptions:
8+
9+
- An egg that survives a fall can be used again.
10+
- A broken egg must be discarded.
11+
- The effect of a fall is the same for all eggs.
12+
- If an egg breaks when dropped, then it would break if dropped from a higher floor.
13+
- If an egg survives a fall then it would survive a shorter fall.
14+
15+
If only one egg is available and we wish to be sure of obtaining the right result, the experiment can be carried out in only one way. Drop the egg from the first-floor window; if it survives, drop it from the second floor window. Continue upward until it breaks. In the worst case, this method may require 36 droppings. Suppose 2 eggs are available. What is the least number of egg-droppings that is guaranteed to work in all cases?
16+
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.
17+
18+
## Solution
19+
- eggNumber -> Number of eggs at the moment
20+
- floorNumber -> Floor number at the moment
21+
- visitingFloor -> Floor being visited at the moment
22+
- attempts -> Minimum number of attempts it will take to find out from which floor egg will break
23+
24+
We store all the solutions in a 2D array. Where rows represents number of eggs and columns represent number of floors.
25+
26+
First, we set base cases:
27+
1) If there's only one egg, it takes as many attempts as number of floors
28+
2) If there are two eggs and one floor, it takes one attempt
29+
30+
```swift
31+
for var floorNumber in (0..<(numberOfFloors+1)){
32+
eggFloor[1][floorNumber] = floorNumber //base case 1: if there's only one egg, it takes 'numberOfFloors' attempts
33+
}
34+
35+
eggFloor[2][1] = 1 //base case 2: if there are two eggs and one floor, it takes one attempt
36+
```
37+
38+
When we drop an egg from a floor 'floorNumber', there can be two cases (1) The egg breaks (2) The egg doesn’t break.
39+
40+
1) If the egg breaks after dropping from 'visitingFloorth' floor, then we only need to check for floors lower than 'visitingFloor' with remaining eggs; so the problem reduces to 'visitingFloor'-1 floors and 'eggNumber'-1 eggs.
41+
2) If the egg doesn’t break after dropping from the 'visitingFloorth' floor, then we only need to check for floors higher than 'visitingFloor'; so the problem reduces to floors-'visitingFloor' floors and 'eggNumber' eggs.
42+
43+
Since we need to minimize the number of trials in worst case, we take the maximum of two cases. We consider the max of above two cases for every floor and choose the floor which yields minimum number of trials.
44+
45+
We find the answer based on the base cases and previously found answers as follows.
46+
```swift
47+
attempts = 1 + max(eggFloor[eggNumber-1][floors-1], eggFloor[eggNumber][floorNumber-floors])//we add one taking into account the attempt we're taking at the moment
48+
49+
if attempts < eggFloor[eggNumber][floorNumber]{ //finding the min
50+
eggFloor[eggNumber][floorNumber] = attempts;
51+
}
52+
```
53+
## Example
54+
Let's assume we have 2 eggs and 2 floors.
55+
1) We drop one egg from the first floor. If it breaks, then we get the answer. If it doesn't we'll have 2 eggs and 1 floors to work with.
56+
attempts = 1 + maximum of 0(got the answer) and eggFloor[2][1] (base case 2 which gives us 1)
57+
attempts = 1 + 1 = 2
58+
2) We drop one egg from the second floor. If it breaks, we'll have 1 egg and 1 floors to work with. If it doesn't, we'll get the answer.
59+
attempts = 1 + maximum of eggFloor[1][1](base case 1 which gives us 1) and 0(got the answer)
60+
attempts = 1 + 1 = 2
61+
3) Finding the minimum of 2 and 2 gives us 2, so the answer is 2.
62+
2 is the minimum number of attempts it will take to find out from which floor egg will break.
63+
64+

swift-algorithm-club.xcworkspace/contents.xcworkspacedata

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

0 commit comments

Comments
 (0)