From 88105e43a9161c15bd73ec6b1b722a9e4f8fe599 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Thu, 6 Jun 2024 01:40:14 +0900 Subject: [PATCH 1/6] phase1 --- arai60/phase1.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 arai60/phase1.py diff --git a/arai60/phase1.py b/arai60/phase1.py new file mode 100644 index 0000000..5782290 --- /dev/null +++ b/arai60/phase1.py @@ -0,0 +1,20 @@ +class Solution: + def kthGrammar(self, n: int, k: int) -> int: + moves = deque([]) # 0: move left 1: move right + # store the order from the leaf + for i in range(n-1): + if (k - 1) % 2 == 0: + moves.appendleft(0) + else: + moves.appendleft(1) + k = (k - 1) // 2 # 0-indexed + k += 1 # 1-indexed + kth_symbol = 0 + for move in moves: + # move 0 kth_symbol 0 -> 0 + # move 1 kth_symbol 0 -> 1 + # move 0 kth_symbol 1 -> 1 + # move 1 kth_symbol 1 -> 0 + kth_symbol = kth_symbol ^ move + + return kth_symbol \ No newline at end of file From f743685e0d90acde017d60dd95e2107ab1c5973c Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Thu, 6 Jun 2024 01:40:23 +0900 Subject: [PATCH 2/6] phase2 --- arai60/phase2.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 arai60/phase2.py diff --git a/arai60/phase2.py b/arai60/phase2.py new file mode 100644 index 0000000..17056e0 --- /dev/null +++ b/arai60/phase2.py @@ -0,0 +1,50 @@ +""" +Reference: +Mike0121: https://github.com/Mike0121/LeetCode/pull/18/files +再帰で書く書き方, 僕がphase1で書いたやり方は移動順を取り出してきて根から辿る方法だが, こっちだと明示的にfor文を書かなくてもかける + +Exzrgs: https://github.com/Exzrgs/LeetCode/pull/12/files +shining-ai: https://github.com/shining-ai/leetcode/pull/46/files +kを1で表した時の数の1の数と反転回数に注目, bitcountを用いて解く +hayashi-ay: https://github.com/hayashi-ay/leetcode/pull/46/files +親を表す単語が問題文にないことに注意して命名を行う +""" + +# k = (k-1) // 2 + 1がk = (k+1) // 2でまとめられることに気づきまとめる +class Solution: + def kthGrammar(self, n: int, k: int) -> int: + moves = deque([]) # 0: move left 1: move right + # store the order from the leaf + for i in range(n-1): + if (k - 1) % 2 == 0: + moves.appendleft(0) + else: + moves.appendleft(1) + k = (k + 1) // 2 + kth_symbol = 0 + for move in moves: + # move 0 kth_symbol 0 -> 0 + # move 1 kth_symbol 0 -> 1 + # move 0 kth_symbol 1 -> 1 + # move 1 kth_symbol 1 -> 0 + kth_symbol = kth_symbol ^ move + + return kth_symbol + +# 再帰 +class Solution: + def kthGrammar(self, n: int, k: int) -> int: + if k == 1: + return 0 + + previous_value = self.kthGrammar(n - 1, (k + 1) // 2) + + if k % 2 == 0: + return 1 - previous_value + else: + return previous_value + +# bit count +class Solution: + def kthGrammar(self, n: int, k: int) -> int: + return (k - 1).bit_count() % 2 \ No newline at end of file From 9f510bb7cf48be0eaebbb40b5e18ac9610fce08b Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Thu, 6 Jun 2024 01:40:30 +0900 Subject: [PATCH 3/6] phase3 --- arai60/phase3.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 arai60/phase3.py diff --git a/arai60/phase3.py b/arai60/phase3.py new file mode 100644 index 0000000..80db24b --- /dev/null +++ b/arai60/phase3.py @@ -0,0 +1,12 @@ +# 再帰で解く方法を選択 + +class Solution: + def kthGrammar(self, n: int, k: int) -> int: + if k == 1: + return 0 + + previous_value = self.kthGrammar(n - 1, (k + 1) // 2) + if k % 2 == 0: + return 1 - previous_value + else: + return previous_value \ No newline at end of file From bf9c0c84e0df875ef01caf63f0641d6164a7d594 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Thu, 6 Jun 2024 01:42:17 +0900 Subject: [PATCH 4/6] Gather in directory --- arai60/kth_symbol_in_grammar/phase1.py | 20 +++++++++++ arai60/kth_symbol_in_grammar/phase2.py | 50 ++++++++++++++++++++++++++ arai60/kth_symbol_in_grammar/phase3.py | 12 +++++++ 3 files changed, 82 insertions(+) create mode 100644 arai60/kth_symbol_in_grammar/phase1.py create mode 100644 arai60/kth_symbol_in_grammar/phase2.py create mode 100644 arai60/kth_symbol_in_grammar/phase3.py diff --git a/arai60/kth_symbol_in_grammar/phase1.py b/arai60/kth_symbol_in_grammar/phase1.py new file mode 100644 index 0000000..5782290 --- /dev/null +++ b/arai60/kth_symbol_in_grammar/phase1.py @@ -0,0 +1,20 @@ +class Solution: + def kthGrammar(self, n: int, k: int) -> int: + moves = deque([]) # 0: move left 1: move right + # store the order from the leaf + for i in range(n-1): + if (k - 1) % 2 == 0: + moves.appendleft(0) + else: + moves.appendleft(1) + k = (k - 1) // 2 # 0-indexed + k += 1 # 1-indexed + kth_symbol = 0 + for move in moves: + # move 0 kth_symbol 0 -> 0 + # move 1 kth_symbol 0 -> 1 + # move 0 kth_symbol 1 -> 1 + # move 1 kth_symbol 1 -> 0 + kth_symbol = kth_symbol ^ move + + return kth_symbol \ No newline at end of file diff --git a/arai60/kth_symbol_in_grammar/phase2.py b/arai60/kth_symbol_in_grammar/phase2.py new file mode 100644 index 0000000..17056e0 --- /dev/null +++ b/arai60/kth_symbol_in_grammar/phase2.py @@ -0,0 +1,50 @@ +""" +Reference: +Mike0121: https://github.com/Mike0121/LeetCode/pull/18/files +再帰で書く書き方, 僕がphase1で書いたやり方は移動順を取り出してきて根から辿る方法だが, こっちだと明示的にfor文を書かなくてもかける + +Exzrgs: https://github.com/Exzrgs/LeetCode/pull/12/files +shining-ai: https://github.com/shining-ai/leetcode/pull/46/files +kを1で表した時の数の1の数と反転回数に注目, bitcountを用いて解く +hayashi-ay: https://github.com/hayashi-ay/leetcode/pull/46/files +親を表す単語が問題文にないことに注意して命名を行う +""" + +# k = (k-1) // 2 + 1がk = (k+1) // 2でまとめられることに気づきまとめる +class Solution: + def kthGrammar(self, n: int, k: int) -> int: + moves = deque([]) # 0: move left 1: move right + # store the order from the leaf + for i in range(n-1): + if (k - 1) % 2 == 0: + moves.appendleft(0) + else: + moves.appendleft(1) + k = (k + 1) // 2 + kth_symbol = 0 + for move in moves: + # move 0 kth_symbol 0 -> 0 + # move 1 kth_symbol 0 -> 1 + # move 0 kth_symbol 1 -> 1 + # move 1 kth_symbol 1 -> 0 + kth_symbol = kth_symbol ^ move + + return kth_symbol + +# 再帰 +class Solution: + def kthGrammar(self, n: int, k: int) -> int: + if k == 1: + return 0 + + previous_value = self.kthGrammar(n - 1, (k + 1) // 2) + + if k % 2 == 0: + return 1 - previous_value + else: + return previous_value + +# bit count +class Solution: + def kthGrammar(self, n: int, k: int) -> int: + return (k - 1).bit_count() % 2 \ No newline at end of file diff --git a/arai60/kth_symbol_in_grammar/phase3.py b/arai60/kth_symbol_in_grammar/phase3.py new file mode 100644 index 0000000..80db24b --- /dev/null +++ b/arai60/kth_symbol_in_grammar/phase3.py @@ -0,0 +1,12 @@ +# 再帰で解く方法を選択 + +class Solution: + def kthGrammar(self, n: int, k: int) -> int: + if k == 1: + return 0 + + previous_value = self.kthGrammar(n - 1, (k + 1) // 2) + if k % 2 == 0: + return 1 - previous_value + else: + return previous_value \ No newline at end of file From 2020a22a0106bc4210803ca752680a4a1bf44b52 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Thu, 6 Jun 2024 01:43:32 +0900 Subject: [PATCH 5/6] =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E6=9C=AB=E5=B0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arai60/kth_symbol_in_grammar/phase1.py | 2 +- arai60/kth_symbol_in_grammar/phase2.py | 2 +- arai60/kth_symbol_in_grammar/phase3.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/arai60/kth_symbol_in_grammar/phase1.py b/arai60/kth_symbol_in_grammar/phase1.py index 5782290..fa23270 100644 --- a/arai60/kth_symbol_in_grammar/phase1.py +++ b/arai60/kth_symbol_in_grammar/phase1.py @@ -17,4 +17,4 @@ def kthGrammar(self, n: int, k: int) -> int: # move 1 kth_symbol 1 -> 0 kth_symbol = kth_symbol ^ move - return kth_symbol \ No newline at end of file + return kth_symbol diff --git a/arai60/kth_symbol_in_grammar/phase2.py b/arai60/kth_symbol_in_grammar/phase2.py index 17056e0..0f2a052 100644 --- a/arai60/kth_symbol_in_grammar/phase2.py +++ b/arai60/kth_symbol_in_grammar/phase2.py @@ -47,4 +47,4 @@ def kthGrammar(self, n: int, k: int) -> int: # bit count class Solution: def kthGrammar(self, n: int, k: int) -> int: - return (k - 1).bit_count() % 2 \ No newline at end of file + return (k - 1).bit_count() % 2 diff --git a/arai60/kth_symbol_in_grammar/phase3.py b/arai60/kth_symbol_in_grammar/phase3.py index 80db24b..5afdd0c 100644 --- a/arai60/kth_symbol_in_grammar/phase3.py +++ b/arai60/kth_symbol_in_grammar/phase3.py @@ -9,4 +9,4 @@ def kthGrammar(self, n: int, k: int) -> int: if k % 2 == 0: return 1 - previous_value else: - return previous_value \ No newline at end of file + return previous_value From e2bced83054bc1e384c21c8d45d0110477494b2e Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Thu, 6 Jun 2024 01:43:49 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=E3=81=84=E3=82=89=E3=81=AA=E3=81=84?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arai60/phase1.py | 20 ------------------- arai60/phase2.py | 50 ------------------------------------------------ arai60/phase3.py | 12 ------------ 3 files changed, 82 deletions(-) delete mode 100644 arai60/phase1.py delete mode 100644 arai60/phase2.py delete mode 100644 arai60/phase3.py diff --git a/arai60/phase1.py b/arai60/phase1.py deleted file mode 100644 index 5782290..0000000 --- a/arai60/phase1.py +++ /dev/null @@ -1,20 +0,0 @@ -class Solution: - def kthGrammar(self, n: int, k: int) -> int: - moves = deque([]) # 0: move left 1: move right - # store the order from the leaf - for i in range(n-1): - if (k - 1) % 2 == 0: - moves.appendleft(0) - else: - moves.appendleft(1) - k = (k - 1) // 2 # 0-indexed - k += 1 # 1-indexed - kth_symbol = 0 - for move in moves: - # move 0 kth_symbol 0 -> 0 - # move 1 kth_symbol 0 -> 1 - # move 0 kth_symbol 1 -> 1 - # move 1 kth_symbol 1 -> 0 - kth_symbol = kth_symbol ^ move - - return kth_symbol \ No newline at end of file diff --git a/arai60/phase2.py b/arai60/phase2.py deleted file mode 100644 index 17056e0..0000000 --- a/arai60/phase2.py +++ /dev/null @@ -1,50 +0,0 @@ -""" -Reference: -Mike0121: https://github.com/Mike0121/LeetCode/pull/18/files -再帰で書く書き方, 僕がphase1で書いたやり方は移動順を取り出してきて根から辿る方法だが, こっちだと明示的にfor文を書かなくてもかける - -Exzrgs: https://github.com/Exzrgs/LeetCode/pull/12/files -shining-ai: https://github.com/shining-ai/leetcode/pull/46/files -kを1で表した時の数の1の数と反転回数に注目, bitcountを用いて解く -hayashi-ay: https://github.com/hayashi-ay/leetcode/pull/46/files -親を表す単語が問題文にないことに注意して命名を行う -""" - -# k = (k-1) // 2 + 1がk = (k+1) // 2でまとめられることに気づきまとめる -class Solution: - def kthGrammar(self, n: int, k: int) -> int: - moves = deque([]) # 0: move left 1: move right - # store the order from the leaf - for i in range(n-1): - if (k - 1) % 2 == 0: - moves.appendleft(0) - else: - moves.appendleft(1) - k = (k + 1) // 2 - kth_symbol = 0 - for move in moves: - # move 0 kth_symbol 0 -> 0 - # move 1 kth_symbol 0 -> 1 - # move 0 kth_symbol 1 -> 1 - # move 1 kth_symbol 1 -> 0 - kth_symbol = kth_symbol ^ move - - return kth_symbol - -# 再帰 -class Solution: - def kthGrammar(self, n: int, k: int) -> int: - if k == 1: - return 0 - - previous_value = self.kthGrammar(n - 1, (k + 1) // 2) - - if k % 2 == 0: - return 1 - previous_value - else: - return previous_value - -# bit count -class Solution: - def kthGrammar(self, n: int, k: int) -> int: - return (k - 1).bit_count() % 2 \ No newline at end of file diff --git a/arai60/phase3.py b/arai60/phase3.py deleted file mode 100644 index 80db24b..0000000 --- a/arai60/phase3.py +++ /dev/null @@ -1,12 +0,0 @@ -# 再帰で解く方法を選択 - -class Solution: - def kthGrammar(self, n: int, k: int) -> int: - if k == 1: - return 0 - - previous_value = self.kthGrammar(n - 1, (k + 1) // 2) - if k % 2 == 0: - return 1 - previous_value - else: - return previous_value \ No newline at end of file