Skip to content

Commit 5d4fbf0

Browse files
committed
贪心 +1
1 parent 57da7a3 commit 5d4fbf0

File tree

3 files changed

+79
-26
lines changed

3 files changed

+79
-26
lines changed

src/array/ext/math.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//! * 中等
88
//! * [462. 最小操作次数使数组元素相等 II](min_moves2)
99
//! * [667. 优美的排列 II](construct_array)
10+
//! * [1759. 统计同构子字符串的数目](count_homogenous)
1011
1112
/// [462. 最少移动次数使数组元素相等 II](https://leetcode.cn/problems/minimum-moves-to-equal-array-elements-ii/)
1213
///
@@ -431,7 +432,7 @@ pub fn num_magic_squares_inside(grid: Vec<Vec<i32>>) -> i32 {
431432

432433
/// [1759. 统计同构子字符串的数目](https://leetcode.cn/problems/count-number-of-homogenous-substrings/)
433434
pub fn count_homogenous(s: String) -> i32 {
434-
if s.is_empty(){
435+
if s.is_empty() {
435436
return 0;
436437
}
437438

@@ -442,21 +443,21 @@ pub fn count_homogenous(s: String) -> i32 {
442443
let mut last = chrs.next().unwrap();
443444
let mut count: i64 = 1;
444445

445-
for c in chrs{
446-
if last == c{
446+
for c in chrs {
447+
if last == c {
447448
count += 1;
448449
continue;
449450
}
450451
let cnt = (count + 1) * count / 2;
451452
sum = (sum + (cnt % MODULO) as i32) % MODULO as i32;
452-
453+
453454
last = c;
454455
count = 1;
455456
}
456457

457458
let cnt = (count + 1) * count / 2;
458-
sum = (sum + (cnt % MODULO) as i32) % MODULO as i32;
459-
459+
sum = (sum + (cnt % MODULO) as i32) % MODULO as i32;
460+
460461
sum
461462
}
462463

@@ -466,26 +467,26 @@ mod tests {
466467
use crate::vec2;
467468

468469
#[test]
469-
fn test_count_homogenous(){
470-
struct Testcase{
470+
fn test_count_homogenous() {
471+
struct Testcase {
471472
s: &'static str,
472-
expect: i32
473+
expect: i32,
473474
}
474475

475476
vec![
476-
Testcase{
477+
Testcase {
477478
s: "abbcccaa",
478-
expect: 13
479-
},
480-
Testcase{
481-
s: "xy",
482-
expect: 2
479+
expect: 13,
483480
},
484-
Testcase{
481+
Testcase { s: "xy", expect: 2 },
482+
Testcase {
485483
s: "zzzzz",
486-
expect: 15
487-
}
488-
].into_iter().enumerate().for_each(|(idx, testcase)| {
484+
expect: 15,
485+
},
486+
]
487+
.into_iter()
488+
.enumerate()
489+
.for_each(|(idx, testcase)| {
489490
let Testcase { s, expect } = testcase;
490491
let actual = count_homogenous(s.to_string());
491492
assert_eq!(expect, actual, "case {} failed", idx);

src/moreandmore/mod.rs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! * [455. 分发饼干](find_content_children)
1212
//! * [53. 最大子数组和](max_sub_array)
1313
//! * [942. 增减字符串匹配](di_string_match)
14+
//! * [2027. 转换字符串的最少操作次数](minimum_moves)
1415
//! * 中等
1516
//! * [376. 摆动序列](wiggle_max_length)
1617
//! * [55. 跳跃游戏](can_jump)
@@ -323,22 +324,22 @@ pub fn is_n_straight_hand(hand: Vec<i32>, group_size: i32) -> bool {
323324
hand.sort();
324325

325326
let mut counter = HashMap::new();
326-
for &card in hand.iter(){
327+
for &card in hand.iter() {
327328
*counter.entry(card).or_insert(0) += 1;
328329
}
329330

330-
for i in 0..hand.len(){
331+
for i in 0..hand.len() {
331332
let start = hand[i];
332-
let e= counter.entry(start).or_default();
333-
if *e ==0 {
333+
let e = counter.entry(start).or_default();
334+
if *e == 0 {
334335
// 跳过, 下一个
335336
continue;
336337
}
337338
*e -= 1;
338-
for i in 1..group_size{
339+
for i in 1..group_size {
339340
// 开始假设枚举
340-
let e = counter.entry(start+i).or_default();
341-
if *e == 0{
341+
let e = counter.entry(start + i).or_default();
342+
if *e == 0 {
342343
return false;
343344
}
344345
*e -= 1;
@@ -347,10 +348,61 @@ pub fn is_n_straight_hand(hand: Vec<i32>, group_size: i32) -> bool {
347348
true
348349
}
349350

351+
/// [2027. 转换字符串的最少操作次数](https://leetcode.cn/problems/minimum-moves-to-convert-string/)
352+
///
353+
/// 只要遇到 X, 三个以内不论什么, 都是至少转换一次
354+
/// 既然不管是啥都得转换, 那就直接跳过
355+
pub fn minimum_moves(s: String) -> i32 {
356+
let mut sum = 0;
357+
358+
let bs = s.as_bytes();
359+
let mut idx = 0;
360+
while idx < bs.len(){
361+
if bs[idx] == b'O'{
362+
idx += 1;
363+
continue;
364+
}
365+
sum += 1;
366+
idx += 3;
367+
}
368+
369+
sum
370+
}
371+
350372
#[cfg(test)]
351373
mod tests {
352374
use super::*;
353375

376+
#[test]
377+
fn test_minimum_moves() {
378+
struct TestCase {
379+
s: &'static str,
380+
expect: i32,
381+
}
382+
383+
vec![
384+
TestCase {
385+
s: "XXX",
386+
expect: 1,
387+
},
388+
TestCase {
389+
s: "XXOX",
390+
expect: 2,
391+
},
392+
TestCase {
393+
s: "OOOO",
394+
expect: 0,
395+
},
396+
]
397+
.into_iter()
398+
.enumerate()
399+
.for_each(|(idx, testcase)| {
400+
let TestCase { s, expect } = testcase;
401+
let actual = minimum_moves(s.to_string());
402+
assert_eq!(expect, actual, "case {} failed", idx);
403+
});
404+
}
405+
354406
#[test]
355407
fn test_is_n_straight_hand() {
356408
struct TestCase {

src/moreandmore/no_class.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)