Skip to content

Commit 0bb8f80

Browse files
authored
Improved tasks 84, 207
1 parent 8bb2a08 commit 0bb8f80

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

src/main/scala/g0001_0100/s0084_largest_rectangle_in_histogram/Solution.scala

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package g0001_0100.s0084_largest_rectangle_in_histogram
22

33
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Stack #Monotonic_Stack
4-
// #Big_O_Time_O(n_log_n)_Space_O(log_n) #2023_11_02_Time_904_ms_(71.43%)_Space_77.5_MB_(35.71%)
4+
// #Big_O_Time_O(n_log_n)_Space_O(log_n) #2024_06_02_Time_1092_ms_(71.43%)_Space_76.1_MB_(64.29%)
5+
6+
import scala.util.control.Breaks.{break, breakable}
57

68
object Solution {
79
def largestRectangleArea(heights: Array[Int]): Int = {
@@ -54,12 +56,16 @@ object Solution {
5456
}
5557

5658
private def checkIfSorted(a: Array[Int], start: Int, limit: Int): Boolean = {
57-
for (i <- start + 1 until limit) {
58-
if (a(i) < a(i - 1)) {
59-
return false
59+
var sorted = true
60+
breakable {
61+
for (i <- start + 1 until limit) {
62+
if (a(i) < a(i - 1)) {
63+
sorted = false
64+
break
65+
}
6066
}
6167
}
62-
true
68+
sorted
6369
}
6470

6571
private def maxOfThreeNums(a: Int, b: Int, c: Int): Int = {

src/main/scala/g0201_0300/s0207_course_schedule/Solution.scala

+21-31
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,32 @@ package g0201_0300.s0207_course_schedule
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search
44
// #Breadth_First_Search #Graph #Topological_Sort #Big_O_Time_O(N)_Space_O(N)
5-
// #2023_11_05_Time_548_ms_(87.81%)_Space_60_MB_(12.20%)
6-
7-
import scala.collection.mutable.ArrayBuffer
5+
// #2024_06_02_Time_720_ms_(91.11%)_Space_61.7_MB_(75.56%)
86

97
object Solution {
10-
val WHITE = 0
11-
val GRAY = 1
12-
val BLACK = 2
13-
148
def canFinish(numCourses: Int, prerequisites: Array[Array[Int]]): Boolean = {
15-
val adj = Array.fill(numCourses)(new ArrayBuffer[Int]())
16-
prerequisites.foreach { pre =>
17-
adj(pre(1)).append(pre(0))
18-
}
19-
20-
val colors = Array.fill(numCourses)(WHITE)
21-
(0 until numCourses).forall { i =>
22-
if (colors(i) == WHITE && adj(i).nonEmpty && hasCycle(adj, i, colors)) {
23-
false
24-
} else {
25-
true
26-
}
9+
import scala.collection.mutable.{Queue, ListBuffer}
10+
val indegree = Array.fill(numCourses)(0)
11+
val graph = Array.fill(numCourses)(new ListBuffer[Int])
12+
for (data <- prerequisites) {
13+
val course = data.head
14+
val prerequisiteCourse = data.last
15+
indegree(course) = indegree(course) + 1
16+
graph(prerequisiteCourse) += course
2717
}
28-
}
29-
30-
private def hasCycle(adj: Array[ArrayBuffer[Int]], node: Int, colors: Array[Int]): Boolean = {
31-
colors(node) = GRAY
32-
adj(node).foreach { nei =>
33-
if (colors(nei) == GRAY) {
34-
return true
35-
}
36-
if (colors(nei) == WHITE && hasCycle(adj, nei, colors)) {
37-
return true
18+
val startingCourses = indegree.zipWithIndex.filter(_._1.equals(0)).map(_._2)
19+
val queue = Queue[Int](startingCourses: _*)
20+
var courseTaken = 0
21+
while (queue.nonEmpty) {
22+
val current = queue.dequeue
23+
courseTaken = courseTaken + 1
24+
for (neighbor <- graph(current)) {
25+
indegree(neighbor) = indegree(neighbor) - 1
26+
if (indegree(neighbor).equals(0)) {
27+
queue.enqueue(neighbor)
28+
}
3829
}
3930
}
40-
colors(node) = BLACK
41-
false
31+
courseTaken == numCourses
4232
}
4333
}

0 commit comments

Comments
 (0)