diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/RustIsBestLang.iml b/.idea/RustIsBestLang.iml new file mode 100644 index 0000000..cf84ae4 --- /dev/null +++ b/.idea/RustIsBestLang.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5cc5c53 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..5b2750d --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "RustIsBestLang" +version = "0.1.0" diff --git a/src/bin/cf-1886c.rs b/src/bin/cf-1886c.rs new file mode 100644 index 0000000..4cacf40 --- /dev/null +++ b/src/bin/cf-1886c.rs @@ -0,0 +1,81 @@ +use std::io::stdin; +use std::collections::VecDeque; + +fn calc_sum(n:i32,k:i32) -> i64 { + n as i64 * k as i64 - (k as i64 - 1)* k as i64 / 2 +} + +fn lower_bound(n:i32,x:i64) -> (i32,i64) { + let mut l = 0; + let mut r = n; + while l != r { + let mid = l + r + 1 >> 1; + if calc_sum(n,mid) >= x { + r = mid - 1; + } else { + l = mid; + } + } + (l, x-calc_sum(n,l)) +} + +fn solve() { + let mut buf = String::new(); + stdin().read_line(&mut buf).ok(); + let at = buf.to_owned(); + let a = at.trim().as_bytes(); + let n = a.len(); + let mut ena = vec![true;n]; + let mut cnt = 0; + let mut st:VecDeque = VecDeque::new(); + buf.clear(); + stdin().read_line(&mut buf).ok(); + let pos:i64 = buf.trim().parse().unwrap(); + let (prev, diff) = lower_bound(n as i32,pos); + // println!("{prev} {diff}"); + for (i,&c) in a.iter().enumerate() { + if cnt == prev{ + break; + } + while let Some(&j) = st.back() { + if a[j] <= c { + break; + } + cnt += 1; + ena[j] = false; + if cnt == prev { + break; + } + st.pop_back(); + } + st.push_back(i); + } + for i in (0..n).rev() { + if cnt == prev { + break; + } + if ena[i] { + ena[i] = false; + cnt+=1; + } + } + let mut p = 0; + for (i,&c) in a.iter().enumerate() { + if ena[i] { + p += 1; + if p == diff { + print!("{}",c as char); + return; + } + } + } +} + +fn main() { + let mut buf = String::new(); + stdin().read_line(&mut buf).ok(); + let t:i32 = buf.trim().parse().unwrap(); + for _ in 0..t { + solve(); + } +} \ No newline at end of file diff --git a/src/bin/cf-620c.rs b/src/bin/cf-620c.rs new file mode 100644 index 0000000..94c5eca --- /dev/null +++ b/src/bin/cf-620c.rs @@ -0,0 +1,31 @@ +fn main() { + use std::io::stdin; + use std::collections::HashSet; + let mut buf:String = String::new(); + stdin().read_line(&mut buf).ok(); + let n:usize = buf.trim().parse().unwrap(); + buf.clear(); + let mut a:Vec = Vec::with_capacity(n); + stdin().read_line(&mut buf).ok(); + buf.trim().split(' ').for_each(|val| a.push(val.parse().unwrap())); + let mut mp:HashSet = HashSet::new(); + let mut res = vec![]; + let mut l = 0usize; + for i in 0..n { + if mp.contains(&a[i]) { + res.push([l,i]); + l = i + 1; + mp.clear(); + } else { + mp.insert(a[i]); + } + } + if res.is_empty() { + println!("-1"); + } else { + let m = res.len(); + println!("{}",res.len()); + res[m-1] = [res[m-1][0],n-1]; + res.into_iter().for_each(|[l,r]| println!("{} {}",l+1,r+1)); + } +} \ No newline at end of file diff --git a/src/bin/lc-15.rs b/src/bin/lc-15.rs new file mode 100644 index 0000000..754ec82 --- /dev/null +++ b/src/bin/lc-15.rs @@ -0,0 +1,43 @@ +struct Solution; + +impl Solution { + pub fn three_sum(nums: Vec) -> Vec> { + let mut a = nums.to_owned(); + a.sort(); + let n = a.len(); + let mut res:Vec> = vec![]; + for i in 0..n-2 { + if i>0 && a[i]==a[i-1] { continue; } + let mut l = i + 1; + let mut r = n - 1; + while l < r { + if a[l] + a[r] == -a[i] { + res.push(vec![a[i],a[l],a[r]]); + } + if a[l] + a[r] < -a[i] { + while l < r { + l += 1; + if a[l] != a[l-1] { break; } + } + } else { + while l < r { + r -= 1; + if a[r] != a[r+1] { break; } + } + } + } + } + res + } +} + +fn main() { + let nums = vec![-1,0,1,2,-1,-4]; + let res = Solution::three_sum(nums); + for x in res { + for y in x { + println!("{y} "); + } + println!(); + } +} \ No newline at end of file diff --git a/src/bin/lc-179.rs b/src/bin/lc-179.rs new file mode 100644 index 0000000..81e1923 --- /dev/null +++ b/src/bin/lc-179.rs @@ -0,0 +1,35 @@ +struct Solution; + +impl Solution { + pub fn largest_number(nums: Vec) -> String { + use std::cmp::{Ordering, max}; + let n = nums.len(); + let mut a = Vec::with_capacity(n); + for x in nums { + a.push(x.to_string().into_bytes()); + } + a.sort_by(|s,t| { + let sl = s.len(); + let tl = t.len(); + let L = max(sl,tl); + for i in 0..L { + let i = L % sl; + let j = L % tl; + if s[i]!=t[j] { + return t[j].cmp(&s[i]); + } + } + Ordering::Equal + }); + let mut s = String::new(); + for x in a { + s.push_str(String::from_utf8(x).unwrap().as_str()); + } + s + } +} + +fn main() { + let nums = vec![10,2]; + println!("{}",Solution::largest_number(nums)); +} \ No newline at end of file diff --git a/src/bin/lc-22.rs b/src/bin/lc-22.rs new file mode 100644 index 0000000..9972a28 --- /dev/null +++ b/src/bin/lc-22.rs @@ -0,0 +1,53 @@ +use std::collections::VecDeque; + +struct Solution; + +struct St { + n:i32, + stack:VecDeque, + res:Vec +} + +impl St { + fn solve(&mut self, i: i32, open: i32, close: i32){ + if i == 2 * self.n { + let mut s = String::with_capacity(self.n as usize); + for x in &self.stack { + match x { + 1 => s.push('('), + 0 => s.push(')'), + _ => () + } + } + self.res.push(s); + return; + } + if open < self.n { + self.stack.push_back(1); + self.solve(i+1,open+1,close); + self.stack.pop_back(); + } + if close < open { + self.stack.push_back(0); + self.solve(i+1,open,close+1); + self.stack.pop_back(); + } + + } +} + +impl Solution { + pub fn generate_parenthesis(n: i32) -> Vec { + let mut st = St{n, stack: Default::default(), res: vec![] }; + st.solve(0,0,0); + st.res + } +} + +fn main() { + let n = 3; + let res = Solution::generate_parenthesis(n); + for s in &res { + println!("{s}"); + } +} \ No newline at end of file diff --git a/src/bin/lc-2448.rs b/src/bin/lc-2448.rs new file mode 100644 index 0000000..55c5590 --- /dev/null +++ b/src/bin/lc-2448.rs @@ -0,0 +1,33 @@ +struct Solution; + +impl Solution { + pub fn min_cost(nums: Vec, cost: Vec) -> i64 { + let n = nums.len(); + let mut idx = (0..n).collect::>(); + idx.sort_by(|&i,&j| nums[i].cmp(&nums[j])); + let mut l = 0; + let mut r = 0; + cost.iter().for_each(|&x|r+=x as i64); + let mut min_diff = (r - l).abs(); + let mut med = 0; + for (t,&i) in idx.iter().enumerate() { + r -= cost[i] as i64; + l += cost[i] as i64; + if (r - l).abs() < min_diff { + min_diff = (r-l).abs(); + med = if r > l { nums[idx[t + 1]] } else { nums[i] }; + } + } + let mut res = 0; + for i in 0..n { + res += (nums[idx[i]] - med).abs() as i64 * cost[idx[i]] as i64; + } + res + } +} + +fn main() { + let nums = vec![1,3,5,2]; + let cost = vec![2,3,1,14]; + println!("{}",Solution::min_cost(nums,cost)); +} \ No newline at end of file diff --git a/src/bin/lc-2454.rs b/src/bin/lc-2454.rs new file mode 100644 index 0000000..a3d0861 --- /dev/null +++ b/src/bin/lc-2454.rs @@ -0,0 +1,9 @@ +struct Solution {} + +impl Solution { + pub fn second_greater_element(nums: Vec) -> Vec { + let n = nums.len(); + } +} + +fn main() {} \ No newline at end of file diff --git a/src/bin/lc-2589.rs b/src/bin/lc-2589.rs new file mode 100644 index 0000000..620bc73 --- /dev/null +++ b/src/bin/lc-2589.rs @@ -0,0 +1,39 @@ +struct Solution; + +impl Solution { + pub fn find_minimum_time(tasks: Vec>) -> i32 { + use std::collections::BinaryHeap; + use std::cmp::{min, max}; + let mut tasks = tasks.to_owned(); + tasks.sort(); + let mut cnt = 0; + let mut res = 0; + let mut pq:BinaryHeap<(i32,i32)> = BinaryHeap::new(); + for it in &tasks { + let s = it[0]; + let e = it[1]; + let req = it[2]; + while let Some(&(pe,pc)) = pq.peek() { + if -pe >= s { + pq.pop(); + let new_pc = min(pc, - pe - s + 1); + pq.push((pe, new_pc)); + cnt -= pc - new_pc; + break; + } + pq.pop(); + cnt -= pc; + } + let c = max(req - cnt, 0); + cnt += c; + res += c; + pq.push((-e,c)); + } + res + } +} + +fn main() { + let tasks = vec![vec![2,3,1],vec![4,5,1],vec![1,5,2]]; + println!("{}",Solution::find_minimum_time(tasks)); +} \ No newline at end of file diff --git a/src/bin/lc-2697.rs b/src/bin/lc-2697.rs new file mode 100644 index 0000000..9a862ca --- /dev/null +++ b/src/bin/lc-2697.rs @@ -0,0 +1,22 @@ +struct Solution {} + +impl Solution { + pub fn make_smallest_palindrome(s: String) -> String { + let n = s.len(); + let r = n >> 1; + let mut ss = s.into_bytes(); + for i in 0..r { + if ss[i] > ss[n - 1 - i] { + ss[i] = ss[n-1-i]; + } else { + ss[n-1-i] = ss[i]; + } + } + String::from_utf8(ss).unwrap() + } +} + +fn main() { + let s = "atie".to_string(); + println!("{}",Solution::make_smallest_palindrome(s)); +} \ No newline at end of file diff --git a/src/bin/lc-2828.rs b/src/bin/lc-2828.rs new file mode 100644 index 0000000..e8d8d6a --- /dev/null +++ b/src/bin/lc-2828.rs @@ -0,0 +1,19 @@ +struct Solution; + +impl Solution { + pub fn is_acronym(words: Vec, s: String) -> bool { + let n = words.len(); + let mut s = s.as_bytes(); + if s.len()!=n {return false;} + for i in 0..n { + if words[i].as_bytes()[0] != s[i] { + return false; + } + } + true + } +} + +fn main() { + +} \ No newline at end of file diff --git a/src/bin/lc-321.rs b/src/bin/lc-321.rs new file mode 100644 index 0000000..b479803 --- /dev/null +++ b/src/bin/lc-321.rs @@ -0,0 +1,80 @@ +use std::collections::VecDeque; + +struct Solution; + +impl Solution { + pub fn max_number(a: Vec, b: Vec, k: i32) -> Vec { + let k = k as usize; + let mut i = 0; let n = a.len(); + let mut j = 0; let m = b.len(); + let mut st1 = Vec::new(); + let mut st2 = Vec::new(); + let mut cnt = 0; + while ib[j] || j+1==m || i+1!=n && a[i+1]>b[j+1]) { + while let Some(t) = st1.last() { + if cnt-1+n+m-i-j st2[j] { + res.push(st1[i]); + i += 1; + } else if st1[i] < st2[j] { + res.push(st2[j]); + j += 1; + } else { + if i + 1 == st1.len() { + res.push(st2[j]); + j += 1; + } else if j + 1 == st2.len() { + res.push(st1[i]); + i += 1; + } else if st1[i+1]>st2[j+1] { + res.push(st1[i]); + i += 1; + } else { + res.push(st2[j]); + j += 1; + } + } + } + } + res + } +} + +fn main() { + let nums1 = vec![3,9]; + let nums2 = vec![8,9]; + let k = 3; + let res = Solution::max_number(nums1,nums2, k); + for x in res { + print!("{x} "); + } +} \ No newline at end of file diff --git a/src/bin/lc-376.rs b/src/bin/lc-376.rs new file mode 100644 index 0000000..5c12443 --- /dev/null +++ b/src/bin/lc-376.rs @@ -0,0 +1,41 @@ +struct Solution; + +impl Solution { + pub fn wiggle_max_length(nums: Vec) -> i32 { + use std::collections::VecDeque; + type pii = (i32,i32); + let mut up:VecDeque = VecDeque::new(); + let mut down:VecDeque = VecDeque::new(); + let mut up_max = 0; + let mut down_max = 0; + for x in nums { + while let Some(&(y,_)) = up.back() { + if y < x { break; } + up.pop_back(); + } + while let Some(&(y,_)) = down.back() { + if y > x { break; } + down.pop_back(); + } + let mut u = 1; + let mut d = 1; + if let Some(&(_,l)) = up.back() { + u = u.max(l+1); + } + if let Some(&(_,l)) = down.back() { + d = d.max(l+1); + } + up_max = up_max.max(u); + down_max = down_max.max(d); + down.push_back((x,u)); + up.push_back((x,d)); + } + up_max.max(down_max) + } +} + +fn main() { + let nums = vec![1,7,4,9,2,5]; + let b = nums[1..2].iter().collect::>(); + println!("{}",Solution::wiggle_max_length(nums)); +} \ No newline at end of file diff --git a/src/bin/lc-5.rs b/src/bin/lc-5.rs new file mode 100644 index 0000000..32f2058 --- /dev/null +++ b/src/bin/lc-5.rs @@ -0,0 +1,36 @@ +struct Solution; + +impl Solution { + pub fn longest_palindrome(s: String) -> String { + use std::cmp::max; + let n = s.len(); + let mut res = (0,0); + let mut mx = 0; + let mut dp=vec![vec![false;n];n]; + let ss = s.into_bytes(); + for i in (0..n).rev() { + for j in i..n { + let &c = &ss[i]; + let &d = &ss[j]; + if j == i { + dp[i][j] = true; + } else if j == i + 1 { + dp[i][j] = c==d; + } else { + dp[i][j] = dp[i+1][j-1] && c==d; + } + if dp[i][j] && (j-i+1 > mx) { + mx = max(mx,j-i+1); + res = (i,j); + } + } + } + let (i,j) = res; + String::from_utf8(ss[i..=j].to_owned()).unwrap() + } +} + +fn main() { + let s = String::from("babad"); + println!("{}",Solution::longest_palindrome(s)); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e7a11a9..41cd7a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,15 @@ -fn main() { - println!("Hello, world!"); +trait Printable { + fn print(a:T); +} + +struct Test; + +impl Printable for Test where T: std::fmt::Display { + fn print(a:T) { + println!("{a}"); + } +} + +fn main() { + ::print(1.5); }