diff --git a/solution/2800-2899/2864.Maximum Odd Binary Number/README.md b/solution/2800-2899/2864.Maximum Odd Binary Number/README.md
new file mode 100644
index 0000000000000..dcb219b2a408f
--- /dev/null
+++ b/solution/2800-2899/2864.Maximum Odd Binary Number/README.md
@@ -0,0 +1,127 @@
+# [2864. 最大二进制奇数](https://leetcode.cn/problems/maximum-odd-binary-number)
+
+[English Version](/solution/2800-2899/2864.Maximum%20Odd%20Binary%20Number/README_EN.md)
+
+## 题目描述
+
+
+
+
给你一个 二进制 字符串 s
,其中至少包含一个 '1'
。
+
+你必须按某种方式 重新排列 字符串中的位,使得到的二进制数字是可以由该组合生成的 最大二进制奇数 。
+
+以字符串形式,表示并返回可以由给定组合生成的最大二进制奇数。
+
+注意 返回的结果字符串 可以 含前导零。
+
+
+
+示例 1:
+
+
+输入:s = "010"
+输出:"001"
+解释:因为字符串 s 中仅有一个 '1' ,其必须出现在最后一位上。所以答案是 "001" 。
+
+
+示例 2:
+
+
+输入:s = "0101"
+输出:"1001"
+解释:其中一个 '1' 必须出现在最后一位上。而由剩下的数字可以生产的最大数字是 "100" 。所以答案是 "1001" 。
+
+
+
+
+提示:
+
+
+ 1 <= s.length <= 100
+ s
仅由 '0'
和 '1'
组成
+ s
中至少包含一个 '1'
+
+
+## 解法
+
+
+
+
+
+### **Python3**
+
+
+
+```python
+class Solution:
+ def maximumOddBinaryNumber(self, s: str) -> str:
+ cnt = s.count("1")
+ return "1" * (cnt - 1) + (len(s) - cnt) * "0" + "1"
+```
+
+### **Java**
+
+
+
+```java
+class Solution {
+ public String maximumOddBinaryNumber(String s) {
+ int cnt = 0;
+ for (char c : s.toCharArray()) {
+ if (c == '1') {
+ ++cnt;
+ }
+ }
+ return "1".repeat(cnt - 1) + "0".repeat(s.length() - cnt) + "1";
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ string maximumOddBinaryNumber(string s) {
+ int cnt = count_if(s.begin(), s.end(), [](char c) { return c == '1'; });
+ string ans;
+ for (int i = 1; i < cnt; ++i) { + ans.push_back('1'); + } + for (int i = 0; i < s.size() - cnt; ++i) { + ans.push_back('0'); + } + ans.push_back('1'); + return ans; + } +}; +``` + +### **Go** + +```go +func maximumOddBinaryNumber(s string) string { + cnt := strings.Count(s, "1") + return strings.Repeat("1", cnt-1) + strings.Repeat("0", len(s)-cnt) + "1" +} +``` + +### **TypeScript** + +```ts +function maximumOddBinaryNumber(s: string): string { + let cnt = 0; + for (const c of s) { + cnt += c === '1' ? 1 : 0; + } + return '1'.repeat(cnt - 1) + '0'.repeat(s.length - cnt) + '1'; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2864.Maximum Odd Binary Number/README_EN.md b/solution/2800-2899/2864.Maximum Odd Binary Number/README_EN.md new file mode 100644 index 0000000000000..aea0116d74e5f --- /dev/null +++ b/solution/2800-2899/2864.Maximum Odd Binary Number/README_EN.md @@ -0,0 +1,117 @@ +# [2864. Maximum Odd Binary Number](https://leetcode.com/problems/maximum-odd-binary-number) + +[中文文档](/solution/2800-2899/2864.Maximum%20Odd%20Binary%20Number/README.md) + +## Description + +You are given a binary string s
that contains at least one '1'
.
+
+You have to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number that can be created from this combination.
+
+Return a string representing the maximum odd binary number that can be created from the given combination.
+
+Note that the resulting string can have leading zeros.
+
+
+Example 1:
+
+
+Input: s = "010"
+Output: "001"
+Explanation: Because there is just one '1', it must be in the last position. So the answer is "001".
+
+
+Example 2:
+
+
+Input: s = "0101"
+Output: "1001"
+Explanation: One of the '1's must be in the last position. The maximum number that can be made with the remaining digits is "100". So the answer is "1001".
+
+
+
+Constraints:
+
+
+ 1 <= s.length <= 100
+ s
consists only of '0'
and '1'
.
+ s
contains at least one '1'
.
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+class Solution:
+ def maximumOddBinaryNumber(self, s: str) -> str:
+ cnt = s.count("1")
+ return "1" * (cnt - 1) + (len(s) - cnt) * "0" + "1"
+```
+
+### **Java**
+
+```java
+class Solution {
+ public String maximumOddBinaryNumber(String s) {
+ int cnt = 0;
+ for (char c : s.toCharArray()) {
+ if (c == '1') {
+ ++cnt;
+ }
+ }
+ return "1".repeat(cnt - 1) + "0".repeat(s.length() - cnt) + "1";
+ }
+}
+```
+
+### **C++**
+
+```cpp
+class Solution {
+public:
+ string maximumOddBinaryNumber(string s) {
+ int cnt = count_if(s.begin(), s.end(), [](char c) { return c == '1'; });
+ string ans;
+ for (int i = 1; i < cnt; ++i) { + ans.push_back('1'); + } + for (int i = 0; i < s.size() - cnt; ++i) { + ans.push_back('0'); + } + ans.push_back('1'); + return ans; + } +}; +``` + +### **Go** + +```go +func maximumOddBinaryNumber(s string) string { + cnt := strings.Count(s, "1") + return strings.Repeat("1", cnt-1) + strings.Repeat("0", len(s)-cnt) + "1" +} +``` + +### **TypeScript** + +```ts +function maximumOddBinaryNumber(s: string): string { + let cnt = 0; + for (const c of s) { + cnt += c === '1' ? 1 : 0; + } + return '1'.repeat(cnt - 1) + '0'.repeat(s.length - cnt) + '1'; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.cpp b/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.cpp new file mode 100644 index 0000000000000..cb6caa8afcede --- /dev/null +++ b/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + string maximumOddBinaryNumber(string s) { + int cnt = count_if(s.begin(), s.end(), [](char c) { return c == '1'; }); + string ans; + for (int i = 1; i < cnt; ++i) { + ans.push_back('1'); + } + for (int i = 0; i < s.size() - cnt; ++i) { + ans.push_back('0'); + } + ans.push_back('1'); + return ans; + } +}; \ No newline at end of file diff --git a/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.go b/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.go new file mode 100644 index 0000000000000..2782713930c24 --- /dev/null +++ b/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.go @@ -0,0 +1,4 @@ +func maximumOddBinaryNumber(s string) string { + cnt := strings.Count(s, "1") + return strings.Repeat("1", cnt-1) + strings.Repeat("0", len(s)-cnt) + "1" +} \ No newline at end of file diff --git a/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.java b/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.java new file mode 100644 index 0000000000000..699654d0897b4 --- /dev/null +++ b/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.java @@ -0,0 +1,11 @@ +class Solution { + public String maximumOddBinaryNumber(String s) { + int cnt = 0; + for (char c : s.toCharArray()) { + if (c == '1') { + ++cnt; + } + } + return "1".repeat(cnt - 1) + "0".repeat(s.length() - cnt) + "1"; + } +} \ No newline at end of file diff --git a/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.py b/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.py new file mode 100644 index 0000000000000..6beedc385d79b --- /dev/null +++ b/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.py @@ -0,0 +1,4 @@ +class Solution: + def maximumOddBinaryNumber(self, s: str) -> str:
+ cnt = s.count("1")
+ return "1" * (cnt - 1) + (len(s) - cnt) * "0" + "1"
diff --git a/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.ts b/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.ts
new file mode 100644
index 0000000000000..f5fc7b3e2e792
--- /dev/null
+++ b/solution/2800-2899/2864.Maximum Odd Binary Number/Solution.ts
@@ -0,0 +1,7 @@
+function maximumOddBinaryNumber(s: string): string {
+ let cnt = 0;
+ for (const c of s) {
+ cnt += c === '1' ? 1 : 0;
+ }
+ return '1'.repeat(cnt - 1) + '0'.repeat(s.length - cnt) + '1';
+}
diff --git a/solution/2800-2899/2865.Beautiful Towers I/README.md b/solution/2800-2899/2865.Beautiful Towers I/README.md
new file mode 100644
index 0000000000000..7e7140c8fb833
--- /dev/null
+++ b/solution/2800-2899/2865.Beautiful Towers I/README.md
@@ -0,0 +1,220 @@
+# [2865. 美丽塔 I](https://leetcode.cn/problems/beautiful-towers-i)
+
+[English Version](/solution/2800-2899/2865.Beautiful%20Towers%20I/README_EN.md)
+
+## 题目描述
+
+
+
+给你一个长度为 n
下标从 0 开始的整数数组 maxHeights
。
+
+你的任务是在坐标轴上建 n
座塔。第 i
座塔的下标为 i
,高度为 heights[i]
。
+
+如果以下条件满足,我们称这些塔是 美丽 的:
+
+
+ 1 <= heights[i] <= maxHeights[i]
+ heights
是一个 山状 数组。
+
+
+如果存在下标 i
满足以下条件,那么我们称数组 heights
是一个 山状 数组:
+
+
+ - 对于所有
0 < j <= i
,都有 heights[j - 1] <= heights[j]
+ - 对于所有
i <= k < n - 1
,都有 heights[k + 1] <= heights[k]
+
+
+请你返回满足 美丽塔 要求的方案中,高度和的最大值 。
+
+
+
+示例 1:
+
+
+输入:maxHeights = [5,3,4,1,1]
+输出:13
+解释:和最大的美丽塔方案为 heights = [5,3,3,1,1] ,这是一个美丽塔方案,因为:
+- 1 <= heights[i] <= maxHeights[i]
+- heights 是个山状数组,峰值在 i = 0 处。
+13 是所有美丽塔方案中的最大高度和。
+
+示例 2:
+
+
+输入:maxHeights = [6,5,3,9,2,7]
+输出:22
+解释: 和最大的美丽塔方案为 heights = [3,3,3,9,2,2] ,这是一个美丽塔方案,因为:
+- 1 <= heights[i] <= maxHeights[i]
+- heights 是个山状数组,峰值在 i = 3 处。
+22 是所有美丽塔方案中的最大高度和。
+
+示例 3:
+
+
+输入:maxHeights = [3,2,5,5,2,3]
+输出:18
+解释:和最大的美丽塔方案为 heights = [2,2,5,5,2,2] ,这是一个美丽塔方案,因为:
+- 1 <= heights[i] <= maxHeights[i]
+- heights 是个山状数组,最大值在 i = 2 处。
+注意,在这个方案中,i = 3 也是一个峰值。
+18 是所有美丽塔方案中的最大高度和。
+
+
+
+
+提示:
+
+
+ 1 <= n == maxHeights <= 103
+ 1 <= maxHeights[i] <= 109
+
+
+## 解法
+
+
+
+
+
+### **Python3**
+
+
+
+```python
+class Solution:
+ def maximumSumOfHeights(self, maxHeights: List[int]) -> int:
+ ans, n = 0, len(maxHeights)
+ for i, x in enumerate(maxHeights):
+ y = t = x
+ for j in range(i - 1, -1, -1):
+ y = min(y, maxHeights[j])
+ t += y
+ y = x
+ for j in range(i + 1, n):
+ y = min(y, maxHeights[j])
+ t += y
+ ans = max(ans, t)
+ return ans
+```
+
+### **Java**
+
+
+
+```java
+class Solution {
+ public long maximumSumOfHeights(List maxHeights) {
+ long ans = 0;
+ int n = maxHeights.size();
+ for (int i = 0; i < n; ++i) { + int y = maxHeights.get(i); + long t = y; + for (int j = i - 1; j>= 0; --j) {
+ y = Math.min(y, maxHeights.get(j));
+ t += y;
+ }
+ y = maxHeights.get(i);
+ for (int j = i + 1; j < n; ++j) { + y = Math.min(y, maxHeights.get(j)); + t += y; + } + ans = Math.max(ans, t); + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long maximumSumOfHeights(vector& maxHeights) {
+ long long ans = 0;
+ int n = maxHeights.size();
+ for (int i = 0; i < n; ++i) { + long long t = maxHeights[i]; + int y = t; + for (int j = i - 1; ~j; --j) { + y = min(y, maxHeights[j]); + t += y; + } + y = maxHeights[i]; + for (int j = i + 1; j < n; ++j) { + y = min(y, maxHeights[j]); + t += y; + } + ans = max(ans, t); + } + return ans; + } +}; +``` + +### **Go** + +```go +func maximumSumOfHeights(maxHeights []int) (ans int64) { + n := len(maxHeights) + for i, x := range maxHeights { + y, t := x, x + for j := i - 1; j>= 0; j-- {
+ y = min(y, maxHeights[j])
+ t += y
+ }
+ y = x
+ for j := i + 1; j < n; j++ { + y = min(y, maxHeights[j]) + t += y + } + ans = max(ans, int64(t)) + } + return +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} + +func max(a, b int64) int64 { + if a> b {
+ return a
+ }
+ return b
+}
+```
+
+### **TypeScript**
+
+```ts
+function maximumSumOfHeights(maxHeights: number[]): number {
+ let ans = 0;
+ const n = maxHeights.length;
+ for (let i = 0; i < n; ++i) { + const x = maxHeights[i]; + let [y, t] = [x, x]; + for (let j = i - 1; ~j; --j) { + y = Math.min(y, maxHeights[j]); + t += y; + } + y = x; + for (let j = i + 1; j < n; ++j) { + y = Math.min(y, maxHeights[j]); + t += y; + } + ans = Math.max(ans, t); + } + return ans; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2865.Beautiful Towers I/README_EN.md b/solution/2800-2899/2865.Beautiful Towers I/README_EN.md new file mode 100644 index 0000000000000..34671bba393d9 --- /dev/null +++ b/solution/2800-2899/2865.Beautiful Towers I/README_EN.md @@ -0,0 +1,210 @@ +# [2865. Beautiful Towers I](https://leetcode.com/problems/beautiful-towers-i) + +[中文文档](/solution/2800-2899/2865.Beautiful%20Towers%20I/README.md) + +## Description + +You are given a 0-indexed array maxHeights
of n
integers.
+
+You are tasked with building n
towers in the coordinate line. The ith
tower is built at coordinate i
and has a height of heights[i]
.
+
+A configuration of towers is beautiful if the following conditions hold:
+
+
+ 1 <= heights[i] <= maxHeights[i]
+ heights
is a mountain array.
+
+
+Array heights
is a mountain if there exists an index i
such that:
+
+
+ - For all
0 < j <= i
, heights[j - 1] <= heights[j]
+ - For all
i <= k < n - 1
, heights[k + 1] <= heights[k]
+
+
+Return the maximum possible sum of heights of a beautiful configuration of towers.
+
+
+Example 1:
+
+
+Input: maxHeights = [5,3,4,1,1]
+Output: 13
+Explanation: One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since:
+- 1 <= heights[i] <= maxHeights[i]
+- heights is a mountain of peak i = 0.
+It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13.
+
+Example 2:
+
+
+Input: maxHeights = [6,5,3,9,2,7]
+Output: 22
+Explanation: One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since:
+- 1 <= heights[i] <= maxHeights[i]
+- heights is a mountain of peak i = 3.
+It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22.
+
+Example 3:
+
+
+Input: maxHeights = [3,2,5,5,2,3]
+Output: 18
+Explanation: One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since:
+- 1 <= heights[i] <= maxHeights[i]
+- heights is a mountain of peak i = 2.
+Note that, for this configuration, i = 3 can also be considered a peak.
+It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18.
+
+
+
+Constraints:
+
+
+ 1 <= n == maxHeights <= 103
+ 1 <= maxHeights[i] <= 109
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+class Solution:
+ def maximumSumOfHeights(self, maxHeights: List[int]) -> int:
+ ans, n = 0, len(maxHeights)
+ for i, x in enumerate(maxHeights):
+ y = t = x
+ for j in range(i - 1, -1, -1):
+ y = min(y, maxHeights[j])
+ t += y
+ y = x
+ for j in range(i + 1, n):
+ y = min(y, maxHeights[j])
+ t += y
+ ans = max(ans, t)
+ return ans
+```
+
+### **Java**
+
+```java
+class Solution {
+ public long maximumSumOfHeights(List maxHeights) {
+ long ans = 0;
+ int n = maxHeights.size();
+ for (int i = 0; i < n; ++i) { + int y = maxHeights.get(i); + long t = y; + for (int j = i - 1; j>= 0; --j) {
+ y = Math.min(y, maxHeights.get(j));
+ t += y;
+ }
+ y = maxHeights.get(i);
+ for (int j = i + 1; j < n; ++j) { + y = Math.min(y, maxHeights.get(j)); + t += y; + } + ans = Math.max(ans, t); + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long maximumSumOfHeights(vector& maxHeights) {
+ long long ans = 0;
+ int n = maxHeights.size();
+ for (int i = 0; i < n; ++i) { + long long t = maxHeights[i]; + int y = t; + for (int j = i - 1; ~j; --j) { + y = min(y, maxHeights[j]); + t += y; + } + y = maxHeights[i]; + for (int j = i + 1; j < n; ++j) { + y = min(y, maxHeights[j]); + t += y; + } + ans = max(ans, t); + } + return ans; + } +}; +``` + +### **Go** + +```go +func maximumSumOfHeights(maxHeights []int) (ans int64) { + n := len(maxHeights) + for i, x := range maxHeights { + y, t := x, x + for j := i - 1; j>= 0; j-- {
+ y = min(y, maxHeights[j])
+ t += y
+ }
+ y = x
+ for j := i + 1; j < n; j++ { + y = min(y, maxHeights[j]) + t += y + } + ans = max(ans, int64(t)) + } + return +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} + +func max(a, b int64) int64 { + if a> b {
+ return a
+ }
+ return b
+}
+```
+
+### **TypeScript**
+
+```ts
+function maximumSumOfHeights(maxHeights: number[]): number {
+ let ans = 0;
+ const n = maxHeights.length;
+ for (let i = 0; i < n; ++i) { + const x = maxHeights[i]; + let [y, t] = [x, x]; + for (let j = i - 1; ~j; --j) { + y = Math.min(y, maxHeights[j]); + t += y; + } + y = x; + for (let j = i + 1; j < n; ++j) { + y = Math.min(y, maxHeights[j]); + t += y; + } + ans = Math.max(ans, t); + } + return ans; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2865.Beautiful Towers I/Solution.cpp b/solution/2800-2899/2865.Beautiful Towers I/Solution.cpp new file mode 100644 index 0000000000000..91cb9631dd967 --- /dev/null +++ b/solution/2800-2899/2865.Beautiful Towers I/Solution.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + long long maximumSumOfHeights(vector& maxHeights) {
+ long long ans = 0;
+ int n = maxHeights.size();
+ for (int i = 0; i < n; ++i) { + long long t = maxHeights[i]; + int y = t; + for (int j = i - 1; ~j; --j) { + y = min(y, maxHeights[j]); + t += y; + } + y = maxHeights[i]; + for (int j = i + 1; j < n; ++j) { + y = min(y, maxHeights[j]); + t += y; + } + ans = max(ans, t); + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2800-2899/2865.Beautiful Towers I/Solution.go b/solution/2800-2899/2865.Beautiful Towers I/Solution.go new file mode 100644 index 0000000000000..734b369d70afd --- /dev/null +++ b/solution/2800-2899/2865.Beautiful Towers I/Solution.go @@ -0,0 +1,31 @@ +func maximumSumOfHeights(maxHeights []int) (ans int64) { + n := len(maxHeights) + for i, x := range maxHeights { + y, t := x, x + for j := i - 1; j>= 0; j-- {
+ y = min(y, maxHeights[j])
+ t += y
+ }
+ y = x
+ for j := i + 1; j < n; j++ { + y = min(y, maxHeights[j]) + t += y + } + ans = max(ans, int64(t)) + } + return +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} + +func max(a, b int64) int64 { + if a> b {
+ return a
+ }
+ return b
+}
\ No newline at end of file
diff --git a/solution/2800-2899/2865.Beautiful Towers I/Solution.java b/solution/2800-2899/2865.Beautiful Towers I/Solution.java
new file mode 100644
index 0000000000000..9cc531d57d376
--- /dev/null
+++ b/solution/2800-2899/2865.Beautiful Towers I/Solution.java
@@ -0,0 +1,21 @@
+class Solution {
+ public long maximumSumOfHeights(List maxHeights) {
+ long ans = 0;
+ int n = maxHeights.size();
+ for (int i = 0; i < n; ++i) { + int y = maxHeights.get(i); + long t = y; + for (int j = i - 1; j>= 0; --j) {
+ y = Math.min(y, maxHeights.get(j));
+ t += y;
+ }
+ y = maxHeights.get(i);
+ for (int j = i + 1; j < n; ++j) { + y = Math.min(y, maxHeights.get(j)); + t += y; + } + ans = Math.max(ans, t); + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2800-2899/2865.Beautiful Towers I/Solution.py b/solution/2800-2899/2865.Beautiful Towers I/Solution.py new file mode 100644 index 0000000000000..b5a1ba420fa1e --- /dev/null +++ b/solution/2800-2899/2865.Beautiful Towers I/Solution.py @@ -0,0 +1,14 @@ +class Solution: + def maximumSumOfHeights(self, maxHeights: List[int]) -> int:
+ ans, n = 0, len(maxHeights)
+ for i, x in enumerate(maxHeights):
+ y = t = x
+ for j in range(i - 1, -1, -1):
+ y = min(y, maxHeights[j])
+ t += y
+ y = x
+ for j in range(i + 1, n):
+ y = min(y, maxHeights[j])
+ t += y
+ ans = max(ans, t)
+ return ans
diff --git a/solution/2800-2899/2865.Beautiful Towers I/Solution.ts b/solution/2800-2899/2865.Beautiful Towers I/Solution.ts
new file mode 100644
index 0000000000000..e226293fc9d14
--- /dev/null
+++ b/solution/2800-2899/2865.Beautiful Towers I/Solution.ts
@@ -0,0 +1,19 @@
+function maximumSumOfHeights(maxHeights: number[]): number {
+ let ans = 0;
+ const n = maxHeights.length;
+ for (let i = 0; i < n; ++i) { + const x = maxHeights[i]; + let [y, t] = [x, x]; + for (let j = i - 1; ~j; --j) { + y = Math.min(y, maxHeights[j]); + t += y; + } + y = x; + for (let j = i + 1; j < n; ++j) { + y = Math.min(y, maxHeights[j]); + t += y; + } + ans = Math.max(ans, t); + } + return ans; +} diff --git a/solution/2800-2899/2866.Beautiful Towers II/README.md b/solution/2800-2899/2866.Beautiful Towers II/README.md new file mode 100644 index 0000000000000..411226f9629cc --- /dev/null +++ b/solution/2800-2899/2866.Beautiful Towers II/README.md @@ -0,0 +1,376 @@ +# [2866. 美丽塔 II](https://leetcode.cn/problems/beautiful-towers-ii) + +[English Version](/solution/2800-2899/2866.Beautiful%20Towers%20II/README_EN.md) + +## 题目描述 + + + +给你一个长度为 n
下标从 0 开始的整数数组 maxHeights
。
+
+你的任务是在坐标轴上建 n
座塔。第 i
座塔的下标为 i
,高度为 heights[i]
。
+
+如果以下条件满足,我们称这些塔是 美丽 的:
+
+
+ 1 <= heights[i] <= maxHeights[i]
+ heights
是一个 山状 数组。
+
+
+如果存在下标 i
满足以下条件,那么我们称数组 heights
是一个 山状 数组:
+
+
+ - 对于所有
0 < j <= i
,都有 heights[j - 1] <= heights[j]
+ - 对于所有
i <= k < n - 1
,都有 heights[k + 1] <= heights[k]
+
+
+请你返回满足 美丽塔 要求的方案中,高度和的最大值 。
+
+
+
+示例 1:
+
+
+输入:maxHeights = [5,3,4,1,1]
+输出:13
+解释:和最大的美丽塔方案为 heights = [5,3,3,1,1] ,这是一个美丽塔方案,因为:
+- 1 <= heights[i] <= maxHeights[i]
+- heights 是个山状数组,峰值在 i = 0 处。
+13 是所有美丽塔方案中的最大高度和。
+
+示例 2:
+
+
+输入:maxHeights = [6,5,3,9,2,7]
+输出:22
+解释: 和最大的美丽塔方案为 heights = [3,3,3,9,2,2] ,这是一个美丽塔方案,因为:
+- 1 <= heights[i] <= maxHeights[i]
+- heights 是个山状数组,峰值在 i = 3 处。
+22 是所有美丽塔方案中的最大高度和。
+
+示例 3:
+
+
+输入:maxHeights = [3,2,5,5,2,3]
+输出:18
+解释:和最大的美丽塔方案为 heights = [2,2,5,5,2,2] ,这是一个美丽塔方案,因为:
+- 1 <= heights[i] <= maxHeights[i]
+- heights 是个山状数组,最大值在 i = 2 处。
+注意,在这个方案中,i = 3 也是一个峰值。
+18 是所有美丽塔方案中的最大高度和。
+
+
+
+
+提示:
+
+
+ 1 <= n == maxHeights <= 105
+ 1 <= maxHeights[i] <= 109
+
+
+## 解法
+
+
+
+
+
+### **Python3**
+
+
+
+```python
+class Solution:
+ def maximumSumOfHeights(self, maxHeights: List[int]) -> int:
+ n = len(maxHeights)
+ stk = []
+ left = [-1] * n
+ for i, x in enumerate(maxHeights):
+ while stk and maxHeights[stk[-1]]> x:
+ stk.pop()
+ if stk:
+ left[i] = stk[-1]
+ stk.append(i)
+ stk = []
+ right = [n] * n
+ for i in range(n - 1, -1, -1):
+ x = maxHeights[i]
+ while stk and maxHeights[stk[-1]]>= x:
+ stk.pop()
+ if stk:
+ right[i] = stk[-1]
+ stk.append(i)
+ f = [0] * n
+ for i, x in enumerate(maxHeights):
+ if i and x>= maxHeights[i - 1]:
+ f[i] = f[i - 1] + x
+ else:
+ j = left[i]
+ f[i] = x * (i - j) + (f[j] if j != -1 else 0)
+ g = [0] * n
+ for i in range(n - 1, -1, -1):
+ if i < n - 1 and maxHeights[i]>= maxHeights[i + 1]:
+ g[i] = g[i + 1] + maxHeights[i]
+ else:
+ j = right[i]
+ g[i] = maxHeights[i] * (j - i) + (g[j] if j != n else 0)
+ return max(a + b - c for a, b, c in zip(f, g, maxHeights))
+```
+
+### **Java**
+
+
+
+```java
+class Solution {
+ public long maximumSumOfHeights(List maxHeights) {
+ int n = maxHeights.size();
+ Deque stk = new ArrayDeque();
+ int[] left = new int[n];
+ int[] right = new int[n];
+ Arrays.fill(left, -1);
+ Arrays.fill(right, n);
+ for (int i = 0; i < n; ++i) { + int x = maxHeights.get(i); + while (!stk.isEmpty() && maxHeights.get(stk.peek())> x) {
+ stk.pop();
+ }
+ if (!stk.isEmpty()) {
+ left[i] = stk.peek();
+ }
+ stk.push(i);
+ }
+ stk.clear();
+ for (int i = n - 1; i>= 0; --i) {
+ int x = maxHeights.get(i);
+ while (!stk.isEmpty() && maxHeights.get(stk.peek())>= x) {
+ stk.pop();
+ }
+ if (!stk.isEmpty()) {
+ right[i] = stk.peek();
+ }
+ stk.push(i);
+ }
+ long[] f = new long[n];
+ long[] g = new long[n];
+ for (int i = 0; i < n; ++i) { + int x = maxHeights.get(i); + if (i> 0 && x>= maxHeights.get(i - 1)) {
+ f[i] = f[i - 1] + x;
+ } else {
+ int j = left[i];
+ f[i] = 1L * x * (i - j) + (j>= 0 ? f[j] : 0);
+ }
+ }
+ for (int i = n - 1; i>= 0; --i) {
+ int x = maxHeights.get(i);
+ if (i < n - 1 && x>= maxHeights.get(i + 1)) {
+ g[i] = g[i + 1] + x;
+ } else {
+ int j = right[i];
+ g[i] = 1L * x * (j - i) + (j < n ? g[j] : 0); + } + } + long ans = 0; + for (int i = 0; i < n; ++i) { + ans = Math.max(ans, f[i] + g[i] - maxHeights.get(i)); + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long maximumSumOfHeights(vector& maxHeights) {
+ int n = maxHeights.size();
+ stack stk;
+ vector left(n, -1);
+ vector right(n, n);
+ for (int i = 0; i < n; ++i) { + int x = maxHeights[i]; + while (!stk.empty() && maxHeights[stk.top()]> x) {
+ stk.pop();
+ }
+ if (!stk.empty()) {
+ left[i] = stk.top();
+ }
+ stk.push(i);
+ }
+ stk = stack();
+ for (int i = n - 1; ~i; --i) {
+ int x = maxHeights[i];
+ while (!stk.empty() && maxHeights[stk.top()]>= x) {
+ stk.pop();
+ }
+ if (!stk.empty()) {
+ right[i] = stk.top();
+ }
+ stk.push(i);
+ }
+ long long f[n], g[n];
+ for (int i = 0; i < n; ++i) { + int x = maxHeights[i]; + if (i && x>= maxHeights[i - 1]) {
+ f[i] = f[i - 1] + x;
+ } else {
+ int j = left[i];
+ f[i] = 1LL * x * (i - j) + (j != -1 ? f[j] : 0);
+ }
+ }
+ for (int i = n - 1; ~i; --i) {
+ int x = maxHeights[i];
+ if (i < n - 1 && x>= maxHeights[i + 1]) {
+ g[i] = g[i + 1] + x;
+ } else {
+ int j = right[i];
+ g[i] = 1LL * x * (j - i) + (j != n ? g[j] : 0);
+ }
+ }
+ long long ans = 0;
+ for (int i = 0; i < n; ++i) { + ans = max(ans, f[i] + g[i] - maxHeights[i]); + } + return ans; + } +}; +``` + +### **Go** + +```go +func maximumSumOfHeights(maxHeights []int) (ans int64) { + n := len(maxHeights) + stk := []int{} + left := make([]int, n) + right := make([]int, n) + for i := range left { + left[i] = -1 + right[i] = n + } + for i, x := range maxHeights { + for len(stk)> 0 && maxHeights[stk[len(stk)-1]]> x {
+ stk = stk[:len(stk)-1]
+ }
+ if len(stk)> 0 {
+ left[i] = stk[len(stk)-1]
+ }
+ stk = append(stk, i)
+ }
+ stk = []int{}
+ for i := n - 1; i>= 0; i-- {
+ x := maxHeights[i]
+ for len(stk)> 0 && maxHeights[stk[len(stk)-1]]>= x {
+ stk = stk[:len(stk)-1]
+ }
+ if len(stk)> 0 {
+ right[i] = stk[len(stk)-1]
+ }
+ stk = append(stk, i)
+ }
+ f := make([]int64, n)
+ g := make([]int64, n)
+ for i, x := range maxHeights {
+ if i> 0 && x>= maxHeights[i-1] {
+ f[i] = f[i-1] + int64(x)
+ } else {
+ j := left[i]
+ f[i] = int64(x) * int64(i-j)
+ if j != -1 {
+ f[i] += f[j]
+ }
+ }
+ }
+ for i := n - 1; i>= 0; i-- {
+ x := maxHeights[i]
+ if i < n-1 && x>= maxHeights[i+1] {
+ g[i] = g[i+1] + int64(x)
+ } else {
+ j := right[i]
+ g[i] = int64(x) * int64(j-i)
+ if j != n {
+ g[i] += g[j]
+ }
+ }
+ }
+ for i, x := range maxHeights {
+ ans = max(ans, f[i]+g[i]-int64(x))
+ }
+ return
+}
+
+func max(a, b int64) int64 {
+ if a> b {
+ return a
+ }
+ return b
+}
+```
+
+### **TypeScript**
+
+```ts
+function maximumSumOfHeights(maxHeights: number[]): number {
+ const n = maxHeights.length;
+ const stk: number[] = [];
+ const left: number[] = Array(n).fill(-1);
+ const right: number[] = Array(n).fill(n);
+ for (let i = 0; i < n; ++i) { + const x = maxHeights[i]; + while (stk.length && maxHeights[stk.at(-1)]> x) {
+ stk.pop();
+ }
+ if (stk.length) {
+ left[i] = stk.at(-1);
+ }
+ stk.push(i);
+ }
+ stk.length = 0;
+ for (let i = n - 1; ~i; --i) {
+ const x = maxHeights[i];
+ while (stk.length && maxHeights[stk.at(-1)]>= x) {
+ stk.pop();
+ }
+ if (stk.length) {
+ right[i] = stk.at(-1);
+ }
+ stk.push(i);
+ }
+ const f: number[] = Array(n).fill(0);
+ const g: number[] = Array(n).fill(0);
+ for (let i = 0; i < n; ++i) { + const x = maxHeights[i]; + if (i && x>= maxHeights[i - 1]) {
+ f[i] = f[i - 1] + x;
+ } else {
+ const j = left[i];
+ f[i] = x * (i - j) + (j>= 0 ? f[j] : 0);
+ }
+ }
+ for (let i = n - 1; ~i; --i) {
+ const x = maxHeights[i];
+ if (i + 1 < n && x>= maxHeights[i + 1]) {
+ g[i] = g[i + 1] + x;
+ } else {
+ const j = right[i];
+ g[i] = x * (j - i) + (j < n ? g[j] : 0); + } + } + let ans = 0; + for (let i = 0; i < n; ++i) { + ans = Math.max(ans, f[i] + g[i] - maxHeights[i]); + } + return ans; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2866.Beautiful Towers II/README_EN.md b/solution/2800-2899/2866.Beautiful Towers II/README_EN.md new file mode 100644 index 0000000000000..e24de69433a1d --- /dev/null +++ b/solution/2800-2899/2866.Beautiful Towers II/README_EN.md @@ -0,0 +1,366 @@ +# [2866. Beautiful Towers II](https://leetcode.com/problems/beautiful-towers-ii) + +[中文文档](/solution/2800-2899/2866.Beautiful%20Towers%20II/README.md) + +## Description + +You are given a 0-indexed array maxHeights
of n
integers.
+
+You are tasked with building n
towers in the coordinate line. The ith
tower is built at coordinate i
and has a height of heights[i]
.
+
+A configuration of towers is beautiful if the following conditions hold:
+
+
+ 1 <= heights[i] <= maxHeights[i]
+ heights
is a mountain array.
+
+
+Array heights
is a mountain if there exists an index i
such that:
+
+
+ - For all
0 < j <= i
, heights[j - 1] <= heights[j]
+ - For all
i <= k < n - 1
, heights[k + 1] <= heights[k]
+
+
+Return the maximum possible sum of heights of a beautiful configuration of towers.
+
+
+Example 1:
+
+
+Input: maxHeights = [5,3,4,1,1]
+Output: 13
+Explanation: One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since:
+- 1 <= heights[i] <= maxHeights[i]
+- heights is a mountain of peak i = 0.
+It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13.
+
+Example 2:
+
+
+Input: maxHeights = [6,5,3,9,2,7]
+Output: 22
+Explanation: One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since:
+- 1 <= heights[i] <= maxHeights[i]
+- heights is a mountain of peak i = 3.
+It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22.
+
+Example 3:
+
+
+Input: maxHeights = [3,2,5,5,2,3]
+Output: 18
+Explanation: One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since:
+- 1 <= heights[i] <= maxHeights[i]
+- heights is a mountain of peak i = 2.
+Note that, for this configuration, i = 3 can also be considered a peak.
+It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18.
+
+
+
+Constraints:
+
+
+ 1 <= n == maxHeights <= 105
+ 1 <= maxHeights[i] <= 109
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+class Solution:
+ def maximumSumOfHeights(self, maxHeights: List[int]) -> int:
+ n = len(maxHeights)
+ stk = []
+ left = [-1] * n
+ for i, x in enumerate(maxHeights):
+ while stk and maxHeights[stk[-1]]> x:
+ stk.pop()
+ if stk:
+ left[i] = stk[-1]
+ stk.append(i)
+ stk = []
+ right = [n] * n
+ for i in range(n - 1, -1, -1):
+ x = maxHeights[i]
+ while stk and maxHeights[stk[-1]]>= x:
+ stk.pop()
+ if stk:
+ right[i] = stk[-1]
+ stk.append(i)
+ f = [0] * n
+ for i, x in enumerate(maxHeights):
+ if i and x>= maxHeights[i - 1]:
+ f[i] = f[i - 1] + x
+ else:
+ j = left[i]
+ f[i] = x * (i - j) + (f[j] if j != -1 else 0)
+ g = [0] * n
+ for i in range(n - 1, -1, -1):
+ if i < n - 1 and maxHeights[i]>= maxHeights[i + 1]:
+ g[i] = g[i + 1] + maxHeights[i]
+ else:
+ j = right[i]
+ g[i] = maxHeights[i] * (j - i) + (g[j] if j != n else 0)
+ return max(a + b - c for a, b, c in zip(f, g, maxHeights))
+```
+
+### **Java**
+
+```java
+class Solution {
+ public long maximumSumOfHeights(List maxHeights) {
+ int n = maxHeights.size();
+ Deque stk = new ArrayDeque();
+ int[] left = new int[n];
+ int[] right = new int[n];
+ Arrays.fill(left, -1);
+ Arrays.fill(right, n);
+ for (int i = 0; i < n; ++i) { + int x = maxHeights.get(i); + while (!stk.isEmpty() && maxHeights.get(stk.peek())> x) {
+ stk.pop();
+ }
+ if (!stk.isEmpty()) {
+ left[i] = stk.peek();
+ }
+ stk.push(i);
+ }
+ stk.clear();
+ for (int i = n - 1; i>= 0; --i) {
+ int x = maxHeights.get(i);
+ while (!stk.isEmpty() && maxHeights.get(stk.peek())>= x) {
+ stk.pop();
+ }
+ if (!stk.isEmpty()) {
+ right[i] = stk.peek();
+ }
+ stk.push(i);
+ }
+ long[] f = new long[n];
+ long[] g = new long[n];
+ for (int i = 0; i < n; ++i) { + int x = maxHeights.get(i); + if (i> 0 && x>= maxHeights.get(i - 1)) {
+ f[i] = f[i - 1] + x;
+ } else {
+ int j = left[i];
+ f[i] = 1L * x * (i - j) + (j>= 0 ? f[j] : 0);
+ }
+ }
+ for (int i = n - 1; i>= 0; --i) {
+ int x = maxHeights.get(i);
+ if (i < n - 1 && x>= maxHeights.get(i + 1)) {
+ g[i] = g[i + 1] + x;
+ } else {
+ int j = right[i];
+ g[i] = 1L * x * (j - i) + (j < n ? g[j] : 0); + } + } + long ans = 0; + for (int i = 0; i < n; ++i) { + ans = Math.max(ans, f[i] + g[i] - maxHeights.get(i)); + } + return ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + long long maximumSumOfHeights(vector& maxHeights) {
+ int n = maxHeights.size();
+ stack stk;
+ vector left(n, -1);
+ vector right(n, n);
+ for (int i = 0; i < n; ++i) { + int x = maxHeights[i]; + while (!stk.empty() && maxHeights[stk.top()]> x) {
+ stk.pop();
+ }
+ if (!stk.empty()) {
+ left[i] = stk.top();
+ }
+ stk.push(i);
+ }
+ stk = stack();
+ for (int i = n - 1; ~i; --i) {
+ int x = maxHeights[i];
+ while (!stk.empty() && maxHeights[stk.top()]>= x) {
+ stk.pop();
+ }
+ if (!stk.empty()) {
+ right[i] = stk.top();
+ }
+ stk.push(i);
+ }
+ long long f[n], g[n];
+ for (int i = 0; i < n; ++i) { + int x = maxHeights[i]; + if (i && x>= maxHeights[i - 1]) {
+ f[i] = f[i - 1] + x;
+ } else {
+ int j = left[i];
+ f[i] = 1LL * x * (i - j) + (j != -1 ? f[j] : 0);
+ }
+ }
+ for (int i = n - 1; ~i; --i) {
+ int x = maxHeights[i];
+ if (i < n - 1 && x>= maxHeights[i + 1]) {
+ g[i] = g[i + 1] + x;
+ } else {
+ int j = right[i];
+ g[i] = 1LL * x * (j - i) + (j != n ? g[j] : 0);
+ }
+ }
+ long long ans = 0;
+ for (int i = 0; i < n; ++i) { + ans = max(ans, f[i] + g[i] - maxHeights[i]); + } + return ans; + } +}; +``` + +### **Go** + +```go +func maximumSumOfHeights(maxHeights []int) (ans int64) { + n := len(maxHeights) + stk := []int{} + left := make([]int, n) + right := make([]int, n) + for i := range left { + left[i] = -1 + right[i] = n + } + for i, x := range maxHeights { + for len(stk)> 0 && maxHeights[stk[len(stk)-1]]> x {
+ stk = stk[:len(stk)-1]
+ }
+ if len(stk)> 0 {
+ left[i] = stk[len(stk)-1]
+ }
+ stk = append(stk, i)
+ }
+ stk = []int{}
+ for i := n - 1; i>= 0; i-- {
+ x := maxHeights[i]
+ for len(stk)> 0 && maxHeights[stk[len(stk)-1]]>= x {
+ stk = stk[:len(stk)-1]
+ }
+ if len(stk)> 0 {
+ right[i] = stk[len(stk)-1]
+ }
+ stk = append(stk, i)
+ }
+ f := make([]int64, n)
+ g := make([]int64, n)
+ for i, x := range maxHeights {
+ if i> 0 && x>= maxHeights[i-1] {
+ f[i] = f[i-1] + int64(x)
+ } else {
+ j := left[i]
+ f[i] = int64(x) * int64(i-j)
+ if j != -1 {
+ f[i] += f[j]
+ }
+ }
+ }
+ for i := n - 1; i>= 0; i-- {
+ x := maxHeights[i]
+ if i < n-1 && x>= maxHeights[i+1] {
+ g[i] = g[i+1] + int64(x)
+ } else {
+ j := right[i]
+ g[i] = int64(x) * int64(j-i)
+ if j != n {
+ g[i] += g[j]
+ }
+ }
+ }
+ for i, x := range maxHeights {
+ ans = max(ans, f[i]+g[i]-int64(x))
+ }
+ return
+}
+
+func max(a, b int64) int64 {
+ if a> b {
+ return a
+ }
+ return b
+}
+```
+
+### **TypeScript**
+
+```ts
+function maximumSumOfHeights(maxHeights: number[]): number {
+ const n = maxHeights.length;
+ const stk: number[] = [];
+ const left: number[] = Array(n).fill(-1);
+ const right: number[] = Array(n).fill(n);
+ for (let i = 0; i < n; ++i) { + const x = maxHeights[i]; + while (stk.length && maxHeights[stk.at(-1)]> x) {
+ stk.pop();
+ }
+ if (stk.length) {
+ left[i] = stk.at(-1);
+ }
+ stk.push(i);
+ }
+ stk.length = 0;
+ for (let i = n - 1; ~i; --i) {
+ const x = maxHeights[i];
+ while (stk.length && maxHeights[stk.at(-1)]>= x) {
+ stk.pop();
+ }
+ if (stk.length) {
+ right[i] = stk.at(-1);
+ }
+ stk.push(i);
+ }
+ const f: number[] = Array(n).fill(0);
+ const g: number[] = Array(n).fill(0);
+ for (let i = 0; i < n; ++i) { + const x = maxHeights[i]; + if (i && x>= maxHeights[i - 1]) {
+ f[i] = f[i - 1] + x;
+ } else {
+ const j = left[i];
+ f[i] = x * (i - j) + (j>= 0 ? f[j] : 0);
+ }
+ }
+ for (let i = n - 1; ~i; --i) {
+ const x = maxHeights[i];
+ if (i + 1 < n && x>= maxHeights[i + 1]) {
+ g[i] = g[i + 1] + x;
+ } else {
+ const j = right[i];
+ g[i] = x * (j - i) + (j < n ? g[j] : 0); + } + } + let ans = 0; + for (let i = 0; i < n; ++i) { + ans = Math.max(ans, f[i] + g[i] - maxHeights[i]); + } + return ans; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2866.Beautiful Towers II/Solution.cpp b/solution/2800-2899/2866.Beautiful Towers II/Solution.cpp new file mode 100644 index 0000000000000..30f76fc4e000b --- /dev/null +++ b/solution/2800-2899/2866.Beautiful Towers II/Solution.cpp @@ -0,0 +1,54 @@ +class Solution { +public: + long long maximumSumOfHeights(vector& maxHeights) {
+ int n = maxHeights.size();
+ stack stk;
+ vector left(n, -1);
+ vector right(n, n);
+ for (int i = 0; i < n; ++i) { + int x = maxHeights[i]; + while (!stk.empty() && maxHeights[stk.top()]> x) {
+ stk.pop();
+ }
+ if (!stk.empty()) {
+ left[i] = stk.top();
+ }
+ stk.push(i);
+ }
+ stk = stack();
+ for (int i = n - 1; ~i; --i) {
+ int x = maxHeights[i];
+ while (!stk.empty() && maxHeights[stk.top()]>= x) {
+ stk.pop();
+ }
+ if (!stk.empty()) {
+ right[i] = stk.top();
+ }
+ stk.push(i);
+ }
+ long long f[n], g[n];
+ for (int i = 0; i < n; ++i) { + int x = maxHeights[i]; + if (i && x>= maxHeights[i - 1]) {
+ f[i] = f[i - 1] + x;
+ } else {
+ int j = left[i];
+ f[i] = 1LL * x * (i - j) + (j != -1 ? f[j] : 0);
+ }
+ }
+ for (int i = n - 1; ~i; --i) {
+ int x = maxHeights[i];
+ if (i < n - 1 && x>= maxHeights[i + 1]) {
+ g[i] = g[i + 1] + x;
+ } else {
+ int j = right[i];
+ g[i] = 1LL * x * (j - i) + (j != n ? g[j] : 0);
+ }
+ }
+ long long ans = 0;
+ for (int i = 0; i < n; ++i) { + ans = max(ans, f[i] + g[i] - maxHeights[i]); + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2800-2899/2866.Beautiful Towers II/Solution.go b/solution/2800-2899/2866.Beautiful Towers II/Solution.go new file mode 100644 index 0000000000000..6b161d592b4dc --- /dev/null +++ b/solution/2800-2899/2866.Beautiful Towers II/Solution.go @@ -0,0 +1,66 @@ +func maximumSumOfHeights(maxHeights []int) (ans int64) { + n := len(maxHeights) + stk := []int{} + left := make([]int, n) + right := make([]int, n) + for i := range left { + left[i] = -1 + right[i] = n + } + for i, x := range maxHeights { + for len(stk)> 0 && maxHeights[stk[len(stk)-1]]> x {
+ stk = stk[:len(stk)-1]
+ }
+ if len(stk)> 0 {
+ left[i] = stk[len(stk)-1]
+ }
+ stk = append(stk, i)
+ }
+ stk = []int{}
+ for i := n - 1; i>= 0; i-- {
+ x := maxHeights[i]
+ for len(stk)> 0 && maxHeights[stk[len(stk)-1]]>= x {
+ stk = stk[:len(stk)-1]
+ }
+ if len(stk)> 0 {
+ right[i] = stk[len(stk)-1]
+ }
+ stk = append(stk, i)
+ }
+ f := make([]int64, n)
+ g := make([]int64, n)
+ for i, x := range maxHeights {
+ if i> 0 && x>= maxHeights[i-1] {
+ f[i] = f[i-1] + int64(x)
+ } else {
+ j := left[i]
+ f[i] = int64(x) * int64(i-j)
+ if j != -1 {
+ f[i] += f[j]
+ }
+ }
+ }
+ for i := n - 1; i>= 0; i-- {
+ x := maxHeights[i]
+ if i < n-1 && x>= maxHeights[i+1] {
+ g[i] = g[i+1] + int64(x)
+ } else {
+ j := right[i]
+ g[i] = int64(x) * int64(j-i)
+ if j != n {
+ g[i] += g[j]
+ }
+ }
+ }
+ for i, x := range maxHeights {
+ ans = max(ans, f[i]+g[i]-int64(x))
+ }
+ return
+}
+
+func max(a, b int64) int64 {
+ if a> b {
+ return a
+ }
+ return b
+}
\ No newline at end of file
diff --git a/solution/2800-2899/2866.Beautiful Towers II/Solution.java b/solution/2800-2899/2866.Beautiful Towers II/Solution.java
new file mode 100644
index 0000000000000..6ec05124753f9
--- /dev/null
+++ b/solution/2800-2899/2866.Beautiful Towers II/Solution.java
@@ -0,0 +1,56 @@
+class Solution {
+ public long maximumSumOfHeights(List maxHeights) {
+ int n = maxHeights.size();
+ Deque stk = new ArrayDeque();
+ int[] left = new int[n];
+ int[] right = new int[n];
+ Arrays.fill(left, -1);
+ Arrays.fill(right, n);
+ for (int i = 0; i < n; ++i) { + int x = maxHeights.get(i); + while (!stk.isEmpty() && maxHeights.get(stk.peek())> x) {
+ stk.pop();
+ }
+ if (!stk.isEmpty()) {
+ left[i] = stk.peek();
+ }
+ stk.push(i);
+ }
+ stk.clear();
+ for (int i = n - 1; i>= 0; --i) {
+ int x = maxHeights.get(i);
+ while (!stk.isEmpty() && maxHeights.get(stk.peek())>= x) {
+ stk.pop();
+ }
+ if (!stk.isEmpty()) {
+ right[i] = stk.peek();
+ }
+ stk.push(i);
+ }
+ long[] f = new long[n];
+ long[] g = new long[n];
+ for (int i = 0; i < n; ++i) { + int x = maxHeights.get(i); + if (i> 0 && x>= maxHeights.get(i - 1)) {
+ f[i] = f[i - 1] + x;
+ } else {
+ int j = left[i];
+ f[i] = 1L * x * (i - j) + (j>= 0 ? f[j] : 0);
+ }
+ }
+ for (int i = n - 1; i>= 0; --i) {
+ int x = maxHeights.get(i);
+ if (i < n - 1 && x>= maxHeights.get(i + 1)) {
+ g[i] = g[i + 1] + x;
+ } else {
+ int j = right[i];
+ g[i] = 1L * x * (j - i) + (j < n ? g[j] : 0); + } + } + long ans = 0; + for (int i = 0; i < n; ++i) { + ans = Math.max(ans, f[i] + g[i] - maxHeights.get(i)); + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2800-2899/2866.Beautiful Towers II/Solution.py b/solution/2800-2899/2866.Beautiful Towers II/Solution.py new file mode 100644 index 0000000000000..dfd09de02718f --- /dev/null +++ b/solution/2800-2899/2866.Beautiful Towers II/Solution.py @@ -0,0 +1,35 @@ +class Solution: + def maximumSumOfHeights(self, maxHeights: List[int]) -> int:
+ n = len(maxHeights)
+ stk = []
+ left = [-1] * n
+ for i, x in enumerate(maxHeights):
+ while stk and maxHeights[stk[-1]]> x:
+ stk.pop()
+ if stk:
+ left[i] = stk[-1]
+ stk.append(i)
+ stk = []
+ right = [n] * n
+ for i in range(n - 1, -1, -1):
+ x = maxHeights[i]
+ while stk and maxHeights[stk[-1]]>= x:
+ stk.pop()
+ if stk:
+ right[i] = stk[-1]
+ stk.append(i)
+ f = [0] * n
+ for i, x in enumerate(maxHeights):
+ if i and x>= maxHeights[i - 1]:
+ f[i] = f[i - 1] + x
+ else:
+ j = left[i]
+ f[i] = x * (i - j) + (f[j] if j != -1 else 0)
+ g = [0] * n
+ for i in range(n - 1, -1, -1):
+ if i < n - 1 and maxHeights[i]>= maxHeights[i + 1]:
+ g[i] = g[i + 1] + maxHeights[i]
+ else:
+ j = right[i]
+ g[i] = maxHeights[i] * (j - i) + (g[j] if j != n else 0)
+ return max(a + b - c for a, b, c in zip(f, g, maxHeights))
diff --git a/solution/2800-2899/2866.Beautiful Towers II/Solution.ts b/solution/2800-2899/2866.Beautiful Towers II/Solution.ts
new file mode 100644
index 0000000000000..2fc5670d40843
--- /dev/null
+++ b/solution/2800-2899/2866.Beautiful Towers II/Solution.ts
@@ -0,0 +1,52 @@
+function maximumSumOfHeights(maxHeights: number[]): number {
+ const n = maxHeights.length;
+ const stk: number[] = [];
+ const left: number[] = Array(n).fill(-1);
+ const right: number[] = Array(n).fill(n);
+ for (let i = 0; i < n; ++i) { + const x = maxHeights[i]; + while (stk.length && maxHeights[stk.at(-1)]> x) {
+ stk.pop();
+ }
+ if (stk.length) {
+ left[i] = stk.at(-1);
+ }
+ stk.push(i);
+ }
+ stk.length = 0;
+ for (let i = n - 1; ~i; --i) {
+ const x = maxHeights[i];
+ while (stk.length && maxHeights[stk.at(-1)]>= x) {
+ stk.pop();
+ }
+ if (stk.length) {
+ right[i] = stk.at(-1);
+ }
+ stk.push(i);
+ }
+ const f: number[] = Array(n).fill(0);
+ const g: number[] = Array(n).fill(0);
+ for (let i = 0; i < n; ++i) { + const x = maxHeights[i]; + if (i && x>= maxHeights[i - 1]) {
+ f[i] = f[i - 1] + x;
+ } else {
+ const j = left[i];
+ f[i] = x * (i - j) + (j>= 0 ? f[j] : 0);
+ }
+ }
+ for (let i = n - 1; ~i; --i) {
+ const x = maxHeights[i];
+ if (i + 1 < n && x>= maxHeights[i + 1]) {
+ g[i] = g[i + 1] + x;
+ } else {
+ const j = right[i];
+ g[i] = x * (j - i) + (j < n ? g[j] : 0); + } + } + let ans = 0; + for (let i = 0; i < n; ++i) { + ans = Math.max(ans, f[i] + g[i] - maxHeights[i]); + } + return ans; +} diff --git a/solution/2800-2899/2867.Count Valid Paths in a Tree/README.md b/solution/2800-2899/2867.Count Valid Paths in a Tree/README.md new file mode 100644 index 0000000000000..d9d595add5b5f --- /dev/null +++ b/solution/2800-2899/2867.Count Valid Paths in a Tree/README.md @@ -0,0 +1,108 @@ +# [2867. 统计树中的合法路径数目](https://leetcode.cn/problems/count-valid-paths-in-a-tree) + +[English Version](/solution/2800-2899/2867.Count%20Valid%20Paths%20in%20a%20Tree/README_EN.md) + +## 题目描述 + + + +给你一棵 n
个节点的无向树,节点编号为 1
到 n
。给你一个整数 n
和一个长度为 n - 1
的二维整数数组 edges
,其中 edges[i] = [ui, vi]
表示节点 ui
和 vi
在树中有一条边。
+
+请你返回树中的 合法路径数目 。
+
+如果在节点 a
到节点 b
之间 恰好有一个 节点的编号是质数,那么我们称路径 (a, b)
是 合法的 。
+
+注意:
+
+
+ - 路径
(a, b)
指的是一条从节点 a
开始到节点 b
结束的一个节点序列,序列中的节点 互不相同 ,且相邻节点之间在树上有一条边。
+ - 路径
(a, b)
和路径 (b, a)
视为 同一条 路径,且只计入答案 一次 。
+
+
+
+
+示例 1:
+
+
+
+
+输入:n = 5, edges = [[1,2],[1,3],[2,4],[2,5]]
+输出:4
+解释:恰好有一个质数编号的节点路径有:
+- (1, 2) 因为路径 1 到 2 只包含一个质数 2 。
+- (1, 3) 因为路径 1 到 3 只包含一个质数 3 。
+- (1, 4) 因为路径 1 到 4 只包含一个质数 2 。
+- (2, 4) 因为路径 2 到 4 只包含一个质数 2 。
+只有 4 条合法路径。
+
+
+示例 2:
+
+
+
+
+输入:n = 6, edges = [[1,2],[1,3],[2,4],[3,5],[3,6]]
+输出:6
+解释:恰好有一个质数编号的节点路径有:
+- (1, 2) 因为路径 1 到 2 只包含一个质数 2 。
+- (1, 3) 因为路径 1 到 3 只包含一个质数 3 。
+- (1, 4) 因为路径 1 到 4 只包含一个质数 2 。
+- (1, 6) 因为路径 1 到 6 只包含一个质数 3 。
+- (2, 4) 因为路径 2 到 4 只包含一个质数 2 。
+- (3, 6) 因为路径 3 到 6 只包含一个质数 3 。
+只有 6 条合法路径。
+
+
+
+
+提示:
+
+
+ 1 <= n <= 105
+ edges.length == n - 1
+ edges[i].length == 2
+ 1 <= ui, vi <= n
+ - 输入保证
edges
形成一棵合法的树。
+
+
+## 解法
+
+
+
+
+
+### **Python3**
+
+
+
+```python
+
+```
+
+### **Java**
+
+
+
+```java
+
+```
+
+### **C++**
+
+```cpp
+
+```
+
+### **Go**
+
+```go
+
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/2800-2899/2867.Count Valid Paths in a Tree/README_EN.md b/solution/2800-2899/2867.Count Valid Paths in a Tree/README_EN.md
new file mode 100644
index 0000000000000..5a30fe77220d6
--- /dev/null
+++ b/solution/2800-2899/2867.Count Valid Paths in a Tree/README_EN.md
@@ -0,0 +1,94 @@
+# [2867. Count Valid Paths in a Tree](https://leetcode.com/problems/count-valid-paths-in-a-tree)
+
+[中文文档](/solution/2800-2899/2867.Count%20Valid%20Paths%20in%20a%20Tree/README.md)
+
+## Description
+
+There is an undirected tree with n
nodes labeled from 1
to n
. You are given the integer n
and a 2D integer array edges
of length n - 1
, where edges[i] = [ui, vi]
indicates that there is an edge between nodes ui
and vi
in the tree.
+
+Return the number of valid paths in the tree.
+
+A path (a, b)
is valid if there exists exactly one prime number among the node labels in the path from a
to b
.
+
+Note that:
+
+
+ - The path
(a, b)
is a sequence of distinct nodes starting with node a
and ending with node b
such that every two adjacent nodes in the sequence share an edge in the tree.
+ - Path
(a, b)
and path (b, a)
are considered the same and counted only once.
+
+
+
+Example 1:
+
+
+Input: n = 5, edges = [[1,2],[1,3],[2,4],[2,5]]
+Output: 4
+Explanation: The pairs with exactly one prime number on the path between them are:
+- (1, 2) since the path from 1 to 2 contains prime number 2.
+- (1, 3) since the path from 1 to 3 contains prime number 3.
+- (1, 4) since the path from 1 to 4 contains prime number 2.
+- (2, 4) since the path from 2 to 4 contains prime number 2.
+It can be shown that there are only 4 valid paths.
+
+
+Example 2:
+
+
+Input: n = 6, edges = [[1,2],[1,3],[2,4],[3,5],[3,6]]
+Output: 6
+Explanation: The pairs with exactly one prime number on the path between them are:
+- (1, 2) since the path from 1 to 2 contains prime number 2.
+- (1, 3) since the path from 1 to 3 contains prime number 3.
+- (1, 4) since the path from 1 to 4 contains prime number 2.
+- (1, 6) since the path from 1 to 6 contains prime number 3.
+- (2, 4) since the path from 2 to 4 contains prime number 2.
+- (3, 6) since the path from 3 to 6 contains prime number 3.
+It can be shown that there are only 6 valid paths.
+
+
+
+Constraints:
+
+
+ 1 <= n <= 105
+ edges.length == n - 1
+ edges[i].length == 2
+ 1 <= ui, vi <= n
+ - The input is generated such that
edges
represent a valid tree.
+
+
+## Solutions
+
+
+
+### **Python3**
+
+```python
+
+```
+
+### **Java**
+
+```java
+
+```
+
+### **C++**
+
+```cpp
+
+```
+
+### **Go**
+
+```go
+
+```
+
+### **...**
+
+```
+
+```
+
+
diff --git a/solution/2800-2899/2867.Count Valid Paths in a Tree/images/example1.png b/solution/2800-2899/2867.Count Valid Paths in a Tree/images/example1.png
new file mode 100644
index 0000000000000..b7acac14537a1
Binary files /dev/null and b/solution/2800-2899/2867.Count Valid Paths in a Tree/images/example1.png differ
diff --git a/solution/2800-2899/2867.Count Valid Paths in a Tree/images/example2.png b/solution/2800-2899/2867.Count Valid Paths in a Tree/images/example2.png
new file mode 100644
index 0000000000000..a7b6174119c94
Binary files /dev/null and b/solution/2800-2899/2867.Count Valid Paths in a Tree/images/example2.png differ
diff --git a/solution/CONTEST_README.md b/solution/CONTEST_README.md
index 17ce98f042ec5..2cd0c78fb0229 100644
--- a/solution/CONTEST_README.md
+++ b/solution/CONTEST_README.md
@@ -22,6 +22,13 @@
## 往期竞赛
+#### 第 364 场周赛(2023年09月24日 10:30, 90 分钟) 参赛人数 4304
+
+- [2864. 最大二进制奇数](/solution/2800-2899/2864.Maximum%20Odd%20Binary%20Number/README.md)
+- [2865. 美丽塔 I](/solution/2800-2899/2865.Beautiful%20Towers%20I/README.md)
+- [2866. 美丽塔 II](/solution/2800-2899/2866.Beautiful%20Towers%20II/README.md)
+- [2867. 统计树中的合法路径数目](/solution/2800-2899/2867.Count%20Valid%20Paths%20in%20a%20Tree/README.md)
+
#### 第 363 场周赛(2023年09月17日 10:30, 90 分钟) 参赛人数 4768
- [2859. 计算 K 置位下标对应元素的和](/solution/2800-2899/2859.Sum%20of%20Values%20at%20Indices%20With%20K%20Set%20Bits/README.md)
diff --git a/solution/CONTEST_README_EN.md b/solution/CONTEST_README_EN.md
index f625b437840af..ac78a39463c6b 100644
--- a/solution/CONTEST_README_EN.md
+++ b/solution/CONTEST_README_EN.md
@@ -25,6 +25,13 @@ Get your rating changes right after the completion of LeetCode contests, https:/
## Past Contests
+#### Weekly Contest 364
+
+- [2864. Maximum Odd Binary Number](/solution/2800-2899/2864.Maximum%20Odd%20Binary%20Number/README_EN.md)
+- [2865. Beautiful Towers I](/solution/2800-2899/2865.Beautiful%20Towers%20I/README_EN.md)
+- [2866. Beautiful Towers II](/solution/2800-2899/2866.Beautiful%20Towers%20II/README_EN.md)
+- [2867. Count Valid Paths in a Tree](/solution/2800-2899/2867.Count%20Valid%20Paths%20in%20a%20Tree/README_EN.md)
+
#### Weekly Contest 363
- [2859. Sum of Values at Indices With K Set Bits](/solution/2800-2899/2859.Sum%20of%20Values%20at%20Indices%20With%20K%20Set%20Bits/README_EN.md)
diff --git a/solution/README.md b/solution/README.md
index ac4940e179dbd..f35c478b4cb66 100644
--- a/solution/README.md
+++ b/solution/README.md
@@ -2874,6 +2874,10 @@
| 2861 | [最大合金数](/solution/2800-2899/2861.Maximum%20Number%20of%20Alloys/README.md) | | 中等 | 第 363 场周赛 |
| 2862 | [完全子集的最大元素和](/solution/2800-2899/2862.Maximum%20Element-Sum%20of%20a%20Complete%20Subset%20of%20Indices/README.md) | | 困难 | 第 363 场周赛 |
| 2863 | [Maximum Length of Semi-Decreasing Subarrays](/solution/2800-2899/2863.Maximum%20Length%20of%20Semi-Decreasing%20Subarrays/README.md) | | 中等 | 🔒 |
+| 2864 | [最大二进制奇数](/solution/2800-2899/2864.Maximum%20Odd%20Binary%20Number/README.md) | | 简单 | 第 364 场周赛 |
+| 2865 | [美丽塔 I](/solution/2800-2899/2865.Beautiful%20Towers%20I/README.md) | | 中等 | 第 364 场周赛 |
+| 2866 | [美丽塔 II](/solution/2800-2899/2866.Beautiful%20Towers%20II/README.md) | | 中等 | 第 364 场周赛 |
+| 2867 | [统计树中的合法路径数目](/solution/2800-2899/2867.Count%20Valid%20Paths%20in%20a%20Tree/README.md) | | 困难 | 第 364 场周赛 |
## 版权
diff --git a/solution/README_EN.md b/solution/README_EN.md
index 5769d45f22bed..1ee2a32ea4d50 100644
--- a/solution/README_EN.md
+++ b/solution/README_EN.md
@@ -2872,6 +2872,10 @@ Press Control+F(or Command+F on the
| 2861 | [Maximum Number of Alloys](/solution/2800-2899/2861.Maximum%20Number%20of%20Alloys/README_EN.md) | | Medium | Weekly Contest 363 |
| 2862 | [Maximum Element-Sum of a Complete Subset of Indices](/solution/2800-2899/2862.Maximum%20Element-Sum%20of%20a%20Complete%20Subset%20of%20Indices/README_EN.md) | | Hard | Weekly Contest 363 |
| 2863 | [Maximum Length of Semi-Decreasing Subarrays](/solution/2800-2899/2863.Maximum%20Length%20of%20Semi-Decreasing%20Subarrays/README_EN.md) | | Medium | 🔒 |
+| 2864 | [Maximum Odd Binary Number](/solution/2800-2899/2864.Maximum%20Odd%20Binary%20Number/README_EN.md) | | Easy | Weekly Contest 364 |
+| 2865 | [Beautiful Towers I](/solution/2800-2899/2865.Beautiful%20Towers%20I/README_EN.md) | | Medium | Weekly Contest 364 |
+| 2866 | [Beautiful Towers II](/solution/2800-2899/2866.Beautiful%20Towers%20II/README_EN.md) | | Medium | Weekly Contest 364 |
+| 2867 | [Count Valid Paths in a Tree](/solution/2800-2899/2867.Count%20Valid%20Paths%20in%20a%20Tree/README_EN.md) | | Hard | Weekly Contest 364 |
## Copyright
diff --git a/solution/summary.md b/solution/summary.md
index 0e3109f5f8179..a03efcc22e2e0 100644
--- a/solution/summary.md
+++ b/solution/summary.md
@@ -2919,3 +2919,7 @@
- [2861.最大合金数](/solution/2800-2899/2861.Maximum%20Number%20of%20Alloys/README.md)
- [2862.完全子集的最大元素和](/solution/2800-2899/2862.Maximum%20Element-Sum%20of%20a%20Complete%20Subset%20of%20Indices/README.md)
- [2863.Maximum Length of Semi-Decreasing Subarrays](/solution/2800-2899/2863.Maximum%20Length%20of%20Semi-Decreasing%20Subarrays/README.md)
+ - [2864.最大二进制奇数](/solution/2800-2899/2864.Maximum%20Odd%20Binary%20Number/README.md)
+ - [2865.美丽塔 I](/solution/2800-2899/2865.Beautiful%20Towers%20I/README.md)
+ - [2866.美丽塔 II](/solution/2800-2899/2866.Beautiful%20Towers%20II/README.md)
+ - [2867.统计树中的合法路径数目](/solution/2800-2899/2867.Count%20Valid%20Paths%20in%20a%20Tree/README.md)
diff --git a/solution/summary_en.md b/solution/summary_en.md
index f902bfd234a4f..e04529e6f0739 100644
--- a/solution/summary_en.md
+++ b/solution/summary_en.md
@@ -2919,3 +2919,7 @@
- [2861.Maximum Number of Alloys](/solution/2800-2899/2861.Maximum%20Number%20of%20Alloys/README_EN.md)
- [2862.Maximum Element-Sum of a Complete Subset of Indices](/solution/2800-2899/2862.Maximum%20Element-Sum%20of%20a%20Complete%20Subset%20of%20Indices/README_EN.md)
- [2863.Maximum Length of Semi-Decreasing Subarrays](/solution/2800-2899/2863.Maximum%20Length%20of%20Semi-Decreasing%20Subarrays/README_EN.md)
+ - [2864.Maximum Odd Binary Number](/solution/2800-2899/2864.Maximum%20Odd%20Binary%20Number/README_EN.md)
+ - [2865.Beautiful Towers I](/solution/2800-2899/2865.Beautiful%20Towers%20I/README_EN.md)
+ - [2866.Beautiful Towers II](/solution/2800-2899/2866.Beautiful%20Towers%20II/README_EN.md)
+ - [2867.Count Valid Paths in a Tree](/solution/2800-2899/2867.Count%20Valid%20Paths%20in%20a%20Tree/README_EN.md)