Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Point {
- let x : Double
- let y : Double
- }
- struct Rect {
- let h : Double
- let w : Double
- }
- func getPoint(_ out : Rect, _ array : [Rect] ) -> [Point]{
- guard array.count > 0 else { return []}
- var res = [Point]()
- var currx: Double = 0
- var curry: Double = 0
- var maxCurrW : Double = 0
- var i = 0
- while i < array.count {
- let rect = array[i]
- if rect.w + currx > out.w { return [] } // not valid input
- if rect.h + curry > out.h {
- if curry == 0 { return [] } // not valid input
- // need to col
- curry = 0
- currx += maxCurrW
- maxCurrW = 0
- continue
- }
- // put on current col
- res.append(Point(x: currx, y: curry))
- maxCurrW = max(maxCurrW, rect.w)
- curry += rect.h
- i += 1
- }
- return res
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement