Skip to content

Commit 634613a

Browse files
committed
7-20-2020
1 parent 09ac214 commit 634613a

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/_0724_find_pivot_index.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn pivot_index(nums: Vec<i32>) -> i32 {
5+
let sum_all: i32 = nums.iter().sum();
6+
let mut sum: i32 = 0;
7+
for (i, v) in nums.iter().enumerate() {
8+
if sum == sum_all - v - sum {
9+
return i as i32;
10+
}
11+
sum += v;
12+
}
13+
-1
14+
}
15+
}
16+
17+
#[test]
18+
fn test() {
19+
assert_eq!(Solution::pivot_index(vec![1, 7, 3, 6, 5, 6]), 3);
20+
assert_eq!(Solution::pivot_index(vec![1, 2, 3]), -1);
21+
}

src/_1029_two_city_scheduling.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn two_city_sched_cost(costs: Vec<Vec<i32>>) -> i32 {
5+
let n: usize = costs.len();
6+
let mut diff: Vec<i32> = costs.iter().map(|v| v[0] - v[1]).collect();
7+
diff.sort_unstable();
8+
let sum_cityb: i32 = costs.iter().map(|v| v[1]).sum();
9+
let sum_diff: i32 = diff.iter().take(n / 2).sum();
10+
sum_cityb + sum_diff
11+
}
12+
}
13+
14+
#[test]
15+
fn test() {
16+
assert_eq!(
17+
Solution::two_city_sched_cost(vec_vec_i32![[10, 20], [30, 200], [400, 50], [30, 20]]),
18+
110
19+
);
20+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ mod _0605_can_place_flowers;
110110
//
111111
mod _0680_valid_palindrome_ii;
112112
//
113+
mod _0724_find_pivot_index;
114+
//
113115
mod _0735_asteroid_collision;
114116
//
115117
mod _0771_jewels_and_stones;
@@ -122,6 +124,8 @@ mod _0953_verifying_an_alien_dictionary;
122124
//
123125
mod _0977_squares_of_a_sorted_array;
124126
//
127+
mod _1029_two_city_scheduling;
128+
//
125129
mod _1281_subtract_the_product_and_sum_of_digits_of_an_integer;
126130
//
127131
mod _1287_element_appearing_more_than_25_in_sorted_array;

0 commit comments

Comments
 (0)