Skip to content

Commit

Permalink
leetcode 605 can-place-flowers
Browse files Browse the repository at this point in the history
  • Loading branch information
520MianXiangDuiXiang520 committed Sep 29, 2023
1 parent b860041 commit a506e53
Show file tree
Hide file tree
Showing 131 changed files with 270 additions and 25 deletions.
27 changes: 27 additions & 0 deletions LeetCode/1266-minimum-time-visiting-all-points.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
fn main() {
let input = vec![vec![1, 1], vec![3, 4], vec![-1, 0]];
let res = Solution::min_time_to_visit_all_points2(input);
println!("{res}")
}

struct Solution {}

impl Solution {
pub fn min_time_to_visit_all_points(points: Vec<Vec<i32>>) -> i32 {
let mut res = 0;
let size = points.len();
for idx in 0..size - 1 {
let p0 = &points[idx];
let p1 = &points[idx + 1];
res += (p0[0] - p1[0]).abs().max((p0[1] - p1[1]).abs())
}
res
}

pub fn min_time_to_visit_all_points2(points: Vec<Vec<i32>>) -> i32 {
points
.windows(2)
.map(|x| (x[0][0] - x[1][0]).abs().max((x[0][1] - x[1][1]).abs()))
.sum()
}
}
35 changes: 35 additions & 0 deletions LeetCode/1460-make-two-arrays-equal-by-reversing-subarrays.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::collections::HashMap;

fn main() {
let input = vec![1, 2, 3,4];
let arr = vec![4, 2, 3, 1];
let res: bool = Solution::can_be_equal(input, arr);
println!("{res}")
}

struct Solution {}

impl Solution {
pub fn can_be_equal(target: Vec<i32>, arr: Vec<i32>) -> bool {
let size = target.len();
if size != arr.len() {
return false;
}
let mut dict: HashMap<i32, i32> = HashMap::new();
for idx in 0..size {
let v1 = dict.entry(target[idx]).or_insert(0);
*v1 +=1;
let v2 = dict.entry(arr[idx]).or_insert(0);
*v2 -= 1;
if v2.abs() > (size - idx) as i32 {
return false;
}
}
for ele in dict.values() {
if *ele != 0 {
return false;
}
}
true
}
}
13 changes: 13 additions & 0 deletions LeetCode/1523-count-odd-numbers-in-an-interval-range.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn main() {

let res = Solution::count_odds(3, 8);
println!("{:?}", res)
}

struct Solution {}

impl Solution {
pub fn count_odds(low: i32, high: i32) -> i32 {
(low & 1) + (high & 1) + (high - low) >> 1
}
}
31 changes: 31 additions & 0 deletions LeetCode/1582-special-positions-in-a-binary-matrix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
fn main() {

let res = Solution::num_special(vec![
vec![1,0,0],
vec![0,0,1],
vec![1,0,0],
]);
println!("{:?}", res)
}

struct Solution {}

impl Solution {
pub fn num_special(mat: Vec<Vec<i32>>) -> i32 {

let mut res: i32 = 0;

let lines: Vec<i32> = mat.iter().map(|line: &Vec<i32>| line.iter().sum::<i32>()).collect::<Vec<_>>();
let rows: Vec<i32> = (0..mat[0].len()).map(|idx| mat.iter().map(move |line| line[idx])).map(|x| x.sum::<i32>()).collect();

for i in 0..mat.len() {
for j in 0..mat[0].len() {
if mat[i][j] == 1 && lines[i] == 1 && rows[j] == 1{
res += 1;
}
}
}

res
}
}
22 changes: 22 additions & 0 deletions LeetCode/1828-queries-on-number-of-points-inside-a-circle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
fn main() {
let input = vec![vec![1, 3], vec![3, 3], vec![5, 3], vec![2, 2]];
let que = vec![vec![2, 3, 1], vec![4, 3, 1], vec![1, 1, 2]];
let res = Solution::count_points(input, que);
println!("{:?}", res)
}

struct Solution {}

impl Solution {
pub fn count_points(points: Vec<Vec<i32>>, queries: Vec<Vec<i32>>) -> Vec<i32> {
queries
.iter()
.map(|x: &Vec<i32>| {
points
.iter()
.filter(|p: &&Vec<i32>| (p[0] - x[0]).pow(2) + (p[1] - x[1]).pow(2) <= x[2] * x[2])
.count() as i32
})
.collect()
}
}
38 changes: 38 additions & 0 deletions LeetCode/2594-minimum-time-to-repair-cars.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
fn main() {
// let res = Solution::repair_cars(vec![3,3,1,2,1,1,3,2,1], 58);
// let res = Solution::repair_cars(vec![4, 2, 3, 1], 10);
let res = Solution::repair_cars(vec![100], 1000000);
println!("{:?}", res)
}

