Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d055949

Browse files
committed
Add more solutions
1 parent 9d40423 commit d055949

File tree

10 files changed

+393
-1
lines changed

10 files changed

+393
-1
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use std::collections::HashSet;
2+
3+
// 1041. Robot Bounded In Circle, Medium
4+
// https://leetcode.com/problems/robot-bounded-in-circle/submissions/
5+
impl Solution {
6+
pub fn is_robot_bounded(instructions: String) -> bool {
7+
#[derive(PartialEq, Eq)]
8+
enum Direction {
9+
North,
10+
East,
11+
South,
12+
West,
13+
}
14+
15+
let mut pos = (0, 0);
16+
let mut direction = Direction::North;
17+
18+
for instruction in instructions.chars() {
19+
// move
20+
match instruction {
21+
'G' => match direction {
22+
Direction::North => pos.1 += 1,
23+
Direction::East => pos.0 += 1,
24+
Direction::South => pos.1 += -1,
25+
Direction::West => pos.0 += -1,
26+
},
27+
'L' => match direction {
28+
Direction::North => direction = Direction::West,
29+
Direction::East => direction = Direction::North,
30+
Direction::South => direction = Direction::East,
31+
Direction::West => direction = Direction::South,
32+
},
33+
'R' => match direction {
34+
Direction::North => direction = Direction::East,
35+
Direction::East => direction = Direction::South,
36+
Direction::South => direction = Direction::West,
37+
Direction::West => direction = Direction::North,
38+
},
39+
_ => {}
40+
}
41+
}
42+
43+
pos == (0, 0) || direction != Direction::North
44+
}
45+
}
46+
47+
struct Solution {}
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use super::*;
52+
use crate::vec_string;
53+
54+
#[test]
55+
fn test_is_robot_bounded() {
56+
assert_eq!(Solution::is_robot_bounded("GGLLGG".to_string()), true);
57+
}
58+
59+
#[test]
60+
fn test_is_robot_bounded2() {
61+
assert_eq!(Solution::is_robot_bounded("GG".to_string()), false);
62+
}
63+
64+
#[test]
65+
fn test_is_robot_bounded3() {
66+
assert_eq!(Solution::is_robot_bounded("GL".to_string()), true);
67+
}
68+
69+
#[test]
70+
fn test_is_robot_bounded4() {
71+
assert_eq!(Solution::is_robot_bounded("GLGLGGLGL".to_string()), false);
72+
}
73+
}

‎src/leetcode/interview/amazon/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
automod::dir!("src/leetcode/interview/amazon");
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// 937. Reorder Data in Log Files, Easy
2+
// https://leetcode.com/problems/reorder-data-in-log-files/
3+
impl Solution {
4+
pub fn reorder_log_files(logs: Vec<String>) -> Vec<String> {
5+
let mut letter_logs = vec![];
6+
let mut digit_logs = vec![];
7+
8+
for log in logs {
9+
let chunks = log.split(" ").map(|s| s.to_string()).collect::<Vec<String>>();
10+
let id = chunks[0].clone();
11+
let msg = &chunks[1..];
12+
let kind = {
13+
if msg[0].chars().next().unwrap().is_alphabetic() {
14+
0 // letter
15+
} else {
16+
1 // digit
17+
}
18+
};
19+
20+
if kind == 0 {
21+
letter_logs.push((id, msg.join(" ")));
22+
} else {
23+
digit_logs.push(log);
24+
}
25+
}
26+
27+
letter_logs.sort_by(|a, b| {
28+
let (id_a, msg_a) = a;
29+
let (id_b, msg_b) = b;
30+
msg_a.cmp(&msg_b).then_with(|| id_a.cmp(&id_b))
31+
});
32+
33+
letter_logs.into_iter().map(|(id, msg)| format!("{} {}", id, msg)).into_iter().chain(digit_logs).collect()
34+
}
35+
}
36+
37+
struct Solution {}
38+
39+
#[cfg(test)]
40+
mod tests {
41+
use super::*;
42+
use crate::vec_string;
43+
44+
#[test]
45+
fn test_reorder_log_files() {
46+
assert_eq!(
47+
Solution::reorder_log_files(vec_string!["dig1 8 1 5 1", "let1 art can", "dig2 3 6", "let2 own kit dig", "let3 art zero"]),
48+
vec_string!["let1 art can", "let3 art zero", "let2 own kit dig", "dig1 8 1 5 1", "dig2 3 6"]
49+
);
50+
}
51+
52+
#[test]
53+
fn test_reorder_log_files2() {
54+
assert_eq!(
55+
Solution::reorder_log_files(vec_string!["a1 9 2 3 1", "g1 act car", "zo4 4 7", "ab1 off key dog", "a8 act zoo"]),
56+
vec_string!["g1 act car", "a8 act zoo", "ab1 off key dog", "a1 9 2 3 1", "zo4 4 7"]
57+
);
58+
}
59+
}

