Skip to content

Commit 600f16b

Browse files
committed
feat(leet_code): add 2516
1 parent f84ae4d commit 600f16b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+284
-40
lines changed

rust/.idea/csv-editor.xml

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/.idea/rust.iml

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/.idea/workspace.xml

+65-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

rust/leet_code/2073.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright © 2010-2024 <a href="https://github.com/jerodg/">JerodG</a>
2+
//
3+
// This program is free software: you can redistribute it and/or modify it under the terms of the
4+
// Server Side Public License (SSPL) as published by MongoDB, Inc., either version 1 of the License,
5+
// or (at your option) any later version.
6+
//
7+
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8+
// even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the SSPL
9+
// for more details.
10+
//
11+
// The above copyright notice and this permission notice shall be included in all copies or
12+
// substantial portions of the Software. You should have received a copy of the SSPL along with this
13+
// program. If not, see <a href="https://www.mongodb.com/licensing/server-side-public-license">SSPL</a>.
14+
15+
/// This struct provides a solution to calculate the time required to buy tickets.
16+
///
17+
/// The `Solution` struct contains a method to determine the total time required for a person
18+
/// at position `k` in the queue to buy their tickets, given the number of tickets each person
19+
/// in the queue wants to buy.
20+
impl Solution {
21+
/// Calculates the time required for the person at position `k` to buy their tickets.
22+
///
23+
/// # Arguments
24+
///
25+
/// * `tickets` - A vector of integers where each integer represents the number of tickets
26+
/// each person in the queue wants to buy.
27+
/// * `k` - An integer representing the position (0-indexed) of the person in the queue.
28+
///
29+
/// # Returns
30+
///
31+
/// * An integer representing the total time required for the person at position `k` to buy
32+
/// their tickets.
33+
///
34+
/// # Examples
35+
///
36+
/// ```
37+
/// let tickets = vec![2, 3, 2];
38+
/// let k = 2;
39+
/// let result = Solution::time_required_to_buy(tickets, k);
40+
/// assert_eq!(result, 6);
41+
/// ```
42+
pub fn time_required_to_buy(tickets: Vec<i32>, k: i32) -> i32 {
43+
// Iterate over the tickets vector with indices and values, and accumulate the total time
44+
tickets.iter().enumerate().fold(0, |s, (i, &v)|
45+
// Calculate the minimum time required for each person in the queue
46+
s + std::cmp::min(tickets[k as usize] - i32::from(i as i32 > k), v)
47+
)
48+
}
49+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

rust/leet_code/2516.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright © 2010-2024 <a href="https://github.com/jerodg/">JerodG</a>
2+
//
3+
// This program is free software: you can redistribute it and/or modify it under the terms of the
4+
// Server Side Public License (SSPL) as published by MongoDB, Inc., either version 1 of the License,
5+
// or (at your option) any later version.
6+
//
7+
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8+
// even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the SSPL
9+
// for more details.
10+
//
11+
// The above copyright notice and this permission notice shall be included in all copies or
12+
// substantial portions of the Software. You should have received a copy of the SSPL along with this
13+
// program. If not, see <a href="https://www.mongodb.com/licensing/server-side-public-license">SSPL</a>.
14+
15+
/// This module provides a solution to the problem of taking characters from a string
16+
/// such that each character appears at least `k` times in the resulting substring.
17+
impl Solution {
18+
/// Takes characters from the string `s` such that each character appears at least `k` times.
19+
///
20+
/// # Arguments
21+
///
22+
/// * `s` - A string from which characters are taken.
23+
/// * `k` - An integer representing the minimum number of times each character should appear.
24+
///
25+
/// # Returns
26+
///
27+
/// * An integer representing the minimum length of the substring that satisfies the condition.
28+
/// Returns `-1` if it's not possible to satisfy the condition.
29+
///
30+
/// # Examples
31+
///
32+
/// ```
33+
/// let result = Solution::take_characters("aabbcc".to_string(), 2);
34+
/// assert_eq!(result, 6);
35+
/// ```
36+
pub fn take_characters(s: String, k: i32) -> i32 {
37+
// Vector to count occurrences of 'a', 'b', and 'c'
38+
let mut count = vec![0; 3];
39+
40+
// Count the occurrences of each character in the string
41+
for c in s.chars() {
42+
count[(c as u8 - b'a') as usize] += 1;
43+
}
44+
45+
// If any character appears less than `k` times, return -1
46+
if count.iter().min().unwrap() < &k {
47+
return -1;
48+
}
49+
50+
// Convert the string to bytes for easier manipulation
51+
let s_bytes = s.as_bytes();
52+
// Initialize the result with the maximum possible value
53+
let mut res = i32::MAX;
54+
// Left pointer for the sliding window
55+
let mut l = 0;
56+
57+
// Iterate over the string with the right pointer
58+
for r in 0..s_bytes.len() {
59+
// Decrease the count of the current character
60+
count[(s_bytes[r] - b'a') as usize] -= 1;
61+
62+
// Adjust the left pointer to maintain the condition
63+
while count.iter().min().unwrap() < &k {
64+
count[(s_bytes[l] - b'a') as usize] += 1;
65+
l += 1;
66+
}
67+
// Update the result with the minimum length of the valid substring
68+
res = res.min(s_bytes.len() as i32 - (r as i32 - l as i32 + 1));
69+
}
70+
res
71+
}
72+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)