Compare commits
3 Commits
628d674712
...
dd88ef2c57
Author | SHA1 | Date |
---|---|---|
Gleb Koval | dd88ef2c57 | |
Gleb Koval | 5edf7fa141 | |
Gleb Koval | cbcd0a685a |
|
@ -4,6 +4,8 @@
|
||||||
ARG VARIANT="buster"
|
ARG VARIANT="buster"
|
||||||
FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}
|
FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}
|
||||||
|
|
||||||
|
RUN sudo apt install gnupg2 -y
|
||||||
|
|
||||||
# ** [Optional] Uncomment this section to install additional packages. **
|
# ** [Optional] Uncomment this section to install additional packages. **
|
||||||
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
||||||
# && apt-get -y install --no-install-recommends <your-package-list-here>
|
# && apt-get -y install --no-install-recommends <your-package-list-here>
|
||||||
|
|
|
@ -14,7 +14,8 @@
|
||||||
|
|
||||||
// Add the IDs of extensions you want installed when the container is created.
|
// Add the IDs of extensions you want installed when the container is created.
|
||||||
"extensions": [
|
"extensions": [
|
||||||
"rust-lang.rust-analyzer"
|
"rust-lang.rust-analyzer",
|
||||||
|
"LeetCode.vscode-leetcode"
|
||||||
],
|
],
|
||||||
|
|
||||||
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
||||||
|
|
|
@ -1 +1,3 @@
|
||||||
/target
|
/target
|
||||||
|
Cargo.lock
|
||||||
|
Cargo.toml
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"leetcode.filePath": {
|
||||||
|
"default": {
|
||||||
|
"folder": "${kebab-case-name}",
|
||||||
|
"filename": "sol.${ext}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @lc app=leetcode id=16 lang=rust
|
||||||
|
*
|
||||||
|
* [16] 3Sum Closest
|
||||||
|
*/
|
||||||
|
struct Solution {}
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn three_sum_closest(mut nums: Vec<i32>, target: i32) -> i32 {
|
||||||
|
nums.sort_unstable();
|
||||||
|
let mut result = nums[0] + nums[1] + nums[2];
|
||||||
|
for (i, &n) in nums.iter().enumerate() {
|
||||||
|
let (mut l, mut r) = (i + 1, nums.len() - 1);
|
||||||
|
while l < r {
|
||||||
|
let sum = n + nums[l] + nums[r];
|
||||||
|
if (target - sum).abs() < (target - result).abs() {
|
||||||
|
result = sum;
|
||||||
|
}
|
||||||
|
match sum.cmp(&target) {
|
||||||
|
Ordering::Equal => return sum,
|
||||||
|
Ordering::Less => l += 1,
|
||||||
|
Ordering::Greater => r -= 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* @lc app=leetcode id=1448 lang=golang
|
||||||
|
*
|
||||||
|
* [1448] Count Good Nodes in Binary Tree
|
||||||
|
*/
|
||||||
|
package main
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* type TreeNode struct {
|
||||||
|
* Val int
|
||||||
|
* Left *TreeNode
|
||||||
|
* Right *TreeNode
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
func goodNodes(root *TreeNode) int {
|
||||||
|
return subGoodNodes(root, math.MinInt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func subGoodNodes(root *TreeNode, previousMax int) int {
|
||||||
|
if root == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
sum := 0
|
||||||
|
max := previousMax
|
||||||
|
if root.Val >= previousMax {
|
||||||
|
sum = 1
|
||||||
|
max = root.Val
|
||||||
|
}
|
||||||
|
sum += subGoodNodes(root.Left, max)
|
||||||
|
sum += subGoodNodes(root.Right, max)
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
|
type TreeNode struct {
|
||||||
|
Val int
|
||||||
|
Left *TreeNode
|
||||||
|
Right *TreeNode
|
||||||
|
}
|
Loading…
Reference in New Issue