Skip to content

Commit 0344b04

Browse files
authored
Added tasks 553, 554, 556, 557
1 parent aca1c92 commit 0344b04

File tree

14 files changed

+391
-0
lines changed

14 files changed

+391
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
186186
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
187187
|-|-|-|-|-|-
188188
| 0503 |[Next Greater Element II](src/main/kotlin/g0501_0600/s0503_next_greater_element_ii/Solution.kt)| Medium | Array, Stack, Monotonic_Stack | 331 | 92.68
189+
| 0556 |[Next Greater Element III](src/main/kotlin/g0501_0600/s0556_next_greater_element_iii/Solution.kt)| Medium | String, Math, Two_Pointers | 137 | 80.00
189190

190191
#### Day 11
191192

@@ -1098,6 +1099,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
10981099
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10991100
|-|-|-|-|-|-
11001101
| 0344 |[Reverse String](src.save/main/kotlin/g0301_0400/s0344_reverse_string/Solution.kt)| Easy | Top_Interview_Questions, String, Two_Pointers, Recursion | 445 | 69.75
1102+
| 0557 |[Reverse Words in a String III](src/main/kotlin/g0501_0600/s0557_reverse_words_in_a_string_iii/Solution.kt)| Easy | String, Two_Pointers | 215 | 98.10
11011103

11021104
#### Day 5 Two Pointers
11031105