struct Solution {}

impl Solution {
pub fn repair_cars(ranks: Vec<i32>, cars: i32) -> i64 {
let dc = cars as i64 * cars as i64;
let mut max = ranks[0] as i64 * dc;
if max < 0 {
max = i64::MAX;
}
let mut min = 0i64;
while min < max {
let mid = (min + max) >> 1;

// let num:i64 = ranks.iter().map(|r| Solution::get_cars_by_t(mid, *r)).sum();
let mut num = 0i32;
for rank in &ranks {
num += ((mid as f64) / (*rank as f64)).sqrt().floor() as i32;
if num >= cars {
break;
}
}

if num < cars {
min = mid + 1
} else {
max = mid
}
}
min
}
}
14 changes: 14 additions & 0 deletions LeetCode/268-missing-number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fn main() {
let input = vec![3, 0, 1];
let res = Solution::missing_number(input);
println!("{:?}", res)
}

struct Solution {}

impl Solution {
pub fn missing_number(nums: Vec<i32>) -> i32 {
let n: i32 = nums.len() as i32;
(1 + n) * n / 2 - nums.iter().sum::<i32>()
}
}
13 changes: 13 additions & 0 deletions LeetCode/2710-remove-trailing-zeros-from-a-string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn main() {
let input = String::from("658878000");
let res = Solution::remove_trailing_zeros(input);
println!("{res}")
}

struct Solution {}

impl Solution {
pub fn remove_trailing_zeros(num: String) -> String {
num.trim_end_matches('0').to_owned()
}
}
37 changes: 37 additions & 0 deletions LeetCode/605-can-place-flowers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
fn main() {
// let res = Solution::repair_cars(vec![3,3,1,2,1,1,3,2,1], 58);
// let res = Solution::repair_cars(vec![4, 2, 3, 1], 10);
let res = Solution::can_place_flowers(vec![1, 0, 0, 0, 0, 1], 2);
println!("{:?}", res)
}

struct Solution {}

impl Solution {
pub fn can_place_flowers(flowerbed: Vec<i32>, n: i32) -> bool {
if n == 0 {
return true;
}
let mut count = 0;
let size = flowerbed.len();

let mut i: usize = 0;
while i < size {
if flowerbed[i] == 1
|| (i > 0 && flowerbed[i - 1] == 1)
|| (i < size - 1 && flowerbed[i + 1] == 1)
{
i += 1;
continue;
}

i += 2;
count += 1;
if count >= n {
return true;
}
}

false
}
}
15 changes: 12 additions & 3 deletions LeetCode/leetcode_rust/cp.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/usr/bin/env/bash