‎src/leetcode/interview/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod amazon;
2+
mod robinhood;
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
use std::collections::{HashMap, HashSet};
2+
3+
// 1604. Alert Using Same Key-Card Three or More Times in a One Hour Period, Medium
4+
// https://leetcode.com/problems/alert-using-same-key-card-three-or-more-times-in-a-one-hour-period/
5+
impl Solution {
6+
pub fn alert_names(key_name: Vec<String>, key_time: Vec<String>) -> Vec<String> {
7+
let n = key_name.len();
8+
let mut time_map = HashMap::<String, Vec<i32>>::new();
9+
10+
// HH:MM
11+
fn parse_time(time: &String) -> i32 {
12+
let chars = time.chars().collect::<Vec<char>>();
13+
((chars[0].to_digit(10).unwrap() * 10 + chars[1].to_digit(10).unwrap()) * 60 + (chars[3].to_digit(10).unwrap() * 10 + chars[4].to_digit(10).unwrap())) as i32
14+
}
15+
16+
let mut ans = HashSet::<String>::new();
17+
for i in 0..n {
18+
let name = &key_name[i];
19+
let time = parse_time(&key_time[i]);
20+
21+
time_map.entry(name.clone()).or_insert(Vec::<i32>::new());
22+
time_map.get_mut(name).unwrap().push(time);
23+
}
24+
25+
time_map.iter_mut().for_each(|(_, times)| {
26+
times.sort_unstable();
27+
});
28+
29+
time_map.iter().for_each(|(name, times)| {
30+
let mut i = 0;
31+
while times.len() - i > 2 {
32+
if i32::abs(times[i] - times[i + 2]) <= 60 {
33+
ans.insert(name.clone());
34+
}
35+
i += 1;
36+
}
37+
});
38+
39+
let mut ans = ans.iter().map(|f| f.clone()).collect::<Vec<String>>();
40+
ans.sort_unstable();
41+
ans
42+
}
43+
}
44+
45+
struct Solution {}
46+
47+
#[cfg(test)]
48+
mod tests {
49+
use super::*;
50+
use crate::vec_string;
51+
52+
#[test]
53+
fn test_alert_names() {
54+
assert_eq!(
55+
Solution::alert_names(
56+
vec_string!["daniel", "daniel", "daniel", "luis", "luis", "luis", "luis"],
57+
vec_string!["10:00", "10:40", "11:00", "09:00", "11:00", "13:00", "15:00"]
58+
),
59+
vec_string!["daniel"]
60+
);
61+
}
62+
63+
#[test]
64+
fn test_alert_names2() {
65+
assert_eq!(
66+
Solution::alert_names(
67+
vec_string!["alice", "alice", "alice", "bob", "bob", "bob", "bob"],
68+
vec_string!["12:01", "12:00", "18:00", "21:00", "21:20", "21:30", "23:00"]
69+
),
70+
vec_string!["bob"]
71+
);
72+
}
73+
74+
#[test]
75+
fn test_alert_names3() {
76+
assert_eq!(
77+
Solution::alert_names(vec_string!["john", "john", "john"], vec_string!["23:58", "23:59", "00:01"]),
78+
vec_string![]
79+
);
80+
}
81+
82+
#[test]
83+
fn test_alert_names4() {
84+
assert_eq!(
85+
Solution::alert_names(
86+
vec_string!["leslie", "leslie", "leslie", "clare", "clare", "clare", "clare"],
87+
vec_string!["13:00", "13:20", "14:00", "18:00", "18:51", "19:30", "19:49"]
88+
),
89+
vec_string!["clare", "leslie"]
90+
);
91+
}
92+
93+
#[test]
94+
fn test_alert_names5() {
95+
assert_eq!(
96+
Solution::alert_names(
97+
vec_string!["a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b"],
98+
vec_string!["23:27", "03:14", "12:57", "13:35", "13:18", "21:58", "22:39", "10:49", "19:37", "14:14", "10:41"]
99+
),
100+
vec_string!["a"]
101+
);
102+
}
103+
104+
#[test]
105+
fn test_alert_names6() {
106+
assert_eq!(
107+
Solution::alert_names(
108+
vec_string!["a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b"],
109+
vec_string!["23:20", "11:09", "23:30", "23:02", "15:28", "22:57", "23:40", "03:43", "21:55", "20:38", "00:19"]
110+
),
111+
vec_string!["a"]
112+
);
113+
}
114+
}

‎src/leetcode/interview/robinhood/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
automod::dir!("src/leetcode/interview/robinhood");

‎src/leetcode/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod challenge;
22
mod contest;
3+
mod interview;
34
mod problem;
45
mod top_interview;
56
#[cfg_attr(test, macro_use)]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// 1013. Partition Array Into Three Parts With Equal Sum, Easy
2+
// https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/
3+
impl Solution {
4+
pub fn can_three_parts_equal_sum(arr: Vec<i32>) -> bool {
5+
let sum = arr.iter().sum::<i32>();
6+
7+
if sum % 3 != 0 {
8+
return false;
9+
}
10+
11+
let target = sum / 3;
12+
let mut buckets = [0, 0, 0];
13+
let mut buckets_size = [0, 0, 0];
14+
let mut i = 0;
15+
for num in arr.iter() {
16+
buckets[i % 3] += num;
17+
buckets_size[i % 3] += 1;
18+
if buckets[i % 3] == target {
19+
i += 1;
20+
}
21+
}
22+
println!("{:?}", buckets);
23+
buckets_size[0] > 0 && buckets_size[1] > 0 && buckets_size[2] > 0 && buckets[0] == buckets[1] && buckets[1] == buckets[2]
24+
}
25+
}
26+
27+
struct Solution {}
28+
29+
#[cfg(test)]
30+
mod tests {
31+
use super::*;
32+
33+
#[test]
34+
fn test_can_three_parts_equal_sum() {
35+
assert_eq!(Solution::can_three_parts_equal_sum(vec![0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1]), true);
36+
}
37+
38+
#[test]
39+
fn test_can_three_parts_equal_sum2() {
40+
assert_eq!(Solution::can_three_parts_equal_sum(vec![0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1]), true);
41+
}
42+
43+
#[test]
44+
fn test_can_three_parts_equal_sum3() {
45+
assert_eq!(Solution::can_three_parts_equal_sum(vec![0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1]), true);
46+
}
47+
48+
#[test]
49+
fn test_can_three_parts_equal_sum4() {
50+
assert_eq!(Solution::can_three_parts_equal_sum(vec![1, -1, 1, -1]), false);
51+
}
52+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /