From 34c7eec18d2b182c01e57487fa7817dcdca8473f Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 7 Dec 2025 08:48:41 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.3766,3767 --- .../README.md | 178 +++++++++++++++++- .../README_EN.md | 178 +++++++++++++++++- .../Solution.cpp | 34 ++++ .../Solution.go | 37 ++++ .../Solution.java | 45 +++++ .../Solution.py | 19 ++ .../Solution.ts | 28 +++ .../README.md | 144 +++++++++++++- .../README_EN.md | 144 +++++++++++++- .../Solution.cpp | 33 ++++ .../Solution.go | 32 ++++ .../Solution.java | 25 +++ .../Solution.py | 13 ++ .../Solution.ts | 24 +++ 14 files changed, 918 insertions(+), 16 deletions(-) create mode 100644 solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.cpp create mode 100644 solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.go create mode 100644 solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.java create mode 100644 solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.py create mode 100644 solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.ts create mode 100644 solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.cpp create mode 100644 solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.go create mode 100644 solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.java create mode 100644 solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.py create mode 100644 solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.ts diff --git a/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/README.md b/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/README.md index f79b90194bbc5..270c47b5c0957 100644 --- a/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/README.md +++ b/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/README.md @@ -155,32 +155,202 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3766.Mi -### 方法一 +### 方法一:预处理 + 二分查找 + +我们注意到,题目中给定的数字范围仅为 $[1, 5000]$,因此,我们直接预处理 $[0, 2^{14})$ 范围内的所有二进制回文数,并将其存储在一个数组中,记为 $\textit{primes}$。 + +接下来,对于每个数字 $x$,我们使用二分查找在数组 $\textit{primes}$ 中找到第一个大于等于 $x$ 的回文数 $\textit{primes}[i]$,以及第一个小于 $x$ 的回文数 $\textit{primes}[i - 1]$。然后,我们计算将 $x$ 转换为这两个回文数所需的操作次数,并取其中的最小值作为答案。 + +时间复杂度 $O(n \times \log M)$,空间复杂度 $O(M)$。其中 $n$ 是数组 $\textit{nums}$ 的长度,而 $M$ 是预处理的二进制回文数的数量。 #### Python3 ```python - +primes = [] +for i in range(1 << 14): + s = bin(i)[2:] + if s == s[::-1]: + primes.append(i) + + +class Solution: + def minOperations(self, nums: List[int]) -> List[int]: + ans = [] + for x in nums: + i = bisect_left(primes, x) + times = inf + if i < len(primes): + times = min(times, primes[i] - x) + if i >= 1: + times = min(times, x - primes[i - 1]) + ans.append(times) + return ans ``` #### Java ```java - +class Solution { + private static final List primes = new ArrayList<>(); + + static { + int N = 1 << 14; + for (int i = 0; i < N; i++) { + String s = Integer.toBinaryString(i); + String rs = new StringBuilder(s).reverse().toString(); + if (s.equals(rs)) { + primes.add(i); + } + } + } + + public int[] minOperations(int[] nums) { + int n = nums.length; + int[] ans = new int[n]; + Arrays.fill(ans, Integer.MAX_VALUE); + for (int k = 0; k < n; ++k) { + int x = nums[k]; + int i = binarySearch(primes, x); + if (i < primes.size()) { + ans[k] = Math.min(ans[k], primes.get(i) - x); + } + if (i >= 1) { + ans[k] = Math.min(ans[k], x - primes.get(i - 1)); + } + } + + return ans; + } + + private int binarySearch(List primes, int x) { + int l = 0, r = primes.size(); + while (l < r) { + int mid = (l + r) >>> 1; + if (primes.get(mid) >= x) { + r = mid; + } else { + l = mid + 1; + } + } + return l; + } +} ``` #### C++ ```cpp - +vector primes; + +auto init = [] { + int N = 1 << 14; + for (int i = 0; i < N; ++i) { + string s = bitset<14>(i).to_string(); + s = s.substr(s.find_first_not_of('0') == string::npos ? 13 : s.find_first_not_of('0')); + string rs = s; + reverse(rs.begin(), rs.end()); + if (s == rs) { + primes.push_back(i); + } + } + return 0; +}(); + +class Solution { +public: + vector minOperations(vector& nums) { + int n = nums.size(); + vector ans(n, INT_MAX); + for (int k = 0; k < n; ++k) { + int x = nums[k]; + int i = lower_bound(primes.begin(), primes.end(), x) - primes.begin(); + if (i < (int) primes.size()) { + ans[k] = min(ans[k], primes[i] - x); + } + if (i >= 1) { + ans[k] = min(ans[k], x - primes[i - 1]); + } + } + return ans; + } +}; ``` #### Go ```go +var primes []int + +func init() { + N := 1 << 14 + for i := 0; i < N; i++ { + s := strconv.FormatInt(int64(i), 2) + if isPalindrome(s) { + primes = append(primes, i) + } + } +} + +func isPalindrome(s string) bool { + runes := []rune(s) + for i := 0; i < len(runes)/2; i++ { + if runes[i] != runes[len(runes)-1-i] { + return false + } + } + return true +} + +func minOperations(nums []int) []int { + ans := make([]int, len(nums)) + for k, x := range nums { + i := sort.SearchInts(primes, x) + t := math.MaxInt32 + if i < len(primes) { + t = primes[i] - x + } + if i >= 1 { + t = min(t, x-primes[i-1]) + } + ans[k] = t + } + return ans +} +``` +#### TypeScript + +```ts +const primes: number[] = (() => { + const res: number[] = []; + const N = 1 << 14; + for (let i = 0; i < N; i++) { + const s = i.toString(2); + if (s === s.split('').reverse().join('')) { + res.push(i); + } + } + return res; +})(); + +function minOperations(nums: number[]): number[] { + const ans: number[] = Array(nums.length).fill(Number.MAX_SAFE_INTEGER); + + for (let k = 0; k < nums.length; k++) { + const x = nums[k]; + const i = _.sortedIndex(primes, x); + if (i < primes.length) { + ans[k] = primes[i] - x; + } + if (i >= 1) { + ans[k] = Math.min(ans[k], x - primes[i - 1]); + } + } + + return ans; +} ``` diff --git a/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/README_EN.md b/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/README_EN.md index 739328134af9b..3809bf5c2f745 100644 --- a/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/README_EN.md +++ b/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/README_EN.md @@ -153,32 +153,202 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3766.Mi -### Solution 1 +### Solution 1: Preprocessing + Binary Search + +We observe that the range of numbers given in the problem is only $[1, 5000]$. Therefore, we directly preprocess all binary palindromic numbers in the range $[0, 2^{14})$ and store them in an array, denoted as $\textit{primes}$. + +Next, for each number $x$, we use binary search to find the first palindromic number greater than or equal to $x$ in the array $\textit{primes}$, denoted as $\textit{primes}[i]$, as well as the first palindromic number less than $x$, denoted as $\textit{primes}[i - 1]$. Then, we calculate the number of operations required to convert $x$ to these two palindromic numbers and take the minimum value as the answer. + +The time complexity is $O(n \times \log M)$, and the space complexity is $O(M)$. Where $n$ is the length of the array $\textit{nums}$, and $M$ is the number of preprocessed binary palindromic numbers. #### Python3 ```python - +primes = [] +for i in range(1 << 14): + s = bin(i)[2:] + if s == s[::-1]: + primes.append(i) + + +class Solution: + def minOperations(self, nums: List[int]) -> List[int]: + ans = [] + for x in nums: + i = bisect_left(primes, x) + times = inf + if i < len(primes): + times = min(times, primes[i] - x) + if i >= 1: + times = min(times, x - primes[i - 1]) + ans.append(times) + return ans ``` #### Java ```java - +class Solution { + private static final List primes = new ArrayList<>(); + + static { + int N = 1 << 14; + for (int i = 0; i < N; i++) { + String s = Integer.toBinaryString(i); + String rs = new StringBuilder(s).reverse().toString(); + if (s.equals(rs)) { + primes.add(i); + } + } + } + + public int[] minOperations(int[] nums) { + int n = nums.length; + int[] ans = new int[n]; + Arrays.fill(ans, Integer.MAX_VALUE); + for (int k = 0; k < n; ++k) { + int x = nums[k]; + int i = binarySearch(primes, x); + if (i < primes.size()) { + ans[k] = Math.min(ans[k], primes.get(i) - x); + } + if (i >= 1) { + ans[k] = Math.min(ans[k], x - primes.get(i - 1)); + } + } + + return ans; + } + + private int binarySearch(List primes, int x) { + int l = 0, r = primes.size(); + while (l < r) { + int mid = (l + r) >>> 1; + if (primes.get(mid) >= x) { + r = mid; + } else { + l = mid + 1; + } + } + return l; + } +} ``` #### C++ ```cpp - +vector primes; + +auto init = [] { + int N = 1 << 14; + for (int i = 0; i < N; ++i) { + string s = bitset<14>(i).to_string(); + s = s.substr(s.find_first_not_of('0') == string::npos ? 13 : s.find_first_not_of('0')); + string rs = s; + reverse(rs.begin(), rs.end()); + if (s == rs) { + primes.push_back(i); + } + } + return 0; +}(); + +class Solution { +public: + vector minOperations(vector& nums) { + int n = nums.size(); + vector ans(n, INT_MAX); + for (int k = 0; k < n; ++k) { + int x = nums[k]; + int i = lower_bound(primes.begin(), primes.end(), x) - primes.begin(); + if (i < (int) primes.size()) { + ans[k] = min(ans[k], primes[i] - x); + } + if (i >= 1) { + ans[k] = min(ans[k], x - primes[i - 1]); + } + } + return ans; + } +}; ``` #### Go ```go +var primes []int + +func init() { + N := 1 << 14 + for i := 0; i < N; i++ { + s := strconv.FormatInt(int64(i), 2) + if isPalindrome(s) { + primes = append(primes, i) + } + } +} + +func isPalindrome(s string) bool { + runes := []rune(s) + for i := 0; i < len(runes)/2; i++ { + if runes[i] != runes[len(runes)-1-i] { + return false + } + } + return true +} + +func minOperations(nums []int) []int { + ans := make([]int, len(nums)) + for k, x := range nums { + i := sort.SearchInts(primes, x) + t := math.MaxInt32 + if i < len(primes) { + t = primes[i] - x + } + if i >= 1 { + t = min(t, x-primes[i-1]) + } + ans[k] = t + } + return ans +} +``` +#### TypeScript + +```ts +const primes: number[] = (() => { + const res: number[] = []; + const N = 1 << 14; + for (let i = 0; i < N; i++) { + const s = i.toString(2); + if (s === s.split('').reverse().join('')) { + res.push(i); + } + } + return res; +})(); + +function minOperations(nums: number[]): number[] { + const ans: number[] = Array(nums.length).fill(Number.MAX_SAFE_INTEGER); + + for (let k = 0; k < nums.length; k++) { + const x = nums[k]; + const i = _.sortedIndex(primes, x); + if (i < primes.length) { + ans[k] = primes[i] - x; + } + if (i >= 1) { + ans[k] = Math.min(ans[k], x - primes[i - 1]); + } + } + + return ans; +} ``` diff --git a/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.cpp b/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.cpp new file mode 100644 index 0000000000000..72d68a8d88958 --- /dev/null +++ b/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.cpp @@ -0,0 +1,34 @@ +vector primes; + +auto init = [] { + int N = 1 << 14; + for (int i = 0; i < N; ++i) { + string s = bitset<14>(i).to_string(); + s = s.substr(s.find_first_not_of('0') == string::npos ? 13 : s.find_first_not_of('0')); + string rs = s; + reverse(rs.begin(), rs.end()); + if (s == rs) { + primes.push_back(i); + } + } + return 0; +}(); + +class Solution { +public: + vector minOperations(vector& nums) { + int n = nums.size(); + vector ans(n, INT_MAX); + for (int k = 0; k < n; ++k) { + int x = nums[k]; + int i = lower_bound(primes.begin(), primes.end(), x) - primes.begin(); + if (i < (int) primes.size()) { + ans[k] = min(ans[k], primes[i] - x); + } + if (i >= 1) { + ans[k] = min(ans[k], x - primes[i - 1]); + } + } + return ans; + } +}; diff --git a/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.go b/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.go new file mode 100644 index 0000000000000..21ac84f658888 --- /dev/null +++ b/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.go @@ -0,0 +1,37 @@ +var primes []int + +func init() { + N := 1 << 14 + for i := 0; i < N; i++ { + s := strconv.FormatInt(int64(i), 2) + if isPalindrome(s) { + primes = append(primes, i) + } + } +} + +func isPalindrome(s string) bool { + runes := []rune(s) + for i := 0; i < len(runes)/2; i++ { + if runes[i] != runes[len(runes)-1-i] { + return false + } + } + return true +} + +func minOperations(nums []int) []int { + ans := make([]int, len(nums)) + for k, x := range nums { + i := sort.SearchInts(primes, x) + t := math.MaxInt32 + if i < len(primes) { + t = primes[i] - x + } + if i >= 1 { + t = min(t, x-primes[i-1]) + } + ans[k] = t + } + return ans +} diff --git a/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.java b/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.java new file mode 100644 index 0000000000000..7fdfc0e56e781 --- /dev/null +++ b/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.java @@ -0,0 +1,45 @@ +class Solution { + private static final List primes = new ArrayList<>(); + + static { + int N = 1 << 14; + for (int i = 0; i < N; i++) { + String s = Integer.toBinaryString(i); + String rs = new StringBuilder(s).reverse().toString(); + if (s.equals(rs)) { + primes.add(i); + } + } + } + + public int[] minOperations(int[] nums) { + int n = nums.length; + int[] ans = new int[n]; + Arrays.fill(ans, Integer.MAX_VALUE); + for (int k = 0; k < n; ++k) { + int x = nums[k]; + int i = binarySearch(primes, x); + if (i < primes.size()) { + ans[k] = Math.min(ans[k], primes.get(i) - x); + } + if (i >= 1) { + ans[k] = Math.min(ans[k], x - primes.get(i - 1)); + } + } + + return ans; + } + + private int binarySearch(List primes, int x) { + int l = 0, r = primes.size(); + while (l < r) { + int mid = (l + r) >>> 1; + if (primes.get(mid) >= x) { + r = mid; + } else { + l = mid + 1; + } + } + return l; + } +} diff --git a/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.py b/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.py new file mode 100644 index 0000000000000..5833ab90722b7 --- /dev/null +++ b/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.py @@ -0,0 +1,19 @@ +primes = [] +for i in range(1 << 14): + s = bin(i)[2:] + if s == s[::-1]: + primes.append(i) + + +class Solution: + def minOperations(self, nums: List[int]) -> List[int]: + ans = [] + for x in nums: + i = bisect_left(primes, x) + times = inf + if i < len(primes): + times = min(times, primes[i] - x) + if i >= 1: + times = min(times, x - primes[i - 1]) + ans.append(times) + return ans diff --git a/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.ts b/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.ts new file mode 100644 index 0000000000000..59a54fcfc1750 --- /dev/null +++ b/solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/Solution.ts @@ -0,0 +1,28 @@ +const primes: number[] = (() => { + const res: number[] = []; + const N = 1 << 14; + for (let i = 0; i < N; i++) { + const s = i.toString(2); + if (s === s.split('').reverse().join('')) { + res.push(i); + } + } + return res; +})(); + +function minOperations(nums: number[]): number[] { + const ans: number[] = Array(nums.length).fill(Number.MAX_SAFE_INTEGER); + + for (let k = 0; k < nums.length; k++) { + const x = nums[k]; + const i = _.sortedIndex(primes, x); + if (i < primes.length) { + ans[k] = primes[i] - x; + } + if (i >= 1) { + ans[k] = Math.min(ans[k], x - primes[i - 1]); + } + } + + return ans; +} diff --git a/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/README.md b/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/README.md index 0973f59b65aa3..0dbe40d2cb74f 100644 --- a/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/README.md +++ b/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/README.md @@ -90,32 +90,168 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3767.Ma -### 方法一 +### 方法一:贪心 + 排序 + +我们可以先将所有任务都分配给技巧 2,因此初始总分数为 $\sum_{i=0}^{n-1} technique2[i]$。 + +然后,我们计算每个任务如果改为使用技巧 1 完成所能增加的分数,记为 $\text{diff}[i] = technique1[i] - technique2[i]$。我们将其按照从大到小排序,得到任务索引的排序数组 $\text{idx}$。 + +接下来,我们选择前 $k$ 个任务使用技巧 1 完成,并将它们的分数差值加到总分数中。对于剩余的任务,如果某个任务使用技巧 1 完成能够增加分数(即 $\text{diff}[i] \geq 0$),我们也将其选择为使用技巧 1 完成。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是任务的数量。 #### Python3 ```python - +class Solution: + def maxPoints(self, technique1: List[int], technique2: List[int], k: int) -> int: + n = len(technique1) + idx = sorted(range(n), key=lambda i: -(technique1[i] - technique2[i])) + ans = sum(technique2) + for i in idx[:k]: + ans -= technique2[i] + ans += technique1[i] + for i in idx[k:]: + if technique1[i] >= technique2[i]: + ans -= technique2[i] + ans += technique1[i] + return ans ``` #### Java ```java - +class Solution { + public long maxPoints(int[] technique1, int[] technique2, int k) { + int n = technique1.length; + Integer[] idx = new Integer[n]; + Arrays.setAll(idx, i -> i); + Arrays.sort(idx, (i, j) -> technique1[j] - technique2[j] - (technique1[i] - technique2[i])); + long ans = 0; + for (int x : technique2) { + ans += x; + } + for (int i = 0; i < k; i++) { + int index = idx[i]; + ans -= technique2[index]; + ans += technique1[index]; + } + for (int i = k; i < n; i++) { + int index = idx[i]; + if (technique1[index] >= technique2[index]) { + ans -= technique2[index]; + ans += technique1[index]; + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long maxPoints(vector& technique1, vector& technique2, int k) { + int n = technique1.size(); + vector idx(n); + iota(idx.begin(), idx.end(), 0); + + sort(idx.begin(), idx.end(), [&](int i, int j) { + return (technique1[j] - technique2[j]) < (technique1[i] - technique2[i]); + }); + + long long ans = 0; + for (int x : technique2) { + ans += x; + } + + for (int i = 0; i < k; i++) { + int index = idx[i]; + ans -= technique2[index]; + ans += technique1[index]; + } + + for (int i = k; i < n; i++) { + int index = idx[i]; + if (technique1[index] >= technique2[index]) { + ans -= technique2[index]; + ans += technique1[index]; + } + } + + return ans; + } +}; ``` #### Go ```go +func maxPoints(technique1 []int, technique2 []int, k int) int64 { + n := len(technique1) + idx := make([]int, n) + for i := 0; i < n; i++ { + idx[i] = i + } + + sort.Slice(idx, func(i, j int) bool { + return technique1[idx[j]]-technique2[idx[j]] < technique1[idx[i]]-technique2[idx[i]] + }) + + var ans int64 + for _, x := range technique2 { + ans += int64(x) + } + + for i := 0; i < k; i++ { + index := idx[i] + ans -= int64(technique2[index]) + ans += int64(technique1[index]) + } + + for i := k; i < n; i++ { + index := idx[i] + if technique1[index] >= technique2[index] { + ans -= int64(technique2[index]) + ans += int64(technique1[index]) + } + } + + return ans +} +``` + +#### TypeScript + +```ts +function maxPoints(technique1: number[], technique2: number[], k: number): number { + const n = technique1.length; + const idx = Array.from({ length: n }, (_, i) => i); + + idx.sort((i, j) => technique1[j] - technique2[j] - (technique1[i] - technique2[i])); + + let ans = technique2.reduce((sum, x) => sum + x, 0); + + for (let i = 0; i < k; i++) { + const index = idx[i]; + ans -= technique2[index]; + ans += technique1[index]; + } + + for (let i = k; i < n; i++) { + const index = idx[i]; + if (technique1[index] >= technique2[index]) { + ans -= technique2[index]; + ans += technique1[index]; + } + } + return ans; +} ``` diff --git a/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/README_EN.md b/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/README_EN.md index d740b5c1e3fc5..e7ad5cbf5adb4 100644 --- a/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/README_EN.md +++ b/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/README_EN.md @@ -88,32 +88,168 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3767.Ma -### Solution 1 +### Solution 1: Greedy + Sorting + +We can first assign all tasks to technique 2, so the initial total score is $\sum_{i=0}^{n-1} technique2[i]$. + +Then, we calculate the score increase for each task if it were completed using technique 1 instead, denoted as $\text{diff}[i] = technique1[i] - technique2[i]$. We sort this in descending order to obtain a sorted array of task indices $\text{idx}$. + +Next, we select the first $k$ tasks to be completed using technique 1 and add their score differences to the total score. For the remaining tasks, if a task can increase the score by using technique 1 (i.e., $\text{diff}[i] \geq 0$), we also choose to complete it using technique 1. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the number of tasks. #### Python3 ```python - +class Solution: + def maxPoints(self, technique1: List[int], technique2: List[int], k: int) -> int: + n = len(technique1) + idx = sorted(range(n), key=lambda i: -(technique1[i] - technique2[i])) + ans = sum(technique2) + for i in idx[:k]: + ans -= technique2[i] + ans += technique1[i] + for i in idx[k:]: + if technique1[i] >= technique2[i]: + ans -= technique2[i] + ans += technique1[i] + return ans ``` #### Java ```java - +class Solution { + public long maxPoints(int[] technique1, int[] technique2, int k) { + int n = technique1.length; + Integer[] idx = new Integer[n]; + Arrays.setAll(idx, i -> i); + Arrays.sort(idx, (i, j) -> technique1[j] - technique2[j] - (technique1[i] - technique2[i])); + long ans = 0; + for (int x : technique2) { + ans += x; + } + for (int i = 0; i < k; i++) { + int index = idx[i]; + ans -= technique2[index]; + ans += technique1[index]; + } + for (int i = k; i < n; i++) { + int index = idx[i]; + if (technique1[index] >= technique2[index]) { + ans -= technique2[index]; + ans += technique1[index]; + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long maxPoints(vector& technique1, vector& technique2, int k) { + int n = technique1.size(); + vector idx(n); + iota(idx.begin(), idx.end(), 0); + + sort(idx.begin(), idx.end(), [&](int i, int j) { + return (technique1[j] - technique2[j]) < (technique1[i] - technique2[i]); + }); + + long long ans = 0; + for (int x : technique2) { + ans += x; + } + + for (int i = 0; i < k; i++) { + int index = idx[i]; + ans -= technique2[index]; + ans += technique1[index]; + } + + for (int i = k; i < n; i++) { + int index = idx[i]; + if (technique1[index] >= technique2[index]) { + ans -= technique2[index]; + ans += technique1[index]; + } + } + + return ans; + } +}; ``` #### Go ```go +func maxPoints(technique1 []int, technique2 []int, k int) int64 { + n := len(technique1) + idx := make([]int, n) + for i := 0; i < n; i++ { + idx[i] = i + } + + sort.Slice(idx, func(i, j int) bool { + return technique1[idx[j]]-technique2[idx[j]] < technique1[idx[i]]-technique2[idx[i]] + }) + + var ans int64 + for _, x := range technique2 { + ans += int64(x) + } + + for i := 0; i < k; i++ { + index := idx[i] + ans -= int64(technique2[index]) + ans += int64(technique1[index]) + } + + for i := k; i < n; i++ { + index := idx[i] + if technique1[index] >= technique2[index] { + ans -= int64(technique2[index]) + ans += int64(technique1[index]) + } + } + + return ans +} +``` + +#### TypeScript + +```ts +function maxPoints(technique1: number[], technique2: number[], k: number): number { + const n = technique1.length; + const idx = Array.from({ length: n }, (_, i) => i); + + idx.sort((i, j) => technique1[j] - technique2[j] - (technique1[i] - technique2[i])); + + let ans = technique2.reduce((sum, x) => sum + x, 0); + + for (let i = 0; i < k; i++) { + const index = idx[i]; + ans -= technique2[index]; + ans += technique1[index]; + } + + for (let i = k; i < n; i++) { + const index = idx[i]; + if (technique1[index] >= technique2[index]) { + ans -= technique2[index]; + ans += technique1[index]; + } + } + return ans; +} ``` diff --git a/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.cpp b/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.cpp new file mode 100644 index 0000000000000..c41c323bc176b --- /dev/null +++ b/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + long long maxPoints(vector& technique1, vector& technique2, int k) { + int n = technique1.size(); + vector idx(n); + iota(idx.begin(), idx.end(), 0); + + sort(idx.begin(), idx.end(), [&](int i, int j) { + return (technique1[j] - technique2[j]) < (technique1[i] - technique2[i]); + }); + + long long ans = 0; + for (int x : technique2) { + ans += x; + } + + for (int i = 0; i < k; i++) { + int index = idx[i]; + ans -= technique2[index]; + ans += technique1[index]; + } + + for (int i = k; i < n; i++) { + int index = idx[i]; + if (technique1[index] >= technique2[index]) { + ans -= technique2[index]; + ans += technique1[index]; + } + } + + return ans; + } +}; diff --git a/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.go b/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.go new file mode 100644 index 0000000000000..2800477f08a84 --- /dev/null +++ b/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.go @@ -0,0 +1,32 @@ +func maxPoints(technique1 []int, technique2 []int, k int) int64 { + n := len(technique1) + idx := make([]int, n) + for i := 0; i < n; i++ { + idx[i] = i + } + + sort.Slice(idx, func(i, j int) bool { + return technique1[idx[j]]-technique2[idx[j]] < technique1[idx[i]]-technique2[idx[i]] + }) + + var ans int64 + for _, x := range technique2 { + ans += int64(x) + } + + for i := 0; i < k; i++ { + index := idx[i] + ans -= int64(technique2[index]) + ans += int64(technique1[index]) + } + + for i := k; i < n; i++ { + index := idx[i] + if technique1[index] >= technique2[index] { + ans -= int64(technique2[index]) + ans += int64(technique1[index]) + } + } + + return ans +} diff --git a/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.java b/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.java new file mode 100644 index 0000000000000..6470efb1e7eb6 --- /dev/null +++ b/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.java @@ -0,0 +1,25 @@ +class Solution { + public long maxPoints(int[] technique1, int[] technique2, int k) { + int n = technique1.length; + Integer[] idx = new Integer[n]; + Arrays.setAll(idx, i -> i); + Arrays.sort(idx, (i, j) -> technique1[j] - technique2[j] - (technique1[i] - technique2[i])); + long ans = 0; + for (int x : technique2) { + ans += x; + } + for (int i = 0; i < k; i++) { + int index = idx[i]; + ans -= technique2[index]; + ans += technique1[index]; + } + for (int i = k; i < n; i++) { + int index = idx[i]; + if (technique1[index] >= technique2[index]) { + ans -= technique2[index]; + ans += technique1[index]; + } + } + return ans; + } +} diff --git a/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.py b/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.py new file mode 100644 index 0000000000000..4eb3fdbef157f --- /dev/null +++ b/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.py @@ -0,0 +1,13 @@ +class Solution: + def maxPoints(self, technique1: List[int], technique2: List[int], k: int) -> int: + n = len(technique1) + idx = sorted(range(n), key=lambda i: -(technique1[i] - technique2[i])) + ans = sum(technique2) + for i in idx[:k]: + ans -= technique2[i] + ans += technique1[i] + for i in idx[k:]: + if technique1[i] >= technique2[i]: + ans -= technique2[i] + ans += technique1[i] + return ans diff --git a/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.ts b/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.ts new file mode 100644 index 0000000000000..2605d3ac75deb --- /dev/null +++ b/solution/3700-3799/3767.Maximize Points After Choosing K Tasks/Solution.ts @@ -0,0 +1,24 @@ +function maxPoints(technique1: number[], technique2: number[], k: number): number { + const n = technique1.length; + const idx = Array.from({ length: n }, (_, i) => i); + + idx.sort((i, j) => technique1[j] - technique2[j] - (technique1[i] - technique2[i])); + + let ans = technique2.reduce((sum, x) => sum + x, 0); + + for (let i = 0; i < k; i++) { + const index = idx[i]; + ans -= technique2[index]; + ans += technique1[index]; + } + + for (let i = k; i < n; i++) { + const index = idx[i]; + if (technique1[index] >= technique2[index]) { + ans -= technique2[index]; + ans += technique1[index]; + } + } + + return ans; +}