From 2a7ec7e34b0159ddb9036f70be697508ae7189ff Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Sun, 29 May 2022 20:21:38 +0100 Subject: [PATCH] A few problems --- .devcontainer/Dockerfile | 10 +++++++ .devcontainer/devcontainer.json | 40 ++++++++++++++++++++++++++ .gitignore | 1 + contains-duplicate/sol.rs | 8 ++++++ daily-temperatures/sol.go | 19 ++++++++++++ first-missing-positive/sol.rs | 20 +++++++++++++ longest-valid-parentheses/sol.rs | 19 ++++++++++++ maximum-product-of-word-lengths/sol.rs | 32 +++++++++++++++++++++ maximum-subarray/sol.rs | 10 +++++++ merge-sorted-array/sol.rs | 17 +++++++++++ missing-number/sol.rs | 8 ++++++ number-of-1-bits/sol.rs | 12 ++++++++ rotate-image/sol.rs | 16 +++++++++++ tenth-line/sol.sh | 0 transpose-file/sol.sh | 1 - two-sum/sol.rs | 16 +++++++++++ word-frequency/sol.sh | 0 17 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json create mode 100644 .gitignore create mode 100644 contains-duplicate/sol.rs create mode 100644 daily-temperatures/sol.go create mode 100644 first-missing-positive/sol.rs create mode 100644 longest-valid-parentheses/sol.rs create mode 100644 maximum-product-of-word-lengths/sol.rs create mode 100644 maximum-subarray/sol.rs create mode 100644 merge-sorted-array/sol.rs create mode 100644 missing-number/sol.rs create mode 100644 number-of-1-bits/sol.rs create mode 100644 rotate-image/sol.rs mode change 100755 => 100644 tenth-line/sol.sh mode change 100755 => 100644 transpose-file/sol.sh create mode 100644 two-sum/sol.rs mode change 100755 => 100644 word-frequency/sol.sh diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..ed98d33 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,10 @@ +# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.234.0/containers/debian/.devcontainer/base.Dockerfile + +# [Choice] Debian version (use bullseye on local arm64/Apple Silicon): bullseye, buster +ARG VARIANT="buster" +FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT} + +# ** [Optional] Uncomment this section to install additional packages. ** +# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ +# && apt-get -y install --no-install-recommends + diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..842e520 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,40 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: +// https://github.com/microsoft/vscode-dev-containers/tree/v0.234.0/containers/debian +{ + "name": "Debian", + "build": { + "dockerfile": "Dockerfile", + // Update 'VARIANT' to pick an Debian version: bullseye, buster + // Use bullseye on local arm64/Apple Silicon. + "args": { "VARIANT": "bullseye" } + }, + + // Set *default* container specific settings.json values on container create. + "settings": {}, + + // Add the IDs of extensions you want installed when the container is created. + "extensions": [ + "rust-lang.rust-analyzer" + ], + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker. + // "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ], + + // Uncomment when using a ptrace-based debugger like C++, Go, and Rust + // "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ], + + // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. + "remoteUser": "vscode", + "containerUser": "vscode", + "workspaceMount": "source=${localWorkspaceFolder},target=/workspaces/${localWorkspaceFolderBasename},type=bind,Z", + "runArgs": [ + "--userns=keep-id" + ], + + "features": { + "rust": "latest" + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c41cc9e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target \ No newline at end of file diff --git a/contains-duplicate/sol.rs b/contains-duplicate/sol.rs new file mode 100644 index 0000000..9a5c3c7 --- /dev/null +++ b/contains-duplicate/sol.rs @@ -0,0 +1,8 @@ +use std::collections::HashSet; + +impl Solution { + pub fn contains_duplicate(nums: Vec) -> bool { + let mut exists = HashSet::new(); + !nums.into_iter().all(|n| exists.insert(n)) + } +} \ No newline at end of file diff --git a/daily-temperatures/sol.go b/daily-temperatures/sol.go new file mode 100644 index 0000000..6c4f26f --- /dev/null +++ b/daily-temperatures/sol.go @@ -0,0 +1,19 @@ +package main + +func dailyTemperatures(temperatures []int) []int { + out := make([]int, len(temperatures)) + stack := make([][2]int, 0) + l := 0 + for i := range temperatures { + if l != 0 && temperatures[i] > stack[l-1][1] { + for l != 0 && temperatures[i] > stack[l-1][1] { + out[stack[l-1][0]] = i - stack[l-1][0] + stack = stack[:l-1] + l-- + } + } + stack = append(stack, [2]int{i, temperatures[i]}) + l++ + } + return out +} diff --git a/first-missing-positive/sol.rs b/first-missing-positive/sol.rs new file mode 100644 index 0000000..70e8832 --- /dev/null +++ b/first-missing-positive/sol.rs @@ -0,0 +1,20 @@ +// TODO: Incomplete +struct Solution {} +impl Solution { + pub fn first_missing_positive(nums: Vec) -> i32 { + let (max, sum) = nums + .iter() + .fold((0, 0), |(max, sum), n| { + if *n > 0 { + return (if *n > max { *n } else { max }, sum + *n); + } + (max, sum) + }); + let missing = max * (max + 1) / 2 - sum; + return if missing == 0 { max + 1 } else { missing }; + } +} + +fn main() { + println!("{}", Solution::first_missing_positive(vec![99,94,96,11,92,5,91,89,57,85,66,63,84,81,79,61,74,78,77,30,64,13,58,18,70,69,51,12,32,34,9,43,39,8,1,38,49,27,21,45,47,44,53,52,48,19,50,59,3,40,31,82,23,56,37,41,16,28,22,33,65,42,54,20,29,25,10,26,4,60,67,83,62,71,24,35,72,55,75,0,2,46,15,80,6,36,14,73,76,86,88,7,17,87,68,90,95,93,97,98])); +} \ No newline at end of file diff --git a/longest-valid-parentheses/sol.rs b/longest-valid-parentheses/sol.rs new file mode 100644 index 0000000..0342d57 --- /dev/null +++ b/longest-valid-parentheses/sol.rs @@ -0,0 +1,19 @@ +// Incomplete +impl Solution { + pub fn longest_valid_parentheses(s: String) -> i32 { + if s.len() == 0 { 0 } + let mut s = 0; + let mut open = 0; + let mut acc = 0; + for c in s.chars().into_iter() { + if c == '(' { + open += 1; + } else { + open -= 1; + } + if open == 0 { + + } + } + } +} \ No newline at end of file diff --git a/maximum-product-of-word-lengths/sol.rs b/maximum-product-of-word-lengths/sol.rs new file mode 100644 index 0000000..ecb2e9e --- /dev/null +++ b/maximum-product-of-word-lengths/sol.rs @@ -0,0 +1,32 @@ +use std::collections::HashMap; + +impl Solution { + #[inline] + pub fn letter_bitmap(word: &str) -> u32 { + word.bytes() + .fold(0u32, |bitmap, chr| bitmap | (1 << (chr-97))) + } + + pub fn max_product(words: Vec) -> i32 { + let vals: HashMap = words + .iter() + .fold(HashMap::new(), |mut vals, word| { + let bitmap = Solution::letter_bitmap(word); + vals.insert(bitmap, word.len().max(*vals.get(&bitmap).unwrap_or(&0))); + vals + }); + vals.iter() + .map(|(&bitmap1, &value1)| { + vals.iter() + .filter_map(|(&bitmap2, &value2)| { + if bitmap1 & bitmap2 == 0 { + return Some(value1*value2); + } + None + }) + .max() + .unwrap_or(0) + }) + .max().unwrap_or(0) as i32 + } +} \ No newline at end of file diff --git a/maximum-subarray/sol.rs b/maximum-subarray/sol.rs new file mode 100644 index 0000000..9c2d283 --- /dev/null +++ b/maximum-subarray/sol.rs @@ -0,0 +1,10 @@ +impl Solution { + pub fn max_sub_array(nums: Vec) -> i32 { + nums.iter() + .fold((nums[0], 0), |(max, sum), n| { + let new_sum = sum + n; + (max.max(new_sum), 0.max(new_sum)) + }) + .0 + } +} \ No newline at end of file diff --git a/merge-sorted-array/sol.rs b/merge-sorted-array/sol.rs new file mode 100644 index 0000000..92224a8 --- /dev/null +++ b/merge-sorted-array/sol.rs @@ -0,0 +1,17 @@ +impl Solution { + pub fn merge(nums1: &mut Vec, m: i32, nums2: &mut Vec, n: i32) { + let mut position_1 = m as usize; + let mut insert_position = position_1 + n as usize; + while !nums2.is_empty() { + insert_position -= 1; + let last = *nums2.last().unwrap(); + let max = if position_1 == 0 { last } else { nums1[position_1-1].max(last) }; + if last == max { + nums2.pop(); + } else { + position_1 -= 1; + } + nums1[insert_position] = max; + } + } +} \ No newline at end of file diff --git a/missing-number/sol.rs b/missing-number/sol.rs new file mode 100644 index 0000000..f8e8b90 --- /dev/null +++ b/missing-number/sol.rs @@ -0,0 +1,8 @@ +impl Solution { + pub fn missing_number(nums: Vec) -> i32 { + let expected_sum = (nums.len() * (nums.len() + 1) / 2) as i32; + nums + .iter() + .fold(expected_sum, |acc, x| acc - *x) + } +} \ No newline at end of file diff --git a/number-of-1-bits/sol.rs b/number-of-1-bits/sol.rs new file mode 100644 index 0000000..896791a --- /dev/null +++ b/number-of-1-bits/sol.rs @@ -0,0 +1,12 @@ +impl Solution { + pub fn hammingWeight (n: u32) -> i32 { + (0..32).into_iter() + .fold(0, |acc, p| { + let compare_bit = 1 << p; + if n & compare_bit == compare_bit { + return acc + 1; + } + acc + }) + } +} \ No newline at end of file diff --git a/rotate-image/sol.rs b/rotate-image/sol.rs new file mode 100644 index 0000000..31090d9 --- /dev/null +++ b/rotate-image/sol.rs @@ -0,0 +1,16 @@ +impl Solution { + pub fn rotate(matrix: &mut Vec>) { + let n = matrix.len() - 1; + for y in 0..matrix.len()/2 { + for x in y..matrix.len()-1-y { + let mut a = matrix[y][x]; + matrix[y][x] = matrix[n-x][y]; + let mut b = matrix[x][n-y]; + matrix[x][n-y] = a; + a = matrix[n-y][n-x]; + matrix[n-y][n-x] = b; + matrix[n-x][y] = a; + } + } + } +} \ No newline at end of file diff --git a/tenth-line/sol.sh b/tenth-line/sol.sh old mode 100755 new mode 100644 diff --git a/transpose-file/sol.sh b/transpose-file/sol.sh old mode 100755 new mode 100644 index d3277e9..0e724ac --- a/transpose-file/sol.sh +++ b/transpose-file/sol.sh @@ -7,4 +7,3 @@ for i in $(seq 1 $top); do echo $(echo $lines | tr " " "\n" | awk "NR % $n == $i") done echo $(echo $lines | tr " " "\n" | awk "NR % $n == 0") - diff --git a/two-sum/sol.rs b/two-sum/sol.rs new file mode 100644 index 0000000..5c6313e --- /dev/null +++ b/two-sum/sol.rs @@ -0,0 +1,16 @@ +use std::collections::HashMap; + +impl Solution { + pub fn two_sum(nums: Vec, target: i32) -> Vec { + let mut done: HashMap = HashMap::new(); + nums + .into_iter() + .enumerate() + .find_map(|(i2, n2)| + done.get(&n2) + .and_then(|i1| Some(vec![*i1, i2 as i32])) + .or_else(|| done.insert(target-n2, i2 as i32).and(None)) + ) + .unwrap() + } +} \ No newline at end of file diff --git a/word-frequency/sol.sh b/word-frequency/sol.sh old mode 100755 new mode 100644