Skip to content

Commit 07dc782

Browse files
solve 855
1 parent b65079b commit 07dc782

File tree

4 files changed

+75
-20
lines changed

4 files changed

+75
-20
lines changed

src/problem/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
mod p0855_exam_room;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* [925] Long Pressed Name
3+
*
4+
* Your friend is typing his name into a keyboard. Sometimes, when typing a character c, the key might get long pressed, and the character will be typed 1 or more times.
5+
* You examine the typed characters of the keyboard. Return True if it is possible that it was your friends name, with some characters (possibly none) being long pressed.
6+
*
7+
* Example 1:
8+
*
9+
* Input: name = "alex", typed = "aaleex"
10+
* Output: true
11+
* Explanation: 'a' and 'e' in 'alex' were long pressed.
12+
*
13+
* Example 2:
14+
*
15+
* Input: name = "saeed", typed = "ssaaedd"
16+
* Output: false
17+
* Explanation: 'e' must have been pressed twice, but it wasn't in the typed output.
18+
*
19+
* Example 3:
20+
*
21+
* Input: name = "leelee", typed = "lleeelee"
22+
* Output: true
23+
*
24+
* Example 4:
25+
*
26+
* Input: name = "laiden", typed = "laiden"
27+
* Output: true
28+
* Explanation: It's not necessary to long press any character.
29+
*
30+
*
31+
* Constraints:
32+
*
33+
* 1 <= name.length <= 1000
34+
* 1 <= typed.length <= 1000
35+
* name and typed contain only lowercase English letters.
36+
*
37+
*/
38+
pub struct Solution {}
39+
40+
// problem: https://leetcode.com/problems/long-pressed-name/
41+
// discuss: https://leetcode.com/problems/long-pressed-name/discuss/?currentPage=1&orderBy=most_votes&query=
42+
43+
// submission codes start here
44+
45+
impl Solution {
46+
pub fn is_long_pressed_name(name: String, typed: String) -> bool {
47+
48+
false
49+
}
50+
}
51+
52+
// submission codes end
53+
54+
#[cfg(test)]
55+
mod tests {
56+
use super::*;
57+
58+
#[test]
59+
fn test_925() {
60+
}
61+
}

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ mod s0203_remove_linked_list_elements;
4040
mod s1049_last_stone_weight_ii;
4141
mod s0455_assign_cookies;
4242
mod s0986_interval_list_intersections;
43+
mod s0855_exam_room;

src/problem/p0855_exam_room.rs renamed to src/solution/s0855_exam_room.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ pub struct Solution {}
4242

4343
// submission codes start here
4444

45-
use std::collections::HashSet;
45+
use std::collections::BTreeSet;
4646

4747
struct ExamRoom {
48-
seats: HashSet<i32>,
48+
seats: BTreeSet<i32>,
4949
capacity: i32,
5050
}
5151

@@ -58,12 +58,13 @@ impl ExamRoom {
5858

5959
fn new(n: i32) -> Self {
6060
ExamRoom {
61-
seats : HashSet::new(),
61+
seats : BTreeSet::new(),
6262
capacity: n,
6363
}
6464
}
6565

6666
fn seat(&mut self) -> i32 {
67+
// println!("seats is {:?}", self.seats);
6768
if self.seats.len() == 0 {
6869
self.seats.insert(0);
6970
return 0;
@@ -72,19 +73,22 @@ impl ExamRoom {
7273
let (mut left, mut right) = (0, 0);
7374
let mut max_distance = 0;
7475
let mut pre = 0;
76+
let mut pick = 0;
7577
for (i, item) in self.seats.iter().enumerate() {
76-
println!("i is {}, item is {}", i, *item);
77-
if pre == 0 {
78+
// println!("i is {}, item is {}", i, *item);
79+
if i == 0 && *item != 0 {
7880
if *item - pre > max_distance {
7981
left = pre;
8082
right = *item;
8183
max_distance = right - left;
84+
pick = 0;
8285
}
8386
} else {
8487
if (*item - pre) / 2 > max_distance {
8588
left = pre;
8689
right = *item;
8790
max_distance = (right - left) / 2;
91+
pick = left + max_distance;
8892
}
8993
}
9094

@@ -95,22 +99,12 @@ impl ExamRoom {
9599
left = pre;
96100
right = self.capacity - 1;
97101
max_distance = right - left;
102+
pick = right;
98103
}
99-
println!("left is {}, right is {}", left, right);
104+
// println!("left is {}, right is {}", left, right);
100105

101-
if left == 0 {
102-
self.seats.insert(left);
103-
return left;
104-
} else if right == self.capacity - 1 {
105-
self.seats.insert(right);
106-
return right;
107-
} else {
108-
let res = left + (right - left) / 2;
109-
self.seats.insert(res);
110-
return res;
111-
}
112-
113-
0
106+
self.seats.insert(pick);
107+
pick
114108
}
115109

116110
fn leave(&mut self, p: i32) {

0 commit comments

Comments
 (0)