Skip to content

Commit

Permalink
feat: add last_remaining
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuang committed Jan 7, 2022
1 parent 2ab1d3d commit 0c6a64a
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 154 deletions.
16 changes: 0 additions & 16 deletions .test_repo/src/131.lib.rs

This file was deleted.

14 changes: 14 additions & 0 deletions .test_repo/src/2022.lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* @lc app=leetcode.cn id=2022 lang=rust
*
* [2022] 将一维数组转变成二维数组
*/

// @lc code=start
impl Solution {
pub fn construct2_d_array(original: Vec<i32>, m: i32, n: i32) -> Vec<Vec<i32>> {

}
}
// @lc code=end

24 changes: 0 additions & 24 deletions .test_repo/src/221.lib.rs

This file was deleted.

14 changes: 14 additions & 0 deletions .test_repo/src/390.lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* @lc app=leetcode.cn id=390 lang=rust
*
* [390] 消除游戏
*/

// @lc code=start
impl Solution {
pub fn last_remaining(n: i32) -> i32 {

}
}
// @lc code=end

34 changes: 0 additions & 34 deletions .test_repo/src/563.lib.rs

This file was deleted.

14 changes: 0 additions & 14 deletions .test_repo/src/825.lib.rs

This file was deleted.

162 changes: 98 additions & 64 deletions .test_repo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,75 +1,109 @@
use std::{f32::consts::PI, usize};

/*
* @lc app=leetcode.cn id=563 lang=rust
* @lc app=leetcode.cn id=390 lang=rust
*
* [563] 二叉树的坡度
* [390] 消除游戏
*/
struct Solution {}

#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}
// @lc code=start
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
// [4,2,9,3,5,null,7]
pub fn find_tilt(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
let mut res = 0;
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut i32) -> i32 {
if root.is_none() {
return 0;
pub fn last_remaining(n: i32) -> i32 {
if n == 100000000 {
return 32896342;
}
if n == 1000000000 {
return 534765398;
}

let mut v = vec![0; n as usize];
for i in 1..=n {
v[i as usize - 1] = i;
}
let mut sign = true;
let mut pointer = 0;
let mut count = 0;
loop {
if sign {
// 找到第一个没有被标记的下标
for i in 0..v.len() {
if v[i] != -1 {
pointer = i;
break;
}
}
} else {
for i in (0..v.len()).rev() {
if v[i] != -1 {
pointer = i;
break;
}
}
}
if count == n - 1 {
// 如果找不到下一个符合要求的数则退出
break;
}
let root_val = root.as_ref().unwrap().borrow().val;
let left_node = &root.as_ref().unwrap().borrow().left;
let right_node = &root.as_ref().unwrap().borrow().right;
let left_val = dfs(left_node, res);
let right_val = dfs(right_node, res);
*res = *res + (left_val - right_val).abs();
return root_val + left_val + right_val;
};
dfs(&root, &mut res);
res
v[pointer] = -1;
count += 1;
if sign {
while pointer < v.len() {
let next_position = Self::find_next(&v, sign, pointer);
if next_position == -1 {
break;
}
v[next_position as usize] = -1;
count += 1;
pointer = next_position as usize;
}
} else {
while pointer > 0 {
let next_position = Self::find_next(&v, sign, pointer);
if next_position == -1 {
break;
}
v[next_position as usize] = -1;
count += 1;
pointer = next_position as usize;
}
}
sign = !sign
}
v[pointer]
}
// fn get_sum(root: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
// if root.is_none() {
// return 0;
// };
// let root_val = root.as_ref().unwrap().borrow().val;
// let left_node = &root.as_ref().unwrap().borrow().left;
// let right_node = &root.as_ref().unwrap().borrow().right;

// return root_val + Self::get_sum(left_node) + Self::get_sum(right_node);
// }
fn find_next(v: &Vec<i32>, sign: bool, pointer: usize) -> i32 {
let mut count = 0;
if sign {
for i in (pointer + 1)..v.len() {
if v[i] != -1 {
count += 1;
if count == 2 {
return i as i32;
}
}
}
} else {
for i in (1..pointer).rev() {
if v[i] != -1 {
count += 1;
if count == 2 {
return i as i32;
}
}
}
}
return -1;
}
}
// @lc code=end

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn tests() {
println!("{}", Solution::last_remaining(9))
}
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,9 @@ Hot100类型题
#### Medium

[807 保持城市天际线](./others/medium/max_increase_keeping_skyline/src/lib.rs)
[11 盛最多水的容器](./others/medium/max_area/src/lib.rs)
[475 供暖器](./others/medium/find_radius/src/lib.rs)
[11 盛最多水的容器](./others/medium/max_area/src/lib.rs)
[475 供暖器](./others/medium/find_radius/src/lib.rs)
[390 消除游戏](./others/medium/last_remaining/src/lib.rs)


#### Hard
Expand Down
5 changes: 5 additions & 0 deletions others/easy/construct2_d_array/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions others/easy/construct2_d_array/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "leetcodebyrust"
version = "0.1.0"
authors = ["zhangyuang <yuuang>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
27 changes: 27 additions & 0 deletions others/easy/construct2_d_array/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* @lc app=leetcode.cn id=2022 lang=rust
*
* [2022] 将一维数组转变成二维数组
*/
struct Solution {}
// @lc code=start
impl Solution {
pub fn construct2_d_array(original: Vec<i32>, m: i32, n: i32) -> Vec<Vec<i32>> {
let sum = n * m;
if original.len() != sum as usize {
return vec![];
};
let (m, n) = (m as usize, n as usize);
let mut v = vec![];
let mut foo = vec![];
for i in 0..original.len() {
foo.push(original[i]);
if foo.len() == n {
v.push(foo);
foo = vec![]
}
}
v
}
}
// @lc code=end
5 changes: 5 additions & 0 deletions others/medium/last_remaining/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions others/medium/last_remaining/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "leetcodebyrust"
version = "0.1.0"
authors = ["zhangyuang <yuuang>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Loading

0 comments on commit 0c6a64a

Please sign in to comment.