Skip to content

Commit

Permalink
Merge pull request youngyangyang04#1476 from wzqwtt/greedy06
Browse files Browse the repository at this point in the history
添加(0452.用最少数量的箭引爆气球、0435.无重叠区间、0763.划分字母区间) Scala版本
  • Loading branch information
youngyangyang04 authored Jul 25, 2022
2 parents 0198a16 + 94350c0 commit 7c1e401
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
22 changes: 21 additions & 1 deletion problems/0435.无重叠区间.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,27 @@ function eraseOverlapIntervals(intervals: number[][]): number {
};
```


### Scala

```scala
object Solution {
def eraseOverlapIntervals(intervals: Array[Array[Int]]): Int = {
var result = 0
var interval = intervals.sortWith((a, b) => {
a(1) < b(1)
})
var edge = Int.MinValue
for (i <- 0 until interval.length) {
if (edge <= interval(i)(0)) {
edge = interval(i)(1)
} else {
result += 1
}
}
result
}
}
```



Expand Down
25 changes: 25 additions & 0 deletions problems/0452.用最少数量的箭引爆气球.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,30 @@ impl Solution {
}
}
```

### Scala

```scala
object Solution {
def findMinArrowShots(points: Array[Array[Int]]): Int = {
if (points.length == 0) return 0
// 排序
var point = points.sortWith((a, b) => {
a(0) < b(0)
})

var result = 1 // points不为空就至少需要一只箭
for (i <- 1 until point.length) {
if (point(i)(0) > point(i - 1)(1)) {
result += 1
} else {
point(i)(1) = math.min(point(i - 1)(1), point(i)(1))
}
}
result // 返回结果
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
24 changes: 24 additions & 0 deletions problems/0763.划分字母区间.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,31 @@ function partitionLabels(s: string): number[] {
};
```

### Scala

```scala
object Solution {
import scala.collection.mutable
def partitionLabels(s: String): List[Int] = {
var hash = new Array[Int](26)
for (i <- s.indices) {
hash(s(i) - 'a') = i
}

var res = mutable.ListBuffer[Int]()
var (left, right) = (0, 0)
for (i <- s.indices) {
right = math.max(hash(s(i) - 'a'), right)
if (i == right) {
res.append(right - left + 1)
left = i + 1
}
}

res.toList
}
}
```


-----------------------
Expand Down

0 comments on commit 7c1e401

Please sign in to comment.