You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/2600-2699/2697.Lexicographically Smallest Palindrome/README_EN.md
+21-37
Original file line number
Diff line number
Diff line change
@@ -47,18 +47,23 @@
47
47
48
48
## Solutions
49
49
50
+
**Solution 1: Greedy + Two Pointers**
51
+
52
+
We use two pointers $i$ and $j$ to point to the beginning and end of the string, initially $i=0,j=n-1$, where $n$ is the length of the string. Each time we compare $s[i]$ and $s[j]$, if they are not the same, we modify the larger character to the smaller one to make them the same. After the modification, the original string $s$ becomes a palindrome.
53
+
54
+
The time complexity is $O(n)$, where $n$ is the length of the string. We only need to traverse the string once. Ignoring the space consumption of the answer, the space complexity is $O(1)$.
55
+
50
56
<!-- tabs:start -->
51
57
52
58
### **Python3**
53
59
54
60
```python
55
61
classSolution:
56
62
defmakeSmallestPalindrome(self, s: str) -> str:
57
-
i, j =0, len(s) -1
58
63
cs =list(s)
64
+
i, j =0, len(s) -1
59
65
while i < j:
60
-
if s[i] != s[j]:
61
-
cs[i] = cs[j] =min(s[i], s[j])
66
+
cs[i] = cs[j] =min(cs[i], cs[j])
62
67
i, j = i +1, j -1
63
68
return"".join(cs)
64
69
```
@@ -70,11 +75,9 @@ class Solution {
70
75
publicStringmakeSmallestPalindrome(Strings) {
71
76
char[] cs = s.toCharArray();
72
77
for (int i =0, j = cs.length -1; i < j; ++i, --j) {
73
-
if (cs[i] != cs[j]) {
74
-
cs[i] = cs[j] = cs[i] < cs[j] ? cs[i] : cs[j];
75
-
}
78
+
cs[i] = cs[j] = (char) Math.min(cs[i], cs[j]);
76
79
}
77
-
returnString.valueOf(cs);
80
+
returnnewString(cs);
78
81
}
79
82
}
80
83
```
@@ -86,9 +89,7 @@ class Solution {
86
89
public:
87
90
string makeSmallestPalindrome(string s) {
88
91
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
Copy file name to clipboardExpand all lines: solution/2700-2799/2702.Minimum Operations to Make Numbers Non-positive/README_EN.md
+6
Original file line number
Diff line number
Diff line change
@@ -45,6 +45,12 @@ Now, all the numbers in nums are non-positive. Therefore, we return 3.
45
45
46
46
## Solutions
47
47
48
+
**Solution 1: Binary Search**
49
+
50
+
We notice that if an operation count $t$ can make all numbers less than or equal to $0$, then for any $t' > t$, the operation count $t'$ can also make all numbers less than or equal to $0$. Therefore, we can use binary search to find the minimum operation count.
51
+
52
+
We define the left boundary of the binary search as $l=0$, and the right boundary as $r=\max(nums)$. Each time we perform a binary search, we find the middle value $mid=\lfloor\frac{l+r}{2}\rfloor$, and then determine whether there exists an operation method that does not exceed $mid$ and makes all numbers less than or equal to $0$. If it exists, we update the right boundary $r = mid$, otherwise, we update the left boundary
0 commit comments