From 84882e24f80f9865ddac8d6837a9de27e5583acd Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 23 Jul 2024 08:43:13 +0800 Subject: [PATCH 1/4] feat: update lc problems --- .../README.md" | 8 +- .../README.md | 17 ++-- .../README_EN.md | 17 ++-- .../Solution.ts | 9 +-- .../README.md | 47 +++++++++-- .../README_EN.md | 62 +++++++++++++-- .../Solution.cpp | 2 +- .../Solution.go | 2 +- .../Solution.java | 4 +- .../Solution.py | 2 +- .../Solution.ts | 32 ++++++++ .../README.md | 74 ++++++++++------- .../README_EN.md | 74 ++++++++++------- .../Solution.cpp | 18 +++-- .../Solution.go | 11 +-- .../Solution.java | 18 ++--- .../Solution.py | 5 +- .../Solution.ts | 9 +++ .../3220.Odd and Even Transactions/README.md | 2 + .../README_EN.md | 2 + .../README.md | 5 ++ .../README_EN.md | 5 ++ .../README.md | 4 + .../README_EN.md | 4 + .../README.md | 4 + .../README_EN.md | 4 + .../README.md | 4 + .../README_EN.md | 4 + .../README.md | 5 ++ .../README_EN.md | 5 ++ .../README.md | 2 + .../README_EN.md | 2 + .../3227.Vowels Game in a String/README.md | 5 ++ .../3227.Vowels Game in a String/README_EN.md | 5 ++ .../README.md | 4 + .../README_EN.md | 4 + .../README.md | 6 ++ .../README_EN.md | 6 ++ .../README.md | 79 ++++++++++--------- .../README_EN.md | 2 + solution/DATABASE_README.md | 4 +- solution/DATABASE_README_EN.md | 4 +- solution/README.md | 22 +++--- solution/README_EN.md | 22 +++--- 44 files changed, 442 insertions(+), 184 deletions(-) create mode 100644 solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.ts create mode 100644 solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.ts diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 073. \347\213\222\347\213\222\345\220\203\351\246\231\350\225\211/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 073. \347\213\222\347\213\222\345\220\203\351\246\231\350\225\211/README.md" index 1dc1296f71cdc..bd642ce6f118e 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 073. \347\213\222\347\213\222\345\220\203\351\246\231\350\225\211/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 073. \347\213\222\347\213\222\345\220\203\351\246\231\350\225\211/README.md" @@ -189,22 +189,22 @@ class Solution { func minEatingSpeed(_ piles: [Int], _ h: Int) -> Int { var left = 1 var right = piles.max() ?? 0 - + while left < right { let mid = (left + right) / 2 var hours = 0 - + for pile in piles { hours += (pile + mid - 1) / mid } - + if hours <= h { right = mid } else { left = mid + 1 } } - + return left } } diff --git a/solution/2000-2099/2095.Delete the Middle Node of a Linked List/README.md b/solution/2000-2099/2095.Delete the Middle Node of a Linked List/README.md index 75130395fb9d6..6cacbc5c91348 100644 --- a/solution/2000-2099/2095.Delete the Middle Node of a Linked List/README.md +++ b/solution/2000-2099/2095.Delete the Middle Node of a Linked List/README.md @@ -81,7 +81,13 @@ tags: -### 方法一 +### 方法一:快慢指针 + +快慢指针法是一种用于解决链表中的问题的常用技巧。我们可以维护两个指针,一个慢指针 $\textit{slow}$ 和一个快指针 $\textit{fast}$。初始时 $\textit{slow}$ 指向一个虚拟节点,该虚拟节点的 $\textit{next}$ 指针指向链表的头节点 $\textit{head}$,而 $\textit{fast}$ 指向链表的头节点 $\textit{head}$。 + +然后,我们每次将慢指针向后移动一个位置,将快指针向后移动两个位置,直到快指针到达链表的末尾。此时,慢指针指向的节点的下一个节点就是链表的中间节点。我们将慢指针指向的节点的 $\textit{next}$ 指针指向下下个节点,即可删除中间节点。 + +时间复杂度 $O(n)$,其中 $n$ 是链表的长度。空间复杂度 $O(1)$。 @@ -197,15 +203,14 @@ func deleteMiddle(head *ListNode) *ListNode { */ function deleteMiddle(head: ListNode | null): ListNode | null { - if (!head || !head.next) return null; - let fast = head.next, - slow = head; - while (fast.next && fast.next.next) { + const dummy = new ListNode(0, head); + let [slow, fast] = [dummy, head]; + while (fast && fast.next) { slow = slow.next; fast = fast.next.next; } slow.next = slow.next.next; - return head; + return dummy.next; } ``` diff --git a/solution/2000-2099/2095.Delete the Middle Node of a Linked List/README_EN.md b/solution/2000-2099/2095.Delete the Middle Node of a Linked List/README_EN.md index 3f244977be916..4bef07b444f84 100644 --- a/solution/2000-2099/2095.Delete the Middle Node of a Linked List/README_EN.md +++ b/solution/2000-2099/2095.Delete the Middle Node of a Linked List/README_EN.md @@ -73,7 +73,13 @@ Node 0 with value 2 is the only node remaining after removing node 1. -### Solution 1 +### Solution 1: Fast and Slow Pointers + +The fast and slow pointer technique is a common method used to solve problems related to linked lists. We can maintain two pointers, a slow pointer $\textit{slow}$ and a fast pointer $\textit{fast}$. Initially, $\textit{slow}$ points to a dummy node, whose $\textit{next}$ pointer points to the head node $\textit{head}$ of the list, while $\textit{fast}$ points to the head node $\textit{head}$. + +Then, we move the slow pointer one position backward and the fast pointer two positions backward each time, until the fast pointer reaches the end of the list. At this point, the node next to the node pointed by the slow pointer is the middle node of the list. We can remove the middle node by setting the $\textit{next}$ pointer of the node pointed by the slow pointer to point to the next next node. + +The time complexity is $O(n)$, where $n$ is the length of the list. The space complexity is $O(1)$. @@ -189,15 +195,14 @@ func deleteMiddle(head *ListNode) *ListNode { */ function deleteMiddle(head: ListNode | null): ListNode | null { - if (!head || !head.next) return null; - let fast = head.next, - slow = head; - while (fast.next && fast.next.next) { + const dummy = new ListNode(0, head); + let [slow, fast] = [dummy, head]; + while (fast && fast.next) { slow = slow.next; fast = fast.next.next; } slow.next = slow.next.next; - return head; + return dummy.next; } ``` diff --git a/solution/2000-2099/2095.Delete the Middle Node of a Linked List/Solution.ts b/solution/2000-2099/2095.Delete the Middle Node of a Linked List/Solution.ts index 6b62c29710025..28c984b082d62 100644 --- a/solution/2000-2099/2095.Delete the Middle Node of a Linked List/Solution.ts +++ b/solution/2000-2099/2095.Delete the Middle Node of a Linked List/Solution.ts @@ -11,13 +11,12 @@ */ function deleteMiddle(head: ListNode | null): ListNode | null { - if (!head || !head.next) return null; - let fast = head.next, - slow = head; - while (fast.next && fast.next.next) { + const dummy = new ListNode(0, head); + let [slow, fast] = [dummy, head]; + while (fast && fast.next) { slow = slow.next; fast = fast.next.next; } slow.next = slow.next.next; - return head; + return dummy.next; } diff --git a/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/README.md b/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/README.md index 3dc021a1729e5..0bd10f01d81b3 100644 --- a/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/README.md +++ b/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/README.md @@ -107,7 +107,7 @@ class Solution: else: mi1 = x ans = max(ans - mi1 + mx1, ans - mi2 + mx2, -1) - return -1 if ans % 2 else ans + return -1 if ans < 0 else ans ``` #### Java @@ -141,8 +141,8 @@ class Solution { mi1 = nums[i]; } } - ans = Math.max(-1, Math.max(ans - mi1 + mx1, ans - mi2 + mx2)); - return ans % 2 != 0 ? -1 : ans; + ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2); + return ans < 0 ? -1 : ans; } } ``` @@ -180,7 +180,7 @@ public: } } ans = max(ans - mi1 + mx1, ans - mi2 + mx2); - return ans % 2 || ans < 0 ? -1 : ans; + return ans < 0 ? -1 : ans; } }; ``` @@ -216,13 +216,50 @@ func largestEvenSum(nums []int, k int) int64 { } } ans = max(-1, max(ans-mi1+mx1, ans-mi2+mx2)) - if ans%2 != 0 { + if ans%2 < 0 { return -1 } return int64(ans) } ``` +#### TypeScript + +```ts +function largestEvenSum(nums: number[], k: number): number { + nums.sort((a, b) => a - b); + let ans = 0; + const n = nums.length; + for (let i = 0; i < k; ++i) { + ans += nums[n - i - 1]; + } + if (ans % 2 === 0) { + return ans; + } + const inf = 1 << 29; + let mx1 = -inf, + mx2 = -inf; + for (let i = 0; i < n - k; ++i) { + if (nums[i] % 2 === 1) { + mx1 = nums[i]; + } else { + mx2 = nums[i]; + } + } + let mi1 = inf, + mi2 = inf; + for (let i = n - 1; i >= n - k; --i) { + if (nums[i] % 2 === 1) { + mi2 = nums[i]; + } else { + mi1 = nums[i]; + } + } + ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2); + return ans < 0 ? -1 : ans; +} +``` + diff --git a/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/README_EN.md b/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/README_EN.md index 9090b2463b126..2538ca9cc7aad 100644 --- a/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/README_EN.md +++ b/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/README_EN.md @@ -67,7 +67,20 @@ No subsequence of nums with length 1 has an even sum. -### Solution 1 +### Solution 1: Greedy + Sorting + +We notice that the problem involves selecting a subsequence, so we can consider sorting the array first. + +Next, we greedily select the largest $k$ numbers. If the sum of these numbers is even, we directly return this sum $ans$. + +Otherwise, we have two greedy strategies: + +1. Among the largest $k$ numbers, find the smallest even number $mi1$, and then among the remaining $n - k$ numbers, find the largest odd number $mx1$. Replace $mi1$ with $mx1$. If such a replacement exists, then the sum after replacement $ans - mi1 + mx1$ is guaranteed to be even; +2. Among the largest $k$ numbers, find the smallest odd number $mi2$, and then among the remaining $n - k$ numbers, find the largest even number $mx2$. Replace $mi2$ with $mx2$. If such a replacement exists, then the sum after replacement $ans - mi2 + mx2$ is guaranteed to be even. + +We take the largest even sum as the answer. If no even sum exists, return $-1$. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array. @@ -94,7 +107,7 @@ class Solution: else: mi1 = x ans = max(ans - mi1 + mx1, ans - mi2 + mx2, -1) - return -1 if ans % 2 else ans + return -1 if ans < 0 else ans ``` #### Java @@ -128,8 +141,8 @@ class Solution { mi1 = nums[i]; } } - ans = Math.max(-1, Math.max(ans - mi1 + mx1, ans - mi2 + mx2)); - return ans % 2 != 0 ? -1 : ans; + ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2); + return ans < 0 ? -1 : ans; } } ``` @@ -167,7 +180,7 @@ public: } } ans = max(ans - mi1 + mx1, ans - mi2 + mx2); - return ans % 2 || ans < 0 ? -1 : ans; + return ans < 0 ? -1 : ans; } }; ``` @@ -203,13 +216,50 @@ func largestEvenSum(nums []int, k int) int64 { } } ans = max(-1, max(ans-mi1+mx1, ans-mi2+mx2)) - if ans%2 != 0 { + if ans%2 < 0 { return -1 } return int64(ans) } ``` +#### TypeScript + +```ts +function largestEvenSum(nums: number[], k: number): number { + nums.sort((a, b) => a - b); + let ans = 0; + const n = nums.length; + for (let i = 0; i < k; ++i) { + ans += nums[n - i - 1]; + } + if (ans % 2 === 0) { + return ans; + } + const inf = 1 << 29; + let mx1 = -inf, + mx2 = -inf; + for (let i = 0; i < n - k; ++i) { + if (nums[i] % 2 === 1) { + mx1 = nums[i]; + } else { + mx2 = nums[i]; + } + } + let mi1 = inf, + mi2 = inf; + for (let i = n - 1; i >= n - k; --i) { + if (nums[i] % 2 === 1) { + mi2 = nums[i]; + } else { + mi1 = nums[i]; + } + } + ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2); + return ans < 0 ? -1 : ans; +} +``` + diff --git a/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.cpp b/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.cpp index 7d0894f604888..d280c2090cbfa 100644 --- a/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.cpp +++ b/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.cpp @@ -28,6 +28,6 @@ class Solution { } } ans = max(ans - mi1 + mx1, ans - mi2 + mx2); - return ans % 2 || ans < 0 ? -1 : ans; + return ans < 0 ? -1 : ans; } }; \ No newline at end of file diff --git a/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.go b/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.go index b0319c30d2745..49a2c4f081d11 100644 --- a/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.go +++ b/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.go @@ -26,7 +26,7 @@ func largestEvenSum(nums []int, k int) int64 { } } ans = max(-1, max(ans-mi1+mx1, ans-mi2+mx2)) - if ans%2 != 0 { + if ans%2 < 0 { return -1 } return int64(ans) diff --git a/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.java b/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.java index 4a268601e1490..0cb10b68b372c 100644 --- a/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.java +++ b/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.java @@ -26,7 +26,7 @@ public long largestEvenSum(int[] nums, int k) { mi1 = nums[i]; } } - ans = Math.max(-1, Math.max(ans - mi1 + mx1, ans - mi2 + mx2)); - return ans % 2 != 0 ? -1 : ans; + ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2); + return ans < 0 ? -1 : ans; } } \ No newline at end of file diff --git a/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.py b/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.py index ce4ab3bcc3bbb..483525449645e 100644 --- a/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.py +++ b/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.py @@ -18,4 +18,4 @@ def largestEvenSum(self, nums: List[int], k: int) -> int: else: mi1 = x ans = max(ans - mi1 + mx1, ans - mi2 + mx2, -1) - return -1 if ans % 2 else ans + return -1 if ans < 0 else ans diff --git a/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.ts b/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.ts new file mode 100644 index 0000000000000..60a8d46616d5c --- /dev/null +++ b/solution/2000-2099/2098.Subsequence of Size K With the Largest Even Sum/Solution.ts @@ -0,0 +1,32 @@ +function largestEvenSum(nums: number[], k: number): number { + nums.sort((a, b) => a - b); + let ans = 0; + const n = nums.length; + for (let i = 0; i < k; ++i) { + ans += nums[n - i - 1]; + } + if (ans % 2 === 0) { + return ans; + } + const inf = 1 << 29; + let mx1 = -inf, + mx2 = -inf; + for (let i = 0; i < n - k; ++i) { + if (nums[i] % 2 === 1) { + mx1 = nums[i]; + } else { + mx2 = nums[i]; + } + } + let mi1 = inf, + mi2 = inf; + for (let i = n - 1; i >= n - k; --i) { + if (nums[i] % 2 === 1) { + mi2 = nums[i]; + } else { + mi1 = nums[i]; + } + } + ans = Math.max(ans - mi1 + mx1, ans - mi2 + mx2); + return ans < 0 ? -1 : ans; +} diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README.md b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README.md index f4f49b5bb21a4..e2c7e76b7d29d 100644 --- a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README.md +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README.md @@ -69,7 +69,13 @@ tags: -### 方法一 +### 方法一:排序 + +我们先创建一个索引数组 $\textit{idx}$,数组中的每个元素是数组 $\textit{nums}$ 的下标。然后我们根据数组 $\textit{nums}$ 的值对索引数组 $\textit{idx}$ 进行排序,排序的规则是 $\textit{nums}[i] < \textit{nums}[j]$,其中 $i$ 和 $j$ 是索引数组 $\textit{idx}$ 中的两个下标。 + +排序完成后,我们取索引数组 $\textit{idx}$ 的最后 $k$ 个元素,这 $k$ 个元素对应的就是数组 $\textit{nums}$ 中最大的 $k$ 个元素。然后我们对这 $k$ 个下标进行排序,得到的就是最大的 $k$ 个元素在数组 $\textit{nums}$ 中的顺序。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组的长度。 @@ -78,9 +84,8 @@ tags: ```python class Solution: def maxSubsequence(self, nums: List[int], k: int) -> List[int]: - idx = list(range(len(nums))) - idx.sort(key=lambda i: nums[i]) - return [nums[i] for i in sorted(idx[-k:])] + idx = sorted(range(len(nums)), key=lambda i: nums[i])[-k:] + return [nums[i] for i in sorted(idx)] ``` #### Java @@ -88,20 +93,16 @@ class Solution: ```java class Solution { public int[] maxSubsequence(int[] nums, int k) { - int[] ans = new int[k]; - List idx = new ArrayList<>(); int n = nums.length; + Integer[] idx = new Integer[n]; for (int i = 0; i < n; ++i) { - idx.add(i); - } - idx.sort(Comparator.comparingInt(i -> - nums[i])); - int[] t = new int[k]; - for (int i = 0; i < k; ++i) { - t[i] = idx.get(i); + idx[i] = i; } - Arrays.sort(t); - for (int i = 0; i < k; ++i) { - ans[i] = nums[t[i]]; + Arrays.sort(idx, (i, j) -> nums[i] - nums[j]); + Arrays.sort(idx, n - k, n); + int[] ans = new int[k]; + for (int i = n - k; i < n; ++i) { + ans[i - (n - k)] = nums[idx[i]]; } return ans; } @@ -115,14 +116,16 @@ class Solution { public: vector maxSubsequence(vector& nums, int k) { int n = nums.size(); - vector> vals; - for (int i = 0; i < n; ++i) vals.push_back({i, nums[i]}); - sort(vals.begin(), vals.end(), [&](auto x1, auto x2) { - return x1.second > x2.second; - }); - sort(vals.begin(), vals.begin() + k); - vector ans; - for (int i = 0; i < k; ++i) ans.push_back(vals[i].second); + vector idx(n); + for (int i = 0; i < n; ++i) { + idx[i] = i; + } + sort(idx.begin(), idx.end(), [&](int i, int j) { return nums[i] < nums[j]; }); + sort(idx.begin() + (n - k), idx.end()); + vector ans(k); + for (int i = n - k; i < n; ++i) { + ans[i - (n - k)] = nums[idx[i]]; + } return ans; } }; @@ -132,20 +135,35 @@ public: ```go func maxSubsequence(nums []int, k int) []int { - idx := make([]int, len(nums)) + n := len(nums) + idx := make([]int, n) for i := range idx { idx[i] = i } - sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] > nums[idx[j]] }) - sort.Ints(idx[:k]) + sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] < nums[idx[j]] }) + sort.Ints(idx[n-k:]) ans := make([]int, k) - for i, j := range idx[:k] { - ans[i] = nums[j] + for i := n - k; i < n; i++ { + ans[i-(n-k)] = nums[idx[i]] } return ans } ``` +#### TypeScript + +```ts +function maxSubsequence(nums: number[], k: number): number[] { + const n = nums.length; + const idx: number[] = Array.from({ length: n }, (_, i) => i); + idx.sort((i, j) => nums[i] - nums[j]); + return idx + .slice(n - k) + .sort((i, j) => i - j) + .map(i => nums[i]); +} +``` + diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README_EN.md b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README_EN.md index 93d2dba9f0118..b8f474ff133b9 100644 --- a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README_EN.md +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README_EN.md @@ -70,7 +70,13 @@ Another possible subsequence is [4, 3]. -### Solution 1 +### Solution 1: Sorting + +First, we create an index array $\textit{idx}$, where each element is an index of the array $\textit{nums}$. Then, we sort the index array $\textit{idx}$ based on the values in $\textit{nums}$, with the sorting rule being $\textit{nums}[i] < \textit{nums}[j]$, where $i$ and $j$ are two indices in the index array $\textit{idx}$. + +After sorting, we take the last $k$ elements of the index array $\textit{idx}$. These $k$ elements correspond to the largest $k$ elements in the array $\textit{nums}$. Then, we sort these $k$ indices to get the order of the largest $k$ elements in the array $\textit{nums}$. + +The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array. @@ -79,9 +85,8 @@ Another possible subsequence is [4, 3]. ```python class Solution: def maxSubsequence(self, nums: List[int], k: int) -> List[int]: - idx = list(range(len(nums))) - idx.sort(key=lambda i: nums[i]) - return [nums[i] for i in sorted(idx[-k:])] + idx = sorted(range(len(nums)), key=lambda i: nums[i])[-k:] + return [nums[i] for i in sorted(idx)] ``` #### Java @@ -89,20 +94,16 @@ class Solution: ```java class Solution { public int[] maxSubsequence(int[] nums, int k) { - int[] ans = new int[k]; - List idx = new ArrayList<>(); int n = nums.length; + Integer[] idx = new Integer[n]; for (int i = 0; i < n; ++i) { - idx.add(i); - } - idx.sort(Comparator.comparingInt(i -> - nums[i])); - int[] t = new int[k]; - for (int i = 0; i < k; ++i) { - t[i] = idx.get(i); + idx[i] = i; } - Arrays.sort(t); - for (int i = 0; i < k; ++i) { - ans[i] = nums[t[i]]; + Arrays.sort(idx, (i, j) -> nums[i] - nums[j]); + Arrays.sort(idx, n - k, n); + int[] ans = new int[k]; + for (int i = n - k; i < n; ++i) { + ans[i - (n - k)] = nums[idx[i]]; } return ans; } @@ -116,14 +117,16 @@ class Solution { public: vector maxSubsequence(vector& nums, int k) { int n = nums.size(); - vector> vals; - for (int i = 0; i < n; ++i) vals.push_back({i, nums[i]}); - sort(vals.begin(), vals.end(), [&](auto x1, auto x2) { - return x1.second > x2.second; - }); - sort(vals.begin(), vals.begin() + k); - vector ans; - for (int i = 0; i < k; ++i) ans.push_back(vals[i].second); + vector idx(n); + for (int i = 0; i < n; ++i) { + idx[i] = i; + } + sort(idx.begin(), idx.end(), [&](int i, int j) { return nums[i] < nums[j]; }); + sort(idx.begin() + (n - k), idx.end()); + vector ans(k); + for (int i = n - k; i < n; ++i) { + ans[i - (n - k)] = nums[idx[i]]; + } return ans; } }; @@ -133,20 +136,35 @@ public: ```go func maxSubsequence(nums []int, k int) []int { - idx := make([]int, len(nums)) + n := len(nums) + idx := make([]int, n) for i := range idx { idx[i] = i } - sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] > nums[idx[j]] }) - sort.Ints(idx[:k]) + sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] < nums[idx[j]] }) + sort.Ints(idx[n-k:]) ans := make([]int, k) - for i, j := range idx[:k] { - ans[i] = nums[j] + for i := n - k; i < n; i++ { + ans[i-(n-k)] = nums[idx[i]] } return ans } ``` +#### TypeScript + +```ts +function maxSubsequence(nums: number[], k: number): number[] { + const n = nums.length; + const idx: number[] = Array.from({ length: n }, (_, i) => i); + idx.sort((i, j) => nums[i] - nums[j]); + return idx + .slice(n - k) + .sort((i, j) => i - j) + .map(i => nums[i]); +} +``` + diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.cpp b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.cpp index fcc326c5028a4..60350b1b94b0a 100644 --- a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.cpp +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.cpp @@ -2,14 +2,16 @@ class Solution { public: vector maxSubsequence(vector& nums, int k) { int n = nums.size(); - vector> vals; - for (int i = 0; i < n; ++i) vals.push_back({i, nums[i]}); - sort(vals.begin(), vals.end(), [&](auto x1, auto x2) { - return x1.second > x2.second; - }); - sort(vals.begin(), vals.begin() + k); - vector ans; - for (int i = 0; i < k; ++i) ans.push_back(vals[i].second); + vector idx(n); + for (int i = 0; i < n; ++i) { + idx[i] = i; + } + sort(idx.begin(), idx.end(), [&](int i, int j) { return nums[i] < nums[j]; }); + sort(idx.begin() + (n - k), idx.end()); + vector ans(k); + for (int i = n - k; i < n; ++i) { + ans[i - (n - k)] = nums[idx[i]]; + } return ans; } }; \ No newline at end of file diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.go b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.go index 1c5a5e2a663ee..6dac3c53afae9 100644 --- a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.go +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.go @@ -1,13 +1,14 @@ func maxSubsequence(nums []int, k int) []int { - idx := make([]int, len(nums)) + n := len(nums) + idx := make([]int, n) for i := range idx { idx[i] = i } - sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] > nums[idx[j]] }) - sort.Ints(idx[:k]) + sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] < nums[idx[j]] }) + sort.Ints(idx[n-k:]) ans := make([]int, k) - for i, j := range idx[:k] { - ans[i] = nums[j] + for i := n - k; i < n; i++ { + ans[i-(n-k)] = nums[idx[i]] } return ans } \ No newline at end of file diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.java b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.java index 4220a22cbc3c7..c4286fe13513d 100644 --- a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.java +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.java @@ -1,19 +1,15 @@ class Solution { public int[] maxSubsequence(int[] nums, int k) { - int[] ans = new int[k]; - List idx = new ArrayList<>(); int n = nums.length; + Integer[] idx = new Integer[n]; for (int i = 0; i < n; ++i) { - idx.add(i); - } - idx.sort(Comparator.comparingInt(i -> - nums[i])); - int[] t = new int[k]; - for (int i = 0; i < k; ++i) { - t[i] = idx.get(i); + idx[i] = i; } - Arrays.sort(t); - for (int i = 0; i < k; ++i) { - ans[i] = nums[t[i]]; + Arrays.sort(idx, (i, j) -> nums[i] - nums[j]); + Arrays.sort(idx, n - k, n); + int[] ans = new int[k]; + for (int i = n - k; i < n; ++i) { + ans[i - (n - k)] = nums[idx[i]]; } return ans; } diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.py b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.py index ed68ad97710d3..22f2f72f4b6fe 100644 --- a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.py +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.py @@ -1,5 +1,4 @@ class Solution: def maxSubsequence(self, nums: List[int], k: int) -> List[int]: - idx = list(range(len(nums))) - idx.sort(key=lambda i: nums[i]) - return [nums[i] for i in sorted(idx[-k:])] + idx = sorted(range(len(nums)), key=lambda i: nums[i])[-k:] + return [nums[i] for i in sorted(idx)] diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.ts b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.ts new file mode 100644 index 0000000000000..7a8af12d45454 --- /dev/null +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.ts @@ -0,0 +1,9 @@ +function maxSubsequence(nums: number[], k: number): number[] { + const n = nums.length; + const idx: number[] = Array.from({ length: n }, (_, i) => i); + idx.sort((i, j) => nums[i] - nums[j]); + return idx + .slice(n - k) + .sort((i, j) => i - j) + .map(i => nums[i]); +} diff --git a/solution/3200-3299/3220.Odd and Even Transactions/README.md b/solution/3200-3299/3220.Odd and Even Transactions/README.md index 365fdba217daa..9c1911430f85e 100644 --- a/solution/3200-3299/3220.Odd and Even Transactions/README.md +++ b/solution/3200-3299/3220.Odd and Even Transactions/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md +tags: + - 数据库 --- diff --git a/solution/3200-3299/3220.Odd and Even Transactions/README_EN.md b/solution/3200-3299/3220.Odd and Even Transactions/README_EN.md index fc3bea24ca75c..6df1b8b12509f 100644 --- a/solution/3200-3299/3220.Odd and Even Transactions/README_EN.md +++ b/solution/3200-3299/3220.Odd and Even Transactions/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md +tags: + - Database --- diff --git a/solution/3200-3299/3221.Maximum Array Hopping Score II/README.md b/solution/3200-3299/3221.Maximum Array Hopping Score II/README.md index 65f98df2f0f66..3d515cc38f08c 100644 --- a/solution/3200-3299/3221.Maximum Array Hopping Score II/README.md +++ b/solution/3200-3299/3221.Maximum Array Hopping Score II/README.md @@ -2,6 +2,11 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3221.Maximum%20Array%20Hopping%20Score%20II/README.md +tags: + - 栈 + - 贪心 + - 数组 + - 单调栈 --- diff --git a/solution/3200-3299/3221.Maximum Array Hopping Score II/README_EN.md b/solution/3200-3299/3221.Maximum Array Hopping Score II/README_EN.md index d1b3ab984a648..e19680963ad34 100644 --- a/solution/3200-3299/3221.Maximum Array Hopping Score II/README_EN.md +++ b/solution/3200-3299/3221.Maximum Array Hopping Score II/README_EN.md @@ -2,6 +2,11 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3221.Maximum%20Array%20Hopping%20Score%20II/README_EN.md +tags: + - Stack + - Greedy + - Array + - Monotonic Stack --- diff --git a/solution/3200-3299/3222.Find the Winning Player in Coin Game/README.md b/solution/3200-3299/3222.Find the Winning Player in Coin Game/README.md index 208aebc3ea07b..7677d66714fe3 100644 --- a/solution/3200-3299/3222.Find the Winning Player in Coin Game/README.md +++ b/solution/3200-3299/3222.Find the Winning Player in Coin Game/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3222.Find%20the%20Winning%20Player%20in%20Coin%20Game/README.md +tags: + - 数学 + - 博弈 + - 模拟 --- diff --git a/solution/3200-3299/3222.Find the Winning Player in Coin Game/README_EN.md b/solution/3200-3299/3222.Find the Winning Player in Coin Game/README_EN.md index 5814c49dd1849..81e55b55eca37 100644 --- a/solution/3200-3299/3222.Find the Winning Player in Coin Game/README_EN.md +++ b/solution/3200-3299/3222.Find the Winning Player in Coin Game/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3222.Find%20the%20Winning%20Player%20in%20Coin%20Game/README_EN.md +tags: + - Math + - Game Theory + - Simulation --- diff --git a/solution/3200-3299/3223.Minimum Length of String After Operations/README.md b/solution/3200-3299/3223.Minimum Length of String After Operations/README.md index 3202331b2c1df..c2124a46f956b 100644 --- a/solution/3200-3299/3223.Minimum Length of String After Operations/README.md +++ b/solution/3200-3299/3223.Minimum Length of String After Operations/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3223.Minimum%20Length%20of%20String%20After%20Operations/README.md +tags: + - 哈希表 + - 字符串 + - 计数 --- diff --git a/solution/3200-3299/3223.Minimum Length of String After Operations/README_EN.md b/solution/3200-3299/3223.Minimum Length of String After Operations/README_EN.md index 5ad52edd05036..39c068822b53f 100644 --- a/solution/3200-3299/3223.Minimum Length of String After Operations/README_EN.md +++ b/solution/3200-3299/3223.Minimum Length of String After Operations/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3223.Minimum%20Length%20of%20String%20After%20Operations/README_EN.md +tags: + - Hash Table + - String + - Counting --- diff --git a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README.md b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README.md index c32559d8f35b5..82cb851a642af 100644 --- a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README.md +++ b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3224.Minimum%20Array%20Changes%20to%20Make%20Differences%20Equal/README.md +tags: + - 数组 + - 哈希表 + - 前缀和 --- diff --git a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README_EN.md b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README_EN.md index 1336d565f7735..2b232818621b2 100644 --- a/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README_EN.md +++ b/solution/3200-3299/3224.Minimum Array Changes to Make Differences Equal/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3224.Minimum%20Array%20Changes%20to%20Make%20Differences%20Equal/README_EN.md +tags: + - Array + - Hash Table + - Prefix Sum --- diff --git a/solution/3200-3299/3225.Maximum Score From Grid Operations/README.md b/solution/3200-3299/3225.Maximum Score From Grid Operations/README.md index f027a8e839e28..cae9e276ea6af 100644 --- a/solution/3200-3299/3225.Maximum Score From Grid Operations/README.md +++ b/solution/3200-3299/3225.Maximum Score From Grid Operations/README.md @@ -2,6 +2,11 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3225.Maximum%20Score%20From%20Grid%20Operations/README.md +tags: + - 数组 + - 动态规划 + - 矩阵 + - 前缀和 --- diff --git a/solution/3200-3299/3225.Maximum Score From Grid Operations/README_EN.md b/solution/3200-3299/3225.Maximum Score From Grid Operations/README_EN.md index 3f8ab82940cc4..69a71198c1ab4 100644 --- a/solution/3200-3299/3225.Maximum Score From Grid Operations/README_EN.md +++ b/solution/3200-3299/3225.Maximum Score From Grid Operations/README_EN.md @@ -2,6 +2,11 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3225.Maximum%20Score%20From%20Grid%20Operations/README_EN.md +tags: + - Array + - Dynamic Programming + - Matrix + - Prefix Sum --- diff --git a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README.md b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README.md index 5e75e24d309ad..68da25abe8251 100644 --- a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README.md +++ b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README.md @@ -2,6 +2,8 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3226.Number%20of%20Bit%20Changes%20to%20Make%20Two%20Integers%20Equal/README.md +tags: + - 位运算 --- diff --git a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README_EN.md b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README_EN.md index 453d423551859..bbf7790d96365 100644 --- a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README_EN.md +++ b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3226.Number%20of%20Bit%20Changes%20to%20Make%20Two%20Integers%20Equal/README_EN.md +tags: + - Bit Manipulation --- diff --git a/solution/3200-3299/3227.Vowels Game in a String/README.md b/solution/3200-3299/3227.Vowels Game in a String/README.md index f864618c7259f..7e67af7f5dcdb 100644 --- a/solution/3200-3299/3227.Vowels Game in a String/README.md +++ b/solution/3200-3299/3227.Vowels Game in a String/README.md @@ -2,6 +2,11 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3227.Vowels%20Game%20in%20a%20String/README.md +tags: + - 脑筋急转弯 + - 数学 + - 字符串 + - 博弈 --- diff --git a/solution/3200-3299/3227.Vowels Game in a String/README_EN.md b/solution/3200-3299/3227.Vowels Game in a String/README_EN.md index eeb9fe4929ae8..db43ca010d722 100644 --- a/solution/3200-3299/3227.Vowels Game in a String/README_EN.md +++ b/solution/3200-3299/3227.Vowels Game in a String/README_EN.md @@ -2,6 +2,11 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3227.Vowels%20Game%20in%20a%20String/README_EN.md +tags: + - Brainteaser + - Math + - String + - Game Theory --- diff --git a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README.md b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README.md index 5c33c98846b34..cdc31cc1f0b15 100644 --- a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README.md +++ b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3228.Maximum%20Number%20of%20Operations%20to%20Move%20Ones%20to%20the%20End/README.md +tags: + - 贪心 + - 字符串 + - 计数 --- diff --git a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README_EN.md b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README_EN.md index 4135cc9bab56c..614bd3ab469a0 100644 --- a/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README_EN.md +++ b/solution/3200-3299/3228.Maximum Number of Operations to Move Ones to the End/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3228.Maximum%20Number%20of%20Operations%20to%20Move%20Ones%20to%20the%20End/README_EN.md +tags: + - Greedy + - String + - Counting --- diff --git a/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README.md b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README.md index 5d87a2fbdbfb5..97d1002d0ad32 100644 --- a/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README.md +++ b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README.md @@ -2,6 +2,12 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3229.Minimum%20Operations%20to%20Make%20Array%20Equal%20to%20Target/README.md +tags: + - 栈 + - 贪心 + - 数组 + - 动态规划 + - 单调栈 --- diff --git a/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README_EN.md b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README_EN.md index e32a4fd400e3a..c7d7a11e2b6cc 100644 --- a/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README_EN.md +++ b/solution/3200-3299/3229.Minimum Operations to Make Array Equal to Target/README_EN.md @@ -2,6 +2,12 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3229.Minimum%20Operations%20to%20Make%20Array%20Equal%20to%20Target/README_EN.md +tags: + - Stack + - Greedy + - Array + - Dynamic Programming + - Monotonic Stack --- diff --git a/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README.md b/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README.md index 675787b9cac98..847d5c0de0427 100644 --- a/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README.md +++ b/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README.md @@ -2,11 +2,13 @@ comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README.md +tags: + - 数据库 --- -# [3230. Customer Purchasing Behavior Analysis 🔒](https://leetcode.cn/problems/customer-purchasing-behavior-analysis) +# [3230. 客户购买行为分析 🔒](https://leetcode.cn/problems/customer-purchasing-behavior-analysis) [English Version](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README_EN.md) @@ -14,7 +16,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3230.Cu -

