Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions binary-tree-maximum-path-sum/yhkee0404.go
Original file line number Diff line number Diff line change
@@ -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
}
48 changes: 48 additions & 0 deletions graph-valid-tree/yhkee0404.kt
Original file line number Diff line number Diff line change
@@ -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<IntArray>): Boolean {
// write your code here
val adj = List(n) {mutableListOf<Int>()} // 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<MutableList<Int>>(mutableListOf(0, 0, -1))
val hareStack = mutableListOf<MutableList<Int>>(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
}
}
21 changes: 21 additions & 0 deletions merge-intervals/yhkee0404.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use itertools::Itertools;
use std::cmp::max;

impl Solution {
pub fn merge(intervals: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut intervals: Vec<_> = intervals.iter()
.cloned()
.sorted_by(|a, b| a.cmp(b)) // T(n) = O(nlogn)
.collect();
let mut ans: Vec<Vec<i32>> = 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
}
}
5 changes: 5 additions & 0 deletions missing-number/yhkee0404.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
37 changes: 37 additions & 0 deletions reorder-list/yhkee0404.swift
Original file line number Diff line number Diff line change
@@ -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
}
}