-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0335_self_crossing.rs
More file actions
68 lines (60 loc) · 1.8 KB
/
s0335_self_crossing.rs
File metadata and controls
68 lines (60 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#![allow(unused)]
pub struct Solution {}
impl Solution {
pub fn is_self_crossing(x: Vec<i32>) -> bool {
let n = x.len();
if n < 4 {
return false;
}
for i in 3..n {
if x[i] >= x[i - 2] && x[i - 1] <= x[i - 3] {
return true;
}
if i >= 4 {
if x[i - 1] == x[i - 3] && x[i] + x[i - 4] >= x[i - 2] {
return true;
}
}
if i >= 5 {
if x[i - 2] - x[i - 4] >= 0
&& x[i] >= x[i - 2] - x[i - 4]
&& x[i - 1] >= x[i - 3] - x[i - 5]
&& x[i - 1] <= x[i - 3]
{
return true;
}
}
}
return false;
}
}
// Categorize the self-crossing scenarios, there are 3 of them:
// 1. Fourth line crosses first line and works for fifth line crosses second line and so on...
// 2. Fifth line meets first line and works for the lines after
// 3. Sixth line crosses first line and works for the lines after
/* i-2
case 1 : i-1┌─┐
└─┼─>i
i-3
i-2
case 2 : i-1 ┌────┐
└─══>┘i-3
i i-4 (i overlapped i-4)
case 3 : i-4
┌──┐
│i<┼─┐
i-3│ i-5│i-1
└────┘
i-2
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_399() {
// code here
assert_eq!(Solution::is_self_crossing(vec![2, 1, 1, 2,]), true);
assert_eq!(Solution::is_self_crossing(vec![1, 2, 3, 4]), false);
assert_eq!(Solution::is_self_crossing(vec![1, 1, 1, 1,]), true);
}
}