From bac33bba6aed06c007be8a29d2e78c1b53ea1536 Mon Sep 17 00:00:00 2001 From: Samarth Swami <70163465+samarthswami1016@users.noreply.github.com> Date: Tue, 11 Mar 2025 19:19:40 +0530 Subject: [PATCH 1/6] Update README.md --- .../3400-3499/3470.Permutations IV/README.md | 324 +++++++++++++++++- 1 file changed, 320 insertions(+), 4 deletions(-) diff --git a/solution/3400-3499/3470.Permutations IV/README.md b/solution/3400-3499/3470.Permutations IV/README.md index bceccae2ecccc..19d074f0d1f3e 100644 --- a/solution/3400-3499/3470.Permutations IV/README.md +++ b/solution/3400-3499/3470.Permutations IV/README.md @@ -111,25 +111,341 @@ tags: #### Python3 ```python - +from typing import List +from math import factorial +import heapq + +class Solution: + def permute(self, xxy: int, yyz: int) -> List[int]: + + kasu = {} + nnss = [] + majs = [] + ajwi = heapq.heappush + laoq = [] + + zzp = [i for i in range(1, xxy + 1) if i % 2 == 1] + zzq = [i for i in range(1, xxy + 1) if i % 2 == 0] + + ppp = [] + nxr = None + + for pps in range(xxy): + if pps == 0: + cnd = sorted(zzp + zzq) + else: + cnd = zzp if nxr == 1 else zzq + + fff = False + for cndt in cnd: + if cndt % 2 == 1: + nxt = 0 + noo = len(zzp) - 1 + nee = len(zzq) + else: + nxt = 1 + noo = len(zzp) + nee = len(zzq) - 1 + + llq = noo + nee + if llq == 0: + cnt = 1 + else: + if nxt == 1: + if noo != (llq + 1) // 2 or nee != llq // 2: + cnt = 0 + else: + cnt = factorial(noo) * factorial(nee) + else: + if nee != (llq + 1) // 2 or noo != llq // 2: + cnt = 0 + else: + cnt = factorial(noo) * factorial(nee) + + ajwi(nnss, cnt) + ajwi(majs, llq) + + if cnt >= yyz: + ppp.append(cndt) + if cndt % 2 == 1: + zzp.remove(cndt) + nxr = 0 + else: + zzq.remove(cndt) + nxr = 1 + fff = True + break + else: + yyz -= cnt + + ajwi(laoq, len(ppp)) + + if not fff: + return [] + return ppp ``` #### Java ```java - +import java.util.*; + +class DPHelper { + static final long ok = 10000000000000000L; + long[][][] dp = new long[101][101][2]; + boolean[][][] vis = new boolean[101][101][2]; + + long compute(int o, int e, int p) { + if (o == 0 && e == 0) return 1; + if (vis[o][e][p]) return dp[o][e][p]; + + long r = 0; + if (p == 1) { + if (o == 0) r = 0; + else r = o * compute(o - 1, e, 0); + } else { + if (e == 0) r = 0; + else r = e * compute(o, e - 1, 1); + } + + if (r > ok) r = ok; + vis[o][e][p] = true; + dp[o][e][p] = r; + return r; + } +} + +class SortHelper { + void sortList(ArrayList list) { + Collections.sort(list); + } +} + +class PermutationHelper { + List buildPermutation(int p, ArrayList O, ArrayList E, long k, DPHelper d) { + List ans = new ArrayList<>(); + if (O.size() + E.size() == 0) return ans; + int i = 0; + + if (p == 1) { + while (i < O.size()) { + long cnt = d.compute(O.size() - 1, E.size(), 0); + if (k > cnt) { + k -= cnt; + i++; + } else { + int x = O.get(i); + O.remove(i); + ans.add(x); + ans.addAll(buildPermutation(0, O, E, k, d)); + return ans; + } + } + } else { + while (i < E.size()) { + long cnt = d.compute(O.size(), E.size() - 1, 1); + if (k > cnt) { + k -= cnt; + i++; + } else { + int x = E.get(i); + E.remove(i); + ans.add(x); + ans.addAll(buildPermutation(1, O, E, k, d)); + return ans; + } + } + } + return ans; + } + + List alternateFormation(ArrayList O, ArrayList E, long k, DPHelper d, int n, SortHelper s) { + List ans = new ArrayList<>(); + int tot = O.size() + E.size(); + if (tot % 2 == 1) { + int i = 0; + while (i < O.size()) { + long cnt = d.compute(O.size() - 1, E.size(), 0); + if (k > cnt) { + k -= cnt; + } else { + int x = O.get(i); + O.remove(i); + ans.add(x); + ans.addAll(buildPermutation(0, O, E, k, d)); + return ans; + } + i++; + } + } else { + ArrayList U = new ArrayList<>(); + U.addAll(O); + U.addAll(E); + s.sortList(U); + int i = 0; + while (i < U.size()) { + int x = U.get(i); + if (O.contains(x)) { + long cnt = d.compute(O.size() - 1, E.size(), 0); + if (k > cnt) { + k -= cnt; + } else { + int idx = O.indexOf(x); + O.remove(idx); + ans.add(x); + ans.addAll(buildPermutation(0, O, E, k, d)); + return ans; + } + } else { + long cnt = d.compute(O.size(), E.size() - 1, 1); + if (k > cnt) { + k -= cnt; + } else { + int idx = E.indexOf(x); + E.remove(idx); + ans.add(x); + ans.addAll(buildPermutation(1, O, E, k, d)); + return ans; + } + } + i++; + } + } + return ans; + } +} + +class Solution { + public int[] permute(int n, long k) { + int o = (n + 1) / 2, e = n / 2; + ArrayList O = new ArrayList<>(), E = new ArrayList<>(); + + for (int i = 1; i <= n; i++) { + if (i % 2 == 1) O.add(i); + else E.add(i); + } + + SortHelper s = new SortHelper(); + s.sortList(O); + s.sortList(E); + + DPHelper d = new DPHelper(); + PermutationHelper ph = new PermutationHelper(); + + long tot = 0; + if (n % 2 == 1) tot = d.compute(O.size() - 1, E.size(), 0) * O.size(); + else tot = d.compute(O.size() - 1, E.size(), 0) * O.size() + d.compute(O.size(), E.size() - 1, 1) * E.size(); + + if (k > tot) return new int[0]; + + List res = ph.alternateFormation(O, E, k, d, n, s); + int[] ans = new int[res.size()]; + for (int i = 0; i < res.size(); i++) ans[i] = res.get(i); + + return ans; + } +} ``` #### C++ ```cpp - +class Solution { + long long f[105]; +public: + vector permute(int n, long long k) { + int i,j; + for(i=f[0]=1;i<=n;i++)if(f[i-1]>=k)f[i]=k; + else f[i]=f[i-1]*(i+1>>1); + if(!(n&1))f[n]*=2; + if(f[n] ans(n),a[2]; + for(i=0;i= k { + f[i] = k + } else { + f[i] = f[i-1] * int64((i+1)>>1) + } + } + if n%2 == 0 { + f[n] *= 2 + } + if f[n] < k { + return []int{} + } + k-- + ans := make([]int, n) + a := [2][]int{} + for i := 0; i < n; i++ { + a[i&1] = append(a[i&1], i) + } + + if n%2 == 1 { + ans[0] = int(k/f[n-1]) * 2 + k -= int64(ans[0]/2) * f[n-1] + } else { + ans[0] = int(k / f[n-1]) + k -= int64(ans[0]) * f[n-1] + } + + index := sort.SearchInts(a[ans[0]&1], ans[0]) + a[ans[0]&1] = append(a[ans[0]&1][:index], a[ans[0]&1][index+1:]...) + + for i := 1; i < n; i++ { + if n%2 == 1 { + ans[i] = a[i&1][k/f[n-i-1]] + } else { + ans[i] = a[(ans[0]^i)&1][k/f[n-i-1]] + } + k %= f[n-i-1] + + index = sort.SearchInts(a[ans[i]&1], ans[i]) + a[ans[i]&1] = append(a[ans[i]&1][:index], a[ans[i]&1][index+1:]...) + } + + for i := 0; i < n; i++ { + ans[i]++ + } + return ans +} ``` From 4f2027d737c311654ce90f47bab063aa305d1088 Mon Sep 17 00:00:00 2001 From: Samarth Swami <70163465+samarthswami1016@users.noreply.github.com> Date: Tue, 11 Mar 2025 19:20:59 +0530 Subject: [PATCH 2/6] Update README_EN.md --- .../3470.Permutations IV/README_EN.md | 324 +++++++++++++++++- 1 file changed, 320 insertions(+), 4 deletions(-) diff --git a/solution/3400-3499/3470.Permutations IV/README_EN.md b/solution/3400-3499/3470.Permutations IV/README_EN.md index 012612842b115..41f4654168597 100644 --- a/solution/3400-3499/3470.Permutations IV/README_EN.md +++ b/solution/3400-3499/3470.Permutations IV/README_EN.md @@ -108,25 +108,341 @@ tags: #### Python3 ```python - +from typing import List +from math import factorial +import heapq + +class Solution: + def permute(self, xxy: int, yyz: int) -> List[int]: + + kasu = {} + nnss = [] + majs = [] + ajwi = heapq.heappush + laoq = [] + + zzp = [i for i in range(1, xxy + 1) if i % 2 == 1] + zzq = [i for i in range(1, xxy + 1) if i % 2 == 0] + + ppp = [] + nxr = None + + for pps in range(xxy): + if pps == 0: + cnd = sorted(zzp + zzq) + else: + cnd = zzp if nxr == 1 else zzq + + fff = False + for cndt in cnd: + if cndt % 2 == 1: + nxt = 0 + noo = len(zzp) - 1 + nee = len(zzq) + else: + nxt = 1 + noo = len(zzp) + nee = len(zzq) - 1 + + llq = noo + nee + if llq == 0: + cnt = 1 + else: + if nxt == 1: + if noo != (llq + 1) // 2 or nee != llq // 2: + cnt = 0 + else: + cnt = factorial(noo) * factorial(nee) + else: + if nee != (llq + 1) // 2 or noo != llq // 2: + cnt = 0 + else: + cnt = factorial(noo) * factorial(nee) + + ajwi(nnss, cnt) + ajwi(majs, llq) + + if cnt >= yyz: + ppp.append(cndt) + if cndt % 2 == 1: + zzp.remove(cndt) + nxr = 0 + else: + zzq.remove(cndt) + nxr = 1 + fff = True + break + else: + yyz -= cnt + + ajwi(laoq, len(ppp)) + + if not fff: + return [] + return ppp ``` #### Java ```java - +import java.util.*; + +class DPHelper { + static final long ok = 10000000000000000L; + long[][][] dp = new long[101][101][2]; + boolean[][][] vis = new boolean[101][101][2]; + + long compute(int o, int e, int p) { + if (o == 0 && e == 0) return 1; + if (vis[o][e][p]) return dp[o][e][p]; + + long r = 0; + if (p == 1) { + if (o == 0) r = 0; + else r = o * compute(o - 1, e, 0); + } else { + if (e == 0) r = 0; + else r = e * compute(o, e - 1, 1); + } + + if (r > ok) r = ok; + vis[o][e][p] = true; + dp[o][e][p] = r; + return r; + } +} + +class SortHelper { + void sortList(ArrayList list) { + Collections.sort(list); + } +} + +class PermutationHelper { + List buildPermutation(int p, ArrayList O, ArrayList E, long k, DPHelper d) { + List ans = new ArrayList<>(); + if (O.size() + E.size() == 0) return ans; + int i = 0; + + if (p == 1) { + while (i < O.size()) { + long cnt = d.compute(O.size() - 1, E.size(), 0); + if (k > cnt) { + k -= cnt; + i++; + } else { + int x = O.get(i); + O.remove(i); + ans.add(x); + ans.addAll(buildPermutation(0, O, E, k, d)); + return ans; + } + } + } else { + while (i < E.size()) { + long cnt = d.compute(O.size(), E.size() - 1, 1); + if (k > cnt) { + k -= cnt; + i++; + } else { + int x = E.get(i); + E.remove(i); + ans.add(x); + ans.addAll(buildPermutation(1, O, E, k, d)); + return ans; + } + } + } + return ans; + } + + List alternateFormation(ArrayList O, ArrayList E, long k, DPHelper d, int n, SortHelper s) { + List ans = new ArrayList<>(); + int tot = O.size() + E.size(); + if (tot % 2 == 1) { + int i = 0; + while (i < O.size()) { + long cnt = d.compute(O.size() - 1, E.size(), 0); + if (k > cnt) { + k -= cnt; + } else { + int x = O.get(i); + O.remove(i); + ans.add(x); + ans.addAll(buildPermutation(0, O, E, k, d)); + return ans; + } + i++; + } + } else { + ArrayList U = new ArrayList<>(); + U.addAll(O); + U.addAll(E); + s.sortList(U); + int i = 0; + while (i < U.size()) { + int x = U.get(i); + if (O.contains(x)) { + long cnt = d.compute(O.size() - 1, E.size(), 0); + if (k > cnt) { + k -= cnt; + } else { + int idx = O.indexOf(x); + O.remove(idx); + ans.add(x); + ans.addAll(buildPermutation(0, O, E, k, d)); + return ans; + } + } else { + long cnt = d.compute(O.size(), E.size() - 1, 1); + if (k > cnt) { + k -= cnt; + } else { + int idx = E.indexOf(x); + E.remove(idx); + ans.add(x); + ans.addAll(buildPermutation(1, O, E, k, d)); + return ans; + } + } + i++; + } + } + return ans; + } +} + +class Solution { + public int[] permute(int n, long k) { + int o = (n + 1) / 2, e = n / 2; + ArrayList O = new ArrayList<>(), E = new ArrayList<>(); + + for (int i = 1; i <= n; i++) { + if (i % 2 == 1) O.add(i); + else E.add(i); + } + + SortHelper s = new SortHelper(); + s.sortList(O); + s.sortList(E); + + DPHelper d = new DPHelper(); + PermutationHelper ph = new PermutationHelper(); + + long tot = 0; + if (n % 2 == 1) tot = d.compute(O.size() - 1, E.size(), 0) * O.size(); + else tot = d.compute(O.size() - 1, E.size(), 0) * O.size() + d.compute(O.size(), E.size() - 1, 1) * E.size(); + + if (k > tot) return new int[0]; + + List res = ph.alternateFormation(O, E, k, d, n, s); + int[] ans = new int[res.size()]; + for (int i = 0; i < res.size(); i++) ans[i] = res.get(i); + + return ans; + } +} ``` #### C++ ```cpp - +class Solution { + long long f[105]; +public: + vector permute(int n, long long k) { + int i,j; + for(i=f[0]=1;i<=n;i++)if(f[i-1]>=k)f[i]=k; + else f[i]=f[i-1]*(i+1>>1); + if(!(n&1))f[n]*=2; + if(f[n] ans(n),a[2]; + for(i=0;i= k { + f[i] = k + } else { + f[i] = f[i-1] * int64((i+1)>>1) + } + } + if n%2 == 0 { + f[n] *= 2 + } + if f[n] < k { + return []int{} + } + k-- + ans := make([]int, n) + a := [2][]int{} + for i := 0; i < n; i++ { + a[i&1] = append(a[i&1], i) + } + + if n%2 == 1 { + ans[0] = int(k/f[n-1]) * 2 + k -= int64(ans[0]/2) * f[n-1] + } else { + ans[0] = int(k / f[n-1]) + k -= int64(ans[0]) * f[n-1] + } + + index := sort.SearchInts(a[ans[0]&1], ans[0]) + a[ans[0]&1] = append(a[ans[0]&1][:index], a[ans[0]&1][index+1:]...) + + for i := 1; i < n; i++ { + if n%2 == 1 { + ans[i] = a[i&1][k/f[n-i-1]] + } else { + ans[i] = a[(ans[0]^i)&1][k/f[n-i-1]] + } + k %= f[n-i-1] + + index = sort.SearchInts(a[ans[i]&1], ans[i]) + a[ans[i]&1] = append(a[ans[i]&1][:index], a[ans[i]&1][index+1:]...) + } + + for i := 0; i < n; i++ { + ans[i]++ + } + return ans +} ``` From 44776a5ace7e074c5c43958814f5b6e8d27ec164 Mon Sep 17 00:00:00 2001 From: Samarth Swami <70163465+samarthswami1016@users.noreply.github.com> Date: Tue, 11 Mar 2025 19:21:36 +0530 Subject: [PATCH 3/6] Add files via upload --- .../3470.Permutations IV/solution.cpp | 40 +++++ .../3470.Permutations IV/solution.go | 51 ++++++ .../3470.Permutations IV/solution.java | 157 ++++++++++++++++++ .../3470.Permutations IV/solution.py | 72 ++++++++ 4 files changed, 320 insertions(+) create mode 100644 solution/3400-3499/3470.Permutations IV/solution.cpp create mode 100644 solution/3400-3499/3470.Permutations IV/solution.go create mode 100644 solution/3400-3499/3470.Permutations IV/solution.java create mode 100644 solution/3400-3499/3470.Permutations IV/solution.py diff --git a/solution/3400-3499/3470.Permutations IV/solution.cpp b/solution/3400-3499/3470.Permutations IV/solution.cpp new file mode 100644 index 0000000000000..d4141209a683e --- /dev/null +++ b/solution/3400-3499/3470.Permutations IV/solution.cpp @@ -0,0 +1,40 @@ +class Solution { + long long f[105]; +public: + vector permute(int n, long long k) { + int i,j; + for(i=f[0]=1;i<=n;i++)if(f[i-1]>=k)f[i]=k; + else f[i]=f[i-1]*(i+1>>1); + if(!(n&1))f[n]*=2; + if(f[n] ans(n),a[2]; + for(i=0;i= k { + f[i] = k + } else { + f[i] = f[i-1] * int64((i+1)>>1) + } + } + if n%2 == 0 { + f[n] *= 2 + } + if f[n] < k { + return []int{} + } + k-- + ans := make([]int, n) + a := [2][]int{} + for i := 0; i < n; i++ { + a[i&1] = append(a[i&1], i) + } + + if n%2 == 1 { + ans[0] = int(k/f[n-1]) * 2 + k -= int64(ans[0]/2) * f[n-1] + } else { + ans[0] = int(k / f[n-1]) + k -= int64(ans[0]) * f[n-1] + } + + index := sort.SearchInts(a[ans[0]&1], ans[0]) + a[ans[0]&1] = append(a[ans[0]&1][:index], a[ans[0]&1][index+1:]...) + + for i := 1; i < n; i++ { + if n%2 == 1 { + ans[i] = a[i&1][k/f[n-i-1]] + } else { + ans[i] = a[(ans[0]^i)&1][k/f[n-i-1]] + } + k %= f[n-i-1] + + index = sort.SearchInts(a[ans[i]&1], ans[i]) + a[ans[i]&1] = append(a[ans[i]&1][:index], a[ans[i]&1][index+1:]...) + } + + for i := 0; i < n; i++ { + ans[i]++ + } + return ans +} \ No newline at end of file diff --git a/solution/3400-3499/3470.Permutations IV/solution.java b/solution/3400-3499/3470.Permutations IV/solution.java new file mode 100644 index 0000000000000..5bb48f9c0010d --- /dev/null +++ b/solution/3400-3499/3470.Permutations IV/solution.java @@ -0,0 +1,157 @@ +import java.util.*; + +class DPHelper { + static final long ok = 10000000000000000L; + long[][][] dp = new long[101][101][2]; + boolean[][][] vis = new boolean[101][101][2]; + + long compute(int o, int e, int p) { + if (o == 0 && e == 0) return 1; + if (vis[o][e][p]) return dp[o][e][p]; + + long r = 0; + if (p == 1) { + if (o == 0) r = 0; + else r = o * compute(o - 1, e, 0); + } else { + if (e == 0) r = 0; + else r = e * compute(o, e - 1, 1); + } + + if (r > ok) r = ok; + vis[o][e][p] = true; + dp[o][e][p] = r; + return r; + } +} + +class SortHelper { + void sortList(ArrayList list) { + Collections.sort(list); + } +} + +class PermutationHelper { + List buildPermutation(int p, ArrayList O, ArrayList E, long k, DPHelper d) { + List ans = new ArrayList<>(); + if (O.size() + E.size() == 0) return ans; + int i = 0; + + if (p == 1) { + while (i < O.size()) { + long cnt = d.compute(O.size() - 1, E.size(), 0); + if (k > cnt) { + k -= cnt; + i++; + } else { + int x = O.get(i); + O.remove(i); + ans.add(x); + ans.addAll(buildPermutation(0, O, E, k, d)); + return ans; + } + } + } else { + while (i < E.size()) { + long cnt = d.compute(O.size(), E.size() - 1, 1); + if (k > cnt) { + k -= cnt; + i++; + } else { + int x = E.get(i); + E.remove(i); + ans.add(x); + ans.addAll(buildPermutation(1, O, E, k, d)); + return ans; + } + } + } + return ans; + } + + List alternateFormation(ArrayList O, ArrayList E, long k, DPHelper d, int n, SortHelper s) { + List ans = new ArrayList<>(); + int tot = O.size() + E.size(); + if (tot % 2 == 1) { + int i = 0; + while (i < O.size()) { + long cnt = d.compute(O.size() - 1, E.size(), 0); + if (k > cnt) { + k -= cnt; + } else { + int x = O.get(i); + O.remove(i); + ans.add(x); + ans.addAll(buildPermutation(0, O, E, k, d)); + return ans; + } + i++; + } + } else { + ArrayList U = new ArrayList<>(); + U.addAll(O); + U.addAll(E); + s.sortList(U); + int i = 0; + while (i < U.size()) { + int x = U.get(i); + if (O.contains(x)) { + long cnt = d.compute(O.size() - 1, E.size(), 0); + if (k > cnt) { + k -= cnt; + } else { + int idx = O.indexOf(x); + O.remove(idx); + ans.add(x); + ans.addAll(buildPermutation(0, O, E, k, d)); + return ans; + } + } else { + long cnt = d.compute(O.size(), E.size() - 1, 1); + if (k > cnt) { + k -= cnt; + } else { + int idx = E.indexOf(x); + E.remove(idx); + ans.add(x); + ans.addAll(buildPermutation(1, O, E, k, d)); + return ans; + } + } + i++; + } + } + return ans; + } +} + +class Solution { + public int[] permute(int n, long k) { + int o = (n + 1) / 2, e = n / 2; + ArrayList O = new ArrayList<>(), E = new ArrayList<>(); + + for (int i = 1; i <= n; i++) { + if (i % 2 == 1) O.add(i); + else E.add(i); + } + + SortHelper s = new SortHelper(); + s.sortList(O); + s.sortList(E); + + DPHelper d = new DPHelper(); + PermutationHelper ph = new PermutationHelper(); + + long tot = 0; + if (n % 2 == 1) tot = d.compute(O.size() - 1, E.size(), 0) * O.size(); + else tot = d.compute(O.size() - 1, E.size(), 0) * O.size() + d.compute(O.size(), E.size() - 1, 1) * E.size(); + + if (k > tot) return new int[0]; + + List res = ph.alternateFormation(O, E, k, d, n, s); + int[] ans = new int[res.size()]; + for (int i = 0; i < res.size(); i++) ans[i] = res.get(i); + + return ans; + } +} \ No newline at end of file diff --git a/solution/3400-3499/3470.Permutations IV/solution.py b/solution/3400-3499/3470.Permutations IV/solution.py new file mode 100644 index 0000000000000..d1f02ccafb6d6 --- /dev/null +++ b/solution/3400-3499/3470.Permutations IV/solution.py @@ -0,0 +1,72 @@ +from typing import List +from math import factorial +import heapq + +class Solution: + def permute(self, xxy: int, yyz: int) -> List[int]: + + kasu = {} + nnss = [] + majs = [] + ajwi = heapq.heappush + laoq = [] + + zzp = [i for i in range(1, xxy + 1) if i % 2 == 1] + zzq = [i for i in range(1, xxy + 1) if i % 2 == 0] + + ppp = [] + nxr = None + + for pps in range(xxy): + if pps == 0: + cnd = sorted(zzp + zzq) + else: + cnd = zzp if nxr == 1 else zzq + + fff = False + for cndt in cnd: + if cndt % 2 == 1: + nxt = 0 + noo = len(zzp) - 1 + nee = len(zzq) + else: + nxt = 1 + noo = len(zzp) + nee = len(zzq) - 1 + + llq = noo + nee + if llq == 0: + cnt = 1 + else: + if nxt == 1: + if noo != (llq + 1) // 2 or nee != llq // 2: + cnt = 0 + else: + cnt = factorial(noo) * factorial(nee) + else: + if nee != (llq + 1) // 2 or noo != llq // 2: + cnt = 0 + else: + cnt = factorial(noo) * factorial(nee) + + ajwi(nnss, cnt) + ajwi(majs, llq) + + if cnt >= yyz: + ppp.append(cndt) + if cndt % 2 == 1: + zzp.remove(cndt) + nxr = 0 + else: + zzq.remove(cndt) + nxr = 1 + fff = True + break + else: + yyz -= cnt + + ajwi(laoq, len(ppp)) + + if not fff: + return [] + return ppp \ No newline at end of file From cecfdab4d9379ef0cd9891f292c87d9aa8323ff1 Mon Sep 17 00:00:00 2001 From: samarthswami1016 <70163465+samarthswami1016@users.noreply.github.com> Date: Tue, 11 Mar 2025 14:39:51 +0000 Subject: [PATCH 4/6] style: format code and docs with prettier --- .../3400-3499/3470.Permutations IV/README.md | 18 +++++++++--------- .../3470.Permutations IV/README_EN.md | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/solution/3400-3499/3470.Permutations IV/README.md b/solution/3400-3499/3470.Permutations IV/README.md index 19d074f0d1f3e..f530d2870c0c7 100644 --- a/solution/3400-3499/3470.Permutations IV/README.md +++ b/solution/3400-3499/3470.Permutations IV/README.md @@ -117,18 +117,18 @@ import heapq class Solution: def permute(self, xxy: int, yyz: int) -> List[int]: - - kasu = {} + + kasu = {} nnss = [] majs = [] ajwi = heapq.heappush laoq = [] - + zzp = [i for i in range(1, xxy + 1) if i % 2 == 1] zzq = [i for i in range(1, xxy + 1) if i % 2 == 0] ppp = [] - nxr = None + nxr = None for pps in range(xxy): if pps == 0: @@ -139,11 +139,11 @@ class Solution: fff = False for cndt in cnd: if cndt % 2 == 1: - nxt = 0 + nxt = 0 noo = len(zzp) - 1 nee = len(zzq) else: - nxt = 1 + nxt = 1 noo = len(zzp) nee = len(zzq) - 1 @@ -164,7 +164,7 @@ class Solution: ajwi(nnss, cnt) ajwi(majs, llq) - + if cnt >= yyz: ppp.append(cndt) if cndt % 2 == 1: @@ -177,9 +177,9 @@ class Solution: break else: yyz -= cnt - + ajwi(laoq, len(ppp)) - + if not fff: return [] return ppp diff --git a/solution/3400-3499/3470.Permutations IV/README_EN.md b/solution/3400-3499/3470.Permutations IV/README_EN.md index 41f4654168597..e9907de529850 100644 --- a/solution/3400-3499/3470.Permutations IV/README_EN.md +++ b/solution/3400-3499/3470.Permutations IV/README_EN.md @@ -114,18 +114,18 @@ import heapq class Solution: def permute(self, xxy: int, yyz: int) -> List[int]: - - kasu = {} + + kasu = {} nnss = [] majs = [] ajwi = heapq.heappush laoq = [] - + zzp = [i for i in range(1, xxy + 1) if i % 2 == 1] zzq = [i for i in range(1, xxy + 1) if i % 2 == 0] ppp = [] - nxr = None + nxr = None for pps in range(xxy): if pps == 0: @@ -136,11 +136,11 @@ class Solution: fff = False for cndt in cnd: if cndt % 2 == 1: - nxt = 0 + nxt = 0 noo = len(zzp) - 1 nee = len(zzq) else: - nxt = 1 + nxt = 1 noo = len(zzp) nee = len(zzq) - 1 @@ -161,7 +161,7 @@ class Solution: ajwi(nnss, cnt) ajwi(majs, llq) - + if cnt >= yyz: ppp.append(cndt) if cndt % 2 == 1: @@ -174,9 +174,9 @@ class Solution: break else: yyz -= cnt - + ajwi(laoq, len(ppp)) - + if not fff: return [] return ppp From 202e12030d86af9943874731fba7a407091da28d Mon Sep 17 00:00:00 2001 From: Samarth Swami <70163465+samarthswami1016@users.noreply.github.com> Date: Tue, 11 Mar 2025 23:35:30 +0530 Subject: [PATCH 5/6] Update README.md --- solution/3400-3499/3470.Permutations IV/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/solution/3400-3499/3470.Permutations IV/README.md b/solution/3400-3499/3470.Permutations IV/README.md index f530d2870c0c7..d79bd31997017 100644 --- a/solution/3400-3499/3470.Permutations IV/README.md +++ b/solution/3400-3499/3470.Permutations IV/README.md @@ -111,6 +111,7 @@ tags: #### Python3 ```python + from typing import List from math import factorial import heapq @@ -183,11 +184,13 @@ class Solution: if not fff: return [] return ppp + ``` #### Java ```java + import java.util.*; class DPHelper { @@ -345,11 +348,13 @@ class Solution { return ans; } } + ``` #### C++ ```cpp + class Solution { long long f[105]; public: @@ -390,11 +395,13 @@ public: return ans; } }; + ``` #### Go ```go + func permute(n int, k int64) []int { var f [105]int64 f[0] = 1 @@ -446,6 +453,7 @@ func permute(n int, k int64) []int { } return ans } + ``` From cb369826e65d6f36840194d8fdadb3100620295f Mon Sep 17 00:00:00 2001 From: Samarth Swami <70163465+samarthswami1016@users.noreply.github.com> Date: Tue, 11 Mar 2025 23:36:21 +0530 Subject: [PATCH 6/6] Update README_EN.md --- solution/3400-3499/3470.Permutations IV/README_EN.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/solution/3400-3499/3470.Permutations IV/README_EN.md b/solution/3400-3499/3470.Permutations IV/README_EN.md index e9907de529850..970a3101aa776 100644 --- a/solution/3400-3499/3470.Permutations IV/README_EN.md +++ b/solution/3400-3499/3470.Permutations IV/README_EN.md @@ -108,6 +108,7 @@ tags: #### Python3 ```python + from typing import List from math import factorial import heapq @@ -180,11 +181,13 @@ class Solution: if not fff: return [] return ppp + ``` #### Java ```java + import java.util.*; class DPHelper { @@ -342,11 +345,13 @@ class Solution { return ans; } } + ``` #### C++ ```cpp + class Solution { long long f[105]; public: @@ -387,11 +392,13 @@ public: return ans; } }; + ``` #### Go ```go + func permute(n int, k int64) []int { var f [105]int64 f[0] = 1 @@ -443,6 +450,7 @@ func permute(n int, k int64) []int { } return ans } + ```