From 1aca2987b65ec0d585eb377cd45d7d447b058ba4 Mon Sep 17 00:00:00 2001 From: abdoorigin Date: Sat, 23 Mar 2024 12:56:54 +0200 Subject: [PATCH] commiting cuz i'm bored commiting every now and then --- binary-subarrays-with-sum/Cargo.lock | 7 ++ binary-subarrays-with-sum/Cargo.toml | 8 ++ binary-subarrays-with-sum/src/main.rs | 30 ++++++ contiguous-array/Cargo.lock | 7 ++ contiguous-array/Cargo.toml | 8 ++ contiguous-array/src/main.rs | 26 +++++ .../Cargo.lock | 7 ++ .../Cargo.toml | 8 ++ .../src/main.rs | 23 +++++ custom-sort-string/Cargo.lock | 7 ++ custom-sort-string/Cargo.toml | 8 ++ custom-sort-string/src/main.rs | 40 ++++++++ find-the-pivot-integer/Cargo.lock | 7 ++ find-the-pivot-integer/Cargo.toml | 8 ++ find-the-pivot-integer/src/main.rs | 24 +++++ insert-interval/Cargo.lock | 7 ++ insert-interval/Cargo.toml | 8 ++ insert-interval/src/main.rs | 42 ++++++++ merge-in-between-linked-lists/Cargo.lock | 7 ++ merge-in-between-linked-lists/Cargo.toml | 8 ++ merge-in-between-linked-lists/src/main.rs | 46 +++++++++ middle-of-the-linked-list/Cargo.lock | 7 ++ middle-of-the-linked-list/Cargo.toml | 8 ++ middle-of-the-linked-list/src/main.rs | 28 ++++++ min_stack/Cargo.lock | 7 ++ minimum-common-value/Cargo.lock | 7 ++ minimum-common-value/Cargo.toml | 8 ++ minimum-common-value/src/main.rs | 28 ++++++ palindrome-linked-list/Cargo.lock | 7 ++ palindrome-linked-list/Cargo.toml | 8 ++ palindrome-linked-list/src/main.rs | 34 +++++++ .../Cargo.lock | 7 ++ .../Cargo.toml | 8 ++ .../src/main.rs | 57 +++++++++++ reorder-list/Cargo.lock | 7 ++ reorder-list/Cargo.toml | 8 ++ reorder-list/src/main.rs | 97 +++++++++++++++++++ reverse-linked-list/Cargo.lock | 7 ++ reverse-linked-list/Cargo.toml | 8 ++ reverse-linked-list/src/main.rs | 34 +++++++ subarray-sum-equals-k/Cargo.lock | 7 ++ subarray-sum-equals-k/Cargo.toml | 8 ++ subarray-sum-equals-k/src/main.rs | 64 ++++++++++++ task-scheduler/Cargo.lock | 7 ++ task-scheduler/Cargo.toml | 8 ++ task-scheduler/src/main.rs | 50 ++++++++++ 46 files changed, 855 insertions(+) create mode 100644 binary-subarrays-with-sum/Cargo.lock create mode 100644 binary-subarrays-with-sum/Cargo.toml create mode 100644 binary-subarrays-with-sum/src/main.rs create mode 100644 contiguous-array/Cargo.lock create mode 100644 contiguous-array/Cargo.toml create mode 100644 contiguous-array/src/main.rs create mode 100644 count-elements-with-maximum-frequency/Cargo.lock create mode 100644 count-elements-with-maximum-frequency/Cargo.toml create mode 100644 count-elements-with-maximum-frequency/src/main.rs create mode 100644 custom-sort-string/Cargo.lock create mode 100644 custom-sort-string/Cargo.toml create mode 100644 custom-sort-string/src/main.rs create mode 100644 find-the-pivot-integer/Cargo.lock create mode 100644 find-the-pivot-integer/Cargo.toml create mode 100644 find-the-pivot-integer/src/main.rs create mode 100644 insert-interval/Cargo.lock create mode 100644 insert-interval/Cargo.toml create mode 100644 insert-interval/src/main.rs create mode 100644 merge-in-between-linked-lists/Cargo.lock create mode 100644 merge-in-between-linked-lists/Cargo.toml create mode 100644 merge-in-between-linked-lists/src/main.rs create mode 100644 middle-of-the-linked-list/Cargo.lock create mode 100644 middle-of-the-linked-list/Cargo.toml create mode 100644 middle-of-the-linked-list/src/main.rs create mode 100644 min_stack/Cargo.lock create mode 100644 minimum-common-value/Cargo.lock create mode 100644 minimum-common-value/Cargo.toml create mode 100644 minimum-common-value/src/main.rs create mode 100644 palindrome-linked-list/Cargo.lock create mode 100644 palindrome-linked-list/Cargo.toml create mode 100644 palindrome-linked-list/src/main.rs create mode 100644 remove-zero-sum-consecutive-nodes-from-linked-list/Cargo.lock create mode 100644 remove-zero-sum-consecutive-nodes-from-linked-list/Cargo.toml create mode 100644 remove-zero-sum-consecutive-nodes-from-linked-list/src/main.rs create mode 100644 reorder-list/Cargo.lock create mode 100644 reorder-list/Cargo.toml create mode 100644 reorder-list/src/main.rs create mode 100644 reverse-linked-list/Cargo.lock create mode 100644 reverse-linked-list/Cargo.toml create mode 100644 reverse-linked-list/src/main.rs create mode 100644 subarray-sum-equals-k/Cargo.lock create mode 100644 subarray-sum-equals-k/Cargo.toml create mode 100644 subarray-sum-equals-k/src/main.rs create mode 100644 task-scheduler/Cargo.lock create mode 100644 task-scheduler/Cargo.toml create mode 100644 task-scheduler/src/main.rs diff --git a/binary-subarrays-with-sum/Cargo.lock b/binary-subarrays-with-sum/Cargo.lock new file mode 100644 index 0000000..c917b5a --- /dev/null +++ b/binary-subarrays-with-sum/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "binary-subarrays-with-sum" +version = "0.1.0" diff --git a/binary-subarrays-with-sum/Cargo.toml b/binary-subarrays-with-sum/Cargo.toml new file mode 100644 index 0000000..d48554a --- /dev/null +++ b/binary-subarrays-with-sum/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "binary-subarrays-with-sum" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/binary-subarrays-with-sum/src/main.rs b/binary-subarrays-with-sum/src/main.rs new file mode 100644 index 0000000..1833cf8 --- /dev/null +++ b/binary-subarrays-with-sum/src/main.rs @@ -0,0 +1,30 @@ +fn main() { + println!("Hello, world!"); + let nums = vec![1, 0, 1, 0, 1]; + let goal = 2; + // Output: 4 + println!("{}", num_subarrays_with_sum(nums, goal)); + // let nums = vec![0, 0, 0, 0, 0]; + // let goal = 0; + // // Output: 4 + // println!("{}", num_subarrays_with_sum(nums, goal)); +} +pub fn num_subarrays_with_sum(nums: Vec, goal: i32) -> i32 { + fn helper(x: i32, n: usize, nums: Vec) -> i32 { + if x < 0 { + return 0; + } + let mut res = 0; + let (mut l, mut cur) = (0, 0); + for r in 0..n { + cur += nums[r]; + while cur > x { + cur -= nums[l]; + l += 1; + } + res += r - l + 1; + } + res as _ + } + helper(goal, nums.len(), nums.clone()) - helper(goal - 1, nums.len(), nums) +} diff --git a/contiguous-array/Cargo.lock b/contiguous-array/Cargo.lock new file mode 100644 index 0000000..00ac444 --- /dev/null +++ b/contiguous-array/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "contiguous-array" +version = "0.1.0" diff --git a/contiguous-array/Cargo.toml b/contiguous-array/Cargo.toml new file mode 100644 index 0000000..e806ff7 --- /dev/null +++ b/contiguous-array/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "contiguous-array" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/contiguous-array/src/main.rs b/contiguous-array/src/main.rs new file mode 100644 index 0000000..bc8aa82 --- /dev/null +++ b/contiguous-array/src/main.rs @@ -0,0 +1,26 @@ +fn main() { + println!("Hello, world!"); + let nums = vec![0, 0, 1, 0, 0, 0, 1, 1]; + println!("{}", find_max_length(nums)); +} + +pub fn find_max_length(nums: Vec) -> i32 { + let n = nums.len(); + // let mut prefix_sum = vec![0; n + 1]; + let mut hm = std::collections::HashMap::from([(0, 0)]); + let mut cur_sum = 0; + let mut maximus = 0; + for i in 1..=n { + let num = if nums[i - 1] == 1 { 1 } else { -1 }; + cur_sum = cur_sum + num; + println!("cur_sum={:?}", cur_sum); + if let Some(idx) = hm.get(&cur_sum) { + println!("idx={:?}", idx); + maximus = maximus.max(i - idx); + } else { + hm.insert(cur_sum, i); + }; + } + println!("hm={:?}", hm); + maximus as _ +} diff --git a/count-elements-with-maximum-frequency/Cargo.lock b/count-elements-with-maximum-frequency/Cargo.lock new file mode 100644 index 0000000..4a39293 --- /dev/null +++ b/count-elements-with-maximum-frequency/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "count-elements-with-maximum-frequency" +version = "0.1.0" diff --git a/count-elements-with-maximum-frequency/Cargo.toml b/count-elements-with-maximum-frequency/Cargo.toml new file mode 100644 index 0000000..e7b9467 --- /dev/null +++ b/count-elements-with-maximum-frequency/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "count-elements-with-maximum-frequency" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/count-elements-with-maximum-frequency/src/main.rs b/count-elements-with-maximum-frequency/src/main.rs new file mode 100644 index 0000000..7f634e8 --- /dev/null +++ b/count-elements-with-maximum-frequency/src/main.rs @@ -0,0 +1,23 @@ +fn main() { + println!("Hello, world!"); +} + +pub fn max_frequency_elements(nums: Vec) -> i32 { + let mut hm = std::collections::HashMap::new(); + for &num in nums.iter() { + *hm.entry(num).or_insert(0) += 1; + } + let (_, max) = hm.iter().max_by(|x, y| x.1.cmp(y.1)).unwrap(); + println!("max={:?}", max); + + let mut count = 0; + for num in nums { + if let Some(s) = hm.get(&num) { + if s == max { + count += 1; + } + } + } + + count +} diff --git a/custom-sort-string/Cargo.lock b/custom-sort-string/Cargo.lock new file mode 100644 index 0000000..a653ef1 --- /dev/null +++ b/custom-sort-string/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "custom-sort-string" +version = "0.1.0" diff --git a/custom-sort-string/Cargo.toml b/custom-sort-string/Cargo.toml new file mode 100644 index 0000000..d0a57f3 --- /dev/null +++ b/custom-sort-string/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "custom-sort-string" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/custom-sort-string/src/main.rs b/custom-sort-string/src/main.rs new file mode 100644 index 0000000..e6280bc --- /dev/null +++ b/custom-sort-string/src/main.rs @@ -0,0 +1,40 @@ +use std::{ + cmp::Reverse, + collections::{BinaryHeap, HashMap}, +}; + +fn main() { + println!("Hello, world!"); + let order = "cba".to_string(); + let s = "abcd".to_string(); + let order = "kqep".to_string(); + let s = "pekeq".to_string(); + + // Output: "cbad" + println!("{}", custom_sort_string(order, s)); +} + +pub fn custom_sort_string(order: String, s: String) -> String { + let mut hm = HashMap::new(); + for (i, c) in order.char_indices() { + hm.insert(c, i); + } + println!("{:?}", hm); + let mut bh = BinaryHeap::new(); + for c in s.chars() { + if let Some(n) = hm.get(&c) { + bh.push((Reverse(*n as i32), c)); + } else { + bh.push((Reverse(-1), c)); + } + } + println!("{:?}", bh); + let mut f: Vec<_> = bh.iter().collect(); + println!("f: {:?}", f); + f.sort_by(|&x, &y| y.0.cmp(&x.0)); + // f.sort(); + println!("{:?}", f); + let rezult: String = f.iter().map(|&x| x.1).collect(); + + rezult +} diff --git a/find-the-pivot-integer/Cargo.lock b/find-the-pivot-integer/Cargo.lock new file mode 100644 index 0000000..3c26f21 --- /dev/null +++ b/find-the-pivot-integer/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "find-the-pivot-integer" +version = "0.1.0" diff --git a/find-the-pivot-integer/Cargo.toml b/find-the-pivot-integer/Cargo.toml new file mode 100644 index 0000000..2f5bd73 --- /dev/null +++ b/find-the-pivot-integer/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "find-the-pivot-integer" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/find-the-pivot-integer/src/main.rs b/find-the-pivot-integer/src/main.rs new file mode 100644 index 0000000..a6b5358 --- /dev/null +++ b/find-the-pivot-integer/src/main.rs @@ -0,0 +1,24 @@ +fn main() { + println!("Hello, world!"); + let n = 8; + println!("{}", pivot_integer(n)); +} + +pub fn pivot_integer(n: i32) -> i32 { + let n = n as usize; + let mut sum_vec = vec![0; n + 1]; + for i in 1..=n { + sum_vec[i] = sum_vec[i - 1] + i; + } + // println!("sum_vec={:?}", sum_vec); + for i in 0..n { + let sum1 = sum_vec[i + 1]; + // println!("sum1={:?}", sum1); + let sum2 = sum_vec[n] - sum_vec[i]; + // println!("sum2={:?}", sum2); + if sum1 == sum2 { + return i as i32 + 1; + } + } + -1 +} diff --git a/insert-interval/Cargo.lock b/insert-interval/Cargo.lock new file mode 100644 index 0000000..ee2eaa0 --- /dev/null +++ b/insert-interval/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "insert-interval" +version = "0.1.0" diff --git a/insert-interval/Cargo.toml b/insert-interval/Cargo.toml new file mode 100644 index 0000000..54ca734 --- /dev/null +++ b/insert-interval/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "insert-interval" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/insert-interval/src/main.rs b/insert-interval/src/main.rs new file mode 100644 index 0000000..8572503 --- /dev/null +++ b/insert-interval/src/main.rs @@ -0,0 +1,42 @@ +fn main() { + println!("Hello, world!"); + let intervals = vec![ + vec![1, 2], + vec![3, 5], + vec![6, 7], + vec![8, 10], + vec![12, 16], + ]; + let new_interval = vec![4, 8]; + // Output: [[1,2],[3,10],[12,16]] + println!("{:?}", insert(intervals, new_interval)); + let intervals = vec![vec![1, 3], vec![6, 9]]; + let new_interval = vec![2, 5]; + println!("{:?}", insert(intervals, new_interval)); + // Output: [[1,5],[6,9]] +} +pub fn insert(intervals: Vec>, new_interval: Vec) -> Vec> { + use std::cmp::{max, min}; + let mut less = vec![]; + let mut more = vec![]; + let mut start = new_interval[0]; + let mut end = new_interval[1]; + + for curr in intervals { + if curr[1] < new_interval[0] { + less.push(curr); + } else if curr[0] > new_interval[1] { + more.push(curr); + } else { + start = min(curr[0], start); + end = max(curr[1], end); + } + println!("less={:?}", less); + println!("more={:?}", more); + println!("start={:?}", start); + println!("end={:?}", end); + } + less.push(vec![start, end]); + less.append(&mut more); + less +} diff --git a/merge-in-between-linked-lists/Cargo.lock b/merge-in-between-linked-lists/Cargo.lock new file mode 100644 index 0000000..0623f90 --- /dev/null +++ b/merge-in-between-linked-lists/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "merge-in-between-linked-lists" +version = "0.1.0" diff --git a/merge-in-between-linked-lists/Cargo.toml b/merge-in-between-linked-lists/Cargo.toml new file mode 100644 index 0000000..640c3d1 --- /dev/null +++ b/merge-in-between-linked-lists/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "merge-in-between-linked-lists" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/merge-in-between-linked-lists/src/main.rs b/merge-in-between-linked-lists/src/main.rs new file mode 100644 index 0000000..cb6df23 --- /dev/null +++ b/merge-in-between-linked-lists/src/main.rs @@ -0,0 +1,46 @@ +fn main() { + println!("Hello, world!"); +} + +#[derive(PartialEq, Eq, Clone, Debug)] +pub struct ListNode { + pub val: i32, + pub next: Option>, +} + +impl ListNode { + #[inline] + fn new(val: i32) -> Self { + ListNode { next: None, val } + } +} + +pub fn merge_in_between( + list1: Option>, + a: i32, + b: i32, + mut list2: Option>, +) -> Option> { + let mut head = list1.unwrap(); + let mut current = head.as_mut(); + + for _ in 0..a - 1 { + current = current.next.as_mut().unwrap(); + } + println!("current={:?}", current); + + std::mem::swap(&mut current.next, &mut list2); + println!("current={:?}", current); + println!("list2={:?}", list2); + + while current.next.is_some() { + current = current.next.as_mut().unwrap(); + } + println!("current={:?}", current); + for _ in 0..b - a + 1 { + list2 = list2.unwrap().next; + } + println!("list2={:?}", list2); + current.next = list2; + Some(head) +} diff --git a/middle-of-the-linked-list/Cargo.lock b/middle-of-the-linked-list/Cargo.lock new file mode 100644 index 0000000..6378e4d --- /dev/null +++ b/middle-of-the-linked-list/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "middle-of-the-linked-list" +version = "0.1.0" diff --git a/middle-of-the-linked-list/Cargo.toml b/middle-of-the-linked-list/Cargo.toml new file mode 100644 index 0000000..418ac03 --- /dev/null +++ b/middle-of-the-linked-list/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "middle-of-the-linked-list" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/middle-of-the-linked-list/src/main.rs b/middle-of-the-linked-list/src/main.rs new file mode 100644 index 0000000..98a2e98 --- /dev/null +++ b/middle-of-the-linked-list/src/main.rs @@ -0,0 +1,28 @@ +fn main() { + println!("Hello, world!"); +} +#[derive(PartialEq, Eq, Clone, Debug)] +pub struct ListNode { + pub val: i32, + pub next: Option>, +} + +impl ListNode { + #[inline] + fn new(val: i32) -> Self { + ListNode { next: None, val } + } +} + +pub fn middle_node(head: Option>) -> Option> { + let mut slow = head.clone(); + let mut fast = head.clone(); + while fast.is_some() || fast.clone().unwrap().next.is_none() { + slow = slow.unwrap().next; + fast = match fast.unwrap().next { + Some(f) => f.next, + None => None, + } + } + slow +} diff --git a/min_stack/Cargo.lock b/min_stack/Cargo.lock new file mode 100644 index 0000000..2eeaeb0 --- /dev/null +++ b/min_stack/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "min_stack" +version = "0.1.0" diff --git a/minimum-common-value/Cargo.lock b/minimum-common-value/Cargo.lock new file mode 100644 index 0000000..4b5d35e --- /dev/null +++ b/minimum-common-value/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "minimum-common-value" +version = "0.1.0" diff --git a/minimum-common-value/Cargo.toml b/minimum-common-value/Cargo.toml new file mode 100644 index 0000000..a1bab05 --- /dev/null +++ b/minimum-common-value/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "minimum-common-value" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/minimum-common-value/src/main.rs b/minimum-common-value/src/main.rs new file mode 100644 index 0000000..7675ac4 --- /dev/null +++ b/minimum-common-value/src/main.rs @@ -0,0 +1,28 @@ +fn main() { + println!("Hello, world!"); + let nums1 = vec![34, 225, 328, 530, 823, 834, 902, 989]; + let nums2 = vec![ + 24, 30, 115, 121, 160, 173, 239, 265, 335, 362, 449, 557, 597, 624, 697, 766, 775, 881, + 898, 919, + ]; + println!("{}", get_common(nums1, nums2)); +} + +pub fn get_common(nums1: Vec, nums2: Vec) -> i32 { + let mut x = 0; + let mut y = 0; + if nums1[nums1.len() - 1] < nums2[0] || nums1[0] > nums2[nums2.len() - 1] { + return -1; + } + while x < nums1.len() && y < nums2.len() { + println!("y={:?}", y); + if nums2[y] > nums1[x] { + x += 1; + } else if nums2[y] < nums1[x] { + y += 1; + } else { + return nums1[x]; + } + } + -1 +} diff --git a/palindrome-linked-list/Cargo.lock b/palindrome-linked-list/Cargo.lock new file mode 100644 index 0000000..2de63b3 --- /dev/null +++ b/palindrome-linked-list/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "palindrome-linked-list" +version = "0.1.0" diff --git a/palindrome-linked-list/Cargo.toml b/palindrome-linked-list/Cargo.toml new file mode 100644 index 0000000..e853a81 --- /dev/null +++ b/palindrome-linked-list/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "palindrome-linked-list" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/palindrome-linked-list/src/main.rs b/palindrome-linked-list/src/main.rs new file mode 100644 index 0000000..c75797a --- /dev/null +++ b/palindrome-linked-list/src/main.rs @@ -0,0 +1,34 @@ +fn main() { + println!("Hello, world!"); +} + +#[derive(PartialEq, Eq, Clone, Debug)] +pub struct ListNode { + pub val: i32, + pub next: Option>, +} + +impl ListNode { + #[inline] + fn new(val: i32) -> Self { + ListNode { next: None, val } + } +} + +pub fn is_palindrome(head: Option>) -> bool { + let vec = list_to_vec(head); + println!("vec={:?}", vec); + + vec == vec.iter().rev().cloned().collect::>() +} + +fn list_to_vec(mut head: Option>) -> Vec { + let mut rezult = Vec::new(); + + while let Some(cur) = head.take() { + rezult.push(cur.as_ref().val); + head = cur.next; + } + + rezult +} diff --git a/remove-zero-sum-consecutive-nodes-from-linked-list/Cargo.lock b/remove-zero-sum-consecutive-nodes-from-linked-list/Cargo.lock new file mode 100644 index 0000000..b5a13db --- /dev/null +++ b/remove-zero-sum-consecutive-nodes-from-linked-list/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "remove-zero-sum-consecutive-nodes-from-linked-list" +version = "0.1.0" diff --git a/remove-zero-sum-consecutive-nodes-from-linked-list/Cargo.toml b/remove-zero-sum-consecutive-nodes-from-linked-list/Cargo.toml new file mode 100644 index 0000000..3052863 --- /dev/null +++ b/remove-zero-sum-consecutive-nodes-from-linked-list/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "remove-zero-sum-consecutive-nodes-from-linked-list" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/remove-zero-sum-consecutive-nodes-from-linked-list/src/main.rs b/remove-zero-sum-consecutive-nodes-from-linked-list/src/main.rs new file mode 100644 index 0000000..dac79ef --- /dev/null +++ b/remove-zero-sum-consecutive-nodes-from-linked-list/src/main.rs @@ -0,0 +1,57 @@ +fn main() { + println!("Hello, world!"); +} + +#[derive(PartialEq, Eq, Clone, Debug)] +pub struct ListNode { + pub val: i32, + pub next: Option>, +} + +impl ListNode { + #[inline] + fn new(val: i32) -> Self { + ListNode { next: None, val } + } +} + +fn vec_to_linked_list(mut nums: Vec) -> Option> { + let mut head = None; + // let mut tail = &mut head; + + while let Some(num) = nums.pop() { + if num != 0 { + let mut node = Box::new(ListNode::new(num)); + node.next = head; + head = Some(node); + } + } + + head +} + +pub fn remove_zero_sum_sublists(mut head: Option>) -> Option> { + let mut vec = vec![]; + // let mut sum_vec = vec![0]; + let mut hs = std::collections::HashSet::from([0]); + let mut sum = 0; + while let Some(cur) = head { + vec.push(cur.val); + sum += cur.val; + // sum_vec.push(cur.val + sum_vec.last().unwrap()); + // println!("sum_vec={:?}", sum_vec); + if !hs.insert(sum) { + let stoppage = sum; + while let Some(x) = vec.pop() { + sum -= x; + hs.remove(&sum); + if sum == stoppage { + break; + } + } + } + head = cur.next; + } + println!("{:?}", vec); + vec_to_linked_list(vec.into_iter().rev().collect()) +} diff --git a/reorder-list/Cargo.lock b/reorder-list/Cargo.lock new file mode 100644 index 0000000..3d71ffb --- /dev/null +++ b/reorder-list/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "reorder-list" +version = "0.1.0" diff --git a/reorder-list/Cargo.toml b/reorder-list/Cargo.toml new file mode 100644 index 0000000..c39061c --- /dev/null +++ b/reorder-list/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "reorder-list" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/reorder-list/src/main.rs b/reorder-list/src/main.rs new file mode 100644 index 0000000..9439edd --- /dev/null +++ b/reorder-list/src/main.rs @@ -0,0 +1,97 @@ +#[derive(PartialEq, Eq, Clone, Debug)] +pub struct ListNode { + pub val: i32, + pub next: Option>, +} + +impl ListNode { + #[inline] + fn new(val: i32) -> Self { + ListNode { next: None, val } + } +} + +struct Solution; + +impl Solution { + pub fn reorder_list(mut head: &mut Option>) { + let mut length = 0; + let mut list = head.as_mut(); + // find the total length of the list + // Using the slow & fast pointer is probably not possible + while let Some(node) = list { + length += 1; + list = node.next.as_mut(); + } + // if length is less than or equal to 2 then exit the function + if length <= 2 { + return; + } + // find the middle node to split the list + let mut mid = head.as_mut(); + for _ in 0..length / 2 { + mid = match mid { + None => unreachable!("Traversing half of the list. Cannot reach the end."), + Some(node) => node.next.as_mut(), + } + } + println!("mid={:?}", mid); + // Tail is the right half of the list. + // Here we use take() to break the linked list + // so the end of the first half now points to None + // and tail holds the second half of the linked list + let mut tail = mid.expect("Mid cannot be None.").as_mut().next.take(); + println!("tail={:?}", tail); + // reverse the tail + let mut reversed: Option> = None; + while let Some(mut node) = tail { + tail = node.next; + node.next = reversed; + reversed = Some(node); + } + println!("reversed={:?}", reversed); + // merge the first half(head) & the second reversed half(reversed) + // This solution to merge two linked lists is clever + // https://leetcode.com/problems/merge-two-sorted-lists/solutions/2947855/simple-and-efficient-rust-8-liner/ + // mover is the variable that traverses and modifies "head" + let mut mover: &mut _ = &mut reversed; + while head.is_some() { + std::mem::swap(mover, &mut head); + mover = &mut mover.as_mut().unwrap().next; + } + std::mem::swap(head, &mut reversed); + } +} + +fn main() { + // Example 1 + let mut head1 = Some(Box::new(ListNode { + val: 1, + next: Some(Box::new(ListNode { + val: 2, + next: Some(Box::new(ListNode { + val: 3, + next: Some(Box::new(ListNode::new(4))), + })), + })), + })); + Solution::reorder_list(&mut head1); + println!("{:?}", head1); + + // Example 2 + let mut head2 = Some(Box::new(ListNode { + val: 1, + next: Some(Box::new(ListNode { + val: 2, + next: Some(Box::new(ListNode { + val: 3, + next: Some(Box::new(ListNode { + val: 4, + next: Some(Box::new(ListNode::new(5))), + })), + })), + })), + })); + Solution::reorder_list(&mut head2); + println!("{:?}", head2); +} diff --git a/reverse-linked-list/Cargo.lock b/reverse-linked-list/Cargo.lock new file mode 100644 index 0000000..21f2d71 --- /dev/null +++ b/reverse-linked-list/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "reverse-linked-list" +version = "0.1.0" diff --git a/reverse-linked-list/Cargo.toml b/reverse-linked-list/Cargo.toml new file mode 100644 index 0000000..74fdb0d --- /dev/null +++ b/reverse-linked-list/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "reverse-linked-list" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/reverse-linked-list/src/main.rs b/reverse-linked-list/src/main.rs new file mode 100644 index 0000000..a40bfaa --- /dev/null +++ b/reverse-linked-list/src/main.rs @@ -0,0 +1,34 @@ +fn main() { + println!("Hello, world!"); +} + +#[derive(PartialEq, Eq, Clone, Debug)] +pub struct ListNode { + pub val: i32, + pub next: Option>, +} + +impl ListNode { + #[inline] + fn new(val: i32) -> Self { + ListNode { next: None, val } + } +} + +pub fn reverse_list(head: Option>) -> Option> { + if head.is_none() { + return None; + } + + let mut prev = None; + let mut cur = head.clone(); + + while let Some(mut cur_node) = cur { + let next_node = cur_node.next; + cur_node.next = prev; + prev = Some(cur_node); + cur = next_node; + } + + prev // Return the new head of the reversed list +} diff --git a/subarray-sum-equals-k/Cargo.lock b/subarray-sum-equals-k/Cargo.lock new file mode 100644 index 0000000..bbfb023 --- /dev/null +++ b/subarray-sum-equals-k/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "subarray-sum-equals-k" +version = "0.1.0" diff --git a/subarray-sum-equals-k/Cargo.toml b/subarray-sum-equals-k/Cargo.toml new file mode 100644 index 0000000..7462893 --- /dev/null +++ b/subarray-sum-equals-k/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "subarray-sum-equals-k" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/subarray-sum-equals-k/src/main.rs b/subarray-sum-equals-k/src/main.rs new file mode 100644 index 0000000..5e58bdd --- /dev/null +++ b/subarray-sum-equals-k/src/main.rs @@ -0,0 +1,64 @@ +fn main() { + println!("Hello, world!"); + // Example 1: + + // let nums = vec![1, 1, 1]; + // let k = 2; + // // Output: 2 + // // Example 2: + // println!("{:?}", subarray_sum(nums, k)); + + // let nums = vec![1, 2, 3]; + // let k = 3; + // // Output: 2 + // println!("{:?}", subarray_sum(nums, k)); + + let nums = vec![-1, -1, 1]; + let k = 0; + // Output: 2 + println!("{:?}", subarray_sum(nums, k)); +} + +pub fn subarray_sum(nums: Vec, k: i32) -> i32 { + let n = nums.len(); + if n == 1 { + if k == nums[0] { + return 1; + } else { + return 0; + } + } + let mut prefix_sum = vec![0; n + 1]; + for i in 1..=n { + prefix_sum[i] = prefix_sum[i - 1] + nums[i - 1]; + } + + // let mut st = 0; + // let mut end = 1; + // // if end - st < 2, end ++ + // // if end - st >= 2, st ++ + // let mut count = 0; + // while end <= n && st < end { + // let subarray_sum = prefix_sum[end] - prefix_sum[st]; + // println!("subarray_sum{}", subarray_sum); + // if subarray_sum < k { + // end += 1; + // } else { + // if subarray_sum == k { + // count += 1; + // } + // st += 1; + // } + // } + let mut count = 0; + for i in 0..n { + for j in i + 1..=n { + let mut diff = prefix_sum[j] - prefix_sum[i]; + if diff == k { + count += 1; + } + } + } + + count +} diff --git a/task-scheduler/Cargo.lock b/task-scheduler/Cargo.lock new file mode 100644 index 0000000..0758dae --- /dev/null +++ b/task-scheduler/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "task-scheduler" +version = "0.1.0" diff --git a/task-scheduler/Cargo.toml b/task-scheduler/Cargo.toml new file mode 100644 index 0000000..2a1448f --- /dev/null +++ b/task-scheduler/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "task-scheduler" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/task-scheduler/src/main.rs b/task-scheduler/src/main.rs new file mode 100644 index 0000000..cd0c1c4 --- /dev/null +++ b/task-scheduler/src/main.rs @@ -0,0 +1,50 @@ +use std::collections::BinaryHeap; +use std::collections::VecDeque; + +fn least_interval(tasks: Vec, n: i32) -> i32 { + let mut freq: [i32; 26] = [0; 26]; + + // Building frequency map + for &ch in tasks.iter() { + freq[(ch as u8 - b'A') as usize] += 1; + } + + // Max heap to store frequencies + let mut pq = BinaryHeap::new(); + for &f in freq.iter() { + if f > 0 { + pq.push(f); + } + } + + let mut time = 0; + // Process tasks until the heap is empty + while !pq.is_empty() { + let mut cycle = n + 1; + let mut store = VecDeque::new(); + let mut task_count = 0; + // Execute tasks in each cycle + while cycle > 0 && !pq.is_empty() { + if let Some(top) = pq.pop() { + if top > 1 { + store.push_back(top - 1); + } + task_count += 1; + cycle -= 1; + } + } + // Restore updated frequencies to the heap + for x in store.into_iter() { + pq.push(x); + } + // Add time for the completed cycle + time += if pq.is_empty() { task_count } else { n + 1 }; + } + time +} + +fn main() { + let tasks = vec!['A', 'A', 'A', 'B', 'B', 'B']; + let n = 2; + println!("{}", least_interval(tasks, n)); // Output should be 8 +}