Skip to content

Commit 590801b

Browse files
committed
add scala solutions for Question 28 30 31
1 parent 641f0ee commit 590801b

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
object Solution {
2+
def countBits(num: Int): Array[Int] = {
3+
val res = Array.fill(num + 1)(0)
4+
for (i <- 0 to num) {
5+
res(i) = countOne(i)
6+
}
7+
res
8+
}
9+
10+
def countOne(num: Int): Int = {
11+
var count = 0
12+
var rest = num
13+
while (rest > 1) {
14+
count += rest % 2
15+
rest = rest / 2
16+
}
17+
count + rest
18+
}
19+
}
20+
21+
object Solution2 {
22+
def countBits(num: Int): Array[Int] = Range(0, num + 1).map(Solution.countOne).toArray
23+
}

May-LeetCoding-Challenge/30-K-Closest-Points-To-Origin/K-Closest-Points-To-Origin.scala

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,44 @@ object Solution {
77
(arr1(0) * arr1(0) + arr1(1) * arr1(1)) < (arr2(0) * arr2(0) + arr2(1) * arr2(1))
88

99
}
10+
11+
12+
//simplest solution O(n * log(n))
13+
object Solution2 {
14+
def kClosest(points: Array[Array[Int]], K: Int): Array[Array[Int]] =
15+
points.sortBy(p => p(0) * p(0) + p(1) * p(1)).take(K)
16+
}
17+
18+
//QuickSelect solution with random pivot O(n) to 0(n*n)
19+
object Solution3 {
20+
def kClosest(points: Array[Array[Int]], K: Int): Array[Array[Int]] =
21+
quickSelect(points, 0, points.length - 1, K)
22+
23+
def quickSelect(points: Array[Array[Int]], start: Int, end: Int, K: Int): Array[Array[Int]] = {
24+
val pivot = scala.util.Random.nextInt(end - start + 1) + start
25+
var i = start
26+
var j = start
27+
swap(points, pivot, end)
28+
29+
while (j < end) {
30+
if (SqAddition(points(j)) <= SqAddition(points(end))) {
31+
swap(points, i, j)
32+
i += 1
33+
}
34+
j += 1
35+
}
36+
37+
swap(points, i, j)
38+
if (K == i + 1 - start) points.take(i + 1)
39+
else if (K > i + 1 - start) quickSelect(points, i + 1, end, K - i - 1 + start)
40+
else quickSelect(points, start, i - 1, K)
41+
}
42+
43+
def swap(points: Array[Array[Int]], i: Int, j: Int): Unit = {
44+
val tmp = points(i)
45+
points(i) = points(j)
46+
points(j) = tmp
47+
}
48+
49+
def SqAddition(point: Array[Int]): Int = point(0) * point(0) + point(1) * point(1)
50+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//Dynamic programming solution
2+
object Solution {
3+
def minDistance(word1: String, word2: String): Int = {
4+
val (l1, l2) = (word1.length, word2.length)
5+
val dp = Array.ofDim[Int](l1 + 1, l2 + 1)
6+
for (i <- 0 to l1) { dp(i)(0) = i }
7+
for (j <- 0 to l2) { dp(0)(j) = j }
8+
for (i <- 0 until l1; j <- 0 until l2) {
9+
if (word1(i) == word2(j)) dp(i + 1)(j + 1) = dp(i)(j)
10+
else dp(i + 1)(j + 1) = (dp(i)(j) min (dp(i)(j + 1) min dp(i + 1)(j))) + 1
11+
}
12+
dp(l1)(l2)
13+
}
14+
}

0 commit comments

Comments
 (0)