Table: Transactions

+

表:Transactions

 +------------------+---------+
@@ -26,11 +28,11 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3230.Cu
 | transaction_date | date    |
 | amount           | decimal |
 +------------------+---------+
-transaction_id is the unique identifier for this table.
-Each row of this table contains information about a transaction, including the customer ID, product ID, date, and amount spent.
+transaction_id 是这张表的唯一标识符。
+这张表的每一行包含一次交易的信息,包括客户 ID,产品 ID,日期和总花费。
 
-

Table: Products

+

表:Products

 +-------------+---------+
@@ -40,34 +42,35 @@ Each row of this table contains information about a transaction, including the c
 | category    | varchar |
 | price       | decimal |
 +-------------+---------+
-product_id is the unique identifier for this table.
-Each row of this table contains information about a product, including its category and price.
+product_id 是这张表的唯一标识符。
+这张表的每一行包含一个产品的信息,包括它的分类和价格。
 
-

Write a solution to analyze customer purchasing behavior. For each customer, calculate:

+

编写一个解决方案来分析用户购买行为。对于 每个消费者,计算:

    -
  • The total amount spent.
  • -
  • The number of transactions.
  • -
  • The number of unique product categories purchased.
  • -
  • The average amount spent. 
  • -
  • The most frequently purchased product category (if there is a tie, choose the one with the most recent transaction).
  • -
  • A loyalty score defined as: (Number of transactions * 10) + (Total amount spent / 100).
  • +
  • 总消费额
  • +
  • 交易数量
  • +
  • 购买的 不同 产品类别的数量。
  • +
  • 平均消费金额。
  • +
  • 最常购买 的产品类别(如果相同,选择最近交易的那个)
  • +
  • 忠诚度分数 定义为:(交易数量 * 10) + (总消费 / 100)。
