Skip to content

Commit 948f712

Browse files
committed
solve #136
1 parent 1f9b09a commit 948f712

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,6 @@ mod n0129_sum_root_to_leaf_numbers;
131131
mod n0130_surrounded_regions;
132132
mod n0131_palindrome_partitioning;
133133
mod n0132_palindrome_partitioning_ii;
134+
mod n0134_gas_station;
135+
mod n0135_candy;
136+
mod n0136_single_number;

src/n0134_gas_station.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* [134] Gas Station
3+
*
4+
* There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
5+
*
6+
* You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
7+
*
8+
* Return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1.
9+
*
10+
* Note:
11+
*
12+
*
13+
* If there exists a solution, it is guaranteed to be unique.
14+
* Both input arrays are non-empty and have the same length.
15+
* Each element in the input arrays is a non-negative integer.
16+
*
17+
*
18+
* Example 1:
19+
*
20+
*
21+
* Input:
22+
* gas = [1,2,3,4,5]
23+
* cost = [3,4,5,1,2]
24+
*
25+
* Output: 3
26+
*
27+
* Explanation:
28+
* Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
29+
* Travel to station 4. Your tank = 4 - 1 + 5 = 8
30+
* Travel to station 0. Your tank = 8 - 2 + 1 = 7
31+
* Travel to station 1. Your tank = 7 - 3 + 2 = 6
32+
* Travel to station 2. Your tank = 6 - 4 + 3 = 5
33+
* Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
34+
* Therefore, return 3 as the starting index.
35+
*
36+
*
37+
* Example 2:
38+
*
39+
*
40+
* Input:
41+
* gas = [2,3,4]
42+
* cost = [3,4,3]
43+
*
44+
* Output: -1
45+
*
46+
* Explanation:
47+
* You can't start at station 0 or 1, as there is not enough gas to travel to the next station.
48+
* Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
49+
* Travel to station 0. Your tank = 4 - 3 + 2 = 3
50+
* Travel to station 1. Your tank = 3 - 3 + 3 = 3
51+
* You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
52+
* Therefore, you can't travel around the circuit once no matter where you start.
53+
*
54+
*
55+
*/
56+
pub struct Solution {}
57+
58+
// submission codes start here
59+
60+
impl Solution {
61+
pub fn can_complete_circuit(gas: Vec<i32>, cost: Vec<i32>) -> i32 {
62+
0
63+
}
64+
}
65+
66+
// submission codes end
67+
68+
#[cfg(test)]
69+
mod tests {
70+
use super::*;
71+
72+
#[test]
73+
fn test_134() {
74+
}
75+
}

src/n0135_candy.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* [135] Candy
3+
*
4+
* There are N children standing in a line. Each child is assigned a rating value.
5+
*
6+
* You are giving candies to these children subjected to the following requirements:
7+
*
8+
*
9+
* Each child must have at least one candy.
10+
* Children with a higher rating get more candies than their neighbors.
11+
*
12+
*
13+
* What is the minimum candies you must give?
14+
*
15+
* Example 1:
16+
*
17+
*
18+
* Input: [1,0,2]
19+
* Output: 5
20+
* Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
21+
*
22+
*
23+
* Example 2:
24+
*
25+
*
26+
* Input: [1,2,2]
27+
* Output: 4
28+
* Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
29+
* The third child gets 1 candy because it satisfies the above two conditions.
30+
*
31+
*
32+
*/
33+
pub struct Solution {}
34+
35+
// submission codes start here
36+
37+
impl Solution {
38+
pub fn candy(ratings: Vec<i32>) -> i32 {
39+
let mut from = 0;
40+
let mut n = 1;
41+
let mut last = 1;
42+
let mut ascending = false;
43+
for i in 1..ratings.len() {
44+
if ratings[i] == ratings[i-1] {
45+
n += 1;
46+
from = i;
47+
ascending = false;
48+
} else if ratings[i] >= ratings[i-1] {
49+
from = i;
50+
ascending = true;
51+
last += 1;
52+
n += last;
53+
} else {
54+
if ascending {
55+
last = 1;
56+
from = i;
57+
ascending = false;
58+
}
59+
n += (i - from + 1) as i32;
60+
}
61+
}
62+
n
63+
}
64+
}
65+
66+
// submission codes end
67+
68+
#[cfg(test)]
69+
mod tests {
70+
use super::*;
71+
72+
#[test]
73+
fn test_135() {
74+
assert_eq!(Solution::candy(vec![3,2,1,2,3]), 11);
75+
assert_eq!(Solution::candy(vec![2,2,1,2,2]), 7);
76+
assert_eq!(Solution::candy(vec![1,0,2]), 5);
77+
assert_eq!(Solution::candy(vec![1,2,2]), 4);
78+
assert_eq!(Solution::candy(vec![1,1,1,1,1,1]), 6);
79+
assert_eq!(Solution::candy(vec![1,2,2,2,2,2,2,0]), 10);
80+
}
81+
}

src/n0136_single_number.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* [136] Single Number
3+
*
4+
* Given a non-empty array of integers, every element appears twice except for one. Find that single one.
5+
*
6+
* Note:
7+
*
8+
* Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
9+
*
10+
* Example 1:
11+
*
12+
*
13+
* Input: [2,2,1]
14+
* Output: 1
15+
*
16+
*
17+
* Example 2:
18+
*
19+
*
20+
* Input: [4,1,2,1,2]
21+
* Output: 4
22+
*
23+
*
24+
*/
25+
pub struct Solution {}
26+
27+
// submission codes start here
28+
impl Solution {
29+
pub fn single_number(nums: Vec<i32>) -> i32 {
30+
nums.iter().fold(0, |acc, &num| { acc ^ num })
31+
}
32+
}
33+
34+
// submission codes end
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use super::*;
39+
40+
#[test]
41+
fn test_136() {
42+
assert_eq!(Solution::single_number(vec![2,2,1]), 1);
43+
assert_eq!(Solution::single_number(vec![4,1,2,1,2]), 4);
44+
}
45+
}

0 commit comments

Comments
 (0)