Skip to content

Commit 7492b0d

Browse files
authored
Added tasks 678, 679, 680, 682
1 parent 77f29e5 commit 7492b0d

File tree

13 files changed

+384
-0
lines changed

13 files changed

+384
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16761676
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys/Solution.kt)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
16771677
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16781678
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
1679+
| 0682 |[Baseball Game](src/main/kotlin/g0601_0700/s0682_baseball_game/Solution.kt)| Easy | Array, Stack, Simulation | 159 | 100.00
1680+
| 0680 |[Valid Palindrome II](src/main/kotlin/g0601_0700/s0680_valid_palindrome_ii/Solution.kt)| Easy | String, Greedy, Two_Pointers | 296 | 79.17
1681+
| 0679 |[24 Game](src/main/kotlin/g0601_0700/s0679_24_game/Solution.kt)| Hard | Array, Math, Backtracking | 175 | 100.00
1682+
| 0678 |[Valid Parenthesis String](src/main/kotlin/g0601_0700/s0678_valid_parenthesis_string/Solution.kt)| Medium | String, Dynamic_Programming, Greedy, Stack | 133 | 100.00
16791683
| 0677 |[Map Sum Pairs](src/main/kotlin/g0601_0700/s0677_map_sum_pairs/MapSum.kt)| Medium | String, Hash_Table, Design, Trie | 197 | 80.00
16801684
| 0676 |[Implement Magic Dictionary](src/main/kotlin/g0601_0700/s0676_implement_magic_dictionary/MagicDictionary.kt)| Medium | String, Hash_Table, Design, Trie | 256 | 100.00
16811685
| 0675 |[Cut Off Trees for Golf Event](src/main/kotlin/g0601_0700/s0675_cut_off_trees_for_golf_event/Solution.kt)| Hard | Array, Breadth_First_Search, Matrix, Heap_Priority_Queue | 777 | 100.00
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0601_0700.s0678_valid_parenthesis_string
2+
3+
// #Medium #String #Dynamic_Programming #Greedy #Stack
4+
// #2023_02_16_Time_133_ms_(100.00%)_Space_33.8_MB_(46.15%)
5+
6+
class Solution {
7+
fun checkValidString(s: String): Boolean {
8+
var lo = 0
9+
var hi = 0
10+
for (i in s.indices) {
11+
lo += if (s[i] == '(') 1 else -1
12+
hi += if (s[i] != ')') 1 else -1
13+
if (hi < 0) {
14+
break
15+
}
16+
lo = 0.coerceAtLeast(lo)
17+
}
18+
return lo == 0
19+
}
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
678\. Valid Parenthesis String
2+
3+
Medium
4+
5+
Given a string `s` containing only three types of characters: `'('`, `')'` and `'*'`, return `true` _if_ `s` _is **valid**_.
6+
7+
The following rules define a **valid** string:
8+
9+
* Any left parenthesis `'('` must have a corresponding right parenthesis `')'`.
10+
* Any right parenthesis `')'` must have a corresponding left parenthesis `'('`.
11+
* Left parenthesis `'('` must go before the corresponding right parenthesis `')'`.
12+
* `'*'` could be treated as a single right parenthesis `')'` or a single left parenthesis `'('` or an empty string `""`.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "()"
17+
18+
**Output:** true
19+
20+
**Example 2:**
21+
22+
**Input:** s = "(\*)"
23+
24+
**Output:** true
25+
26+
**Example 3:**
27+
28+
**Input:** s = "(\*))"
29+
30+
**Output:** true
31+
32+
**Constraints:**
33+
34+
* `1 <= s.length <= 100`
35+
* `s[i]` is `'('`, `')'` or `'*'`.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package g0601_0700.s0679_24_game
2+
3+
// #Hard #Array #Math #Backtracking #2023_02_16_Time_175_ms_(100.00%)_Space_34.7_MB_(100.00%)
4+
5+
import java.util.Arrays
6+
import kotlin.math.abs
7+
8+
class Solution {
9+
private fun backtrack(list: DoubleArray, n: Int): Boolean {
10+
if (n == 1) {
11+
return abs(list[0] - 24) < EPS
12+
}
13+
for (i in 0 until n) {
14+
for (j in i + 1 until n) {
15+
val a = list[i]
16+
val b = list[j]
17+
list[j] = list[n - 1]
18+
list[i] = a + b
19+
if (backtrack(list, n - 1)) {
20+
return true
21+
}
22+
list[i] = a - b
23+
if (backtrack(list, n - 1)) {
24+
return true
25+
}
26+
list[i] = b - a
27+
if (backtrack(list, n - 1)) {
28+
return true
29+
}
30+
list[i] = a * b
31+
if (backtrack(list, n - 1)) {
32+
return true
33+
}
34+
if (Math.abs(b) > EPS) {
35+
list[i] = a / b
36+
if (backtrack(list, n - 1)) {
37+
return true
38+
}
39+
}
40+
if (Math.abs(a) > EPS) {
41+
list[i] = b / a
42+
if (backtrack(list, n - 1)) {
43+
return true
44+
}
45+
}
46+
list[i] = a
47+
list[j] = b
48+
}
49+
}
50+
return false
51+
}
52+
53+
fun judgePoint24(nums: IntArray?): Boolean {
54+
val a = Arrays.stream(nums).asDoubleStream().toArray()
55+
return backtrack(a, a.size)
56+
}
57+
58+
companion object {
59+
private const val EPS = 1e-6
60+
}
61+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
679\. 24 Game
2+
3+
Hard
4+
5+
You are given an integer array `cards` of length `4`. You have four cards, each containing a number in the range `[1, 9]`. You should arrange the numbers on these cards in a mathematical expression using the operators `['+', '-', '*', '/']` and the parentheses `'('` and `')'` to get the value 24.
6+
7+
You are restricted with the following rules:
8+
9+
* The division operator `'/'` represents real division, not integer division.
10+
* For example, `4 / (1 - 2 / 3) = 4 / (1 / 3) = 12`.
11+
* Every operation done is between two numbers. In particular, we cannot use `'-'` as a unary operator.
12+
* For example, if `cards = [1, 1, 1, 1]`, the expression `"-1 - 1 - 1 - 1"` is **not allowed**.
13+
* You cannot concatenate numbers together
14+
* For example, if `cards = [1, 2, 1, 2]`, the expression `"12 + 12"` is not valid.
15+
16+
Return `true` if you can get such expression that evaluates to `24`, and `false` otherwise.
17+
18+
**Example 1:**
19+
20+
**Input:** cards = [4,1,8,7]
21+
22+
**Output:** true
23+
24+
**Explanation:** (8-4) \* (7-1) = 24
25+
26+
**Example 2:**
27+
28+
**Input:** cards = [1,2,1,2]
29+
30+
**Output:** false
31+
32+
**Constraints:**
33+
34+
* `cards.length == 4`
35+
* `1 <= cards[i] <= 9`
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0601_0700.s0680_valid_palindrome_ii
2+
3+
// #Easy #String #Greedy #Two_Pointers #2023_02_16_Time_296_ms_(79.17%)_Space_36.7_MB_(75.00%)
4+
5+
class Solution {
6+
fun validPalindrome(s: String): Boolean {
7+
var l = 0
8+
var r = s.length - 1
9+
while (l < r) {
10+
if (s[l] != s[r]) {
11+
return isPalindrome(s.substring(l + 1, r + 1)) || isPalindrome(s.substring(l, r))
12+
}
13+
l++
14+
r--
15+
}
16+
return true
17+
}
18+
19+
private fun isPalindrome(s: String): Boolean {
20+
var l = 0
21+
var r = s.length - 1
22+
while (l < r) {
23+
if (s[l] != s[r]) {
24+
return false
25+
}
26+
l++
27+
r--
28+
}
29+
return true
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
680\. Valid Palindrome II
2+
3+
Easy
4+
5+
Given a string `s`, return `true` _if the_ `s` _can be palindrome after deleting **at most one** character from it_.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "aba"
10+
11+
**Output:** true
12+
13+
**Example 2:**
14+
15+
**Input:** s = "abca"
16+
17+
**Output:** true
18+
19+
**Explanation:** You could delete the character 'c'.
20+
21+
**Example 3:**
22+
23+
**Input:** s = "abc"
24+
25+
**Output:** false
26+
27+
**Constraints:**
28+
29+
* <code>1 <= s.length <= 10<sup>5</sup></code>
30+
* `s` consists of lowercase English letters.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0601_0700.s0682_baseball_game
2+
3+
// #Easy #Array #Stack #Simulation #2023_02_16_Time_159_ms_(100.00%)_Space_35.7_MB_(62.50%)
4+
5+
class Solution {
6+
fun calPoints(ops: Array<String>): Int {
7+
val resultList: MutableList<Int> = ArrayList(ops.size)
8+
for (op in ops) {
9+
val size = resultList.size
10+
if ("C" == op) {
11+
resultList.removeAt(size - 1)
12+
} else if ("D" == op) {
13+
resultList.add(resultList[size - 1] * 2)
14+
} else if ("+" == op) {
15+
resultList.add(resultList[size - 1] + resultList[size - 2])
16+
} else {
17+
resultList.add(op.toInt())
18+
}
19+
}
20+
return resultList.stream().reduce(0) { a: Int, b: Int -> Integer.sum(a, b) }
21+
}
22+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
682\. Baseball Game
2+
3+
Easy
4+
5+
You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.
6+
7+
At the beginning of the game, you start with an empty record. You are given a list of strings `ops`, where `ops[i]` is the <code>i<sup>th</sup></code> operation you must apply to the record and is one of the following:
8+
9+
1. An integer `x` - Record a new score of `x`.
10+
2. `"+"` - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
11+
3. `"D"` - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
12+
4. `"C"` - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.
13+
14+
Return _the sum of all the scores on the record_.
15+
16+
**Example 1:**
17+
18+
**Input:** ops = ["5","2","C","D","+"]
19+
20+
**Output:** 30
21+
22+
**Explanation:**
23+
24+
"5" - Add 5 to the record, record is now [5].
25+
"2" - Add 2 to the record, record is now [5, 2].
26+
"C" - Invalidate and remove the previous score, record is now [5].
27+
"D" - Add 2 \* 5 = 10 to the record, record is now [5, 10].
28+
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
29+
The total sum is 5 + 10 + 15 = 30.
30+
31+
**Example 2:**
32+
33+
**Input:** ops = ["5","-2","4","C","D","9","+","+"]
34+
35+
**Output:** 27
36+
37+
**Explanation:**
38+
39+
"5" - Add 5 to the record, record is now [5].
40+
"-2" - Add -2 to the record, record is now [5, -2].
41+
"4" - Add 4 to the record, record is now [5, -2, 4].
42+
"C" - Invalidate and remove the previous score, record is now [5, -2].
43+
"D" - Add 2 \* -2 = -4 to the record, record is now [5, -2, -4].
44+
"9" - Add 9 to the record, record is now [5, -2, -4, 9].
45+
"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
46+
"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
47+
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
48+
49+
**Example 3:**
50+
51+
**Input:** ops = ["1"]
52+
53+
**Output:** 1
54+
55+
**Constraints:**
56+
57+
* `1 <= ops.length <= 1000`
58+
* `ops[i]` is `"C"`, `"D"`, `"+"`, or a string representing an integer in the range <code>[-3 * 10<sup>4</sup>, 3 * 10<sup>4</sup>]</code>.
59+
* For operation `"+"`, there will always be at least two previous scores on the record.
60+
* For operations `"C"` and `"D"`, there will always be at least one previous score on the record.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0601_0700.s0678_valid_parenthesis_string
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun checkValidString() {
10+
assertThat(Solution().checkValidString("()"), equalTo(true))
11+
}
12+
13+
@Test
14+
fun checkValidString2() {
15+
assertThat(Solution().checkValidString("(*)"), equalTo(true))
16+
}
17+
18+
@Test
19+
fun checkValidString3() {
20+
assertThat(Solution().checkValidString("(*))"), equalTo(true))
21+
}
22+
}

0 commit comments

Comments
 (0)