diff --git a/binary-tree-maximum-path-sum/yhkee0404.go b/binary-tree-maximum-path-sum/yhkee0404.go new file mode 100644 index 000000000..ea1221f8b --- /dev/null +++ b/binary-tree-maximum-path-sum/yhkee0404.go @@ -0,0 +1,23 @@ +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func maxPathSum(root *TreeNode) int { + ans, _ := solve(root) + return ans +} + +func solve(root *TreeNode) (int, int) { + if root == nil { + return -30_000_000, -30_000_000 + } + leftAns, leftRootAns := solve(root.Left) + rightAns, rightRootAns := solve(root.Right) + rootAns := root.Val + max(0, leftRootAns, rightRootAns,) + ans := max(leftAns, rightAns, root.Val + max(0, leftRootAns,) + max(0, rightRootAns,)) + return ans, rootAns +} diff --git a/graph-valid-tree/yhkee0404.kt b/graph-valid-tree/yhkee0404.kt new file mode 100644 index 000000000..7dbb7ca88 --- /dev/null +++ b/graph-valid-tree/yhkee0404.kt @@ -0,0 +1,48 @@ +class Solution { + /** + * @param n: An integer + * @param edges: a list of undirected edges + * @return: true if it's a valid tree, or false + */ + fun validTree(n: Int, edges: Array): Boolean { + // write your code here + val adj = List(n) {mutableListOf()} // S(V, E) = O(V + E) + edges.forEach { + adj[it[0]].add(it[1]) + adj[it[1]].add(it[0]) + } + var t = 0 + val tortoiseStack = mutableListOf>(mutableListOf(0, 0, -1)) + val hareStack = mutableListOf>(mutableListOf(0, 0, -1)) + var tortoise = listOf(-1) + while (! hareStack.isEmpty()) { // T(V, E) = O(V + E) + val hare = hareStack.last() + if (hare[1] == adj[hare[0]].size) { + hareStack.removeLast() + } else if (adj[hare[0]][hare[1]] != hare[2]) { + hareStack.add(mutableListOf(adj[hare[0]][hare[1]], 0, hare[0])) + } + if (hare[1]++ != 0) { + continue + } + if ((t++ and 1) == 1) { + while (! tortoiseStack.isEmpty()) { + tortoise = tortoiseStack.last() + if (tortoise[1] == adj[tortoise[0]].size) { + tortoiseStack.removeLast() + } else if (adj[tortoise[0]][tortoise[1]] != tortoise[2]) { + tortoiseStack.add(mutableListOf(adj[tortoise[0]][tortoise[1]], 0, tortoise[0])) + } + if (tortoise[1]++ != 0) { + continue + } + break + } + } + if (hare[0] == tortoise[0]) { + return false + } + } + return t == n + } +} diff --git a/merge-intervals/yhkee0404.rs b/merge-intervals/yhkee0404.rs new file mode 100644 index 000000000..b3d111514 --- /dev/null +++ b/merge-intervals/yhkee0404.rs @@ -0,0 +1,21 @@ +use itertools::Itertools; +use std::cmp::max; + +impl Solution { + pub fn merge(intervals: Vec>) -> Vec> { + let mut intervals: Vec<_> = intervals.iter() + .cloned() + .sorted_by(|a, b| a.cmp(b)) // T(n) = O(nlogn) + .collect(); + let mut ans: Vec> = vec![]; + let mut end = -1; + for interval in intervals { + if ans.is_empty() || ans.last().unwrap()[1] < interval[0] { + ans.push(interval) // S(n) = O(n) + } else { + ans.last_mut().unwrap()[1] = max(ans.last().unwrap()[1], interval[1]) + } + } + ans + } +} diff --git a/missing-number/yhkee0404.scala b/missing-number/yhkee0404.scala new file mode 100644 index 000000000..8d828cc1a --- /dev/null +++ b/missing-number/yhkee0404.scala @@ -0,0 +1,5 @@ +object Solution { + def missingNumber(nums: Array[Int]): Int = { + (1 to nums.length).reduce(_ ^ _) ^ nums.reduce(_ ^ _) // T(n) = O(n), S(n) = O(1) + } +} diff --git a/reorder-list/yhkee0404.swift b/reorder-list/yhkee0404.swift new file mode 100644 index 000000000..ec8369797 --- /dev/null +++ b/reorder-list/yhkee0404.swift @@ -0,0 +1,37 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * public var val: Int + * public var next: ListNode? + * public init() { self.val = 0; self.next = nil; } + * public init(_ val: Int) { self.val = val; self.next = nil; } + * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } + * } + */ +class Solution { + func reorderList(_ head: ListNode?) { + let temp = ListNode(0, head) + solve(temp, temp, false) + } + func solve(_ tortoise: ListNode, _ hare: ListNode?, _ odd: Bool) -> ListNode? { + guard let safeHare = hare else { + let tail = ListNode(0, tortoise.next) + tortoise.next = nil + return tail + } + let tail = solve(odd ? tortoise : tortoise.next!, safeHare.next, !odd) + if !odd { + return tail + } + guard let safeTail = tail else { + return nil + } + if safeTail.val == 0 { + return safeTail.next + } + let next = safeTail.next + safeTail.next = tortoise.next + tortoise.next = safeTail + return next + } +}