-

Round total_amount, avg_transaction_amount, and loyalty_score to 2 decimal places.

+

将 total_amount, avg_transaction_amount 和 loyalty_score 舍入到 2 位小数。

-

Return the result table ordered by loyalty_score in descending order, then by customer_id in ascending order.

+

返回结果表以 loyalty_score 降序 排序,然后以 customer_id 升序 排序。

-

The query result format is in the following example.

+

查询结果格式如下所示。

 

-

Example:

+ +

示例:

-

Input:

+

输入:

-

Transactions table:

+

Transactions 表:

 +----------------+-------------+------------+------------------+--------+
@@ -81,7 +84,7 @@ Each row of this table contains information about a product, including its categ
 +----------------+-------------+------------+------------------+--------+
 
-

Products table:

+

Products 表:

 +------------+----------+--------+
@@ -93,7 +96,7 @@ Each row of this table contains information about a product, including its categ
 +------------+----------+--------+
 
-

Output:

+

输出:

 +-------------+--------------+-------------------+-------------------+------------------------+--------------+---------------+
@@ -104,32 +107,32 @@ Each row of this table contains information about a product, including its categ
 +-------------+--------------+-------------------+-------------------+------------------------+--------------+---------------+
 
-

Explanation:

+

解释:

    -
  • For customer 101: +
  • 对于消费者 101:
      -
    • Total amount spent: 100.00 + 150.00 + 200.00 = 450.00
    • -
    • Number of transactions: 3
    • -
    • Unique categories: A, B, C (3 categories)
    • -
    • Average transaction amount: 450.00 / 3 = 150.00
    • -
    • Top category: C (Customer 101 made 1 purchase each in categories A, B, and C. Since the count is the same for all categories, we choose the most recent transaction, which is category C on 2023-02-10)
    • -
    • Loyalty score: (3 * 10) + (450.00 / 100) = 34.50
    • +
    • 总消费额:100.00 + 150.00 + 200.00 = 450.00
    • +
    • 交易次数:3
    • +
    • 不同分类:A, B, C (3 个分类)
    • +
    • 平均交易金额:450.00 / 3 = 150.00
    • +
    • 最高分类:C (消费者 101 在分类 A,B,C 分别进行了一次交易。因为所有分类的数量都一样,我们选择最近的那次交易,即在 2023-02-10 的分类 C)
    • +
    • 忠诚度分数:(3 * 10) + (450.00 / 100) = 34.50
  • -
  • For customer 102: +
  • 对于消费者 102:
      -
    • Total amount spent: 100.00 + 200.00 = 300.00
    • -
    • Number of transactions: 2
    • -
    • Unique categories: A, C (2 categories)
    • -
    • Average transaction amount: 300.00 / 2 = 150.00
    • -
    • Top category: C (Customer 102 made 1 purchase each in categories A and C. Since the count is the same for both categories, we choose the most recent transaction, which is category C on 2023-01-22)
    • -
    • Loyalty score: (2 * 10) + (300.00 / 100) = 23.00
    • +
    • 总消费额:100.00 + 200.00 = 300.00
    • +
    • 交易次数:2
    • +
    • 不同分类:A, C(2 个分类)
    • +
    • 平均交易金额:300.00 / 2 = 150.00
    • +
    • 最高分类:C (消费者 102 在分类 A 和 C 分别进行了一次交易。因为所有分类的数量都一样,我们选择最近的那次交易,即在 2023-01-22 的分类 C)
    • +
    • 忠诚度分数:(2 * 10) + (300.00 / 100) = 23.00
