From d5eafc86f4a44bf88224b12364c973b39db460c4 Mon Sep 17 00:00:00 2001 From: li_chx Date: Sat, 14 Mar 2026 13:10:03 +0800 Subject: [PATCH] =?UTF-8?q?1415.=20=E9=95=BF=E5=BA=A6=E4=B8=BA=20n=20?= =?UTF-8?q?=E7=9A=84=E5=BC=80=E5=BF=83=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD?= =?UTF-8?q?=E5=AD=97=E5=85=B8=E5=BA=8F=E7=AC=AC=20k=20=E5=B0=8F=E7=9A=84?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 107 +++++++++++++++++++++++----------------------------- 1 file changed, 48 insertions(+), 59 deletions(-) diff --git a/src/main.rs b/src/main.rs index 45d5802..62cfd32 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,72 +1,61 @@ -use std::cmp::{max, Reverse}; -use std::collections::BinaryHeap; struct Solution; mod arr; -#[derive(Debug)] -struct Node { - time: i32, - cnt: i32, -} -impl Node { - fn new(time: i32, cnt: i32) -> Node { - Node { time,cnt } - } - fn total(&self) -> i64 { - let cnt = self.cnt as i64; - self.time as i64 * cnt * (cnt + 1) / 2 - } -} - -impl Ord for Node { - // 默认的比较方式 - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.total().cmp(&other.total()) - } -} - -impl PartialOrd for Node { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.total().cmp(&other.total())) - } -} - -impl Eq for Node { } - -impl PartialEq for Node { - fn eq(&self, other: &Self) -> bool { - self.total() == other.total() - } - - fn ne(&self, other: &Self) -> bool { - self.total() != other.total() - } -} impl Solution { - pub fn min_number_of_seconds(mountain_height: i32, worker_times: Vec) -> i64 { - let mut heap = BinaryHeap::>::new(); - for x in worker_times { - heap.push(Reverse(Node::new(x, 1))); + pub fn construct(s: &mut Vec, n: usize) { + for _ in s.len()..n { + if s[s.len() - 1] == 'a' { + s.push('b'); + } else { + s.push('a'); + } } - for _ in 0..mountain_height { - let mut node = heap.pop().unwrap(); - node.0.cnt += 1; - heap.push(node); + } + pub fn get_happy_string(n: i32, k: i32) -> String { + let n = n as usize; + let mut x = vec!['a']; + Self::construct(&mut x,n); + for _ in 1..k { + let mut have_ans = false; + for j in (0..n).rev() { + if x[j] == 'a' { + have_ans = true; + if j == 0{ + x = vec!['b']; + Self::construct(&mut x,n); + break; + } + if x[j-1] == 'b'{ + x[j] = 'c'; + }else { + x[j] = 'b'; + } + x = x[0..j+1].to_vec(); + Self::construct(&mut x,n); + break; + } + if x[j] == 'b' { + if j == 0 || x[j-1] != 'c' { + x[j] = 'c'; + x = x[0..j+1].to_vec(); + Self::construct(&mut x,n); + have_ans = true; + break; + } + } + } + if !have_ans { + return String::new(); + } } - let mut ans = 0_i64; - while !heap.is_empty() { - let mut node = heap.pop().unwrap().0; - node.cnt -= 1; - ans = max(ans, node.total()) - } - ans + x.into_iter().collect() } } fn main() { - let result = Solution::min_number_of_seconds( - 5, - vec![1, 7], + let result = Solution::get_happy_string( + 10, + 100, ); println!("{:?}", result); }