Skip to content

Commit 35a4b0c

Browse files
committed
add scala solutions for 20 - 23
1 parent 114de14 commit 35a4b0c

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {
2+
var value: Int = _value
3+
var left: TreeNode = _left
4+
var right: TreeNode = _right
5+
}
6+
7+
//recursive and iteration solution
8+
object Solution {
9+
def kthSmallest(root: TreeNode, k: Int): Int = kthSmallestWithAcc(root, Seq())(k - 1)
10+
11+
def kthSmallestWithAcc(root: TreeNode, acc: Seq[Int]): Seq[Int] = {
12+
if (root == null) acc
13+
else kthSmallestWithAcc(root.right, kthSmallestWithAcc(root.left, acc) ++ Seq(root.value))
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//brute force solution
2+
object Solution {
3+
def countSquares(matrix: Array[Array[Int]]): Int = {
4+
val maxX = matrix(0).length
5+
val maxY = matrix.length
6+
val maxSide = maxX max maxY
7+
val list = for (side <- 0 until maxSide; x <- 0 until (maxX - side); y <- 0 until (maxY - side); if isAllOne(matrix, x, y, side)) yield 1
8+
list.size
9+
}
10+
11+
def isAllOne(matrix: Array[Array[Int]], startX: Int, startY: Int, side: Int): Boolean =
12+
!(startX to startX + side).exists(x => (startY to startY + side).exists(y => matrix(y)(x) == 0))
13+
14+
}
15+
16+
//optimized solution O(x * y * 2)
17+
object Solution2 {
18+
def countSquares(matrix: Array[Array[Int]]): Int = {
19+
val maxX = matrix(0).length
20+
val maxY = matrix.length
21+
val res = Array.fill(maxY)(Array.fill(maxX)(0))
22+
for (x <- 0 until maxX; y <- 0 until maxY; if matrix(y)(x) == 1) {
23+
res(y)(x) = if (x != 0 && y != 0) ((res(y - 1)(x) min res(y - 1)(x - 1)) min res(y)(x - 1)) + 1 else 1
24+
}
25+
res.foldLeft(0)((acc, rang) => acc + rang.sum)
26+
}
27+
}
28+
29+
//optimized solution+ O(x * y)
30+
object Solution3 {
31+
def countSquares(matrix: Array[Array[Int]]): Int = {
32+
val maxX = matrix(0).length
33+
val maxY = matrix.length
34+
val res = Array.fill(maxY)(Array.fill(maxX)(0))
35+
var total = 0
36+
for (x <- 0 until maxX; y <- 0 until maxY; if matrix(y)(x) == 1) {
37+
res(y)(x) = if (x != 0 && y != 0) ((res(y - 1)(x) min res(y - 1)(x - 1)) min res(y)(x - 1)) + 1 else 1
38+
total += res(y)(x)
39+
}
40+
total
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//easiest solution but maybe not the fastest way
2+
object Solution {
3+
def frequencySort(s: String): String = s.groupBy(c => c).toSeq.sortBy(-_._2.length).foldLeft("")((acc, t) => acc + t._2)
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
object Solution {
2+
def intervalIntersection(A: Array[Array[Int]], B: Array[Array[Int]]): Array[Array[Int]] = {
3+
import scala.collection.mutable
4+
var res = mutable.Seq.empty[Array[Int]]
5+
var indexA = 0
6+
var indexB = 0
7+
while (indexA < A.length && indexB < B.length) {
8+
val (intAStart, intAEnd) = (A(indexA)(0), A(indexA)(1))
9+
val (intBStart, intBEnd) = (B(indexB)(0), B(indexB)(1))
10+
val start = intAStart max intBStart
11+
val end = intAEnd min intBEnd
12+
if (intAEnd >= intBEnd) indexB += 1
13+
if (intBEnd >= intAEnd) indexA += 1
14+
if (end >= start) res = res :+ Array(start, end)
15+
}
16+
res.toArray
17+
}
18+
}

0 commit comments

Comments
 (0)