Bạn dùng stream là nó đã lâu hơn r, ko như python dùng built in function chạy nhanh hơn code chay đâuVD: dividePlayers , #2491 (Daily 04/10). Lần đầu em submit theo kiểu Arrays.sort O(nlogn) thì thấy chạy 16ms.
Xem tệp đính kèm 2720641
Lần 2-3 em submit HashMap O(2n) or O(3n) - post #15,780
thì thấy tg chạy ~22ms .

impl Solution {
pub fn min_length(s: String) -> i32 {
let mut stack = vec![];
let (mut pop_count, n) = (0, s.len());
for bc in s.as_bytes().into_iter().copied() {
if stack.is_empty() {
stack.push(bc);
continue;
}
let top = *stack.last().unwrap();
if (top == b'A' && bc == b'B') || (top == b'C' && bc == b'D') {
stack.pop();
pop_count += 1;
continue;
}
stack.push(bc);
}
(n - pop_count * 2) as i32
}
}
Bank mình 50k mở khoá nút like cho
Thế á, sáng đọc lướt tưởng Mao Chủ Tịt code lỏ, hóa ra mắt em lỏ, chủ tịt tha em nhađúng là ngọa hổ tàng long mà, 2 pointer nhìn rối rắm vậy chứ chạy nhanh hơn stack![]()
class Solution {
public int minLength(String s) {
if (s.contains("AB")) {
return minLength(s.replace("AB", ""));
} else if (s.contains("CD")) {
return minLength(s.replace("CD", ""));
} else {
return s.length();
}
}
}
Ặc, vừa thử sửa lại ko dùng stream thì kq chạy nhanh hơn thật.Bạn dùng stream là nó đã lâu hơn r, ko như python dùng built in function chạy nhanh hơn code chay đâu![]()
thanks bro, tuy vậy lúc này kinh tế eo hẹp quá, vợ cho chuyển tối đa 30k thôiBank mình 50k mở khoá nút like cho
.function minSwaps(s: string): number {
const n = s.length;
let st = 0
let res = 0;
for (let i = 0; i <n ; i++) {
if (s[i] === '[') st++;
else {
if (st) st--;
else res++
}
}
return (res + 1) >> 1
};
class Solution {
public:
int minSwaps(string s) {
int count = 0;
int n = s.size();
int res = 0;
for (int i = 0; i < n; ++i){
if (s[i] == '[') {
count += 1;
}
if (s[i] == ']') {
count -= 1;
}
if (count < 0) {
res++;
count += 2;
}
// cout << count << " " << s[i] << "\n";
}
return res;
}
};
class Solution {
public int minSwaps(String s) {
int n = s.length();
int pair = n/2;
int open = 0;
int valid=0;
for(char c: s.toCharArray() ){
if(c=='[') open++;
else{
if(open>0){
open--;
valid++;
}
}
}
return (pair-valid)/2 + (pair-valid)%2;
}
}
class Solution {
public int minSwaps(String s) {
int open = 0;
int bad = 0;
char c;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if (c == '[')
open++;
else if (open == 0)
bad++;
else
open--;
}
return bad % 2 == 0 ? bad / 2 : bad / 2 + 1;
}
}
import java.util.*;
class Solution {
public int minSwaps(String s) {
int n = s.length();
//StringBuilder str = new StringBuilder(s);
int endOpeningBracket = n-1;
int numberClose = 0;
int numberOpen = 0;
int res = 0;
for(int i = 0;i<n;i++){
if(s.charAt(i)==']')
numberClose++;
else
numberOpen++;
if(numberClose>numberOpen){
int j = getEndOpeningBracket(s,endOpeningBracket,i);
// s[i] = '[';
// s[j] = ']';
res++;
numberClose--;
numberOpen++;
endOpeningBracket=j-1;
}
}
return res;
}
int getEndOpeningBracket(String s, int n, int start){
for(int i=n; i>=(n-start+1)/2;i--){
if(s.charAt(i) == '[')
return i;
}
return 0;
}
}
class Solution {
public int minSwaps(String s) {
int count = 0;
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if (c == '[') stack.push(c);
else if (c == ']') {
if (stack.empty()) count++;
else stack.pop();
}
}
return (count + 1) / 2;
}
}
class Solution:
def minSwaps(self, s: str) -> int:
open = 0
close = 0
for i in range(0, len(s)):
if s[i] == '[':
open += 1
if s[i] == ']':
if open == 0:
close += 1
else:
open -= 1
return int((close + 1)/ 2)
class Solution:
def minSwaps(self, s: str) -> int:
opens = 0
for c in s:
if c == ']' :
if opens > 0 :
opens -= 1
else:
opens += 1
return (opens + 1) // 2
class Solution {
public:
int minSwaps(string s) {
int c=0,res=0;
for(auto x:s){
if(x==']') c+=1;
else c-=1;
res=max(res,(c+1)/2);
}
return res;
}
};