@@ -1654,6 +1656,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16541656
| 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
16551657
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
16561658
| 0560 |[Subarray Sum Equals K](src/main/kotlin/g0501_0600/s0560_subarray_sum_equals_k/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array | 692 | 53.27
1659+
| 0557 |[Reverse Words in a String III](src/main/kotlin/g0501_0600/s0557_reverse_words_in_a_string_iii/Solution.kt)| Easy | String, Two_Pointers, Algorithm_I_Day_4_Two_Pointers | 215 | 98.10
1660+
| 0556 |[Next Greater Element III](src/main/kotlin/g0501_0600/s0556_next_greater_element_iii/Solution.kt)| Medium | String, Math, Two_Pointers, Programming_Skills_II_Day_10 | 137 | 80.00
1661+
| 0554 |[Brick Wall](src/main/kotlin/g0501_0600/s0554_brick_wall/Solution.kt)| Medium | Array, Hash_Table | 307 | 100.00
1662+
| 0553 |[Optimal Division](src/main/kotlin/g0501_0600/s0553_optimal_division/Solution.kt)| Medium | Array, Dynamic_Programming, Math | 154 | 100.00
16571663
| 0552 |[Student Attendance Record II](src/main/kotlin/g0501_0600/s0552_student_attendance_record_ii/Solution.kt)| Hard | Dynamic_Programming | 151 | 100.00
16581664
| 0551 |[Student Attendance Record I](src/main/kotlin/g0501_0600/s0551_student_attendance_record_i/Solution.kt)| Easy | String | 151 | 95.00
16591665
| 0547 |[Number of Provinces](src/main/kotlin/g0501_0600/s0547_number_of_provinces/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_8_Standard_Traversal, Level_2_Day_19_Union_Find | 229 | 79.73

src/main/kotlin/g0501_0600/s0552_student_attendance_record_ii/Solution.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package g0501_0600.s0552_student_attendance_record_ii
44

55
import java.util.Arrays
66

7+
@Suppress("NAME_SHADOWING")
78
class Solution {
89
fun checkRecord(n: Int): Int {
910
if (n == 0 || n == 1 || n == 2) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0501_0600.s0553_optimal_division
2+
3+
// #Medium #Array #Dynamic_Programming #Math #2023_01_17_Time_154_ms_(100.00%)_Space_34_MB_(66.67%)
4+
5+
class Solution {
6+
fun optimalDivision(nums: IntArray): String {
7+
val sb = StringBuilder()
8+
if (nums.size == 1) {
9+
sb.append(nums[0])
10+
return sb.toString()
11+
}
12+
if (nums.size == 2) {
13+
sb.append(nums[0])
14+
sb.append("/")
15+
sb.append(nums[1])
16+
return sb.toString()
17+
}
18+
sb.append(nums[0])
19+
sb.append("/")
20+
sb.append("(")
21+
for (i in 1 until nums.size - 1) {
22+
sb.append(nums[i])
23+
sb.append('/')
24+
}
25+
sb.append(nums[nums.size - 1])
26+
sb.append(")")
27+
return sb.toString()
28+
}
29+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
553\. Optimal Division
2+
3+
Medium
4+
5+
You are given an integer array `nums`. The adjacent integers in `nums` will perform the float division.
6+
7+
* For example, for `nums = [2,3,4]`, we will evaluate the expression `"2/3/4"`.
8+
9+
However, you can add any number of parenthesis at any position to change the priority of operations. You want to add these parentheses such the value of the expression after the evaluation is maximum.
10+
11+
Return _the corresponding expression that has the maximum value in string format_.
12+
13+
**Note:** your expression should not contain redundant parenthesis.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [1000,100,10,2]
18+
19+
**Output:** "1000/(100/10/2)"
20+
21+
**Explanation:**
22+
23+
1000/(100/10/2) = 1000/((100/10)/2) = 200
24+
However, the bold parenthesis in "1000/((100/10)/2)" are redundant, since they don't influence the operation priority.
25+
So you should return "1000/(100/10/2)".
26+
Other cases:
27+
1000/(100/10)/2 = 50
28+
1000/(100/(10/2)) = 50
29+
1000/100/10/2 = 0.5
30+
1000/100/(10/2) = 2
31+
32+
**Example 2:**
33+
34+
**Input:** nums = [2,3,4]
35+
36+
**Output:** "2/(3/4)"
37+
38+
**Example 3:**
39+
40+
**Input:** nums = [2]
41+
42+
**Output:** "2"
43+
44+
**Constraints:**
45+
46+
* `1 <= nums.length <= 10`
47+
* `2 <= nums[i] <= 1000`
48+
* There is only one optimal division for the given iput.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0501_0600.s0554_brick_wall
2+
3+
// #Medium #Array #Hash_Table #2023_01_17_Time_307_ms_(100.00%)_Space_47.5_MB_(90.00%)
4+
5+
class Solution {
6+
fun leastBricks(wall: List<List<Int>>): Int {
7+
val gapMap = mutableMapOf<Int, Int>()
8+
9+
wall.forEach { row ->
10+
var pos = 0
11+
for (brickId in 0 until row.lastIndex) {
12+
pos += row[brickId]
13+
gapMap[pos] = gapMap.getOrDefault(pos, 0) + 1
14+
}
15+
}
16+
var value = 0
17+
18+
if (gapMap.size > 0) {
19+
value = gapMap.values.max()
20+
}
21+
return wall.size - value
22+
}
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
554\. Brick Wall
2+
3+
Medium
4+
5+
There is a rectangular brick wall in front of you with `n` rows of bricks. The <code>i<sup>th</sup></code> row has some number of bricks each of the same height (i.e., one unit) but they can be of different widths. The total width of each row is the same.
6+
7+
Draw a vertical line from the top to the bottom and cross the least bricks. If your line goes through the edge of a brick, then the brick is not considered as crossed. You cannot draw a line just along one of the two vertical edges of the wall, in which case the line will obviously cross no bricks.
8+
9+
Given the 2D array `wall` that contains the information about the wall, return _the minimum number of crossed bricks after drawing such a vertical line_.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/04/24/cutwall-grid.jpg)
14+
15+
**Input:** wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
16+
17+
**Output:** 2
18+
19+
**Example 2:**
20+
21+
**Input:** wall = [[1],[1],[1]]
22+
23+
**Output:** 3
24+
25+
**Constraints:**
26+
27+
* `n == wall.length`
28+
* <code>1 <= n <= 10<sup>4</sup></code>
29+
* <code>1 <= wall[i].length <= 10<sup>4</sup></code>
30+
* <code>1 <= sum(wall[i].length) <= 2 * 10<sup>4</sup></code>
31+
* `sum(wall[i])` is the same for each row `i`.
32+
* <code>1 <= wall[i][j] <= 2<sup>31</sup> - 1</code>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package g0501_0600.s0556_next_greater_element_iii
2+
3+
// #Medium #String #Math #Two_Pointers #Programming_Skills_II_Day_10
4+
// #2023_01_20_Time_137_ms_(80.00%)_Space_32.6_MB_(60.00%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
/*
9+
- What this problem wants is finding the next permutation of n
10+
- Steps to find the next permuation:
11+
find largest index k such that inp[k] < inp[k+1];
12+
if k == -1: return -1
13+
else:
14+
look for largest index l such that inp[l] > inp[k]
15+
swap the two index
16+
reverse from k+1 to n.length
17+
*/
18+
fun nextGreaterElement(n: Int): Int {
19+
val inp = n.toString().toCharArray()
20+
// Find k
21+
var k = -1
22+
for (i in inp.size - 2 downTo 0) {
23+
if (inp[i] < inp[i + 1]) {
24+
k = i
25+
break
26+
}
27+
}
28+
if (k == -1) {
29+
return -1
30+
}
31+
// Find l
32+
var largerIdx = inp.size - 1
33+
for (i in inp.indices.reversed()) {
34+
if (inp[i] > inp[k]) {
35+
largerIdx = i
36+
break
37+
}
38+
}
39+
swap(inp, k, largerIdx)
40+
reverse(inp, k + 1, inp.size - 1)
41+
// Build result
42+
var ret = 0
43+
for (c in inp) {
44+
val digit = c.code - '0'.code
45+
// Handle the case if ret > Integer.MAX_VALUE - This idea is borrowed from problem 8.
46+
// String to Integer (atoi)
47+
if (ret > Int.MAX_VALUE / 10 || ret == Int.MAX_VALUE / 10 && digit > Int.MAX_VALUE % 10) {
48+
return -1
49+
}
50+
ret = ret * 10 + (c.code - '0'.code)
51+
}
52+
return ret
53+
}
54+
55+
private fun swap(inp: CharArray, i: Int, j: Int) {
56+
val temp = inp[i]
57+
inp[i] = inp[j]
58+
inp[j] = temp
59+
}
60+
61+
private fun reverse(inp: CharArray, start: Int, end: Int) {
62+
var start = start
63+
var end = end
64+
while (start < end) {
65+
val temp = inp[start]
66+
inp[start] = inp[end]
67+
inp[end] = temp
68+
start++
69+
end--
70+
}
71+
}
72+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
556\. Next Greater Element III
2+
3+
Medium
4+
5+
Given a positive integer `n`, find _the smallest integer which has exactly the same digits existing in the integer_ `n` _and is greater in value than_ `n`. If no such positive integer exists, return `-1`.
6+
7+
**Note** that the returned integer should fit in **32-bit integer**, if there is a valid answer but it does not fit in **32-bit integer**, return `-1`.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 12
12+
13+
**Output:** 21
14+
15+
**Example 2:**
16+
17+
**Input:** n = 21
18+
19+
**Output:** -1
20+
21+
**Constraints:**
22+
23+
* <code>1 <= n <= 2<sup>31</sup> - 1</code>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0501_0600.s0557_reverse_words_in_a_string_iii
2+
3+
// #Easy #String #Two_Pointers #Algorithm_I_Day_4_Two_Pointers
4+
// #2023_01_20_Time_215_ms_(98.10%)_Space_36.1_MB_(94.29%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
fun reverseWords(s: String): String {
9+
var l: Int
10+
var r = 0
11+
val len = s.length
12+
val ch = s.toCharArray()
13+
for (i in 0..len) {
14+
if (i == len || ch[i] == ' ') {
15+
l = r
16+
r = i
17+
reverse(ch, l, r - 1)
18+
r++
19+
}
20+
}
21+
return String(ch)
22+
}
23+
24+
private fun reverse(s: CharArray, l: Int, r: Int) {
25+
var l = l
26+
var r = r
27+
var c: Char
28+
while (r > l) {
29+
c = s[l]
30+
s[l++] = s[r]
31+
s[r--] = c
32+
}
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
557\. Reverse Words in a String III
2+
3+
Easy
4+
5+
Given a string `s`, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "Let's take LeetCode contest"
10+
11+
**Output:** "s'teL ekat edoCteeL tsetnoc"
12+
13+
**Example 2:**
14+
15+
**Input:** s = "God Ding"
16+
17+
**Output:** "doG gniD"
18+
19+
**Constraints:**
20+
21+
* <code>1 <= s.length <= 5 * 10<sup>4</sup></code>
22+
* `s` contains printable **ASCII** characters.
23+
* `s` does not contain any leading or trailing spaces.
24+
* There is **at least one** word in `s`.
25+
* All the words in `s` are separated by a single space.

0 commit comments

Comments
 (0)