From a5e3b1140ec34399e172e0405eb99f323e275a24 Mon Sep 17 00:00:00 2001 From: m4siri Date: Sun, 30 Nov 2025 20:47:11 +0545 Subject: lc starting today - 2025-11-30 --- .gitignore | 2 + 01_two-sum/Cargo.lock | 7 ++ 01_two-sum/Cargo.toml | 6 ++ 01_two-sum/src/main.rs | 26 +++++++ 02_add-two-numbers/Cargo.lock | 7 ++ 02_add-two-numbers/Cargo.toml | 6 ++ 02_add-two-numbers/src/main.rs | 84 ++++++++++++++++++++++ .../.gitignore | 1 + .../Cargo.lock | 7 ++ .../Cargo.toml | 6 ++ .../src/main.rs | 46 ++++++++++++ 04_median-of-two-sorted-arrays/Cargo.toml | 6 ++ 04_median-of-two-sorted-arrays/src/main.rs | 3 + README.md | 1 + init.nu | 39 ++++++++++ 15 files changed, 247 insertions(+) create mode 100644 .gitignore create mode 100644 01_two-sum/Cargo.lock create mode 100644 01_two-sum/Cargo.toml create mode 100644 01_two-sum/src/main.rs create mode 100644 02_add-two-numbers/Cargo.lock create mode 100644 02_add-two-numbers/Cargo.toml create mode 100644 02_add-two-numbers/src/main.rs create mode 100644 03_longest-substring-without-repeating-chars/.gitignore create mode 100644 03_longest-substring-without-repeating-chars/Cargo.lock create mode 100644 03_longest-substring-without-repeating-chars/Cargo.toml create mode 100644 03_longest-substring-without-repeating-chars/src/main.rs create mode 100644 04_median-of-two-sorted-arrays/Cargo.toml create mode 100644 04_median-of-two-sorted-arrays/src/main.rs create mode 100644 README.md create mode 100755 init.nu diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..56ce728 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +*/target diff --git a/01_two-sum/Cargo.lock b/01_two-sum/Cargo.lock new file mode 100644 index 0000000..b32bc1e --- /dev/null +++ b/01_two-sum/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "two_sum" +version = "0.1.0" diff --git a/01_two-sum/Cargo.toml b/01_two-sum/Cargo.toml new file mode 100644 index 0000000..1ee142e --- /dev/null +++ b/01_two-sum/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "two_sum" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/01_two-sum/src/main.rs b/01_two-sum/src/main.rs new file mode 100644 index 0000000..827e46d --- /dev/null +++ b/01_two-sum/src/main.rs @@ -0,0 +1,26 @@ +fn main() { + let nums: Vec = vec![2, 7, 11, 15]; + let target: i32 = 9; + dbg!(Solution::two_sum(nums, target)); +} + +struct Solution(()); +impl Solution { + pub fn two_sum(nums: Vec, target: i32) -> Vec { + let mut result = Vec::new(); + let mut idx = 0; + + while idx < nums.len() { + let cur = nums[idx]; + let offset = idx + 1; + for (i, num) in nums[offset..].into_iter().enumerate() { + if (cur + num) == target { + result.push(idx as i32); + result.push((offset + i) as i32); + } + } + idx += 1; + } + result + } +} diff --git a/02_add-two-numbers/Cargo.lock b/02_add-two-numbers/Cargo.lock new file mode 100644 index 0000000..78f2920 --- /dev/null +++ b/02_add-two-numbers/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "add_two_numbers" +version = "0.1.0" diff --git a/02_add-two-numbers/Cargo.toml b/02_add-two-numbers/Cargo.toml new file mode 100644 index 0000000..518f1a6 --- /dev/null +++ b/02_add-two-numbers/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "add_two_numbers" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/02_add-two-numbers/src/main.rs b/02_add-two-numbers/src/main.rs new file mode 100644 index 0000000..6b46dbf --- /dev/null +++ b/02_add-two-numbers/src/main.rs @@ -0,0 +1,84 @@ +fn main() { + let list1 = Some(Box::new(ListNode { + val: 2, + next: Some(Box::new(ListNode { + val: 4, + next: Some(Box::new(ListNode { val: 3, next: None })), + })), + })); + + let list2 = Some(Box::new(ListNode { + val: 5, + next: Some(Box::new(ListNode { + val: 6, + next: Some(Box::new(ListNode { val: 4, next: None })), + })), + })); + + Solution::add_two_numbers(list1, list2); +} + +struct Solution(()); + +#[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 } + } +} + +impl Solution { + pub fn add_two_numbers( + l1: Option>, + l2: Option>, + ) -> Option> { + let ans = Self::double_walker(l1, l2, false); + ans + } + + fn double_walker( + l1: Option>, + l2: Option>, + carry: bool, + ) -> Option> { + if let (None, None) = (&l1, &l2) { + if carry { + return Some(Box::new(ListNode::new(1))); + } else { + return None; + } + } + + let mut node = ListNode::new(0); + + if let Some(n1) = &l1 { + node.val += n1.val; + } + + if let Some(n2) = &l2 { + node.val += n2.val; + } + + if carry { + node.val += 1; + } + + node.next = Self::double_walker( + l1.and_then(|n| n.next), + l2.and_then(|n| n.next), + node.val >= 10, + ); + + if node.val >= 10 { + node.val = node.val - 10; + } + + Some(Box::new(node)) + } +} diff --git a/03_longest-substring-without-repeating-chars/.gitignore b/03_longest-substring-without-repeating-chars/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/03_longest-substring-without-repeating-chars/.gitignore @@ -0,0 +1 @@ +/target diff --git a/03_longest-substring-without-repeating-chars/Cargo.lock b/03_longest-substring-without-repeating-chars/Cargo.lock new file mode 100644 index 0000000..b4f61c9 --- /dev/null +++ b/03_longest-substring-without-repeating-chars/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "longest_substring_wo_repeating_chars" +version = "0.1.0" diff --git a/03_longest-substring-without-repeating-chars/Cargo.toml b/03_longest-substring-without-repeating-chars/Cargo.toml new file mode 100644 index 0000000..dadbf03 --- /dev/null +++ b/03_longest-substring-without-repeating-chars/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "longest_substring_wo_repeating_chars" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/03_longest-substring-without-repeating-chars/src/main.rs b/03_longest-substring-without-repeating-chars/src/main.rs new file mode 100644 index 0000000..f07bedf --- /dev/null +++ b/03_longest-substring-without-repeating-chars/src/main.rs @@ -0,0 +1,46 @@ +use std::collections::HashSet; + +fn main() { + let s = "dvdf".to_string(); + dbg!(Solution::length_of_longest_substring(s)); +} + +struct Solution(()); + +impl Solution { + pub fn length_of_longest_substring(s: String) -> i32 { + use std::collections::HashMap; + + let s = s.chars().collect::>(); + let mut max_length = 0; + let mut length = 0; + let mut map: HashMap = HashMap::new(); + let mut idx = 0; + + while idx < s.len() { + let ch = &s[idx]; + + if let Some(ref ci) = map.remove(ch) { + map = HashMap::new(); + if max_length < length { + max_length = length; + } + + idx = (ci + 1) as usize; + length = 0; + continue; + } + + map.insert(ch.clone(), idx as i32); + + length += 1; + idx += 1; + } + + if length > max_length { + max_length = length; + } + + max_length + } +} diff --git a/04_median-of-two-sorted-arrays/Cargo.toml b/04_median-of-two-sorted-arrays/Cargo.toml new file mode 100644 index 0000000..c98af21 --- /dev/null +++ b/04_median-of-two-sorted-arrays/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "median-of-two-sorted-arrays" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/04_median-of-two-sorted-arrays/src/main.rs b/04_median-of-two-sorted-arrays/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/04_median-of-two-sorted-arrays/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..4699dc8 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +suffered brainrot, short attention span - now rewiring my brain. diff --git a/init.nu b/init.nu new file mode 100755 index 0000000..def503f --- /dev/null +++ b/init.nu @@ -0,0 +1,39 @@ +#! /usr/bin/env nu + +def main [ name: string idx?: int ] { + let name = $name | str kebab-case | str downcase + let idx = if ($idx | is-empty) { + ( + glob "*_*" + | split row '/' + | get (($in | length) - 1) + | split row '_' + | get 0 + | into int + | $in + 1 + | (if $in < 10 { $"0($in)" } else { $in } ) + ) + + } else { $idx } + + let problem_name = $"($idx)_($name)" + let existing = glob * | where $it =~ $name + let tmp_dir = $"/tmp/($problem_name)" + + if ($existing | is-not-empty) { + let ans = input $"Override existing '($existing)'? \(y/n\): " + match $ans { + 'y' | 'Y' | 'yes' => { mv $problem_name $tmp_dir }, + _ => return + } + } + + cargo new $problem_name --name $name --vcs none + + if $env.LAST_EXIT_CODE == 0 { + rm -r $tmp_dir + } else { + mv $tmp_dir $problem_name + } + +} -- cgit v1.2.3