From cc51a92b09792f70442263f6ee262dc0838e12a9 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Wed, 24 Apr 2024 01:39:11 +0900 Subject: [PATCH 1/7] phase1.py --- next_permutation/phase1.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 next_permutation/phase1.py diff --git a/next_permutation/phase1.py b/next_permutation/phase1.py new file mode 100644 index 0000000..38e3033 --- /dev/null +++ b/next_permutation/phase1.py @@ -0,0 +1,24 @@ +class Solution: + def nextPermutation(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + if len(nums) == 1: + return + + pivot_index = len(nums) - 2 # 比較の中心となるindexを定義 + while pivot_index > -1 and nums[pivot_index] >= nums[pivot_index+1]: + pivot_index -= 1 + + if pivot_index == -1: + nums.reverse() + return + + swap_index = len(nums) - 1 # swapに使うindexを定義 + while nums[swap_index] <= nums[pivot_index]: + swap_index -= 1 + + nums[swap_index], nums[pivot_index] = nums[pivot_index], nums[swap_index] + + nums[pivot_index+1:] = reversed(nums[pivot_index+1:]) + return From a7a0e16db9a91dc42a3cb749795a623c3793e23d Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Wed, 24 Apr 2024 01:39:20 +0900 Subject: [PATCH 2/7] phase2.py --- next_permutation/phase2.py | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 next_permutation/phase2.py diff --git a/next_permutation/phase2.py b/next_permutation/phase2.py new file mode 100644 index 0000000..5be6676 --- /dev/null +++ b/next_permutation/phase2.py @@ -0,0 +1,48 @@ +class Solution: + def nextPermutation(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + def _find_pivot_index(nums: List[int])->int: + # 比較の中心となるindexを定義 + # 末端から見ていって増加関係のないところを比較indexとする + pivot_index = len(nums) - 2 + while pivot_index > -1 and nums[pivot_index] >= nums[pivot_index+1]: + pivot_index -= 1 + return pivot_index + + def _find_swap_index(nums: List[int], pivot_index: int)->int: + # swapに使うindexを定義 + # swapする位置のindexを求める + swap_index = len(nums) - 1 + while nums[swap_index] <= nums[pivot_index]: + swap_index -= 1 + return swap_index + + if len(nums) == 1: + return + + pivot_index = _find_pivot_index(nums) + + if pivot_index == -1: + nums.reverse() + return + + swap_index = _find_swap_index(nums, pivot_index) + + nums[swap_index], nums[pivot_index] = nums[pivot_index], nums[swap_index] + + nums[pivot_index+1:] = reversed(nums[pivot_index+1:]) + return + +""" +None型を返すせいで, 正直PythonよりはC++とかの方が書きやすいと感じたが, とりあえずPythonで書く練習をした + +Reference: +https://github.com/shining-ai/leetcode/pull/58/files 解くことに夢中になりすぎて関数での分割を忘れていた。 +nums[pivot_index+1:] = reversed(nums[pivot_index+1:])よりもnums[left + 1 :] = sorted(nums[left + 1 :]) +の方が認知コストは低いなあと感じた。reversedで良いことは紙とペンで確かめられるけど, それをしなきゃいけない感じのコードなら確かに書くべきではないのかも + +https://github.com/hayashi-ay/leetcode/pull/67/files +markdownに書いてあるNext Permutationアルゴリズムの説明がわかりやすいと思いました。あとコメントにあった通り次から関数名は動詞から書き始めることを意識してみます。 +""" \ No newline at end of file From 0da8973bdf79d91423c1bfca0a7bd05d891588ed Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Wed, 24 Apr 2024 01:47:26 +0900 Subject: [PATCH 3/7] phase3 --- next_permutation/phase3.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 next_permutation/phase3.py diff --git a/next_permutation/phase3.py b/next_permutation/phase3.py new file mode 100644 index 0000000..d5722ad --- /dev/null +++ b/next_permutation/phase3.py @@ -0,0 +1,35 @@ +class Solution: + def nextPermutation(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + def _find_pivot_index(nums: List[int])->int: + # 比較の対象となる数のindexを見つける + pivot_index = len(nums) - 2 + while pivot_index > -1 and nums[pivot_index] >= nums[pivot_index+1]: + pivot_index -= 1 + return pivot_index + + def _find_swap_index(nums: List[int], pivot_index: int)->int: + # 後ろからnums[pivot_index]より大きい数を見つけてswapするindexを取り出す + swap_index = len(nums) - 1 + while nums[swap_index] <= nums[pivot_index]: + swap_index -= 1 + return swap_index + + if len(nums) == 1: + return + + pivot_index = _find_pivot_index(nums) + + if pivot_index == -1: + nums.reverse() + return + + swap_index = _find_swap_index(nums, pivot_index) + + nums[swap_index], nums[pivot_index] = nums[pivot_index], nums[swap_index] + + nums[pivot_index+1:] = sorted(nums[pivot_index+1:]) + + return nums \ No newline at end of file From b7cb63b2c5dba8238247b12fa51cb25fa8b4a90d Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Thu, 25 Apr 2024 02:22:15 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=E5=A4=89=E6=95=B0=E3=81=A8=E9=96=A2?= =?UTF-8?q?=E6=95=B0=E5=90=8D=E3=81=AE=E8=A6=8B=E7=9B=B4=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- next_permutation/phase4.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 next_permutation/phase4.py diff --git a/next_permutation/phase4.py b/next_permutation/phase4.py new file mode 100644 index 0000000..3232d5a --- /dev/null +++ b/next_permutation/phase4.py @@ -0,0 +1,35 @@ +class Solution: + def nextPermutation(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + def _find_first_not_sorted_index(nums: List[int])->int: + # 後ろから走査し, nums[index] >= nums[index+1]ではなくなる初めてのindexを取り出す + index = len(nums) - 2 + while index > -1 and nums[index] >= nums[index+1]: + index -= 1 + return index + + def _find_second_largest_index(nums: List[int], compare_index: int)->int: + # 後ろからnums[compare_index]より大きい数を見つけてswapするindexを取り出す + index = len(nums) - 1 + while nums[index] <= nums[compare_index]: + index -= 1 + return index + + if len(nums) == 1: + return + + left = _find_first_not_sorted_index(nums) + + if left == -1: + nums.reverse() + return + + right = _find_second_largest_index(nums, left) + + nums[right], nums[left] = nums[left], nums[right] + + nums[left+1:] = sorted(nums[left+1:]) + + return nums \ No newline at end of file From 95e6ce22f9ac5ae2de193de84b8db2bf1342f188 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Sun, 28 Apr 2024 03:15:07 +0900 Subject: [PATCH 5/7] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=82=92=E5=8F=97=E3=81=91,=20=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- next_permutation/phase5.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 next_permutation/phase5.py diff --git a/next_permutation/phase5.py b/next_permutation/phase5.py new file mode 100644 index 0000000..38a1f21 --- /dev/null +++ b/next_permutation/phase5.py @@ -0,0 +1,27 @@ +class Solution: + def nextPermutation(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + def _find_first_not_sorted_index() -> int: + index = len(nums) - 2 + while index >= 0 and nums[index] >= nums[index+1]: + index -= 1 + return index + + def _rfind_bigger_than(compare_index: int) -> int: + index = len(nums) - 1 + while nums[index] <= nums[compare_index]: + index -= 1 + return index + + left = _find_first_not_sorted_index() + + if left == -1: + nums.reverse() + return + + right = _rfind_bigger_than(left) + nums[left], nums[right] = nums[right], nums[left] + nums[left+1:].reverse() + return \ No newline at end of file From 56cdf4cf45b6c4203ff2d0ac08c44a3b28e2c71f Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Sun, 28 Apr 2024 16:53:23 +0900 Subject: [PATCH 6/7] =?UTF-8?q?phase6=20in-place=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- next_permutation/phase6.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 next_permutation/phase6.py diff --git a/next_permutation/phase6.py b/next_permutation/phase6.py new file mode 100644 index 0000000..150f651 --- /dev/null +++ b/next_permutation/phase6.py @@ -0,0 +1,33 @@ +class Solution: + def nextPermutation(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + def _rfind_first_not_decreasing_index() -> int: + index = len(nums) - 2 + while index >= 0 and nums[index] >= nums[index+1]: + index -= 1 + return index + + def _rfind_bigger_than(compare_index: int) -> int: + index = len(nums) - 1 + while nums[index] <= nums[compare_index]: + index -= 1 + return index + + left = _rfind_first_not_decreasing_index() + + if left == -1: + nums.reverse() + return + + right = _rfind_bigger_than(left) + nums[left], nums[right] = nums[right], nums[left] + + swap_right = len(nums) - 1 + swap_left = left + 1 + while swap_left < swap_right: + nums[swap_right], nums[swap_left] = nums[swap_left], nums[swap_right] + swap_left += 1 + swap_right -= 1 + return \ No newline at end of file From b90357b0a2f827d7d448aea0a607628dedcf897a Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Mon, 29 Apr 2024 00:52:40 +0900 Subject: [PATCH 7/7] phase7 --- next_permutation/phase7.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 next_permutation/phase7.py diff --git a/next_permutation/phase7.py b/next_permutation/phase7.py new file mode 100644 index 0000000..38b3b91 --- /dev/null +++ b/next_permutation/phase7.py @@ -0,0 +1,31 @@ +class Solution: + def nextPermutation(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + def _rfind_first_not_decreasing_index() -> int: + index = len(nums) - 2 + while index >= 0 and nums[index] >= nums[index+1]: + index -= 1 + return index + + def _rfind_bigger_than(compare_index: int) -> int: + index = len(nums) - 1 + while nums[index] <= nums[compare_index]: + index -= 1 + return index + + left = _rfind_first_not_decreasing_index() + if left == -1: + nums.reverse() + return + + right = _rfind_bigger_than(left) + nums[left], nums[right] = nums[right], nums[left] + swap_left = left + 1 + swap_right = len(nums) - 1 + # in-place reverse + while swap_left < swap_right: + nums[swap_left], nums[swap_right] = nums[swap_right], nums[swap_left] + swap_left += 1 + swap_right -= 1 \ No newline at end of file