Skip to content
RSS feed tkhwang on GitHub tkhwang on Twitter

πŸ€– leetcode 969. Pancake Sorting | medium | greedy | javascript

πŸ—’οΈ Problems

Pancake Sorting - LeetCode

Given an array of integers arr, sort the array
by performing a series of pancake flips.
In one pancake flip we do the following steps:

- Choose an integer k where 1 <= k <= arr.length.
- Reverse the sub-array arr[0...k-1] (0-indexed).

For example, if arr = [3,2,1,4] and we performed a pancake flip choosing k = 3,
we reverse the sub-array [3,2,1], so arr = [1,2,3,4] after the pancake flip at k = 3.

Return an array of the k-values corresponding to a sequence of pancake flips  that sort arr.
Any valid answer that sorts the array within 10 \* arr.length flips will be judged as correct.
Input: arr = [3,2,4,1]
Output: [4,2,4,3]
Explanation:
We perform 4 pancake flips, with k values 4, 2, 4, and 3.
Starting state: arr = [3, 2, 4, 1]
After 1st flip (k = 4): arr = [1, 4, 2, 3]
After 2nd flip (k = 2): arr = [4, 1, 2, 3]
After 3rd flip (k = 4): arr = [3, 2, 1, 4]
After 4th flip (k = 3): arr = [1, 2, 3, 4], which is sorted.

✨ Idea

πŸ’‘ greedy solution

이런 μ’…λ₯˜μ˜ greedy ν•˜κ²Œ λ‘œμ§μ„ μƒκ°ν•˜λŠ” 문제 μŠ€νƒ€μΌμ΄ μ–΄λ €μš΄ 것 κ°™μŠ΅λ‹ˆλ‹€. <br /> 직접 μ•Œμ•„λ‚Έ 것은 μ•„λ‹ˆκ³ , λ‹΅ 보고 ν™•μΈν•œ λ‚΄μš©μ΄μ§€λ§Œ μ •λ¦¬ν•΄λ΄…λ‹ˆλ‹€.

  • (n) 개 μ€‘μ—μ„œ κ°€μž₯ 큰 것을 μ°Ύμ•„μ„œ 이것을 κ°€μž₯ μ•„λž˜μͺ½μœΌλ‘œ λ’€μ§‘λŠ”λ‹€.
    • n 개 μ€‘μ—μ„œ κ°€μž₯ 큰 값을 κ°–λŠ” index maxIndex λ₯Ό μ°ΎλŠ”λ‹€.
    • index [0...maxIndex] λ₯Ό λ’€μ§‘μ–΄μ„œ array [maxValue, ... ] μ˜€λ„λ‘ ν•œ λ‹€μŒμ—
    • 이λ₯Ό λ‹€μ‹œ index [0, ... , n-1] λ’€μ§‘μ—μ„œ array [..., maxValue] κ°€μž₯ 큰 값이 맨 μ•„λž˜ μ˜€λ„λ‘ ν•œλ‹€.
  • 맨 μ•„λž˜ 1κ°œλŠ” 큰 값을 μ°Ύμ•˜μœΌλ‹ˆ κ·Έ μ•žμ— (n-1)κ°œμ— step1 을 λ°˜λ³΅ν•œλ‹€.

πŸ”₯ My Solution

이 greedy solution이 졜적의 solution은 아닐 μˆ˜λ„ μžˆλŠ” λ“― ν•˜μ§€λ§Œ medium μ΄λΌμ„œ κ·ΈλŸ°μ§€ 닡이 맞으면 accept λŠ” ν•΄μ£ΌλŠ” λ“― ν•˜λ„€μš”.

/**
 * @param {number[]} arr
 * @return {number[]}
 */
var pancakeSort = function (arr) {
  const N = arr.length;
  const res = [];

  const revert = (array, i, j) => {
    while (i < j) {
      [array[i], array[j]] = [array[j], array[i]];
      i += 1;
      j -= 1;
    }
  };

  const sort = (array, n) => {
    if (n === 1) return;

    // find max i
    let max = -Infinity;
    let maxIndex;
    for (let i = 0; i < n; i += 1) {
      if (max < array[i]) {
        max = array[i];
        maxIndex = i;
      }
    }

    // revert [0...i]
    revert(array, 0, maxIndex);
    res.push(maxIndex + 1);

    // revert [0...N-1]
    revert(array, 0, n - 1);
    res.push(n);

    sort(array, n - 1);
  };

  sort(arr, N);

  return res;
};

Bill Gates κ°€ μ†”λ£¨μ…˜μ„ ??

discussion λŒ“κΈ€μ— λ³΄λ‹ˆκΉ Bill Gates κ°€ 이 문제의 upper bound 인 O((5n + 5)/3) 해법을 μ•Œμ•„λƒˆλ‹€λŠ” 이야기가 μžˆλ„€μš”. Bill GatesλŠ” algorithm도 잘 ν–ˆμ—ˆλ‚˜ λ³΄κ΅°μš”.. :)

Bill Gates is known for giving an upper bound of O((5n + 5)/3) for this problem
where the flipped window may not necessarily start from beginning index as in this problem.

Read more about it here: https://en.wikipedia.org/wiki/Pancake_sorting