Trapping Rain Water question is probably one of the popular interview question . I have implemented it with Swift 3 for iOS.
I have divided UIImage into 8x8
pieces with using UIImageGraphics to represent image and there are totally 8 Tower
and will be maximum height of 8
Here is the struct to store Tower
heights
This function generate scenario for UIImageView
struct WallHeights{
var wallHeights : [Int] = [0,0,0,0,0,0,0,0];
}
This function generate scenario for UIImageView
func generateScenarioForTrappingRain(c : CGSize , wall : WallHeights)
This function find the maximum height of tower on the left side of index and return its height as Int
func findLeftMaximum(index : Int , wall : WallHeights) -> Int
This function find the maximum height of tower on the right side of index and return its height as Int
func findRightMaximum(index : Int , wall : WallHeights) -> Int
This function fill the area as given section (0 to 7) with given color . It starts to fill from the heightStart to heightStart + height .
func firstSectionWithHeight(heightStart : Int , height : Int , section : Int , color : UIColor , c : CGSize) -> UIImage?
This function draw white lines to image cause of understanding the sections
func drawGraphLines(c : CGSize)
This function checks if there are higher tower on both sides as right and left .
func checkBothSides(index : Int , wall : WallHeights) -> Bool