Skip to content

Commit 1040a57

Browse files
authored
Improved tasks 20, 41, 45, 55, 79
1 parent 77c7413 commit 1040a57

File tree

5 files changed

+58
-50
lines changed

5 files changed

+58
-50
lines changed

src/main/scala/g0001_0100/s0020_valid_parentheses/Solution.scala

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,35 @@ package g0001_0100.s0020_valid_parentheses
22

33
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #String #Stack
44
// #Data_Structure_I_Day_9_Stack_Queue #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
5-
// #2023_10_29_Time_449_ms_(65.58%)_Space_56.6_MB_(22.33%)
5+
// #2024_06_01_Time_587_ms_(69.29%)_Space_58.3_MB_(60.63%)
66

77
import scala.collection.mutable.Stack
8+
import scala.util.control.Breaks._
89

910
object Solution {
1011
def isValid(s: String): Boolean = {
1112
val stack = Stack[Char]()
13+
var result = true
1214

13-
for (i <- 0 until s.length) {
14-
val c = s.charAt(i)
15-
if (c == '(' || c == '[' || c == '{') {
16-
stack.push(c)
17-
} else if (c == ')' && stack.nonEmpty && stack.top == '(') {
18-
stack.pop()
19-
} else if (c == '}' && stack.nonEmpty && stack.top == '{') {
20-
stack.pop()
21-
} else if (c == ']' && stack.nonEmpty && stack.top == '[') {
22-
stack.pop()
23-
} else {
24-
return false
15+
breakable {
16+
for (i <- 0 until s.length) {
17+
val c = s.charAt(i)
18+
if (c == '(' || c == '[' || c == '{') {
19+
stack.push(c)
20+
} else if (c == ')' && stack.nonEmpty && stack.top == '(') {
21+
stack.pop()
22+
} else if (c == '}' && stack.nonEmpty && stack.top == '{') {
23+
stack.pop()
24+
} else if (c == ']' && stack.nonEmpty && stack.top == '[') {
25+
stack.pop()
26+
} else {
27+
result = false
28+
break()
29+
}
2530
}
2631
}
2732

28-
stack.isEmpty
33+
result && stack.isEmpty
2934
}
3035
}
36+
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
package g0001_0100.s0041_first_missing_positive
22

33
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Udemy_Arrays
4-
// #Big_O_Time_O(n)_Space_O(n) #2023_10_30_Time_650_ms_(100.00%)_Space_78.5_MB_(100.00%)
4+
// #Big_O_Time_O(n)_Space_O(n) #2024_06_01_Time_912_ms_(84.21%)_Space_71.6_MB_(100.00%)
55

66
import scala.annotation.tailrec
77

88
object Solution {
99
def firstMissingPositive(nums: Array[Int]): Int = {
1010
for (i <- nums.indices) {
11-
if (nums(i) <= 0 || nums(i) > nums.length || nums(i) == i + 1) {
12-
// Continue the loop
13-
} else {
11+
if (nums(i) > 0 && nums(i) < nums.length && nums(i) != i + 1) {
1412
dfs(nums, nums(i))
1513
}
1614
}
1715

18-
for (i <- nums.indices) {
16+
var result = nums.length + 1
17+
var found = false
18+
for (i <- nums.indices if !found) {
1919
if (nums(i) != i + 1) {
20-
return i + 1
20+
result = i + 1
21+
found = true
2122
}
2223
}
2324

24-
nums.length + 1
25+
result
2526
}
2627

2728
@tailrec
@@ -34,3 +35,4 @@ object Solution {
3435
dfs(nums, temp)
3536
}
3637
}
38+

src/main/scala/g0001_0100/s0045_jump_game_ii/Solution.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ package g0001_0100.s0045_jump_game_ii
22

33
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Greedy
44
// #Algorithm_II_Day_13_Dynamic_Programming #Dynamic_Programming_I_Day_4
5-
// #Big_O_Time_O(n)_Space_O(1) #2023_10_31_Time_510_ms_(88.52%)_Space_56_MB_(65.57%)
5+
// #Big_O_Time_O(n)_Space_O(1) #2024_06_01_Time_653_ms_(83.87%)_Space_58.6_MB_(87.10%)
66

77
object Solution {
88
def jump(nums: Array[Int]): Int = {
99
var length = 0
1010
var maxLength = 0
1111
var minJump = 0
1212

13-
for (i <- 0 until nums.length - 1) {
13+
var result = -1
14+
for (i <- 0 until nums.length - 1 if result == -1) {
1415
length -= 1
1516
maxLength -= 1
1617
maxLength = math.max(maxLength, nums(i))
@@ -21,10 +22,11 @@ object Solution {
2122
}
2223

2324
if (length >= nums.length - i - 1) {
24-
return minJump
25+
result = minJump
2526
}
2627
}
2728

28-
minJump
29+
if (result == -1) minJump else result
2930
}
3031
}
32+

src/main/scala/g0001_0100/s0055_jump_game/Solution.scala

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,38 @@ package g0001_0100.s0055_jump_game
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming #Greedy
44
// #Algorithm_II_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_4 #Udemy_Arrays
5-
// #Big_O_Time_O(n)_Space_O(1) #2023_10_31_Time_622_ms_(88.31%)_Space_56.9_MB_(44.16%)
5+
// #Big_O_Time_O(n)_Space_O(1) #2024_06_01_Time_769_ms_(79.03%)_Space_59.8_MB_(50.00%)
66

77
object Solution {
88
def canJump(nums: Array[Int]): Boolean = {
99
val sz = nums.length
1010
// We set tmp to 1 so it won't break on the first iteration
1111
var tmp = 1
12+
var result = true // Variable to store the result
1213

13-
for (i <- 0 until sz) {
14+
for (i <- 0 until sz if result) {
1415
// We always deduct tmp for every iteration
1516
tmp -= 1
1617
if (tmp < 0) {
1718
// If from the previous iteration tmp is already 0, it will be < 0 here,
1819
// leading to a false value
19-
return false
20-
}
21-
// We get the maximum value because this value is supposed to be our iterator. If both values are 0,
22-
// then the next iteration will return false.
23-
// We can stop the whole iteration with this condition. Without this condition, the code runs in 2ms (79.6%).
24-
// Adding this condition improves the performance to 1ms (100%)
25-
// because if the test case jump value is quite large, instead of just iterating, we can
26-
// just check using this condition.
27-
// Example: [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] -> we can just jump to the end without iterating the whole array.
28-
tmp = math.max(tmp, nums(i))
29-
if (i + tmp >= sz - 1) {
30-
return true
20+
result = false
21+
} else {
22+
// We get the maximum value because this value is supposed to be our iterator. If both values are 0,
23+
// then the next iteration will return false.
24+
// We can stop the whole iteration with this condition. Without this condition, the code runs in 2ms (79.6%).
25+
// Adding this condition improves the performance to 1ms (100%)
26+
// because if the test case jump value is quite large, instead of just iterating, we can
27+
// just check using this condition.
28+
// Example: [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] -> we can just jump to the end without iterating the whole array.
29+
tmp = math.max(tmp, nums(i))
30+
if (i + tmp >= sz - 1) {
31+
result = true
32+
}
3133
}
3234
}
33-
// We can just return true at the end because if tmp is 0 on the previous
34-
// iteration,
35-
// even though the next iteration's index is the last one, it will return false under the
36-
// tmp < 0 condition.
37-
true
35+
36+
result
3837
}
3938
}
39+

src/main/scala/g0001_0100/s0079_word_search/Solution.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package g0001_0100.s0079_word_search
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Matrix #Backtracking
44
// #Algorithm_II_Day_11_Recursion_Backtracking #Big_O_Time_O(4^(m*n))_Space_O(m*n)
5-
// #2023_11_02_Time_783_ms_(94.87%)_Space_54.9_MB_(100.00%)
5+
// #2024_06_01_Time_848_ms_(74.19%)_Space_59.5_MB_(66.67%)
66

77
object Solution {
88
private val directions = Array(Array(-1, 0), Array(1, 0), Array(0, -1), Array(0, 1))
@@ -14,11 +14,10 @@ object Solution {
1414
numRows = board.length
1515
numCols = board(0).length
1616
var result = false
17-
for (row <- 0 until numRows) {
18-
for (col <- 0 until numCols) {
17+
for (row <- 0 until numRows if !result) {
18+
for (col <- 0 until numCols if !result) {
1919
if (board(row)(col) == word(0)) {
2020
result = backTracking(board, row, col, word, 0)
21-
if (result) return true
2221
}
2322
}
2423
}
@@ -33,11 +32,10 @@ object Solution {
3332
val originalValue = board(row)(col)
3433
board(row)(col) = '0'
3534
var output = false
36-
for (dir <- directions) {
35+
for (dir <- directions if !output) {
3736
val newRow = row + dir(0)
3837
val newCol = col + dir(1)
3938
output = backTracking(board, newRow, newCol, word, index + 1)
40-
if (output) return true
4139
}
4240
board(row)(col) = originalValue
4341
output

0 commit comments

Comments
 (0)