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 3852b67

Browse files
authored
Merge pull request #14 from mapx/master
Solve #792, #1013, and upgrade Cargo.lock
2 parents 4cd3348 + 341dc91 commit 3852b67

File tree

8 files changed

+783
-422
lines changed

8 files changed

+783
-422
lines changed

‎Cargo.lock

Lines changed: 442 additions & 417 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,5 @@ mod n0309_best_time_to_buy_and_sell_stock_with_cooldown;
234234
mod n0310_minimum_height_trees;
235235
mod n0312_burst_balloons;
236236
mod n0313_super_ugly_number;
237+
mod n1013_fibonacci_number;
238+
mod n0792_binary_search;

‎src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ fn main() {
4949
.next();
5050
if code.is_none() {
5151
println!("Problem {} has no rust version.", &id);
52-
solved_ids.push(id);
52+
solved_ids.push(problem.question_id);
5353
continue;
5454
}
5555
let code = code.unwrap();
5656

57-
let file_name = format!("n{:04}_{}", id, problem.title_slug.replace("-", "_"));
57+
let file_name = format!("n{:04}_{}", problem.question_id, problem.title_slug.replace("-", "_"));
5858
let file_path = Path::new("./src").join(format!("{}.rs", file_name));
5959
if file_path.exists() {
6060
panic!("problem already initialized");
@@ -65,7 +65,7 @@ fn main() {
6565
.replace("__PROBLEM_TITLE__", &problem.title)
6666
.replace("__PROBLEM_DESC__", &build_desc(&problem.content))
6767
.replace("__PROBLEM_DEFAULT_CODE__", &code.default_code)
68-
.replace("__PROBLEM_ID__", &format!("{}", id))
68+
.replace("__PROBLEM_ID__", &format!("{}", problem.question_id))
6969
.replace("__EXTRA_USE__", &parse_extra_use(&code.default_code));
7070

7171
let mut file = fs::OpenOptions::new()

‎src/n0792_binary_search.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* [792] Binary Search
3+
*
4+
* Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.
5+
*
6+
* <br />
7+
* Example 1:
8+
*
9+
*
10+
* Input: nums = [-1,0,3,5,9,12], target = 9
11+
* Output: 4
12+
* Explanation: 9 exists in nums and its index is 4
13+
*
14+
*
15+
*
16+
* Example 2:
17+
*
18+
*
19+
* Input: nums = [-1,0,3,5,9,12], target = 2
20+
* Output: -1
21+
* Explanation: 2 does not exist in nums so return -1
22+
*
23+
*
24+
*
25+
*
26+
* Note:
27+
*
28+
* <ol>
29+
* You may assume that all elements in nums are unique.
30+
* n will be in the range [1, 10000].
31+
* The value of each element in nums will be in the range [-9999, 9999].
32+
* </ol>
33+
*
34+
*/
35+
pub struct Solution {}
36+
37+
// submission codes start here
38+
use std::cmp::Ordering;
39+
40+
impl Solution {
41+
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
42+
let mut lo = 0i32;
43+
let mut hi = (nums.len() as i32) - 1;
44+
while lo <= hi {
45+
let mid = lo + (hi - lo) / 2;
46+
match nums[mid as usize].cmp(&target) {
47+
Ordering::Less => {
48+
lo = mid + 1;
49+
}
50+
Ordering::Greater => {
51+
hi = mid - 1;
52+
}
53+
Ordering::Equal => {
54+
return mid;
55+
}
56+
}
57+
}
58+
-1
59+
}
60+
}
61+
62+
// submission codes end
63+
64+
#[cfg(test)]
65+
mod tests {
66+
use super::*;
67+
68+
#[test]
69+
fn test_792() {
70+
assert_eq!(Solution::search(vec![-1, 0, 3, 5, 9, 12], 9), 4);
71+
assert_eq!(Solution::search(vec![-1, 0, 3, 5, 9, 12], 2), -1);
72+
assert_eq!(Solution::search(vec![1], 1), 0);
73+
assert_eq!(Solution::search(vec![5], -5), -1);
74+
assert_eq!(Solution::search(vec![5], 6), -1);
75+
assert_eq!(Solution::search(vec![1, 2], 0), -1);
76+
assert_eq!(Solution::search(vec![1, 2], 1), 0);
77+
assert_eq!(Solution::search(vec![1, 2], 2), 1);
78+
assert_eq!(Solution::search(vec![1, 2], 3), -1);
79+
}
80+
}

‎src/n1013_fibonacci_number.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* [1013] Fibonacci Number
3+
*
4+
* The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
5+
*
6+
*
7+
* F(0) = 0, F(1) = 1
8+
* F(N) = F(N - 1) + F(N - 2), for N > 1.
9+
*
10+
*
11+
* Given N, calculate F(N).
12+
*
13+
*
14+
*
15+
* Example 1:
16+
*
17+
*
18+
* Input: 2
19+
* Output: 1
20+
* Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
21+
*
22+
*
23+
* Example 2:
24+
*
25+
*
26+
* Input: 3
27+
* Output: 2
28+
* Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
29+
*
30+
*
31+
* Example 3:
32+
*
33+
*
34+
* Input: 4
35+
* Output: 3
36+
* Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
37+
*
38+
*
39+
*
40+
*
41+
* Note:
42+
*
43+
* 0 &le; N &le; 30.
44+
*
45+
*/
46+
pub struct Solution {}
47+
48+
// submission codes start here
49+
50+
impl Solution {
51+
pub fn fib(n: i32) -> i32 {
52+
if n == 0 {
53+
return 0;
54+
}
55+
let mut f = (0, 1);
56+
for _ in 1..n {
57+
f = (f.1, f.0 + f.1);
58+
}
59+
return f.1;
60+
}
61+
}
62+
63+
// submission codes end
64+
65+
#[cfg(test)]
66+
mod tests {
67+
use super::*;
68+
69+
#[test]
70+
fn test_1013() {
71+
assert_eq!(Solution::fib(2), 1);
72+
assert_eq!(Solution::fib(3), 2);
73+
assert_eq!(Solution::fib(4), 3);
74+
}
75+
}
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+
}

‎src/problem.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ query questionData($titleSlug: String!) {
1717
}"#;
1818
const QUESTION_QUERY_OPERATION: &str = "questionData";
1919

20-
pub fn get_problem(id: u32) -> Option<Problem> {
20+
pub fn get_problem(frontend_question_id: u32) -> Option<Problem> {
2121
let problems = get_problems().unwrap();
2222
for problem in problems.stat_status_pairs.iter() {
23-
if problem.stat.question_id == id {
23+
if problem.stat.frontend_question_id == frontend_question_id {
2424

2525
if problem.paid_only {
2626
return None
@@ -38,6 +38,7 @@ pub fn get_problem(id: u32) -> Option<Problem> {
3838
content: resp.data.question.content,
3939
sample_test_case: resp.data.question.sample_test_case,
4040
difficulty: problem.difficulty.to_string(),
41+
question_id: problem.stat.question_id,
4142
})
4243
}
4344
}
@@ -58,6 +59,7 @@ pub struct Problem {
5859
#[serde(rename = "sampleTestCase")]
5960
pub sample_test_case: String,
6061
pub difficulty: String,
62+
pub question_id: u32,
6163
}
6264

6365
#[derive(Serialize, Deserialize)]

0 commit comments

Comments
(0)

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