Skip to content

Commit 4ef9629

Browse files
committed
A few simple solutions
1 parent c237bce commit 4ef9629

7 files changed

+287
-0
lines changed

1189.maximum-number-of-balloons.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @lc app=leetcode id=1189 lang=rust
3+
*
4+
* [1189] Maximum Number of Balloons
5+
*/
6+
7+
// @lc code=start
8+
use std::collections::HashMap;
9+
use std::cmp;
10+
11+
impl Solution {
12+
pub fn max_number_of_balloons(text: String) -> i32 {
13+
let mut ballon_count = HashMap::new();
14+
for ch in "balon".chars() {
15+
ballon_count.insert(ch, 0);
16+
}
17+
18+
for ch in text.chars() {
19+
match ballon_count.get_mut(&ch) {
20+
Some(val) => *val += 1,
21+
None => ()
22+
}
23+
}
24+
25+
let mut ans = text.len();
26+
27+
for ch in "balon".chars() {
28+
29+
let cnt = *ballon_count.get(&ch).unwrap();
30+
match ch {
31+
'l' | 'o' => {
32+
ans = cmp::min(ans, cnt / 2);
33+
},
34+
'b' | 'a' | 'n' => {
35+
ans = cmp::min(ans, cnt);
36+
},
37+
_ => ()
38+
}
39+
}
40+
41+
ans as i32
42+
}
43+
}
44+
// @lc code=end
45+

1200.minimum-absolute-difference.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode id=1200 lang=rust
3+
*
4+
* [1200] Minimum Absolute Difference
5+
*/
6+
7+
// @lc code=start
8+
use std::collections::HashMap;
9+
use std::i32;
10+
11+
impl Solution {
12+
pub fn minimum_abs_difference(mut arr: Vec<i32>) -> Vec<Vec<i32>> {
13+
let mut diff_map = HashMap::new();
14+
let mut min_diff = i32::MAX;
15+
arr.sort();
16+
17+
for i in 0..arr.len()-1 {
18+
let d = arr[i+1] - arr[i];
19+
if d < min_diff { min_diff = d; }
20+
diff_map.entry(d).or_insert(vec![]).push(vec![arr[i], arr[i+1]]);
21+
}
22+
23+
match diff_map.get(&min_diff) {
24+
Some(v) => v.clone(),
25+
None => vec![]
26+
}
27+
}
28+
}
29+
// @lc code=end
30+

1207.unique-number-of-occurrences.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @lc app=leetcode id=1207 lang=rust
3+
*
4+
* [1207] Unique Number of Occurrences
5+
*/
6+
7+
// @lc code=start
8+
use std::collections::HashMap;
9+
use std::collections::HashSet;
10+
11+
impl Solution {
12+
pub fn unique_occurrences(arr: Vec<i32>) -> bool {
13+
let mut counter = HashMap::new();
14+
15+
for ele in arr {
16+
*counter.entry(ele).or_insert(0) += 1;
17+
}
18+
19+
counter.values().collect::<HashSet<_>>().len() == counter.len()
20+
}
21+
}
22+
// @lc code=end
23+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @lc app=leetcode id=1221 lang=rust
3+
*
4+
* [1221] Split a String in Balanced Strings
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn balanced_string_split(s: String) -> i32 {
10+
let mut balance = 0;
11+
let mut res = 0;
12+
13+
for c in s.chars() {
14+
match c {
15+
'L' => balance += 1,
16+
'R' => balance -= 1,
17+
_ => panic!("Unexpected character found")
18+
}
19+
20+
if balance == 0 {
21+
res += 1;
22+
}
23+
}
24+
25+
res
26+
}
27+
}
28+
// @lc code=end
29+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @lc app=leetcode id=1232 lang=rust
3+
*
4+
* [1232] Check If It Is a Straight Line
5+
*/
6+
7+
// @lc code=start
8+
impl Solution {
9+
pub fn check_straight_line(coordinates: Vec<Vec<i32>>) -> bool {
10+
if (coordinates.len() <= 2) {
11+
return true;
12+
}
13+
14+
let N = coordinates.len();
15+
16+
let p1 = &coordinates[0];
17+
let p2 = &coordinates[1];
18+
19+
for i in 2..N {
20+
let p3 = &coordinates[i];
21+
if !Self::same_line_with(p1, p2, p3) {
22+
return false;
23+
}
24+
}
25+
26+
return true;
27+
}
28+
29+
pub fn same_line_with(p1: &Vec<i32>, p2: &Vec<i32>, p3: &Vec<i32>) -> bool {
30+
(p2[1] - p1[1]) * (p3[0] - p1[0]) == (p3[1] - p1[1]) * (p2[0] - p1[0])
31+
}
32+
}
33+
// @lc code=end
34+

