Swift Warmup : Oversized pancake flipper

Swift Warmup : Oversized pancake flipper

3f7d9611fc919c98512b779cde637dfc?s=128

Johnlin

May 02, 2017
Tweet

Transcript

  1. Swift ఔࣜখ೤਎ Oversized Pancake Flipper John Lin

  2. 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"). • ḦṷళཁᩯҰछ৽ḦṷɼҰ໘༗޼ࠀྗ၏తসᢋ(ਖ਼ ໘)ɼ㠥Ұ໘ॄኄ౎ᔒ༗(൓໘)ɻ
  3. 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 ݸ࿈᠃తḦṷత䌒ࢠɻՄҎ೺ൣᅴ㚎తḦṷኺ ਖ਼໘Ꮣ൓໘ɼ҃ੋ൓໘Ꮣਖ਼໘ɻ຋໘ෆ။վᏓḦṷతࠨӈॱংɻ
  4. 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 ݸؐগతḦṷɼबࢉੋᬑ㐊໵Ұᒬ ʢ᭿൘తᬑ㐊ੋಥग़ိతʣɻ
  5. 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. • 㟬తላె༻ᢜత䌒ࢠʢҰ࣍୞ೳ຋Ұݸʣᬋศ຋ྃزݸḦṷɼ વޙबትஶᢜ䌒ࢠڈ্ኇॴྃʂ㟬୞ೳ༻኷େత䌒ࢠࡏ࠷շ త଎౓Լ೺Ḧṷ຋౸সᢋಹҰ໘ɼṜᒬސ٬࠽ೳ։৺஍཭։ɻ
  6. 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. • څఆݱࡏḦṷత㐫ଶɼؐ༗େ䌒ࢠతईੇɼܭࢉ࠷গ ཁ຋ز࣍࠽ೳ೺Ḧṷ຋౸সᢋ໘ɼ҃ऀੋࠜຊब၏ෆ ౸
  7. ༌ೖ • ୈҰߦੋ័ڞ༗زݸྫࢠɼ࠶ိ㑌ҰߦੋḦṷݱࡏత ᒬࢠ + ੋਖ਼໘ - ੋ൓໘ɼޙ໘తᏐࣈੋ䌒ࢠత低౓ɼ தؒ༻ۭനִ։ɻ •

    3
 ---+-++- 3
 +++++ 4
 -+-+- 4
  8. ༌ग़ • લ໘ੋ ඪࣔग़ྫࢠزɼޙ໘తᏐࣈੋ࠷গཁ຋ز ࣍ɼ೗ՌӬԕ౎ᔒ㭎๏શ෦຋੒ਖ਼໘ɼबሜ IMPOSSILBE • Case #1: 3


    Case #2: 0
 Case #3: IMPOSSIBLE
  9. ൣྫ • ༌ೖ: ---+-++- 3 • ճၚ: Case #1: 3

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

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

    • ዎኄ຋౎ෆߦ
 ୞༗ၷݸՄҎ຋తҐஔɼ຋ိ຋ڈ౎ᔒ㭎๏
  12. ໰୊ࠎՍ func minFlip(pancakes: [Bool], flipLen: Int) -> Int? { }

  13. ႔ཧ ༌ೖ 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 }
  14. ႔ཧ ༌ग़ let resultStr = results.map { $0 == -1

    ? "IMPOSSIBLE" : "\ ($0)" }.enumerated().map{"Case #\($0.offset+1): \ ($0.element)"}.joined(separator: "\n") print(resultStr)
  15. ԋࢉ๏ • ઌᬋศ؃زݸྫࢠ

  16. ൣྫ • ༌ೖ: ++--+-+-- 3 • ճၚ: 3 • ኺୈࡾݸ։࢝຋

    ++--+-+-- Ꮣ੒ ++++--+-- • ኺୈޒݸ։࢝຋ ++++--+-- Ꮣ੒ ++++++--- • ኺୈീݸ։࢝຋ ++++++--- Ꮣ੒ +++++++++
  17. ᧺࡯ • ຋తಈ࡞༗ަ׵཯ɼઌ຋ޙ຋త݁Ռ။Ұᒬ • ࡏಉҰݸҐࢠ຋ၷ࣍ब౳ԙᔒ຋ɼॴҎ㑌Ұݸىᴍ࠷ ଟ຋Ұ࣍ɻ • ຋໘ىᴍࠨᬑతḦṷ׬શෆ။डӨڹ • ࠷ࠨᬑత୞༗Ұݸ຋໘తػ။ɼ೗Ռੋ൓తबҰఆཁ

    ຋ɼਖ਼త࿩बҰఆෆೳ຋
  18. ԋࢉ๏ • ኺࠨᬑ։࢝຋ɼ۰౸൓໘ब຋ɼᙛႫԼతḦṷଠগෆ ೳ຋໘࣌ɼ؃؃ੋෆੋશ෦౎ੋਖ਼໘ɼෆੋबճၚ nilɻ • ༻ᬇ᫮ိܭࢉ຋աత࣍Ꮠɼࠨᬑ؃աိੋਖ਼໘త௚઀ ႟ᎃɼ௚౸۰౸ୈҰݸ൓໘ҝࢭɻ

  19. ԋࢉ๏ ++--+-+-- 3 ‑ 
 ++++--+-- 3 --+-- 3 ‑

    
 ++--- 3 --- 3 ‑ 
 +++ 3 1 2 3
  20. 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)
  21. Q&A