Skip to content

Commit f0ccf97

Browse files
author
Imtiaz Ahmed
committedJan 29, 2023
scala solutions for 72, 97, 215, 322, 394, 485, 509, 746
1 parent 9a39d91 commit f0ccf97

9 files changed

+139
-1
lines changed
 

‎.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.DS_Store
1+
.DS_Store
2+
.metals
3+
.vscode

‎scala/0072-edit-distance.scala

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object Solution {
2+
def minDistance(word1: String, word2: String): Int = {
3+
var minDisArr = Array.ofDim[Int](word1.size+2, word2.size+2)
4+
for(i<- 0 to word1.size; j<- 0 to word2.size){
5+
if(i == 0 || j == 0) minDisArr(i)(j) = i + j
6+
7+
else if (word1(i-1).equals(word2(j-1)))
8+
minDisArr(i)(j) = minDisArr(i-1)(j-1)
9+
10+
else
11+
minDisArr(i)(j) = 1 + (minDisArr(i - 1)(j - 1) min minDisArr(i)(j - 1) min minDisArr(i - 1)(j))
12+
13+
}
14+
minDisArr(word1.size)(word2.size)
15+
}
16+
}

‎scala/0097-interleaving-string.scala

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
object Solution {
2+
def isInterleave(s1: String, s2: String, s3: String): Boolean = {
3+
val c1 = s1.toCharArray()
4+
val c2 = s2.toCharArray()
5+
val c3 = s3.toCharArray()
6+
val m = s1.length()
7+
val n = s2.length()
8+
if(m + n != s3.length()) false;
9+
else dfs(c1, c2, c3, 0, 0, 0, Array.ofDim[Boolean](m+1, n+1))
10+
}
11+
12+
def dfs(c1: Array[Char], c2: Array[Char], c3: Array[Char], i:Int, j:Int, k:Int, invalid: Array[Array[Boolean]]): Boolean = {
13+
if(invalid(i)(j)) return false
14+
if(k == c3.length) return true
15+
val valid =
16+
i < c1.length && c1(i) == c3(k) && dfs(c1, c2, c3, i + 1, j, k + 1, invalid) ||
17+
j < c2.length && c2(j) == c3(k) && dfs(c1, c2, c3, i, j + 1, k + 1, invalid)
18+
if(!valid) invalid(i)(j) = true
19+
valid
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.collection.mutable.PriorityQueue
2+
3+
object Solution {
4+
def findKthLargest(nums: Array[Int], k: Int): Int = {
5+
val pq = PriorityQueue.empty(Ordering[Int].reverse)
6+
for(i <- 0 until nums.size){
7+
if (i < k) pq.enqueue(nums(i))
8+
else {
9+
pq.enqueue(nums(i))
10+
pq.dequeue()
11+
}
12+
}
13+
pq.dequeue()
14+
}
15+
}

‎scala/0322-coin-change.scala

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object Solution {
2+
def coinChange(coins: Array[Int], amount: Int): Int = {
3+
var dp: Array[Int] = List.fill[Int](amount+5)(amount+1).toArray
4+
dp(0) = 0
5+
for(i <- 1 to amount)
6+
{
7+
for(j <- 0 until coins.size)
8+
{
9+
if(coins(j) <= i)
10+
{
11+
dp(i) = dp(i) min (dp(i - coins(j)) + 1)
12+
}
13+
}
14+
}
15+
if(dp(amount) > amount ) -1 else dp(amount);
16+
}
17+
}

‎scala/0394-decode-string.scala

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
object Solution {
2+
3+
def decodeString(s: String): String = {
4+
val (str, res) = decodeStringRecursive(s, 0)
5+
str
6+
}
7+
8+
def decodeStringRecursive(input: String, start: Int): (String, Int) = {
9+
val result = new StringBuilder()
10+
var i = start
11+
while(i < input.length){
12+
input(i) match {
13+
case x if x>= '0' && x<='9' =>
14+
val currNum = input.substring(i, input.indexOf('[', i)).toInt
15+
val (str, maxI) = decodeStringRecursive(input, input.indexOf('[', i)+1)
16+
i = maxI
17+
result.append(str * currNum)
18+
case x if x >= 'a' && x <= 'z' =>
19+
(result.append(x), i)
20+
case ']' =>
21+
return (result.toString, i)
22+
case _ =>
23+
}
24+
i+=1
25+
}
26+
(result.toString, i)
27+
}
28+
29+
}

‎scala/0485-max-consecutive-ones.scala

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Solution {
2+
def findMaxConsecutiveOnes(nums: Array[Int]): Int =
3+
nums.scanLeft(0)((m, x) => if (x == 0) 0 else m + 1).max
4+
}

‎scala/0509-fibonacci-number.scala

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object Solution {
2+
var gen: Boolean = false;
3+
var fibonacci = List(0,1)
4+
def fib(n: Int): Int = {
5+
generate
6+
fibonacci(n)
7+
}
8+
9+
def generate = {
10+
if(!gen){
11+
gen = true
12+
List.range(2, 31).map(x =>
13+
fibonacci = fibonacci :+ fibonacci(x-1) + fibonacci(x-2)
14+
)
15+
}
16+
}
17+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object Solution {
2+
def minCostClimbingStairs(cost: Array[Int]): Int = {
3+
val n = cost.length
4+
var first = cost(0)
5+
var second = cost(1)
6+
if (n<=2)
7+
first min second
8+
else {
9+
for ( i <- 2 until n) {
10+
val curr = cost(i) + (first min second)
11+
first = second
12+
second = curr
13+
}
14+
first min second
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)