From b8dec4a24ecf9f1700fcdce83eed87e1668d8197 Mon Sep 17 00:00:00 2001 From: Samarth Swami Date: Sun, 20 Jul 2025 19:06:30 +0530 Subject: [PATCH 01/15] feat: add solutions to lc problem: No.1960 --- .../README.md | 207 +++++++++++++++++- .../README_EN.md | 206 ++++++++++++++++- .../solution..java | 53 +++++ .../solution.cpp | 37 ++++ .../solution.go | 71 ++++++ .../solution.py | 34 +++ 6 files changed, 601 insertions(+), 7 deletions(-) create mode 100644 solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution..java create mode 100644 solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.cpp create mode 100644 solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.go create mode 100644 solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.py diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README.md b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README.md index b918ee2cb015b..a188265b21356 100644 --- a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README.md +++ b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README.md @@ -5,9 +5,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/1900-1999/1960.Ma rating: 2690 source: 第 58 场双周赛 Q4 tags: - - 字符串 - - 哈希函数 - - 滚动哈希 + - 字符串 + - 哈希函数 + - 滚动哈希 --- @@ -69,28 +69,227 @@ tags: ```python +class Solution: + def maxProduct(self, s: str) -> int: + n = len(s) + hlen = [0] * n + center = right = 0 + + for i in range(n): + if i < right: + hlen[i] = min(right - i, hlen[2 * center - i]) + while ( + 0 <= i - 1 - hlen[i] + and i + 1 + hlen[i] < len(s) + and s[i - 1 - hlen[i]] == s[i + 1 + hlen[i]] + ): + hlen[i] += 1 + if right < i + hlen[i]: + center, right = i, i + hlen[i] + + prefix = [0] * n + suffix = [0] * n + + for i in range(n): + prefix[i + hlen[i]] = max(prefix[i + hlen[i]], 2 * hlen[i] + 1) + suffix[i - hlen[i]] = max(suffix[i - hlen[i]], 2 * hlen[i] + 1) + + for i in range(1, n): + prefix[~i] = max(prefix[~i], prefix[~i + 1] - 2) + suffix[i] = max(suffix[i], suffix[i - 1] - 2) + + for i in range(1, n): + prefix[i] = max(prefix[i - 1], prefix[i]) + suffix[~i] = max(suffix[~i], suffix[~i + 1]) + + return max(prefix[i - 1] * suffix[i] for i in range(1, n)) + ``` #### Java ```java +class Solution { + public long maxProduct(String s) { + int n = s.length(); + if (n == 2) return 1; + int[] len = manachers(s); + long[] left = new long[n]; + int max = 1; + left[0] = max; + for (int i = 1; i <= n - 1; i++) { + if (len[(i - max - 1 + i) / 2] > max) max += 2; + left[i] = max; + } + max = 1; + long[] right = new long[n]; + right[n - 1] = max; + for (int i = n - 2; i >= 0; i--) { + if (len[(i + max + 1 + i) / 2] > max) max += 2; + right[i] = max; + } + long res = 1; + for (int i = 1; i < n; i++) { + res = Math.max(res, left[i - 1] * right[i]); + } + return res; + } + private int[] manachers(String s) { + int len = s.length(); + int[] P = new int[len]; + int c = 0; + int r = 0; + for (int i = 0; i < len; i++) { + int mirror = (2 * c) - i; + if (i < r) { + P[i] = Math.min(r - i, P[mirror]); + } + int a = i + (1 + P[i]); + int b = i - (1 + P[i]); + while (a < len && b >= 0 && s.charAt(a) == s.charAt(b)) { + P[i]++; + a++; + b--; + } + if (i + P[i] > r) { + c = i; + r = i + P[i]; + } + } + for (int i = 0; i < len; i++) { + P[i] = 1 + 2 * P[i]; + } + return P; + } +} + ``` #### C++ ```cpp +class Solution { +public: + long long maxProduct(string s) { + long long res = 0, l = 0, n = s.size(); + vector m(n), r(n); + + for (int i = 0, l = 0, r = -1; i < n; ++i) { + int k = (i > r) ? 1 : min(m[l + r - i], r - i + 1); + while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) + k++; + m[i] = k--; + if (i + k > r) { + l = i - k; + r = i + k; + } + } + + queue> q, q1; + + for (int i = n - 1; i >= 0; --i) { + while (!q.empty() && q.front()[0] - q.front()[1] > i - 1) + q.pop(); + r[i] = 1 + (q.empty() ? 0 : (q.front()[0] - i) * 2); + q.push({i, m[i]}); + } + + for (int i = 0; i < n - 1; i++) { + while (!q1.empty() && q1.front()[0] + q1.front()[1] < i + 1) + q1.pop(); + l = max(l, 1ll + (q1.empty() ? 0 : (i - q1.front()[0]) * 2)); + res = max(res, l * r[i + 1]); + q1.push({i, m[i]}); + } + + return res; + } +}; + ``` #### Go ```go +func maxProduct(s string) int64 { + n := len(s) + hlen := make([]int, n) + center, right := 0, 0 + + for i := 0; i < n; i++ { + if i < right { + mirror := 2*center - i + if mirror >= 0 && mirror < n { + hlen[i] = min(right-i, hlen[mirror]) + } + } + for i-1-hlen[i] >= 0 && i+1+hlen[i] < n && s[i-1-hlen[i]] == s[i+1+hlen[i]] { + hlen[i]++ + } + if i+hlen[i] > right { + center = i + right = i + hlen[i] + } + } + + prefix := make([]int, n) + suffix := make([]int, n) + + for i := 0; i < n; i++ { + r := i + hlen[i] + if r < n { + prefix[r] = max(prefix[r], 2*hlen[i]+1) + } + l := i - hlen[i] + if l >= 0 { + suffix[l] = max(suffix[l], 2*hlen[i]+1) + } + } + + for i := 1; i < n; i++ { + if n-i-1 >= 0 { + prefix[n-i-1] = max(prefix[n-i-1], prefix[n-i]-2) + } + suffix[i] = max(suffix[i], suffix[i-1]-2) + } + + for i := 1; i < n; i++ { + prefix[i] = max(prefix[i-1], prefix[i]) + suffix[n-i-1] = max(suffix[n-i], suffix[n-i-1]) + } + + var res int64 + for i := 1; i < n; i++ { + prod := int64(prefix[i-1]) * int64(suffix[i]) + if prod > res { + res = prod + } + } + + return res +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} + ``` - + \ No newline at end of file diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README_EN.md b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README_EN.md index 044c570412d56..25ed9132234c7 100644 --- a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README_EN.md +++ b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README_EN.md @@ -5,9 +5,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/1900-1999/1960.Ma rating: 2690 source: Biweekly Contest 58 Q4 tags: - - String - - Hash Function - - Rolling Hash + - String + - Hash Function + - Rolling Hash --- @@ -67,24 +67,224 @@ tags: ```python +class Solution: + def maxProduct(self, s: str) -> int: + n = len(s) + hlen = [0] * n + center = right = 0 + + for i in range(n): + if i < right: + hlen[i] = min(right - i, hlen[2 * center - i]) + while ( + 0 <= i - 1 - hlen[i] + and i + 1 + hlen[i] < len(s) + and s[i - 1 - hlen[i]] == s[i + 1 + hlen[i]] + ): + hlen[i] += 1 + if right < i + hlen[i]: + center, right = i, i + hlen[i] + + prefix = [0] * n + suffix = [0] * n + + for i in range(n): + prefix[i + hlen[i]] = max(prefix[i + hlen[i]], 2 * hlen[i] + 1) + suffix[i - hlen[i]] = max(suffix[i - hlen[i]], 2 * hlen[i] + 1) + + for i in range(1, n): + prefix[~i] = max(prefix[~i], prefix[~i + 1] - 2) + suffix[i] = max(suffix[i], suffix[i - 1] - 2) + + for i in range(1, n): + prefix[i] = max(prefix[i - 1], prefix[i]) + suffix[~i] = max(suffix[~i], suffix[~i + 1]) + + return max(prefix[i - 1] * suffix[i] for i in range(1, n)) + + ``` #### Java ```java +class Solution { + public long maxProduct(String s) { + int n = s.length(); + if (n == 2) return 1; + int[] len = manachers(s); + long[] left = new long[n]; + int max = 1; + left[0] = max; + for (int i = 1; i <= n - 1; i++) { + if (len[(i - max - 1 + i) / 2] > max) max += 2; + left[i] = max; + } + max = 1; + long[] right = new long[n]; + right[n - 1] = max; + for (int i = n - 2; i >= 0; i--) { + if (len[(i + max + 1 + i) / 2] > max) max += 2; + right[i] = max; + } + long res = 1; + for (int i = 1; i < n; i++) { + res = Math.max(res, left[i - 1] * right[i]); + } + return res; + } + private int[] manachers(String s) { + int len = s.length(); + int[] P = new int[len]; + int c = 0; + int r = 0; + for (int i = 0; i < len; i++) { + int mirror = (2 * c) - i; + if (i < r) { + P[i] = Math.min(r - i, P[mirror]); + } + int a = i + (1 + P[i]); + int b = i - (1 + P[i]); + while (a < len && b >= 0 && s.charAt(a) == s.charAt(b)) { + P[i]++; + a++; + b--; + } + if (i + P[i] > r) { + c = i; + r = i + P[i]; + } + } + for (int i = 0; i < len; i++) { + P[i] = 1 + 2 * P[i]; + } + return P; + } +} + ``` #### C++ ```cpp +class Solution { +public: + long long maxProduct(string s) { + long long res = 0, l = 0, n = s.size(); + vector m(n), r(n); + + for (int i = 0, l = 0, r = -1; i < n; ++i) { + int k = (i > r) ? 1 : min(m[l + r - i], r - i + 1); + while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) + k++; + m[i] = k--; + if (i + k > r) { + l = i - k; + r = i + k; + } + } + + queue> q, q1; + + for (int i = n - 1; i >= 0; --i) { + while (!q.empty() && q.front()[0] - q.front()[1] > i - 1) + q.pop(); + r[i] = 1 + (q.empty() ? 0 : (q.front()[0] - i) * 2); + q.push({i, m[i]}); + } + + for (int i = 0; i < n - 1; i++) { + while (!q1.empty() && q1.front()[0] + q1.front()[1] < i + 1) + q1.pop(); + l = max(l, 1ll + (q1.empty() ? 0 : (i - q1.front()[0]) * 2)); + res = max(res, l * r[i + 1]); + q1.push({i, m[i]}); + } + + return res; + } +}; + ``` #### Go ```go +func maxProduct(s string) int64 { + n := len(s) + hlen := make([]int, n) + center, right := 0, 0 + + for i := 0; i < n; i++ { + if i < right { + mirror := 2*center - i + if mirror >= 0 && mirror < n { + hlen[i] = min(right-i, hlen[mirror]) + } + } + for i-1-hlen[i] >= 0 && i+1+hlen[i] < n && s[i-1-hlen[i]] == s[i+1+hlen[i]] { + hlen[i]++ + } + if i+hlen[i] > right { + center = i + right = i + hlen[i] + } + } + + prefix := make([]int, n) + suffix := make([]int, n) + + for i := 0; i < n; i++ { + r := i + hlen[i] + if r < n { + prefix[r] = max(prefix[r], 2*hlen[i]+1) + } + l := i - hlen[i] + if l >= 0 { + suffix[l] = max(suffix[l], 2*hlen[i]+1) + } + } + + for i := 1; i < n; i++ { + if n-i-1 >= 0 { + prefix[n-i-1] = max(prefix[n-i-1], prefix[n-i]-2) + } + suffix[i] = max(suffix[i], suffix[i-1]-2) + } + + for i := 1; i < n; i++ { + prefix[i] = max(prefix[i-1], prefix[i]) + suffix[n-i-1] = max(suffix[n-i], suffix[n-i-1]) + } + + var res int64 + for i := 1; i < n; i++ { + prod := int64(prefix[i-1]) * int64(suffix[i]) + if prod > res { + res = prod + } + } + + return res +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} + ``` diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution..java b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution..java new file mode 100644 index 0000000000000..7f6946576cbae --- /dev/null +++ b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution..java @@ -0,0 +1,53 @@ +class Solution { + public long maxProduct(String s) { + int n = s.length(); + if (n == 2) return 1; + int[] len = manachers(s); + long[] left = new long[n]; + int max = 1; + left[0] = max; + for (int i = 1; i <= n - 1; i++) { + if (len[(i - max - 1 + i) / 2] > max) max += 2; + left[i] = max; + } + max = 1; + long[] right = new long[n]; + right[n - 1] = max; + for (int i = n - 2; i >= 0; i--) { + if (len[(i + max + 1 + i) / 2] > max) max += 2; + right[i] = max; + } + long res = 1; + for (int i = 1; i < n; i++) { + res = Math.max(res, left[i - 1] * right[i]); + } + return res; + } + private int[] manachers(String s) { + int len = s.length(); + int[] P = new int[len]; + int c = 0; + int r = 0; + for (int i = 0; i < len; i++) { + int mirror = (2 * c) - i; + if (i < r) { + P[i] = Math.min(r - i, P[mirror]); + } + int a = i + (1 + P[i]); + int b = i - (1 + P[i]); + while (a < len && b >= 0 && s.charAt(a) == s.charAt(b)) { + P[i]++; + a++; + b--; + } + if (i + P[i] > r) { + c = i; + r = i + P[i]; + } + } + for (int i = 0; i < len; i++) { + P[i] = 1 + 2 * P[i]; + } + return P; + } +} diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.cpp b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.cpp new file mode 100644 index 0000000000000..c287fd98d7883 --- /dev/null +++ b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + long long maxProduct(string s) { + long long res = 0, l = 0, n = s.size(); + vector m(n), r(n); + + for (int i = 0, l = 0, r = -1; i < n; ++i) { + int k = (i > r) ? 1 : min(m[l + r - i], r - i + 1); + while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) + k++; + m[i] = k--; + if (i + k > r) { + l = i - k; + r = i + k; + } + } + + queue> q, q1; + + for (int i = n - 1; i >= 0; --i) { + while (!q.empty() && q.front()[0] - q.front()[1] > i - 1) + q.pop(); + r[i] = 1 + (q.empty() ? 0 : (q.front()[0] - i) * 2); + q.push({i, m[i]}); + } + + for (int i = 0; i < n - 1; i++) { + while (!q1.empty() && q1.front()[0] + q1.front()[1] < i + 1) + q1.pop(); + l = max(l, 1ll + (q1.empty() ? 0 : (i - q1.front()[0]) * 2)); + res = max(res, l * r[i + 1]); + q1.push({i, m[i]}); + } + + return res; + } +}; diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.go b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.go new file mode 100644 index 0000000000000..b323a2ddcc6f6 --- /dev/null +++ b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.go @@ -0,0 +1,71 @@ +func maxProduct(s string) int64 { + n := len(s) + hlen := make([]int, n) + center, right := 0, 0 + + for i := 0; i < n; i++ { + if i < right { + mirror := 2*center - i + if mirror >= 0 && mirror < n { + hlen[i] = min(right-i, hlen[mirror]) + } + } + for i-1-hlen[i] >= 0 && i+1+hlen[i] < n && s[i-1-hlen[i]] == s[i+1+hlen[i]] { + hlen[i]++ + } + if i+hlen[i] > right { + center = i + right = i + hlen[i] + } + } + + prefix := make([]int, n) + suffix := make([]int, n) + + for i := 0; i < n; i++ { + r := i + hlen[i] + if r < n { + prefix[r] = max(prefix[r], 2*hlen[i]+1) + } + l := i - hlen[i] + if l >= 0 { + suffix[l] = max(suffix[l], 2*hlen[i]+1) + } + } + + for i := 1; i < n; i++ { + if n-i-1 >= 0 { + prefix[n-i-1] = max(prefix[n-i-1], prefix[n-i]-2) + } + suffix[i] = max(suffix[i], suffix[i-1]-2) + } + + for i := 1; i < n; i++ { + prefix[i] = max(prefix[i-1], prefix[i]) + suffix[n-i-1] = max(suffix[n-i], suffix[n-i-1]) + } + + var res int64 + for i := 1; i < n; i++ { + prod := int64(prefix[i-1]) * int64(suffix[i]) + if prod > res { + res = prod + } + } + + return res +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.py b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.py new file mode 100644 index 0000000000000..196786c37fecd --- /dev/null +++ b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.py @@ -0,0 +1,34 @@ +class Solution: + def maxProduct(self, s: str) -> int: + n = len(s) + hlen = [0] * n + center = right = 0 + + for i in range(n): + if i < right: + hlen[i] = min(right - i, hlen[2 * center - i]) + while ( + 0 <= i - 1 - hlen[i] + and i + 1 + hlen[i] < len(s) + and s[i - 1 - hlen[i]] == s[i + 1 + hlen[i]] + ): + hlen[i] += 1 + if right < i + hlen[i]: + center, right = i, i + hlen[i] + + prefix = [0] * n + suffix = [0] * n + + for i in range(n): + prefix[i + hlen[i]] = max(prefix[i + hlen[i]], 2 * hlen[i] + 1) + suffix[i - hlen[i]] = max(suffix[i - hlen[i]], 2 * hlen[i] + 1) + + for i in range(1, n): + prefix[~i] = max(prefix[~i], prefix[~i + 1] - 2) + suffix[i] = max(suffix[i], suffix[i - 1] - 2) + + for i in range(1, n): + prefix[i] = max(prefix[i - 1], prefix[i]) + suffix[~i] = max(suffix[~i], suffix[~i + 1]) + + return max(prefix[i - 1] * suffix[i] for i in range(1, n)) From 6456b6a3cbfde4e8e4fe3ad58738dcd4e11d4740 Mon Sep 17 00:00:00 2001 From: samarthswami1016 <70163465+samarthswami1016@users.noreply.github.com> Date: Sun, 20 Jul 2025 15:24:49 +0000 Subject: [PATCH 02/15] style: format code and docs with prettier --- .../README.md | 8 ++++---- .../README_EN.md | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README.md b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README.md index a188265b21356..5076cfb5e8fa4 100644 --- a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README.md +++ b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README.md @@ -5,9 +5,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/1900-1999/1960.Ma rating: 2690 source: 第 58 场双周赛 Q4 tags: - - 字符串 - - 哈希函数 - - 滚动哈希 + - 字符串 + - 哈希函数 + - 滚动哈希 --- @@ -292,4 +292,4 @@ func min(a, b int) int { - \ No newline at end of file + diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README_EN.md b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README_EN.md index 25ed9132234c7..566dbdcaab300 100644 --- a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README_EN.md +++ b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README_EN.md @@ -5,9 +5,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/1900-1999/1960.Ma rating: 2690 source: Biweekly Contest 58 Q4 tags: - - String - - Hash Function - - Rolling Hash + - String + - Hash Function + - Rolling Hash --- From 2f2b1da34090dbdcf99492757123ae5b2f5395e5 Mon Sep 17 00:00:00 2001 From: Samarth Swami Date: Tue, 22 Jul 2025 14:27:18 +0530 Subject: [PATCH 03/15] Rename solution..java to Solution..java --- .../{solution..java => Solution..java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/{solution..java => Solution..java} (100%) diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution..java b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution..java similarity index 100% rename from solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution..java rename to solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution..java From 3833ea4128e0d11dc5e349ec2b259bc18ba6fd08 Mon Sep 17 00:00:00 2001 From: Samarth Swami Date: Tue, 22 Jul 2025 14:27:54 +0530 Subject: [PATCH 04/15] Rename solution.py to Solution.py --- .../{solution.py => Solution.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/{solution.py => Solution.py} (100%) diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.py b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution.py similarity index 100% rename from solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.py rename to solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution.py From 9667fd4268081b92751f9739c3dba5eddda8509c Mon Sep 17 00:00:00 2001 From: Samarth Swami Date: Tue, 22 Jul 2025 14:28:28 +0530 Subject: [PATCH 05/15] Rename solution.cpp to Solution.cpp --- .../{solution.cpp => Solution.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/{solution.cpp => Solution.cpp} (100%) diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.cpp b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution.cpp similarity index 100% rename from solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.cpp rename to solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution.cpp From 213b18df56b4a6e365b6fd89fc165dbe09f301e6 Mon Sep 17 00:00:00 2001 From: Samarth Swami Date: Tue, 22 Jul 2025 14:28:49 +0530 Subject: [PATCH 06/15] Rename solution.go to Solution.go --- .../{solution.go => Solution.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/{solution.go => Solution.go} (100%) diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.go b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution.go similarity index 100% rename from solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/solution.go rename to solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution.go From 505a70673feecfd958f047e0e03517c345a24aac Mon Sep 17 00:00:00 2001 From: Samarth Swami Date: Tue, 22 Jul 2025 14:29:12 +0530 Subject: [PATCH 07/15] Rename Solution..java to Solution.java --- .../{Solution..java => Solution.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/{Solution..java => Solution.java} (100%) diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution..java b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution.java similarity index 100% rename from solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution..java rename to solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution.java From d5c7d54b186480b4a44a9bbcc8851be006c7e2b2 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 23 Jul 2025 06:59:36 +0800 Subject: [PATCH 08/15] Update Solution.go --- .../Solution.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution.go b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution.go index b323a2ddcc6f6..56fa1a39b45cb 100644 --- a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution.go +++ b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/Solution.go @@ -55,17 +55,3 @@ func maxProduct(s string) int64 { return res } - -func max(a, b int) int { - if a > b { - return a - } - return b -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} From f098f2914f244fb24694b193872200b232fca89d Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 23 Jul 2025 07:00:14 +0800 Subject: [PATCH 09/15] Update README.md --- .../README.md | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README.md b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README.md index 5076cfb5e8fa4..f80ef7a0f97eb 100644 --- a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README.md +++ b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README.md @@ -68,7 +68,6 @@ tags: #### Python3 ```python - class Solution: def maxProduct(self, s: str) -> int: n = len(s) @@ -103,13 +102,11 @@ class Solution: suffix[~i] = max(suffix[~i], suffix[~i + 1]) return max(prefix[i - 1] * suffix[i] for i in range(1, n)) - ``` #### Java ```java - class Solution { public long maxProduct(String s) { int n = s.length(); @@ -163,13 +160,11 @@ class Solution { return P; } } - ``` #### C++ ```cpp - class Solution { public: long long maxProduct(string s) { @@ -207,13 +202,11 @@ public: return res; } }; - ``` #### Go ```go - func maxProduct(s string) int64 { n := len(s) hlen := make([]int, n) @@ -271,21 +264,6 @@ func maxProduct(s string) int64 { return res } - -func max(a, b int) int { - if a > b { - return a - } - return b -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - ``` From 54aa98dd0fe7a3a4f0e5424a3aecd8f5322ea792 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 23 Jul 2025 07:00:51 +0800 Subject: [PATCH 10/15] Update README_EN.md --- .../README_EN.md | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README_EN.md b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README_EN.md index 566dbdcaab300..7959a940d8336 100644 --- a/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README_EN.md +++ b/solution/1900-1999/1960.Maximum Product of the Length of Two Palindromic Substrings/README_EN.md @@ -66,7 +66,6 @@ tags: #### Python3 ```python - class Solution: def maxProduct(self, s: str) -> int: n = len(s) @@ -101,14 +100,11 @@ class Solution: suffix[~i] = max(suffix[~i], suffix[~i + 1]) return max(prefix[i - 1] * suffix[i] for i in range(1, n)) - - ``` #### Java ```java - class Solution { public long maxProduct(String s) { int n = s.length(); @@ -162,13 +158,11 @@ class Solution { return P; } } - ``` #### C++ ```cpp - class Solution { public: long long maxProduct(string s) { @@ -206,13 +200,11 @@ public: return res; } }; - ``` #### Go ```go - func maxProduct(s string) int64 { n := len(s) hlen := make([]int, n) @@ -270,21 +262,6 @@ func maxProduct(s string) int64 { return res } - -func max(a, b int) int { - if a > b { - return a - } - return b -} - -func min(a, b int) int { - if a < b { - return a - } - return b -} - ``` From b0a399e6aac6e3146625c9d314894f0349c5db6b Mon Sep 17 00:00:00 2001 From: Samarth Swami Date: Fri, 25 Jul 2025 13:03:36 +0530 Subject: [PATCH 11/15] feat: add solutions to lc problem: No.3139 --- .../README.md | 145 +++++++++++++++++- .../README_EN.md | 145 +++++++++++++++++- .../Solution.cpp | 30 ++++ .../Solution.go | 43 ++++++ .../Solution.java | 32 ++++ .../Solution.py | 35 +++++ 6 files changed, 424 insertions(+), 6 deletions(-) create mode 100644 solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.cpp create mode 100644 solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.go create mode 100644 solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.java create mode 100644 solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.py diff --git a/solution/3100-3199/3139.Minimum Cost to Equalize Array/README.md b/solution/3100-3199/3139.Minimum Cost to Equalize Array/README.md index 4a1856f7904b8..08d717a939afd 100644 --- a/solution/3100-3199/3139.Minimum Cost to Equalize Array/README.md +++ b/solution/3100-3199/3139.Minimum Cost to Equalize Array/README.md @@ -5,9 +5,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3139.Mi rating: 2666 source: 第 396 场周赛 Q4 tags: - - 贪心 - - 数组 - - 枚举 + - 贪心 + - 数组 + - 枚举 --- @@ -121,24 +121,163 @@ tags: ```python +class Solution: + MOD = 10 ** 9 + 7 + + def solve(self, k): + sumDifferences = k * len(self.nums) - self.sumNums + + ones = max(2 * (k - self.minNums) - sumDifferences, 0) + if (sumDifferences - ones) & 1 != 0: + ones += 1 + + return ones * self.cost1 + ((sumDifferences - ones) // 2) * self.cost2 + + def minCostToEqualizeArray(self, nums: List[int], cost1: int, cost2: int) -> int: + cost2 = min(2 * cost1, cost2) + + self.nums = nums + self.minNums = min(nums) + self.sumNums = sum(nums) + self.cost1 = cost1 + self.cost2 = cost2 + + m = max(nums) + + sameParity = range(m, 10 ** 18, 2) + diffParity = range(m + 1, 10 ** 18, 2) + i = bisect_left(sameParity, 0, key=lambda i: self.solve(i + 2) - self.solve(i)) + j = bisect_left(diffParity, 0, key=lambda j: self.solve(j + 2) - self.solve(j)) + + return min(self.solve(sameParity[i]), self.solve(diffParity[j])) % Solution.MOD + ``` #### Java ```java +class Solution { + public int minCostToEqualizeArray(int[] A, int c1, int c2) { + int ma = A[0], mi = A[0], n = A.length, mod = 1000000007; + long total = 0; + for (int a: A) { + mi = Math.min(mi, a); + ma = Math.max(ma, a); + total += a; + } + total = 1l * ma * n - total; + + if (c1 * 2 <= c2 || n <= 2) { + return (int) ((total * c1) % mod); + } + + long op1 = Math.max(0L, (ma - mi) * 2L - total); + long op2 = total - op1; + long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2; + + total += op1 / (n - 2) * n; + op1 %= n - 2; + op2 = total - op1; + res = Math.min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2); + + for (int i = 0; i < 2; i++) { + total += n; + res = Math.min(res, total % 2 * c1 + total / 2 * c2); + } + return (int) (res % mod); + } +} + ``` #### C++ ```cpp +class Solution { +public: + int minCostToEqualizeArray(vector& A, int c1, int c2) { + int ma = *max_element(A.begin(), A.end()); + int mi = *min_element(A.begin(), A.end()); + int n = A.size(), mod = 1000000007; + long long su = accumulate(A.begin(), A.end(), 0LL); + long long total = 1LL * ma * n - su; + + if (c1 * 2 <= c2 || n <= 2) { + return (total * c1) % mod; + } + + long long op1 = max(0LL, (ma - mi) * 2 - total); + long long op2 = total - op1; + long long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2; + + total += op1 / (n - 2) * n; + op1 %= n - 2; + op2 = total - op1; + res = min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2); + + for (int i = 0; i < 2; i++) { + total += n; + res = min(res, total % 2 * c1 + total / 2 * c2); + } + + return res % mod; + } +}; + + ``` #### Go ```go +func minCostToEqualizeArray(nums []int, cost1 int, cost2 int) int { + mx, mod := 0, int(1e9)+7 + for _, v := range nums { + if v > mx { + mx = v + } + } + mv, cnt := 0, 0 + for _, v := range nums { + d := mx - v + if cnt += d; d > mv { + mv = d + } + } + ans := find(mv, cnt, cost1, cost2) + if len(nums) <= 2 { + return ans % mod + } + for (mv<<1) > cnt { + mv, cnt = mv + 1, cnt + len(nums) + ans = min(ans, find(mv, cnt, cost1, cost2)) + } + mv, cnt = mv + 1, cnt + len(nums) + ans = min(ans, find(mv, cnt, cost1, cost2)) + return ans % mod +} + +func find(mv, cnt, c1, c2 int) int { + switch { + case c1<<1 <= c2: + return cnt * c1 + case mv<<1 <= cnt: + return (cnt>>1)*c2 + (cnt&1)*c1 + } + return (cnt-mv)*c2 + (mv<<1-cnt)*c1 +} + +func min(x, y int) int { + if x < y { + return x + } + return y +} + + ``` diff --git a/solution/3100-3199/3139.Minimum Cost to Equalize Array/README_EN.md b/solution/3100-3199/3139.Minimum Cost to Equalize Array/README_EN.md index 6ba8643735da5..4e5f3cae94705 100644 --- a/solution/3100-3199/3139.Minimum Cost to Equalize Array/README_EN.md +++ b/solution/3100-3199/3139.Minimum Cost to Equalize Array/README_EN.md @@ -5,9 +5,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3139.Mi rating: 2666 source: Weekly Contest 396 Q4 tags: - - Greedy - - Array - - Enumeration + - Greedy + - Array + - Enumeration --- @@ -119,24 +119,163 @@ tags: ```python +class Solution: + MOD = 10 ** 9 + 7 + + def solve(self, k): + sumDifferences = k * len(self.nums) - self.sumNums + + ones = max(2 * (k - self.minNums) - sumDifferences, 0) + if (sumDifferences - ones) & 1 != 0: + ones += 1 + + return ones * self.cost1 + ((sumDifferences - ones) // 2) * self.cost2 + + def minCostToEqualizeArray(self, nums: List[int], cost1: int, cost2: int) -> int: + cost2 = min(2 * cost1, cost2) + + self.nums = nums + self.minNums = min(nums) + self.sumNums = sum(nums) + self.cost1 = cost1 + self.cost2 = cost2 + + m = max(nums) + + sameParity = range(m, 10 ** 18, 2) + diffParity = range(m + 1, 10 ** 18, 2) + i = bisect_left(sameParity, 0, key=lambda i: self.solve(i + 2) - self.solve(i)) + j = bisect_left(diffParity, 0, key=lambda j: self.solve(j + 2) - self.solve(j)) + + return min(self.solve(sameParity[i]), self.solve(diffParity[j])) % Solution.MOD + ``` #### Java ```java +class Solution { + public int minCostToEqualizeArray(int[] A, int c1, int c2) { + int ma = A[0], mi = A[0], n = A.length, mod = 1000000007; + long total = 0; + for (int a: A) { + mi = Math.min(mi, a); + ma = Math.max(ma, a); + total += a; + } + total = 1l * ma * n - total; + + if (c1 * 2 <= c2 || n <= 2) { + return (int) ((total * c1) % mod); + } + + long op1 = Math.max(0L, (ma - mi) * 2L - total); + long op2 = total - op1; + long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2; + + total += op1 / (n - 2) * n; + op1 %= n - 2; + op2 = total - op1; + res = Math.min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2); + + for (int i = 0; i < 2; i++) { + total += n; + res = Math.min(res, total % 2 * c1 + total / 2 * c2); + } + return (int) (res % mod); + } +} + ``` #### C++ ```cpp +class Solution { +public: + int minCostToEqualizeArray(vector& A, int c1, int c2) { + int ma = *max_element(A.begin(), A.end()); + int mi = *min_element(A.begin(), A.end()); + int n = A.size(), mod = 1000000007; + long long su = accumulate(A.begin(), A.end(), 0LL); + long long total = 1LL * ma * n - su; + + if (c1 * 2 <= c2 || n <= 2) { + return (total * c1) % mod; + } + + long long op1 = max(0LL, (ma - mi) * 2 - total); + long long op2 = total - op1; + long long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2; + + total += op1 / (n - 2) * n; + op1 %= n - 2; + op2 = total - op1; + res = min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2); + + for (int i = 0; i < 2; i++) { + total += n; + res = min(res, total % 2 * c1 + total / 2 * c2); + } + + return res % mod; + } +}; + + ``` #### Go ```go +func minCostToEqualizeArray(nums []int, cost1 int, cost2 int) int { + mx, mod := 0, int(1e9)+7 + for _, v := range nums { + if v > mx { + mx = v + } + } + mv, cnt := 0, 0 + for _, v := range nums { + d := mx - v + if cnt += d; d > mv { + mv = d + } + } + ans := find(mv, cnt, cost1, cost2) + if len(nums) <= 2 { + return ans % mod + } + for (mv<<1) > cnt { + mv, cnt = mv + 1, cnt + len(nums) + ans = min(ans, find(mv, cnt, cost1, cost2)) + } + mv, cnt = mv + 1, cnt + len(nums) + ans = min(ans, find(mv, cnt, cost1, cost2)) + return ans % mod +} + +func find(mv, cnt, c1, c2 int) int { + switch { + case c1<<1 <= c2: + return cnt * c1 + case mv<<1 <= cnt: + return (cnt>>1)*c2 + (cnt&1)*c1 + } + return (cnt-mv)*c2 + (mv<<1-cnt)*c1 +} + +func min(x, y int) int { + if x < y { + return x + } + return y +} + + ``` diff --git a/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.cpp b/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.cpp new file mode 100644 index 0000000000000..7919216bdd089 --- /dev/null +++ b/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int minCostToEqualizeArray(std::vector &A, int c1, int c2) { + int ma = *std::max_element(A.begin(), A.end()); + int mi = *std::min_element(A.begin(), A.end()); + int n = A.size(), mod = 1000000007; + long long su = std::accumulate(A.begin(), A.end(), 0LL); + long long total = 1LL * ma * n - su; + + if (c1 * 2 <= c2 || n <= 2) { + return (total * c1) % mod; + } + + long long op1 = std::max(0LL, (ma - mi) * 2 - total); + long long op2 = total - op1; + long long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2; + + total += op1 / (n - 2) * n; + op1 %= n - 2; + op2 = total - op1; + res = std::min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2); + + for (int i = 0; i < 2; i++) { + total += n; + res = std::min(res, total % 2 * c1 + total / 2 * c2); + } + + return res % mod; + } +}; diff --git a/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.go b/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.go new file mode 100644 index 0000000000000..6dae5e358f746 --- /dev/null +++ b/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.go @@ -0,0 +1,43 @@ +func minCostToEqualizeArray(nums []int, cost1 int, cost2 int) int { + mx, mod := 0, int(1e9)+7 + for _, v := range nums { + if v > mx { + mx = v + } + } + mv, cnt := 0, 0 + for _, v := range nums { + d := mx - v + if cnt += d; d > mv { + mv = d + } + } + ans := find(mv, cnt, cost1, cost2) + if len(nums) <= 2 { + return ans % mod + } + for (mv << 1) > cnt { + mv, cnt = mv+1, cnt+len(nums) + ans = min(ans, find(mv, cnt, cost1, cost2)) + } + mv, cnt = mv+1, cnt+len(nums) + ans = min(ans, find(mv, cnt, cost1, cost2)) + return ans % mod +} + +func find(mv, cnt, c1, c2 int) int { + switch { + case c1<<1 <= c2: + return cnt * c1 + case mv<<1 <= cnt: + return (cnt>>1)*c2 + (cnt&1)*c1 + } + return (cnt-mv)*c2 + (mv<<1-cnt)*c1 +} + +func min(x, y int) int { + if x < y { + return x + } + return y +} diff --git a/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.java b/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.java new file mode 100644 index 0000000000000..803fe22203e13 --- /dev/null +++ b/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.java @@ -0,0 +1,32 @@ +class Solution { + public int minCostToEqualizeArray(int[] A, int c1, int c2) { + int ma = A[0], mi = A[0], n = A.length, mod = 1000000007; + long total = 0; + for (int a : A) { + mi = Math.min(mi, a); + ma = Math.max(ma, a); + total += a; + } + total = 1L * ma * n - total; + + if (c1 * 2 <= c2 || n <= 2) { + return (int) ((total * c1) % mod); + } + + long op1 = Math.max(0L, (ma - mi) * 2L - total); + long op2 = total - op1; + long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2; + + total += op1 / (n - 2) * n; + op1 %= n - 2; + op2 = total - op1; + res = Math.min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2); + + for (int i = 0; i < 2; i++) { + total += n; + res = Math.min(res, total % 2 * c1 + total / 2 * c2); + } + + return (int) (res % mod); + } +} diff --git a/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.py b/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.py new file mode 100644 index 0000000000000..98f06fc102953 --- /dev/null +++ b/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.py @@ -0,0 +1,35 @@ +class Solution: + MOD = 10**9 + 7 + + def solve(self, k): + sumDifferences = k * len(self.nums) - self.sumNums + + ones = max(2 * (k - self.minNums) - sumDifferences, 0) + if (sumDifferences - ones) & 1 != 0: + ones += 1 + + return ones * self.cost1 + ((sumDifferences - ones) // 2) * self.cost2 + + def minCostToEqualizeArray( + self, nums: List[int], cost1: int, cost2: int + ) -> int: + cost2 = min(2 * cost1, cost2) + + self.nums = nums + self.minNums = min(nums) + self.sumNums = sum(nums) + self.cost1 = cost1 + self.cost2 = cost2 + + m = max(nums) + + sameParity = range(m, 10**18, 2) + diffParity = range(m + 1, 10**18, 2) + i = bisect_left( + sameParity, 0, key=lambda i: self.solve(i + 2) - self.solve(i) + ) + j = bisect_left( + diffParity, 0, key=lambda j: self.solve(j + 2) - self.solve(j) + ) + + return min(self.solve(sameParity[i]), self.solve(diffParity[j])) % Solution.MOD From c6d8a350f76c73e19dcd7509b04f718c5097c8a3 Mon Sep 17 00:00:00 2001 From: samarthswami1016 <70163465+samarthswami1016@users.noreply.github.com> Date: Fri, 25 Jul 2025 08:24:03 +0000 Subject: [PATCH 12/15] style: format code and docs with prettier --- .../3100-3199/3139.Minimum Cost to Equalize Array/README.md | 6 +++--- .../3139.Minimum Cost to Equalize Array/README_EN.md | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/solution/3100-3199/3139.Minimum Cost to Equalize Array/README.md b/solution/3100-3199/3139.Minimum Cost to Equalize Array/README.md index 08d717a939afd..1f95e9097a5eb 100644 --- a/solution/3100-3199/3139.Minimum Cost to Equalize Array/README.md +++ b/solution/3100-3199/3139.Minimum Cost to Equalize Array/README.md @@ -5,9 +5,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3139.Mi rating: 2666 source: 第 396 场周赛 Q4 tags: - - 贪心 - - 数组 - - 枚举 + - 贪心 + - 数组 + - 枚举 --- diff --git a/solution/3100-3199/3139.Minimum Cost to Equalize Array/README_EN.md b/solution/3100-3199/3139.Minimum Cost to Equalize Array/README_EN.md index 4e5f3cae94705..7025d886565ad 100644 --- a/solution/3100-3199/3139.Minimum Cost to Equalize Array/README_EN.md +++ b/solution/3100-3199/3139.Minimum Cost to Equalize Array/README_EN.md @@ -5,9 +5,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3139.Mi rating: 2666 source: Weekly Contest 396 Q4 tags: - - Greedy - - Array - - Enumeration + - Greedy + - Array + - Enumeration --- From 221f56c62735782c1fd584af561d2a17234316ff Mon Sep 17 00:00:00 2001 From: Samarth Swami Date: Thu, 31 Jul 2025 11:52:58 +0530 Subject: [PATCH 13/15] Update Solution.py --- .../Solution.py | 20 +++---------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.py b/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.py index 98f06fc102953..835fa193d4f9f 100644 --- a/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.py +++ b/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.py @@ -1,35 +1,21 @@ class Solution: MOD = 10**9 + 7 - def solve(self, k): sumDifferences = k * len(self.nums) - self.sumNums - ones = max(2 * (k - self.minNums) - sumDifferences, 0) if (sumDifferences - ones) & 1 != 0: ones += 1 - return ones * self.cost1 + ((sumDifferences - ones) // 2) * self.cost2 - - def minCostToEqualizeArray( - self, nums: List[int], cost1: int, cost2: int - ) -> int: + def minCostToEqualizeArray(self, nums: List[int], cost1: int, cost2: int) -> int: cost2 = min(2 * cost1, cost2) - self.nums = nums self.minNums = min(nums) self.sumNums = sum(nums) self.cost1 = cost1 self.cost2 = cost2 - m = max(nums) - sameParity = range(m, 10**18, 2) diffParity = range(m + 1, 10**18, 2) - i = bisect_left( - sameParity, 0, key=lambda i: self.solve(i + 2) - self.solve(i) - ) - j = bisect_left( - diffParity, 0, key=lambda j: self.solve(j + 2) - self.solve(j) - ) - + i = bisect_left(sameParity, 0, key=lambda i: self.solve(i + 2) - self.solve(i)) + j = bisect_left(diffParity, 0, key=lambda j: self.solve(j + 2) - self.solve(j)) return min(self.solve(sameParity[i]), self.solve(diffParity[j])) % Solution.MOD From b6c5f3a976be52821b2320b07b091dac54d9da34 Mon Sep 17 00:00:00 2001 From: Samarth Swami Date: Thu, 31 Jul 2025 11:55:29 +0530 Subject: [PATCH 14/15] Update Solution.cpp --- .../Solution.cpp | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.cpp b/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.cpp index 7919216bdd089..df0b469f74f20 100644 --- a/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.cpp +++ b/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.cpp @@ -1,30 +1,30 @@ class Solution { public: - int minCostToEqualizeArray(std::vector &A, int c1, int c2) { - int ma = *std::max_element(A.begin(), A.end()); - int mi = *std::min_element(A.begin(), A.end()); - int n = A.size(), mod = 1000000007; - long long su = std::accumulate(A.begin(), A.end(), 0LL); - long long total = 1LL * ma * n - su; + int minCostToEqualizeArray(std::vector &A, int c1, int c2) { + int ma = *std::max_element(A.begin(), A.end()); + int mi = *std::min_element(A.begin(), A.end()); + int n = A.size(), mod = 1000000007; + long long su = std::accumulate(A.begin(), A.end(), 0LL); + long long total = 1LL * ma * n - su; - if (c1 * 2 <= c2 || n <= 2) { - return (total * c1) % mod; - } + if (c1 * 2 <= c2 || n <= 2) { + return (total * c1) % mod; + } - long long op1 = std::max(0LL, (ma - mi) * 2 - total); - long long op2 = total - op1; - long long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2; + long long op1 = std::max(0LL, (ma - mi) * 2 - total); + long long op2 = total - op1; + long long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2; - total += op1 / (n - 2) * n; - op1 %= n - 2; - op2 = total - op1; - res = std::min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2); + total += op1 / (n - 2) * n; + op1 %= n - 2; + op2 = total - op1; + res = std::min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2); - for (int i = 0; i < 2; i++) { - total += n; - res = std::min(res, total % 2 * c1 + total / 2 * c2); - } + for (int i = 0; i < 2; i++) { + total += n; + res = std::min(res, total % 2 * c1 + total / 2 * c2); + } - return res % mod; - } + return res % mod; + } }; From 0a2f2fdc70cb663b3168d7ad68cee90bbf383152 Mon Sep 17 00:00:00 2001 From: Samarth Swami Date: Thu, 31 Jul 2025 11:56:59 +0530 Subject: [PATCH 15/15] Update Solution.java --- .../3139.Minimum Cost to Equalize Array/Solution.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.java b/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.java index 803fe22203e13..e8bd78bcf65ae 100644 --- a/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.java +++ b/solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.java @@ -17,14 +17,14 @@ public int minCostToEqualizeArray(int[] A, int c1, int c2) { long op2 = total - op1; long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2; - total += op1 / (n - 2) * n; - op1 %= n - 2; + total += (op1 / (n - 2)) * n; + op1 %= (n - 2); op2 = total - op1; res = Math.min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2); for (int i = 0; i < 2; i++) { total += n; - res = Math.min(res, total % 2 * c1 + total / 2 * c2); + res = Math.min(res, (total % 2) * c1 + (total / 2) * c2); } return (int) (res % mod);