no="$1"
name="$2"
url=$1
curl -s -o tmp "$url"
no=$(< tmp awk -F'questionId":"' '{print $2}' | awk -F\" '{print $1}'| grep '\d')
name=$(< tmp awk -F'titleSlug":"' '{print $2}' | awk -F\" '{print $1}'| grep '\w')
echo "$no"
echo "$name"

if (( no == 0 )); then
echo "please use as: bash ./cp.sh 2586 count-the-number-of-vowel-strings-in-range" 1>&2
exit 1
Expand All @@ -18,4 +23,8 @@ if [ ! -f "$file_name" ]; then
fi

cat "./src/main.rs" > "$file_name"
echo "Success: $file_name"
echo "Success: $file_name"
rm tmp

git add ..
git commit -m "leetcode $no $name"
2 changes: 1 addition & 1 deletion LeetCode/leetcode_rust/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ run:
cargo run

cp:
bash ./cp.sh $(id) $(name)
bash ./cp.sh $u

.DEFAULT_GOAL := run
45 changes: 26 additions & 19 deletions LeetCode/leetcode_rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
fn main() {
let strs = ["hey", "aeo", "mu", "ooo", "artro"];
let mut input: Vec<String> = vec![];
for ele in strs {
input.push(String::from(ele));
}
let res: i32 = Solution::vowel_strings(input, 1, 4);
println!("{res}")
// let res = Solution::repair_cars(vec![3,3,1,2,1,1,3,2,1], 58);
// let res = Solution::repair_cars(vec![4, 2, 3, 1], 10);
let res = Solution::can_place_flowers(vec![1, 0, 0, 0, 0, 1], 2);
println!("{:?}", res)
}

struct Solution {}

impl Solution {
pub fn vowel_strings(words: Vec<String>, left: i32, right: i32) -> i32 {
let mut res: i32 = 0;
let helper = |w: char| -> bool {
match w {
'a' | 'o' | 'e' | 'i' | 'u' => true,
_ => false,
pub fn can_place_flowers(flowerbed: Vec<i32>, n: i32) -> bool {
if n == 0 {
return true;
}
let mut count = 0;
let size = flowerbed.len();

let mut i: usize = 0;
while i < size {
if flowerbed[i] == 1
|| (i > 0 && flowerbed[i - 1] == 1)
|| (i < size - 1 && flowerbed[i + 1] == 1)
{
i += 1;
continue;
}
};
for idx in left..right + 1 {
let word = words[idx as usize].as_bytes();
if helper(word[0] as char) && helper(word[word.len() - 1] as char) {
res += 1;

i += 2;
count += 1;
if count >= n {
return true;
}
}
res

false
}
}
2 changes: 1 addition & 1 deletion LeetCode/leetcode_rust/target/.rustc_info.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"rustc_fingerprint":16623122027251931819,"outputs":{"15697416045686424142":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n","stderr":""},"10376369925670944939":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/junebao/.rustup/toolchains/stable-aarch64-apple-darwin\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"aarch64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"aes\"\ntarget_feature=\"crc\"\ntarget_feature=\"dit\"\ntarget_feature=\"dotprod\"\ntarget_feature=\"dpb\"\ntarget_feature=\"dpb2\"\ntarget_feature=\"fcma\"\ntarget_feature=\"fhm\"\ntarget_feature=\"flagm\"\ntarget_feature=\"fp16\"\ntarget_feature=\"frintts\"\ntarget_feature=\"jsconv\"\ntarget_feature=\"lor\"\ntarget_feature=\"lse\"\ntarget_feature=\"neon\"\ntarget_feature=\"paca\"\ntarget_feature=\"pacg\"\ntarget_feature=\"pan\"\ntarget_feature=\"pmuv3\"\ntarget_feature=\"ras\"\ntarget_feature=\"rcpc\"\ntarget_feature=\"rcpc2\"\ntarget_feature=\"rdm\"\ntarget_feature=\"sb\"\ntarget_feature=\"sha2\"\ntarget_feature=\"sha3\"\ntarget_feature=\"ssbs\"\ntarget_feature=\"vh\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.64.0 (a55dd71d5 2022-09-19)\nbinary: rustc\ncommit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52\ncommit-date: 2022-09-19\nhost: aarch64-apple-darwin\nrelease: 1.64.0\nLLVM version: 14.0.6\n","stderr":""}},"successes":{}}
{"rustc_fingerprint":16623122027251931819,"outputs":{"10376369925670944939":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/junebao/.rustup/toolchains/stable-aarch64-apple-darwin\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"aarch64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"aes\"\ntarget_feature=\"crc\"\ntarget_feature=\"dit\"\ntarget_feature=\"dotprod\"\ntarget_feature=\"dpb\"\ntarget_feature=\"dpb2\"\ntarget_feature=\"fcma\"\ntarget_feature=\"fhm\"\ntarget_feature=\"flagm\"\ntarget_feature=\"fp16\"\ntarget_feature=\"frintts\"\ntarget_feature=\"jsconv\"\ntarget_feature=\"lor\"\ntarget_feature=\"lse\"\ntarget_feature=\"neon\"\ntarget_feature=\"paca\"\ntarget_feature=\"pacg\"\ntarget_feature=\"pan\"\ntarget_feature=\"pmuv3\"\ntarget_feature=\"ras\"\ntarget_feature=\"rcpc\"\ntarget_feature=\"rcpc2\"\ntarget_feature=\"rdm\"\ntarget_feature=\"sb\"\ntarget_feature=\"sha2\"\ntarget_feature=\"sha3\"\ntarget_feature=\"ssbs\"\ntarget_feature=\"vh\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.64.0 (a55dd71d5 2022-09-19)\nbinary: rustc\ncommit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52\ncommit-date: 2022-09-19\nhost: aarch64-apple-darwin\nrelease: 1.64.0\nLLVM version: 14.0.6\n","stderr":""},"15697416045686424142":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n","stderr":""}},"successes":{}}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file modified LeetCode/leetcode_rust/target/debug/leetcode_rust
Binary file not shown.
1 change: 0 additions & 1 deletion LeetCode/一周中的第几天.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ class Solution:
def dayOfTheWeek(self, day: int, month: int, year: int) -> str:
w = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
return w[(datetime.datetime(year, month, day).weekday())]
```

0 comments on commit a506e53

Please sign in to comment.