A few problems

This commit is contained in:
Gleb Koval 2022-05-29 20:21:38 +01:00
parent 2c0204028e
commit 2a7ec7e34b
17 changed files with 228 additions and 1 deletions

10
.devcontainer/Dockerfile Normal file
View File

@ -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 <your-package-list-here>

View File

@ -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"
}
}

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

View File

@ -0,0 +1,8 @@
use std::collections::HashSet;
impl Solution {
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
let mut exists = HashSet::new();
!nums.into_iter().all(|n| exists.insert(n))
}
}

19
daily-temperatures/sol.go Normal file
View File

@ -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
}

View File

@ -0,0 +1,20 @@
// TODO: Incomplete
struct Solution {}
impl Solution {
pub fn first_missing_positive(nums: Vec<i32>) -> 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]));
}

View File

@ -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 {
}
}
}
}

View File

@ -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<String>) -> i32 {
let vals: HashMap<u32, usize> = 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
}
}

10
maximum-subarray/sol.rs Normal file
View File

@ -0,0 +1,10 @@
impl Solution {
pub fn max_sub_array(nums: Vec<i32>) -> i32 {
nums.iter()
.fold((nums[0], 0), |(max, sum), n| {
let new_sum = sum + n;
(max.max(new_sum), 0.max(new_sum))
})
.0
}
}

17
merge-sorted-array/sol.rs Normal file
View File

@ -0,0 +1,17 @@
impl Solution {
pub fn merge(nums1: &mut Vec<i32>, m: i32, nums2: &mut Vec<i32>, 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;
}
}
}

8
missing-number/sol.rs Normal file
View File

@ -0,0 +1,8 @@
impl Solution {
pub fn missing_number(nums: Vec<i32>) -> i32 {
let expected_sum = (nums.len() * (nums.len() + 1) / 2) as i32;
nums
.iter()
.fold(expected_sum, |acc, x| acc - *x)
}
}

12
number-of-1-bits/sol.rs Normal file
View File

@ -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
})
}
}

16
rotate-image/sol.rs Normal file
View File

@ -0,0 +1,16 @@
impl Solution {
pub fn rotate(matrix: &mut Vec<Vec<i32>>) {
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;
}
}
}
}

0
tenth-line/sol.sh Executable file → Normal file
View File

1
transpose-file/sol.sh Executable file → Normal file
View File

@ -7,4 +7,3 @@ for i in $(seq 1 $top); do
echo $(echo $lines | tr " " "\n" | awk "NR % $n == $i") echo $(echo $lines | tr " " "\n" | awk "NR % $n == $i")
done done
echo $(echo $lines | tr " " "\n" | awk "NR % $n == 0") echo $(echo $lines | tr " " "\n" | awk "NR % $n == 0")

16
two-sum/sol.rs Normal file
View File

@ -0,0 +1,16 @@
use std::collections::HashMap;
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut done: HashMap<i32, i32> = 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()
}
}

0
word-frequency/sol.sh Executable file → Normal file
View File