Skip to content

Commit 3b90534

Browse files
authored
Merge pull request #1931 from yhkee0404/main
[yhkee0404] WEEK 11 solutions
2 parents 4387c42 + fa374ae commit 3b90534

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func maxPathSum(root *TreeNode) int {
10+
ans, _ := solve(root)
11+
return ans
12+
}
13+
14+
func solve(root *TreeNode) (int, int) {
15+
if root == nil {
16+
return -30_000_000, -30_000_000
17+
}
18+
leftAns, leftRootAns := solve(root.Left)
19+
rightAns, rightRootAns := solve(root.Right)
20+
rootAns := root.Val + max(0, leftRootAns, rightRootAns,)
21+
ans := max(leftAns, rightAns, root.Val + max(0, leftRootAns,) + max(0, rightRootAns,))
22+
return ans, rootAns
23+
}

graph-valid-tree/yhkee0404.kt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
/**
3+
* @param n: An integer
4+
* @param edges: a list of undirected edges
5+
* @return: true if it's a valid tree, or false
6+
*/
7+
fun validTree(n: Int, edges: Array<IntArray>): Boolean {
8+
// write your code here
9+
val adj = List(n) {mutableListOf<Int>()} // S(V, E) = O(V + E)
10+
edges.forEach {
11+
adj[it[0]].add(it[1])
12+
adj[it[1]].add(it[0])
13+
}
14+
var t = 0
15+
val tortoiseStack = mutableListOf<MutableList<Int>>(mutableListOf(0, 0, -1))
16+
val hareStack = mutableListOf<MutableList<Int>>(mutableListOf(0, 0, -1))
17+
var tortoise = listOf(-1)
18+
while (! hareStack.isEmpty()) { // T(V, E) = O(V + E)
19+
val hare = hareStack.last()
20+
if (hare[1] == adj[hare[0]].size) {
21+
hareStack.removeLast()
22+
} else if (adj[hare[0]][hare[1]] != hare[2]) {
23+
hareStack.add(mutableListOf(adj[hare[0]][hare[1]], 0, hare[0]))
24+
}
25+
if (hare[1]++ != 0) {
26+
continue
27+
}
28+
if ((t++ and 1) == 1) {
29+
while (! tortoiseStack.isEmpty()) {
30+
tortoise = tortoiseStack.last()
31+
if (tortoise[1] == adj[tortoise[0]].size) {
32+
tortoiseStack.removeLast()
33+
} else if (adj[tortoise[0]][tortoise[1]] != tortoise[2]) {
34+
tortoiseStack.add(mutableListOf(adj[tortoise[0]][tortoise[1]], 0, tortoise[0]))
35+
}
36+
if (tortoise[1]++ != 0) {
37+
continue
38+
}
39+
break
40+
}
41+
}
42+
if (hare[0] == tortoise[0]) {
43+
return false
44+
}
45+
}
46+
return t == n
47+
}
48+
}

merge-intervals/yhkee0404.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use itertools::Itertools;
2+
use std::cmp::max;
3+
4+
impl Solution {
5+
pub fn merge(intervals: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
6+
let mut intervals: Vec<_> = intervals.iter()
7+
.cloned()
8+
.sorted_by(|a, b| a.cmp(b)) // T(n) = O(nlogn)
9+
.collect();
10+
let mut ans: Vec<Vec<i32>> = vec![];
11+
let mut end = -1;
12+
for interval in intervals {
13+
if ans.is_empty() || ans.last().unwrap()[1] < interval[0] {
14+
ans.push(interval) // S(n) = O(n)
15+
} else {
16+
ans.last_mut().unwrap()[1] = max(ans.last().unwrap()[1], interval[1])
17+
}
18+
}
19+
ans
20+
}
21+
}

missing-number/yhkee0404.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Solution {
2+
def missingNumber(nums: Array[Int]): Int = {
3+
(1 to nums.length).reduce(_ ^ _) ^ nums.reduce(_ ^ _) // T(n) = O(n), S(n) = O(1)
4+
}
5+
}

reorder-list/yhkee0404.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public var val: Int
5+
* public var next: ListNode?
6+
* public init() { self.val = 0; self.next = nil; }
7+
* public init(_ val: Int) { self.val = val; self.next = nil; }
8+
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
func reorderList(_ head: ListNode?) {
13+
let temp = ListNode(0, head)
14+
solve(temp, temp, false)
15+
}
16+
func solve(_ tortoise: ListNode, _ hare: ListNode?, _ odd: Bool) -> ListNode? {
17+
guard let safeHare = hare else {
18+
let tail = ListNode(0, tortoise.next)
19+
tortoise.next = nil
20+
return tail
21+
}
22+
let tail = solve(odd ? tortoise : tortoise.next!, safeHare.next, !odd)
23+
if !odd {
24+
return tail
25+
}
26+
guard let safeTail = tail else {
27+
return nil
28+
}
29+
if safeTail.val == 0 {
30+
return safeTail.next
31+
}
32+
let next = safeTail.next
33+
safeTail.next = tortoise.next
34+
tortoise.next = safeTail
35+
return next
36+
}
37+
}

0 commit comments

Comments
 (0)