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 9ab5308

Browse files
committed
Add 2 problems
1 parent 4f159b6 commit 9ab5308

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ LeetCode is a website that has programming-related questions that are designed t
2424
| 242 | Valid Anagram | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/is_anagram.rs) | [Leetcode](https://leetcode.com/problems/valid-anagram/) | Easy |
2525
| 282 | Expression Add Operators | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/add_operators.rs) | [Leetcode](https://leetcode.com/problems/expression-add-operators/) | Hard |
2626
| 317 | Shortest Distance from All Buildings | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/shortest_distance.rs) | [Leetcode](https://leetcode.com/problems/shortest-distance-from-all-buildings/) | Hard |
27+
| 325 | Maximum Size Subarray Sum Equals k | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/max_sub_array_len.rs) | [Leetcode](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/solution/) | Medium |
2728
| 334 | Increasing Triplet Subsequence | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/top_interview/array_and_string/increasing_triplet.rs) | [Leetcode](https://leetcode.com/problems/increasing-triplet-subsequence/) | Medium |
2829
| 350 | Intersection of Two Arrays II | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/intersect.rs) | [Leetcode](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | Easy |
2930
| 368 | Largest Divisible Subset | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/largest_divisible_subset.rs) | [Leetcode](https://leetcode.com/problems/largest-divisible-subset/) | Medium |
@@ -32,6 +33,7 @@ LeetCode is a website that has programming-related questions that are designed t
3233
| 485 | Max Consecutive Ones | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/find_max_consecutive_ones.rs) | [Leetcode](https://leetcode.com/problems/max-consecutive-ones/) | Easy |
3334
| 495 | Teemo Attacking | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/find_poisoned_duration.rs) | [Leetcode](https://leetcode.com/problems/teemo-attacking/) | Easy |
3435
| 713 | Subarray Product Less Than K | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/num_subarray_product_less_than_k.rs) | [Leetcode](https://leetcode.com/problems/subarray-product-less-than-k/) | Medium |
36+
| 725 | Split Linked List in Parts | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/split_list_to_parts.rs) | [Leetcode](https://leetcode.com/problems/split-linked-list-in-parts/) | Medium |
3537
| 774 | Minimize Max Distance to Gas Station | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/minmax_gas_dist.rs) | [Leetcode](https://leetcode.com/problems/minimize-max-distance-to-gas-station/) | Medium |
3638
| 875 | Koko Eating Bananas | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/min_eating_speed.rs) | [Leetcode](https://leetcode.com/problems/koko-eating-bananas/) | Medium |
3739
| 922 | Sort Array By Parity II | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/sort_array_by_parity_ii.rs) | [Leetcode](https://leetcode.com/problems/sort-array-by-parity-ii/) | Easy |
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use std::collections::HashMap;
2+
3+
// 325. Maximum Size Subarray Sum Equals k, Medium
4+
// https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/solution/
5+
impl Solution {
6+
pub fn max_sub_array_len(nums: Vec<i32>, k: i32) -> i32 {
7+
let mut max_length = 0;
8+
9+
let mut curr_sum = 0;
10+
let mut sum_to_index_mapping = HashMap::new();
11+
for i in 0..nums.len() {
12+
curr_sum += nums[i];
13+
14+
if curr_sum == k {
15+
max_length = max_length.max(i + 1);
16+
} else if let Some(j) = sum_to_index_mapping.get(&(curr_sum - k)) {
17+
max_length = max_length.max(i - j);
18+
}
19+
20+
if !sum_to_index_mapping.contains_key(&curr_sum) {
21+
sum_to_index_mapping.insert(curr_sum, i);
22+
}
23+
}
24+
25+
return max_length as i32;
26+
}
27+
}
28+
29+
struct Solution {}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use super::*;
34+
35+
#[test]
36+
fn test_max_sub_array_len() {
37+
assert_eq!(Solution::max_sub_array_len(vec![1, -1, 5, -2, 3], 3), 4);
38+
}
39+
40+
#[test]
41+
fn test_max_sub_array_len2() {
42+
assert_eq!(Solution::max_sub_array_len(vec![-2, -1, 2, 1], 1), 2);
43+
}
44+
}

‎src/leetcode/challenge/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod break_palindrome;
22
mod find_max_consecutive_ones;
33
mod intersect;
44
mod max_length;
5+
mod max_sub_array_len;
56
mod minmax_gas_dist;
67
mod moves_to_chessboard;
78
mod num_distinct;
@@ -10,5 +11,6 @@ mod shortest_distance;
1011
mod shortest_path;
1112
mod sort_array_by_parity_ii;
1213
mod spiral_order;
14+
mod split_list_to_parts;
1315
mod tictactoe;
1416
mod tribonacci;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Definition for singly-linked list.
2+
#[derive(PartialEq, Eq, Clone, Debug)]
3+
pub struct ListNode {
4+
pub val: i32,
5+
pub next: Option<Box<ListNode>>,
6+
}
7+
8+
impl ListNode {
9+
#[inline]
10+
fn new(val: i32) -> Self {
11+
ListNode { next: None, val }
12+
}
13+
}
14+
15+
// 725. Split Linked List in Parts, Medium
16+
// https://leetcode.com/problems/split-linked-list-in-parts/
17+
impl Solution {
18+
pub fn split_list_to_parts(head: Option<Box<ListNode>>, k: i32) -> Vec<Option<Box<ListNode>>> {
19+
let mut len = 0;
20+
let mut node = &head;
21+
22+
while let Some(n) = node {
23+
len += 1;
24+
node = &n.next;
25+
}
26+
27+
let mut head = head;
28+
let mut answer = Vec::with_capacity(k as usize);
29+
30+
for i in 0..k as usize {
31+
answer.push(head);
32+
let mut node = &mut answer[i];
33+
for _ in 0..len / k + if i < (len % k) as usize { 1 } else { 0 } {
34+
if let Some(n) = node {
35+
node = &mut n.next;
36+
}
37+
}
38+
head = node.take();
39+
}
40+
answer
41+
}
42+
}
43+
44+
struct Solution {}

0 commit comments

Comments
(0)

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