227.basic-calculator-ii.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* @lc app=leetcode id=227 lang=rust
3+
*
4+
* [227] Basic Calculator II
5+
*/
6+
7+
// @lc code=start
8+
use std::collections::HashSet;
9+
10+
impl Solution {
11+
pub fn calculate(mut s: String) -> i32 {
12+
let mut digits = vec![];
13+
let mut ops = vec![];
14+
15+
let mut curr = 0;
16+
s.push('*');
17+
18+
for ch in s.chars() {
19+
if ch.is_ascii_digit() {
20+
curr *= 10;
21+
curr += ch.to_digit(10).unwrap_or_default() as i32;
22+
} else if ch != ' ' {
23+
match ops.pop() {
24+
Some('*') => {
25+
let prev = digits.pop().unwrap();
26+
digits.push(prev * curr);
27+
},
28+
Some('/') => {
29+
let prev = digits.pop().unwrap();
30+
digits.push(prev / curr);
31+
},
32+
Some(op) => {
33+
digits.push(curr);
34+
ops.push(op);
35+
},
36+
None => {
37+
digits.push(curr);
38+
}
39+
}
40+
41+
ops.push(ch);
42+
curr = 0;
43+
}
44+
}
45+
46+
ops.pop();
47+
48+
// println!("digits: {:?}, ops: {:?}, curr: {}", digits, ops, curr);
49+
50+
let mut ans = *digits.first().unwrap_or(&0);
51+
52+
for i in 0..digits.len()-1 {
53+
match ops[i] {
54+
'+' => {
55+
ans += digits[i+1];
56+
},
57+
'-' => {
58+
ans -= digits[i+1];
59+
},
60+
_ => {}
61+
}
62+
}
63+
64+
ans
65+
}
66+
}
67+
// @lc code=end
68+

230.kth-smallest-element-in-a-bst.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* @lc app=leetcode id=230 lang=rust
3+
*
4+
* [230] Kth Smallest Element in a BST
5+
*/
6+
7+
// @lc code=start
8+
// Definition for a binary tree node.
9+
// #[derive(Debug, PartialEq, Eq)]
10+
// pub struct TreeNode {
11+
// pub val: i32,
12+
// pub left: Option<Rc<RefCell<TreeNode>>>,
13+
// pub right: Option<Rc<RefCell<TreeNode>>>,
14+
// }
15+
//
16+
// impl TreeNode {
17+
// #[inline]
18+
// pub fn new(val: i32) -> Self {
19+
// TreeNode {
20+
// val,
21+
// left: None,
22+
// right: None
23+
// }
24+
// }
25+
// }
26+
use std::rc::Rc;
27+
use std::cell::RefCell;
28+
impl Solution {
29+
pub fn kth_smallest(mut root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> i32 {
30+
if root.is_none() { return 0; }
31+
let mut nodes = vec![];
32+
33+
while let Some(rc_node) = root {
34+
nodes.push(Some(rc_node.clone()));
35+
root = rc_node.borrow().left.clone();
36+
}
37+
38+
let mut i = 0;
39+
loop {
40+
root = nodes.pop().unwrap();
41+
i += 1;
42+
if i == k {
43+
let val = root.unwrap().borrow().val;
44+
return val;
45+
}
46+
47+
root = root.unwrap().borrow().right.clone();
48+
while let Some(rc_node) = root {
49+
nodes.push(Some(rc_node.clone()));
50+
root = rc_node.borrow().left.clone();
51+
}
52+
}
53+
54+
return 0;
55+
}
56+
}
57+
// @lc code=end
58+

0 commit comments

Comments
 (0)