-

Note: The output is ordered by loyalty_score in descending order, then by customer_id in ascending order.

+

注意:输出表以 loyalty_score 降序排序,然后以 customer_id 升序排序。

diff --git a/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README_EN.md b/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README_EN.md index 6b9f903ab05e6..8c4ee55a82341 100644 --- a/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README_EN.md +++ b/solution/3200-3299/3230.Customer Purchasing Behavior Analysis/README_EN.md @@ -2,6 +2,8 @@ comments: true difficulty: Medium edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README_EN.md +tags: + - Database --- diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index c1c577078f022..344b03661887e 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -287,8 +287,8 @@ | 3198 | [查找每个州的城市](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README.md) | `数据库` | 简单 | 🔒 | | 3204 | [按位用户权限分析](/solution/3200-3299/3204.Bitwise%20User%20Permissions%20Analysis/README.md) | `数据库` | 中等 | 🔒 | | 3214 | [同比增长率](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README.md) | `数据库` | 困难 | 🔒 | -| 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md) | | 中等 | | -| 3230 | [Customer Purchasing Behavior Analysis](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README.md) | | 中等 | 🔒 | +| 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md) | `数据库` | 中等 | | +| 3230 | [客户购买行为分析](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README.md) | `数据库` | 中等 | 🔒 | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index 60642b1f58123..eab94499e3828 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -285,8 +285,8 @@ Press Control + F(or Command + F on | 3198 | [Find Cities in Each State](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md) | `Database` | Easy | 🔒 | | 3204 | [Bitwise User Permissions Analysis](/solution/3200-3299/3204.Bitwise%20User%20Permissions%20Analysis/README_EN.md) | `Database` | Medium | 🔒 | | 3214 | [Year on Year Growth Rate](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md) | `Database` | Hard | 🔒 | -| 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md) | | Medium | | -| 3230 | [Customer Purchasing Behavior Analysis](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README_EN.md) | | Medium | 🔒 | +| 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md) | `Database` | Medium | | +| 3230 | [Customer Purchasing Behavior Analysis](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README_EN.md) | `Database` | Medium | 🔒 | ## Copyright diff --git a/solution/README.md b/solution/README.md index 9d390d61b487c..7b1131802934d 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3230,17 +3230,17 @@ | 3217 | [从链表中移除在数组中存在的节点](/solution/3200-3299/3217.Delete%20Nodes%20From%20Linked%20List%20Present%20in%20Array/README.md) | `数组`,`哈希表`,`链表` | 中等 | 第 406 场周赛 | | 3218 | [切蛋糕的最小总开销 I](/solution/3200-3299/3218.Minimum%20Cost%20for%20Cutting%20Cake%20I/README.md) | `贪心`,`数组`,`动态规划`,`排序` | 中等 | 第 406 场周赛 | | 3219 | [切蛋糕的最小总开销 II](/solution/3200-3299/3219.Minimum%20Cost%20for%20Cutting%20Cake%20II/README.md) | `贪心`,`数组`,`排序` | 困难 | 第 406 场周赛 | -| 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md) | | 中等 | | -| 3221 | [Maximum Array Hopping Score II](/solution/3200-3299/3221.Maximum%20Array%20Hopping%20Score%20II/README.md) | | 中等 | 🔒 | -| 3222 | [求出硬币游戏的赢家](/solution/3200-3299/3222.Find%20the%20Winning%20Player%20in%20Coin%20Game/README.md) | | 简单 | 第 135 场双周赛 | -| 3223 | [操作后字符串的最短长度](/solution/3200-3299/3223.Minimum%20Length%20of%20String%20After%20Operations/README.md) | | 中等 | 第 135 场双周赛 | -| 3224 | [使差值相等的最少数组改动次数](/solution/3200-3299/3224.Minimum%20Array%20Changes%20to%20Make%20Differences%20Equal/README.md) | | 中等 | 第 135 场双周赛 | -| 3225 | [网格图操作后的最大分数](/solution/3200-3299/3225.Maximum%20Score%20From%20Grid%20Operations/README.md) | | 困难 | 第 135 场双周赛 | -| 3226 | [使两个整数相等的位更改次数](/solution/3200-3299/3226.Number%20of%20Bit%20Changes%20to%20Make%20Two%20Integers%20Equal/README.md) | | 简单 | 第 407 场周赛 | -| 3227 | [字符串元音游戏](/solution/3200-3299/3227.Vowels%20Game%20in%20a%20String/README.md) | | 中等 | 第 407 场周赛 | -| 3228 | [将 1 移动到末尾的最大操作次数](/solution/3200-3299/3228.Maximum%20Number%20of%20Operations%20to%20Move%20Ones%20to%20the%20End/README.md) | | 中等 | 第 407 场周赛 | -| 3229 | [使数组等于目标数组所需的最少操作次数](/solution/3200-3299/3229.Minimum%20Operations%20to%20Make%20Array%20Equal%20to%20Target/README.md) | | 困难 | 第 407 场周赛 | -| 3230 | [Customer Purchasing Behavior Analysis](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README.md) | | 中等 | 🔒 | +| 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md) | `数据库` | 中等 | | +| 3221 | [Maximum Array Hopping Score II](/solution/3200-3299/3221.Maximum%20Array%20Hopping%20Score%20II/README.md) | `栈`,`贪心`,`数组`,`单调栈` | 中等 | 🔒 | +| 3222 | [求出硬币游戏的赢家](/solution/3200-3299/3222.Find%20the%20Winning%20Player%20in%20Coin%20Game/README.md) | `数学`,`博弈`,`模拟` | 简单 | 第 135 场双周赛 | +| 3223 | [操作后字符串的最短长度](/solution/3200-3299/3223.Minimum%20Length%20of%20String%20After%20Operations/README.md) | `哈希表`,`字符串`,`计数` | 中等 | 第 135 场双周赛 | +| 3224 | [使差值相等的最少数组改动次数](/solution/3200-3299/3224.Minimum%20Array%20Changes%20to%20Make%20Differences%20Equal/README.md) | `数组`,`哈希表`,`前缀和` | 中等 | 第 135 场双周赛 | +| 3225 | [网格图操作后的最大分数](/solution/3200-3299/3225.Maximum%20Score%20From%20Grid%20Operations/README.md) | `数组`,`动态规划`,`矩阵`,`前缀和` | 困难 | 第 135 场双周赛 | +| 3226 | [使两个整数相等的位更改次数](/solution/3200-3299/3226.Number%20of%20Bit%20Changes%20to%20Make%20Two%20Integers%20Equal/README.md) | `位运算` | 简单 | 第 407 场周赛 | +| 3227 | [字符串元音游戏](/solution/3200-3299/3227.Vowels%20Game%20in%20a%20String/README.md) | `脑筋急转弯`,`数学`,`字符串`,`博弈` | 中等 | 第 407 场周赛 | +| 3228 | [将 1 移动到末尾的最大操作次数](/solution/3200-3299/3228.Maximum%20Number%20of%20Operations%20to%20Move%20Ones%20to%20the%20End/README.md) | `贪心`,`字符串`,`计数` | 中等 | 第 407 场周赛 | +| 3229 | [使数组等于目标数组所需的最少操作次数](/solution/3200-3299/3229.Minimum%20Operations%20to%20Make%20Array%20Equal%20to%20Target/README.md) | `栈`,`贪心`,`数组`,`动态规划`,`单调栈` | 困难 | 第 407 场周赛 | +| 3230 | [客户购买行为分析](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README.md) | `数据库` | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 54c4f82026a54..30e453506478f 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3228,17 +3228,17 @@ Press Control + F(or Command + F on | 3217 | [Delete Nodes From Linked List Present in Array](/solution/3200-3299/3217.Delete%20Nodes%20From%20Linked%20List%20Present%20in%20Array/README_EN.md) | `Array`,`Hash Table`,`Linked List` | Medium | Weekly Contest 406 | | 3218 | [Minimum Cost for Cutting Cake I](/solution/3200-3299/3218.Minimum%20Cost%20for%20Cutting%20Cake%20I/README_EN.md) | `Greedy`,`Array`,`Dynamic Programming`,`Sorting` | Medium | Weekly Contest 406 | | 3219 | [Minimum Cost for Cutting Cake II](/solution/3200-3299/3219.Minimum%20Cost%20for%20Cutting%20Cake%20II/README_EN.md) | `Greedy`,`Array`,`Sorting` | Hard | Weekly Contest 406 | -| 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md) | | Medium | | -| 3221 | [Maximum Array Hopping Score II](/solution/3200-3299/3221.Maximum%20Array%20Hopping%20Score%20II/README_EN.md) | | Medium | 🔒 | -| 3222 | [Find the Winning Player in Coin Game](/solution/3200-3299/3222.Find%20the%20Winning%20Player%20in%20Coin%20Game/README_EN.md) | | Easy | Biweekly Contest 135 | -| 3223 | [Minimum Length of String After Operations](/solution/3200-3299/3223.Minimum%20Length%20of%20String%20After%20Operations/README_EN.md) | | Medium | Biweekly Contest 135 | -| 3224 | [Minimum Array Changes to Make Differences Equal](/solution/3200-3299/3224.Minimum%20Array%20Changes%20to%20Make%20Differences%20Equal/README_EN.md) | | Medium | Biweekly Contest 135 | -| 3225 | [Maximum Score From Grid Operations](/solution/3200-3299/3225.Maximum%20Score%20From%20Grid%20Operations/README_EN.md) | | Hard | Biweekly Contest 135 | -| 3226 | [Number of Bit Changes to Make Two Integers Equal](/solution/3200-3299/3226.Number%20of%20Bit%20Changes%20to%20Make%20Two%20Integers%20Equal/README_EN.md) | | Easy | Weekly Contest 407 | -| 3227 | [Vowels Game in a String](/solution/3200-3299/3227.Vowels%20Game%20in%20a%20String/README_EN.md) | | Medium | Weekly Contest 407 | -| 3228 | [Maximum Number of Operations to Move Ones to the End](/solution/3200-3299/3228.Maximum%20Number%20of%20Operations%20to%20Move%20Ones%20to%20the%20End/README_EN.md) | | Medium | Weekly Contest 407 | -| 3229 | [Minimum Operations to Make Array Equal to Target](/solution/3200-3299/3229.Minimum%20Operations%20to%20Make%20Array%20Equal%20to%20Target/README_EN.md) | | Hard | Weekly Contest 407 | -| 3230 | [Customer Purchasing Behavior Analysis](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README_EN.md) | | Medium | 🔒 | +| 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md) | `Database` | Medium | | +| 3221 | [Maximum Array Hopping Score II](/solution/3200-3299/3221.Maximum%20Array%20Hopping%20Score%20II/README_EN.md) | `Stack`,`Greedy`,`Array`,`Monotonic Stack` | Medium | 🔒 | +| 3222 | [Find the Winning Player in Coin Game](/solution/3200-3299/3222.Find%20the%20Winning%20Player%20in%20Coin%20Game/README_EN.md) | `Math`,`Game Theory`,`Simulation` | Easy | Biweekly Contest 135 | +| 3223 | [Minimum Length of String After Operations](/solution/3200-3299/3223.Minimum%20Length%20of%20String%20After%20Operations/README_EN.md) | `Hash Table`,`String`,`Counting` | Medium | Biweekly Contest 135 | +| 3224 | [Minimum Array Changes to Make Differences Equal](/solution/3200-3299/3224.Minimum%20Array%20Changes%20to%20Make%20Differences%20Equal/README_EN.md) | `Array`,`Hash Table`,`Prefix Sum` | Medium | Biweekly Contest 135 | +| 3225 | [Maximum Score From Grid Operations](/solution/3200-3299/3225.Maximum%20Score%20From%20Grid%20Operations/README_EN.md) | `Array`,`Dynamic Programming`,`Matrix`,`Prefix Sum` | Hard | Biweekly Contest 135 | +| 3226 | [Number of Bit Changes to Make Two Integers Equal](/solution/3200-3299/3226.Number%20of%20Bit%20Changes%20to%20Make%20Two%20Integers%20Equal/README_EN.md) | `Bit Manipulation` | Easy | Weekly Contest 407 | +| 3227 | [Vowels Game in a String](/solution/3200-3299/3227.Vowels%20Game%20in%20a%20String/README_EN.md) | `Brainteaser`,`Math`,`String`,`Game Theory` | Medium | Weekly Contest 407 | +| 3228 | [Maximum Number of Operations to Move Ones to the End](/solution/3200-3299/3228.Maximum%20Number%20of%20Operations%20to%20Move%20Ones%20to%20the%20End/README_EN.md) | `Greedy`,`String`,`Counting` | Medium | Weekly Contest 407 | +| 3229 | [Minimum Operations to Make Array Equal to Target](/solution/3200-3299/3229.Minimum%20Operations%20to%20Make%20Array%20Equal%20to%20Target/README_EN.md) | `Stack`,`Greedy`,`Array`,`Dynamic Programming`,`Monotonic Stack` | Hard | Weekly Contest 407 | +| 3230 | [Customer Purchasing Behavior Analysis](/solution/3200-3299/3230.Customer%20Purchasing%20Behavior%20Analysis/README_EN.md) | `Database` | Medium | 🔒 | ## Copyright From 39daf27d30d3484f3c0d26798250cd43f2016f09 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 23 Jul 2024 08:51:54 +0800 Subject: [PATCH 2/4] chore: update --- .../README.md | 22 --------------- .../README_EN.md | 22 --------------- .../Solution.cpp | 28 ------------------- 3 files changed, 72 deletions(-) delete mode 100644 solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.cpp diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README.md b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README.md index e2c7e76b7d29d..a0c24322cf7ec 100644 --- a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README.md +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README.md @@ -109,28 +109,6 @@ class Solution { } ``` -#### C++ - -```cpp -class Solution { -public: - vector maxSubsequence(vector& nums, int k) { - int n = nums.size(); - vector idx(n); - for (int i = 0; i < n; ++i) { - idx[i] = i; - } - sort(idx.begin(), idx.end(), [&](int i, int j) { return nums[i] < nums[j]; }); - sort(idx.begin() + (n - k), idx.end()); - vector ans(k); - for (int i = n - k; i < n; ++i) { - ans[i - (n - k)] = nums[idx[i]]; - } - return ans; - } -}; -``` - #### Go ```go diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README_EN.md b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README_EN.md index b8f474ff133b9..e8465c8bb9729 100644 --- a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README_EN.md +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README_EN.md @@ -110,28 +110,6 @@ class Solution { } ``` -#### C++ - -```cpp -class Solution { -public: - vector maxSubsequence(vector& nums, int k) { - int n = nums.size(); - vector idx(n); - for (int i = 0; i < n; ++i) { - idx[i] = i; - } - sort(idx.begin(), idx.end(), [&](int i, int j) { return nums[i] < nums[j]; }); - sort(idx.begin() + (n - k), idx.end()); - vector ans(k); - for (int i = n - k; i < n; ++i) { - ans[i - (n - k)] = nums[idx[i]]; - } - return ans; - } -}; -``` - #### Go ```go diff --git a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.cpp b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.cpp deleted file mode 100644 index 6b6e24b913e51..0000000000000 --- a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.cpp +++ /dev/null @@ -1,28 +0,0 @@ -class Solution { -public: - int sumOfPowers(vector& nums, int k) { - unordered_map f; - const int mod = 1e9 + 7; - int n = nums.size(); - sort(nums.begin(), nums.end()); - function dfs = [&](int i, int j, int k, int mi) { - if (i >= n) { - return k == 0 ? mi : 0; - } - long long key = (1LL * mi) << 18 | (i << 12) | (j << 6) | k; - if (f.contains(key)) { - return f[key]; - } - long long ans = dfs(i + 1, j, k, mi); - if (j == n) { - ans += dfs(i + 1, i, k - 1, mi); - } else { - ans += dfs(i + 1, i, k - 1, min(mi, nums[i] - nums[j])); - } - ans %= mod; - f[key] = ans; - return f[key]; - }; - return dfs(0, n, k, INT_MAX); - } -}; \ No newline at end of file From 096b65eb391df77a4c357c1a207bb2a822303a61 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 23 Jul 2024 08:53:10 +0800 Subject: [PATCH 3/4] fix: update --- .../README.md | 33 ------------------- .../README_EN.md | 33 ------------------- 2 files changed, 66 deletions(-) diff --git a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md index e4fac77acc8af..8a0c24ecc3b36 100644 --- a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md +++ b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md @@ -157,39 +157,6 @@ class Solution { } ``` -#### C++ - -```cpp -class Solution { -public: - int sumOfPowers(vector& nums, int k) { - unordered_map f; - const int mod = 1e9 + 7; - int n = nums.size(); - sort(nums.begin(), nums.end()); - function dfs = [&](int i, int j, int k, int mi) { - if (i >= n) { - return k == 0 ? mi : 0; - } - long long key = (1LL * mi) << 18 | (i << 12) | (j << 6) | k; - if (f.contains(key)) { - return f[key]; - } - long long ans = dfs(i + 1, j, k, mi); - if (j == n) { - ans += dfs(i + 1, i, k - 1, mi); - } else { - ans += dfs(i + 1, i, k - 1, min(mi, nums[i] - nums[j])); - } - ans %= mod; - f[key] = ans; - return f[key]; - }; - return dfs(0, n, k, INT_MAX); - } -}; -``` - #### Go ```go diff --git a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md index 5b0cac31f3d1f..27de317955f43 100644 --- a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md +++ b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md @@ -155,39 +155,6 @@ class Solution { } ``` -#### C++ - -```cpp -class Solution { -public: - int sumOfPowers(vector& nums, int k) { - unordered_map f; - const int mod = 1e9 + 7; - int n = nums.size(); - sort(nums.begin(), nums.end()); - function dfs = [&](int i, int j, int k, int mi) { - if (i >= n) { - return k == 0 ? mi : 0; - } - long long key = (1LL * mi) << 18 | (i << 12) | (j << 6) | k; - if (f.contains(key)) { - return f[key]; - } - long long ans = dfs(i + 1, j, k, mi); - if (j == n) { - ans += dfs(i + 1, i, k - 1, mi); - } else { - ans += dfs(i + 1, i, k - 1, min(mi, nums[i] - nums[j])); - } - ans %= mod; - f[key] = ans; - return f[key]; - }; - return dfs(0, n, k, INT_MAX); - } -}; -``` - #### Go ```go From 28dc715128260cb64d186c1873e200e843e322a2 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 23 Jul 2024 08:54:38 +0800 Subject: [PATCH 4/4] fix: update --- .../3000-3099/3098.Find the Sum of Subsequence Powers/README.md | 2 +- .../3098.Find the Sum of Subsequence Powers/README_EN.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md index 8a0c24ecc3b36..d6035ffb395cc 100644 --- a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md +++ b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md @@ -95,7 +95,7 @@ tags: 为了避免重复计算,我们可以使用记忆化搜索的方法,将已经计算过的结果保存起来。 -时间复杂度 $O(n^5)$,空间复杂度 $O(n^5)$。其中 $n$ 为数组的长度。 +时间复杂度 $O(n^4 \times k)$,空间复杂度 $O(n^4 \times k)$。其中 $n$ 为数组的长度。 diff --git a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md index 27de317955f43..cf67f3a399d49 100644 --- a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md +++ b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md @@ -93,7 +93,7 @@ The execution process of the function $dfs(i, j, k, mi)$ is as follows: To avoid repeated calculations, we can use the method of memoization search to save the calculated results. -The time complexity is $O(n^5)$, and the space complexity is $O(n^5)$. Where $n$ is the length of the array. +The time complexity is $O(n^4 \times k)$, and the space complexity is $O(n^4 \times k)$. Where $n$ is the length of the array.