Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Warmup : Oversized pancake flipper

Swift Warmup : Oversized pancake flipper

Johnlin

May 02, 2017
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. Oversized Pancake Flipper • https://code.google.com/codejam/contest/3264486/ dashboard • Last year, the

    Infinite House of Pancakes introduced a new kind of pancake. It has a happy face made of chocolate chips on one side (the "happy side"), and nothing on the other side (the "blank side"). • ḦṷళཁᩯҰछ৽ḦṷɼҰ໘༗޼ࠀྗ၏తসᢋ(ਖ਼ ໘)ɼ㠥Ұ໘ॄኄ౎ᔒ༗(൓໘)ɻ
  2. Oversized Pancake Flipper • You are the head cook on

    duty. The pancakes are cooked in a single row over a hot surface. As part of its infinite efforts to maximize efficiency, the House has recently given you an oversized pancake flipper that flips exactly K consecutive pancakes. That is, in that range of K pancakes, it changes every happy-side pancake to a blank-side pancake, and vice versa; it does not change the left-to-right order of those pancakes. • 㟬ੋࠓఱతओ኏ɻḦṷࡏ᭿൘্ഉ੒ҰഉɻҝྃఏঋᏈ཰ɼ㟬༗ Ұ੭ՄҎҰ࣍຋K ݸ࿈᠃తḦṷత䌒ࢠɻՄҎ೺ൣᅴ㚎తḦṷኺ ਖ਼໘Ꮣ൓໘ɼ҃ੋ൓໘Ꮣਖ਼໘ɻ຋໘ෆ။վᏓḦṷతࠨӈॱংɻ
  3. Oversized Pancake Flipper • You cannot flip fewer than K

    pancakes at a time with the flipper, even at the ends of the row (since there are raised borders on both sides of the cooking surface). For example, you can flip the first K pancakes, but not the first K - 1 pancakes. • 㟬ෆೳҰ࣍຋ൺK ݸؐগతḦṷɼबࢉੋᬑ㐊໵Ұᒬ ʢ᭿൘తᬑ㐊ੋಥग़ိతʣɻ
  4. Oversized Pancake Flipper • Your apprentice cook, who is still

    learning the job, just used the old-fashioned single-pancake flipper to flip some individual pancakes and then ran to the restroom with it, right before the time when customers come to visit the kitchen. You only have the oversized pancake flipper left, and you need to use it quickly to leave all the cooking pancakes happy side up, so that the customers leave feeling happy with their visit. • 㟬తላె༻ᢜత䌒ࢠʢҰ࣍୞ೳ຋Ұݸʣᬋศ຋ྃزݸḦṷɼ વޙबትஶᢜ䌒ࢠڈ্ኇॴྃʂ㟬୞ೳ༻኷େత䌒ࢠࡏ࠷շ త଎౓Լ೺Ḧṷ຋౸সᢋಹҰ໘ɼṜᒬސ٬࠽ೳ։৺஍཭։ɻ
  5. Oversized Pancake Flipper • Given the current state of the

    pancakes, calculate the minimum number of uses of the oversized pancake flipper needed to leave all pancakes happy side up, or state that there is no way to do it. • څఆݱࡏḦṷత㐫ଶɼؐ༗େ䌒ࢠతईੇɼܭࢉ࠷গ ཁ຋ز࣍࠽ೳ೺Ḧṷ຋౸সᢋ໘ɼ҃ऀੋࠜຊब၏ෆ ౸
  6. ൣྫ • ༌ೖ: ---+-++- 3 • ճၚ: Case #1: 3

    • ઌ຋લࡾݸ Ꮣ੒ ++++-++- • ࠶ኺୈޒݸ։࢝຋ Ꮣ੒ +++++--- • ࠶຋࠷ޙࡾݸ ++++++++
  7. ൣྫ • ༌ೖ: +++++ 4 • ճၚ: Case #2: 0

    • ຊိबੋਖ਼໘ɼෆ༻຋
  8. ൣྫ • ༌ೖ: -+-+- 4 • ճၚ: Case #3: IMPOSSIBLE

    • ዎኄ຋౎ෆߦ
 ୞༗ၷݸՄҎ຋తҐஔɼ຋ိ຋ڈ౎ᔒ㭎๏
  9. ႔ཧ ༌ೖ let filePath = Bundle.main.path(forResource: "A-large- practice", ofType: ".in")

    let fm = FileManager() let fileContentData = fm.contents(atPath: filePath!) let fileContent = String(bytes: fileContentData!, encoding: .utf8) let lines = fileContent?.components(separatedBy: "\n").dropFirst().dropLast(1) let pancakeRows = lines!.map{ $0.components(separatedBy: " ")} let results:[Int] = pancakeRows.map { (pair) -> Int in let pancakes = pair[0].characters.map{$0 == "+"} let flipLen = Int(pair[1]) return minFlip(pancakes: pancakes, flipLen: flipLen!) ?? -1 }
  10. ႔ཧ ༌ग़ let resultStr = results.map { $0 == -1

    ? "IMPOSSIBLE" : "\ ($0)" }.enumerated().map{"Case #\($0.offset+1): \ ($0.element)"}.joined(separator: "\n") print(resultStr)
  11. ൣྫ • ༌ೖ: ++--+-+-- 3 • ճၚ: 3 • ኺୈࡾݸ։࢝຋

    ++--+-+-- Ꮣ੒ ++++--+-- • ኺୈޒݸ։࢝຋ ++++--+-- Ꮣ੒ ++++++--- • ኺୈീݸ։࢝຋ ++++++--- Ꮣ੒ +++++++++
  12. ԋࢉ๏ ++--+-+-- 3 ‑ 
 ++++--+-- 3 --+-- 3 ‑

    
 ++--- 3 --- 3 ‑ 
 +++ 3 1 2 3
  13. func minFlip(pancakes: [Bool], flipLen: Int) -> Int? { //ፙୈҰݸ൓໘తҐஔ guard

    let start = pancakes.index(where: {$0==false}) else{ return 0 //೗Ռᔒ༗൓໘తɼबճၚ0 } //೺લ໘తਖ਼໘౎㟚ᎃ var res = Array(pancakes.dropFirst(start)) //೗Ռෆೳ࠶຋ बճၚ nil if res.count < flipLen { return nil } //຋໘ for i in 0..<flipLen { res[i] = !res[i] } // ᬇ᫮ݺڣ ೗Ռ࠷ޙෆߦबճၚෆߦ guard let count = minFlip(pancakes: res, flipLen: flipLen) else { return nil } // ՄҎతबճၚ ᬇ᫮త݁Ռ + 1 return count + 1 } ࣌ؒෳᯑ౓: O(n)
 ۭؒෳᯑ౓: O(1)
  14. Q&A