Skip to content

Commit f85dfb0

Browse files
authored
Added tasks 65, 66, 67, 68.
1 parent aa759e1 commit f85dfb0

File tree

13 files changed

+495
-0
lines changed

13 files changed

+495
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
558558

559559
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
560560
|-|-|-|-|-|-
561+
| 0066 |[Plus One](src/main/kotlin/g0001_0100/s0066_plus_one/Solution.kt)| Easy | Top_Interview_Questions, Array, Math | 303 | 35.18
561562

562563
#### Day 4
563564

@@ -569,6 +570,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
569570

570571
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
571572
|-|-|-|-|-|-
573+
| 0067 |[Add Binary](src/main/kotlin/g0001_0100/s0067_add_binary/Solution.kt)| Easy | String, Math, Bit_Manipulation, Simulation | 327 | 32.67
572574

573575
#### Day 6
574576

@@ -1039,6 +1041,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
10391041
| 0189 |[Rotate Array](src/main/kotlin/g0101_0200/s0189_rotate_array/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers | 483 | 86.95
10401042
| 0055 |[Jump Game](src/main/kotlin/g0001_0100/s0055_jump_game/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 670 | 66.67
10411043
| 0075 |[Sort Colors](src/main/kotlin/g0001_0100/s0075_sort_colors/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 198 | 85.66
1044+
| 0066 |[Plus One](src/main/kotlin/g0001_0100/s0066_plus_one/Solution.kt)| Easy | Top_Interview_Questions, Array, Math | 303 | 35.18
10421045
| 0238 |[Product of Array Except Self](src/main/kotlin/g0201_0300/s0238_product_of_array_except_self/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum | 669 | 48.96
10431046
| 0041 |[First Missing Positive](src/main/kotlin/g0001_0100/s0041_first_missing_positive/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 345 | 100.00
10441047
| 0239 |[Sliding Window Maximum](src/main/kotlin/g0201_0300/s0239_sliding_window_maximum/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue | 1059 | 86.14
@@ -1528,6 +1531,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
15281531
| 0073 |[Set Matrix Zeroes](src/main/kotlin/g0001_0100/s0073_set_matrix_zeroes/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Udemy_2D_Arrays/Matrix | 255 | 100.00
15291532
| 0072 |[Edit Distance](src/main/kotlin/g0001_0100/s0072_edit_distance/Solution.kt)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 320 | 63.53
15301533
| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_2, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming | 127 | 97.06
1534+
| 0068 |[Add Binary](src/main/kotlin/g0001_0100/s0068_text_justification/Solution.kt)| Hard | Array, String, Simulation | 269 | 50.00
1535+
| 0067 |[Add Binary](src/main/kotlin/g0001_0100/s0067_add_binary/Solution.kt)| Easy | String, Math, Bit_Manipulation, Simulation, Programming_Skills_II_Day_5 | 327 | 32.67
1536+
| 0066 |[Plus One](src/main/kotlin/g0001_0100/s0066_plus_one/Solution.kt)| Easy | Top_Interview_Questions, Array, Math, Programming_Skills_II_Day_3, Udemy_Arrays | 303 | 35.18
1537+
| 0065 |[Valid Number](src/main/kotlin/g0001_0100/s0065_valid_number/Solution.kt)| Hard | String | 310 | 81.82
15311538
| 0064 |[Minimum Path Sum](src/main/kotlin/g0001_0100/s0064_minimum_path_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Udemy_Dynamic_Programming | 222 | 95.70
15321539
| 0063 |[Unique Paths II](src/main/kotlin/g0001_0100/s0063_unique_paths_ii/Solution.kt)| Medium | Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_15 | 187 | 84.62
15331540
| 0062 |[Unique Paths](src/main/kotlin/g0001_0100/s0062_unique_paths/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics, Algorithm_II_Day_13_Dynamic_Programming, Dynamic_Programming_I_Day_15, Level_1_Day_11_Dynamic_Programming | 209 | 49.18
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g0001_0100.s0065_valid_number
2+
3+
// #Hard #String #2022_09_23_Time_310_ms_(81.82%)_Space_35.5_MB_(87.88%)
4+
5+
class Solution {
6+
fun isNumber(s: String): Boolean {
7+
if (s.isEmpty()) {
8+
return false
9+
}
10+
var eSeen = false
11+
var numberSeen = false
12+
var decimalSeen = false
13+
for (i in 0 until s.length) {
14+
val c = s[i]
15+
if (c.code >= 48 && c.code <= 57) {
16+
numberSeen = true
17+
} else if (c == '+' || c == '-') {
18+
if (i == s.length - 1 ||
19+
i != 0 && s[i - 1] != 'e' && s[i - 1] != 'E'
20+
) {
21+
return false
22+
}
23+
} else if (c == '.') {
24+
if (eSeen || decimalSeen) {
25+
return false
26+
}
27+
decimalSeen = true
28+
} else if (c == 'e' || c == 'E') {
29+
if (i == s.length - 1 || eSeen || !numberSeen) {
30+
return false
31+
}
32+
eSeen = true
33+
} else {
34+
return false
35+
}
36+
}
37+
return numberSeen
38+
}
39+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
65\. Valid Number
2+
3+
Hard
4+
5+
A **valid number** can be split up into these components (in order):
6+
7+
1. A **decimal number** or an **integer**.
8+
2. (Optional) An `'e'` or `'E'`, followed by an **integer**.
9+
10+
A **decimal number** can be split up into these components (in order):
11+
12+
1. (Optional) A sign character (either `'+'` or `'-'`).
13+
2. One of the following formats:
14+
1. One or more digits, followed by a dot `'.'`.
15+
2. One or more digits, followed by a dot `'.'`, followed by one or more digits.
16+
3. A dot `'.'`, followed by one or more digits.
17+
18+
An **integer** can be split up into these components (in order):
19+
20+
1. (Optional) A sign character (either `'+'` or `'-'`).
21+
2. One or more digits.
22+
23+
For example, all the following are valid numbers: `["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]`, while the following are not valid numbers: `["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]`.
24+
25+
Given a string `s`, return `true` _if_ `s` _is a **valid number**_.
26+
27+
**Example 1:**
28+
29+
**Input:** s = "0"
30+
31+
**Output:** true
32+
33+
**Example 2:**
34+
35+
**Input:** s = "e"
36+
37+
**Output:** false
38+
39+
**Example 3:**
40+
41+
**Input:** s = "."
42+
43+
**Output:** false
44+
45+
**Constraints:**
46+
47+
* `1 <= s.length <= 20`
48+
* `s` consists of only English letters (both uppercase and lowercase), digits (`0-9`), plus `'+'`, minus `'-'`, or dot `'.'`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0001_0100.s0066_plus_one
2+
3+
// #Easy #Top_Interview_Questions #Array #Math #Programming_Skills_II_Day_3 #Udemy_Arrays
4+
// #2022_09_23_Time_303_ms_(35.18%)_Space_35.1_MB_(80.78%)
5+
6+
class Solution {
7+
fun plusOne(digits: IntArray): IntArray {
8+
val num = 1
9+
var carry = 0
10+
var sum: Int
11+
for (i in digits.indices.reversed()) {
12+
sum = if (i == digits.size - 1) {
13+
digits[i] + carry + num
14+
} else {
15+
digits[i] + carry
16+
}
17+
carry = sum / 10
18+
digits[i] = sum % 10
19+
}
20+
if (carry != 0) {
21+
val ans = IntArray(digits.size + 1)
22+
ans[0] = carry
23+
System.arraycopy(digits, 0, ans, 1, ans.size - 1)
24+
return ans
25+
}
26+
return digits
27+
}
28+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
66\. Plus One
2+
3+
Easy
4+
5+
You are given a **large integer** represented as an integer array `digits`, where each `digits[i]` is the <code>i<sup>th</sup></code> digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading `0`'s.
6+
7+
Increment the large integer by one and return _the resulting array of digits_.
8+
9+
**Example 1:**
10+
11+
**Input:** digits = [1,2,3]
12+
13+
**Output:** [1,2,4]
14+
15+
**Explanation:**
16+
17+
The array represents the integer 123.
18+
19+
Incrementing by one gives 123 + 1 = 124.
20+
21+
Thus, the result should be [1,2,4].
22+
23+
**Example 2:**
24+
25+
**Input:** digits = [4,3,2,1]
26+
27+
**Output:** [4,3,2,2]
28+
29+
**Explanation:**
30+
31+
The array represents the integer 4321.
32+
33+
Incrementing by one gives 4321 + 1 = 4322.
34+
35+
Thus, the result should be [4,3,2,2].
36+
37+
**Example 3:**
38+
39+
**Input:** digits = [9]
40+
41+
**Output:** [1,0]
42+
43+
**Explanation:**
44+
45+
The array represents the integer 9.
46+
47+
Incrementing by one gives 9 + 1 = 10.
48+
49+
Thus, the result should be [1,0].
50+
51+
**Constraints:**
52+
53+
* `1 <= digits.length <= 100`
54+
* `0 <= digits[i] <= 9`
55+
* `digits` does not contain any leading `0`'s.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g0001_0100.s0067_add_binary
2+
3+
// #Easy #String #Math #Bit_Manipulation #Simulation #Programming_Skills_II_Day_5
4+
// #2022_09_23_Time_327_ms_(32.67%)_Space_36_MB_(75.91%)
5+
6+
class Solution {
7+
fun addBinary(a: String, b: String): String {
8+
val aArray = a.toCharArray()
9+
val bArray = b.toCharArray()
10+
val sb = StringBuilder()
11+
var i = aArray.size - 1
12+
var j = bArray.size - 1
13+
var carry = 0
14+
while (i >= 0 || j >= 0) {
15+
val sum = (if (i >= 0) aArray[i] - '0' else 0) + (if (j >= 0) bArray[j] - '0' else 0) + carry
16+
sb.append(sum % 2)
17+
carry = sum / 2
18+
if (i >= 0) {
19+
i--
20+
}
21+
if (j >= 0) {
22+
j--
23+
}
24+
}
25+
if (carry != 0) {
26+
sb.append(carry)
27+
}
28+
return sb.reverse().toString()
29+
}
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
67\. Add Binary
2+
3+
Easy
4+
5+
Given two binary strings `a` and `b`, return _their sum as a binary string_.
6+
7+
**Example 1:**
8+
9+
**Input:** a = "11", b = "1"
10+
11+
**Output:** "100"
12+
13+
**Example 2:**
14+
15+
**Input:** a = "1010", b = "1011"
16+
17+
**Output:** "10101"
18+
19+
**Constraints:**
20+
21+
* <code>1 <= a.length, b.length <= 10<sup>4</sup></code>
22+
* `a` and `b` consist only of `'0'` or `'1'` characters.
23+
* Each string does not contain leading zeros except for the zero itself.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package g0001_0100.s0068_text_justification
2+
3+
// #Hard #Array #String #Simulation #2022_09_23_Time_269_ms_(50.00%)_Space_34.8_MB_(97.06%)
4+
5+
class Solution {
6+
fun fullJustify(words: Array<String>, maxWidth: Int): List<String> {
7+
// Trying to gauge the number of lines so the ArrayList doesn't need to resize
8+
val output: MutableList<String> = ArrayList((words.size + 1) / (1 + maxWidth / 7))
9+
// Setting StringBuilder capacity also
10+
var sb = StringBuilder(maxWidth)
11+
var lineTotal = 0
12+
var numWordsOnLine = 0
13+
var startWord = 0
14+
// looping until the 2nd last word, since we're checking words[i + 1] for
15+
// overflows
16+
for (i in 0 until words.size - 1) {
17+
lineTotal += words[i].length
18+
// tracking line length + #words
19+
numWordsOnLine++
20+
// checking if the next word causes an overflow
21+
if (lineTotal + numWordsOnLine + words[i + 1].length > maxWidth) {
22+
// if only one word fits on the line...
23+
if (numWordsOnLine == 1) {
24+
// append word
25+
sb.append(words[i])
26+
// pad right with spaces
27+
while (lineTotal++ < maxWidth) {
28+
sb.append(' ')
29+
}
30+
} else {
31+
// # of extra spaces
32+
var extraSp = (maxWidth - lineTotal) % (numWordsOnLine - 1)
33+
34+
// Creating the line
35+
for (j in startWord until startWord + numWordsOnLine - 1) {
36+
// appending the word
37+
sb.append(words[j])
38+
if (extraSp-- > 0) {
39+
// appending an extra space, if required
40+
sb.append(' ')
41+
}
42+
// appending the rest of the required spaces
43+
for (k in 0 until (maxWidth - lineTotal) / (numWordsOnLine - 1)) {
44+
sb.append(' ')
45+
}
46+
}
47+
// appending the last word of the line
48+
sb.append(words[startWord + numWordsOnLine - 1])
49+
}
50+
// adding to output list
51+
output.add(sb.toString())
52+
// reset everything for next line
53+
// keeping track of the first word for next line
54+
startWord = i + 1
55+
// resetting these to 0 for processing next line
56+
lineTotal = 0
57+
numWordsOnLine = lineTotal
58+
// need a new StringBuilder for the next line
59+
sb = StringBuilder(maxWidth)
60+
}
61+
}
62+
// handling the final line (no justification, right padded with spaces)
63+
lineTotal = 0
64+
for (i in startWord until words.size) {
65+
lineTotal += words[i].length
66+
sb.append(words[i])
67+
if (lineTotal++ < maxWidth) {
68+
sb.append(' ')
69+
}
70+
}
71+
// padding right side with spaces
72+
while (lineTotal++ < maxWidth) {
73+
sb.append(' ')
74+
}
75+
// add the final line to output list
76+
output.add(sb.toString())
77+
return output
78+
}
79+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
67\. Add Binary
2+
3+
Easy
4+
5+
Given two binary strings `a` and `b`, return _their sum as a binary string_.
6+
7+
**Example 1:**
8+
9+
**Input:** a = "11", b = "1"
10+
11+
**Output:** "100"
12+
13+
**Example 2:**
14+
15+
**Input:** a = "1010", b = "1011"
16+
17+
**Output:** "10101"
18+
19+
**Constraints:**
20+
21+
* <code>1 <= a.length, b.length <= 10<sup>4</sup></code>
22+
* `a` and `b` consist only of `'0'` or `'1'` characters.
23+
* Each string does not contain leading zeros except for the zero itself.

0 commit comments

Comments
 (0)