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 341dc91

Browse files
committed
Reset
1 parent 986ac95 commit 341dc91

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* [1071] Binary Prefix Divisible By 5
3+
*
4+
* Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a binary number (from most-significant-bit to least-significant-bit.)
5+
*
6+
* Return a list of booleans answer, where answer[i] is true if and only if N_i is divisible by 5.
7+
*
8+
* Example 1:
9+
*
10+
*
11+
* Input: <span id="example-input-1-1">[0,1,1]</span>
12+
* Output: <span id="example-output-1">[true,false,false]</span>
13+
* Explanation:
14+
* The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10. Only the first number is divisible by 5, so answer[0] is true.
15+
*
16+
*
17+
* Example 2:
18+
*
19+
*
20+
* Input: <span id="example-input-2-1">[1,1,1]</span>
21+
* Output: <span id="example-output-2">[false,false,false]</span>
22+
*
23+
*
24+
* Example 3:
25+
*
26+
*
27+
* Input: <span id="example-input-3-1">[0,1,1,1,1,1]</span>
28+
* Output: <span id="example-output-3">[true,false,false,false,true,false]</span>
29+
*
30+
*
31+
* Example 4:
32+
*
33+
*
34+
* Input: <span id="example-input-4-1">[1,1,1,0,1]</span>
35+
* Output: <span id="example-output-4">[false,false,false,false,false]</span>
36+
*
37+
*
38+
*
39+
*
40+
* Note:
41+
*
42+
* <ol>
43+
* 1 <= A.length <= 30000
44+
* A[i] is 0 or 1
45+
* </ol>
46+
*
47+
*/
48+
pub struct Solution {}
49+
50+
// submission codes start here
51+
52+
impl Solution {
53+
pub fn prefixes_div_by5(a: Vec<i32>) -> Vec<bool> {
54+
let mut ret = vec![];
55+
let mut n = 0;
56+
for i in a {
57+
let remain = (n * 2 + i) % 5;
58+
ret.push(remain == 0);
59+
n = remain;
60+
}
61+
return ret;
62+
}
63+
}
64+
65+
// submission codes end
66+
67+
#[cfg(test)]
68+
mod tests {
69+
use super::*;
70+
71+
#[test]
72+
fn test_1071() {
73+
assert_eq!(
74+
Solution::prefixes_div_by5(vec![0, 1, 1]),
75+
vec![true, false, false]
76+
);
77+
assert_eq!(
78+
Solution::prefixes_div_by5(vec![1, 1, 1]),
79+
vec![false, false, false]
80+
);
81+
assert_eq!(
82+
Solution::prefixes_div_by5(vec![0, 1, 1, 1, 1, 1]),
83+
vec![true, false, false, false, true, false]
84+
);
85+
assert_eq!(
86+
Solution::prefixes_div_by5(vec![1, 1, 1, 0, 1]),
87+
vec![false, false, false, false, false]
88+
);
89+
assert_eq!(
90+
Solution::prefixes_div_by5(vec![
91+
1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0,
92+
0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1
93+
]),
94+
vec![
95+
false, false, false, false, false, false, false, false, false, false, false, false,
96+
false, false, false, false, false, false, false, false, false, false, false, false,
97+
false, false, false, false, false, false, false, true, false, false, true, true,
98+
true, true, false
99+
]
100+
);
101+
}
102+
}

‎src/n1127_last_stone_weight.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* [1127] Last Stone Weight
3+
*
4+
* We have a collection of rocks, each rock has a positive integer weight.
5+
*
6+
* Each turn, we choose the two heaviest rocks and smash them together. Suppose the stones have weights x and y with x <= y. The result of this smash is:
7+
*
8+
*
9+
* If x == y, both stones are totally destroyed;
10+
* If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x.
11+
*
12+
*
13+
* At the end, there is at most 1 stone left. Return the weight of this stone (or 0 if there are no stones left.)
14+
*
15+
*
16+
*
17+
* Example 1:
18+
*
19+
*
20+
* Input: [2,7,4,1,8,1]
21+
* Output: 1
22+
* Explanation:
23+
* We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
24+
* we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
25+
* we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
26+
* we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone.
27+
*
28+
*
29+
*
30+
* Note:
31+
*
32+
* <ol>
33+
* 1 <= stones.length <= 30
34+
* 1 <= stones[i] <= 1000
35+
* </ol>
36+
*/
37+
pub struct Solution {}
38+
use std::collections::BinaryHeap;
39+
40+
// submission codes start here
41+
42+
impl Solution {
43+
pub fn last_stone_weight(stones: Vec<i32>) -> i32 {
44+
let mut heap = BinaryHeap::new();
45+
heap.extend(stones);
46+
loop {
47+
if let Some(rock1) = heap.pop() {
48+
if let Some(rock2) = heap.pop() {
49+
if rock1 > rock2 {
50+
heap.push(rock1 - rock2);
51+
}
52+
} else {
53+
return rock1;
54+
}
55+
} else {
56+
return 0;
57+
}
58+
}
59+
}
60+
}
61+
62+
// submission codes end
63+
64+
#[cfg(test)]
65+
mod tests {
66+
use super::*;
67+
68+
#[test]
69+
fn test_1127() {
70+
assert_eq!(Solution::last_stone_weight(vec![2, 7, 4, 1, 8, 1]), 1);
71+
assert_eq!(Solution::last_stone_weight(vec![2]), 2);
72+
assert_eq!(Solution::last_stone_weight(vec![2, 2]), 0);
73+
assert_eq!(Solution::last_stone_weight(vec![1, 2, 2]), 1);
74+
}
75+
}

0 commit comments

Comments
(0)

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