Skip to content

Commit 3148609

Browse files
authored
Added tasks 1154, 1155, 1156, 1157, 1158, 1160, 1161, 1162, 1163, 1169, 1170, 1171, 1172, 1175, 1177, 1178, 1179, 1184, 1185, 1186, 1187, 1189, 1190, 1191, 1192, 1195, 1200
1 parent d47ea77 commit 3148609

File tree

82 files changed

+3043
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+3043
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
16141614

16151615
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
16161616
|-|-|-|-|-|-
1617+
| 1162 |[As Far from Land as Possible](src/main/kotlin/g1101_1200/s1162_as_far_from_land_as_possible/Solution.kt)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 362 | 81.25
16171618
| 0417 |[Pacific Atlantic Water Flow](src.save/main/kotlin/g0401_0500/s0417_pacific_atlantic_water_flow/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 319 | 100.00
16181619

16191620
#### Day 5 Matrix Related Problems
@@ -1738,6 +1739,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17381739

17391740
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
17401741
|-|-|-|-|-|-
1742+
| 1158 |[Market Analysis I](src/main/kotlin/g1101_1200/s1158_market_analysis_i/script.sql)| Medium | Database | 2470 | 44.76
17411743

17421744
#### Day 10 Where
17431745

@@ -1749,6 +1751,33 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17491751

17501752
| # | Title | Difficulty | Tag | Time, ms | Time, %
17511753
|------|----------------|-------------|-------------|----------|---------
1754+
| 1200 |[Minimum Absolute Difference](src/main/kotlin/g1101_1200/s1200_minimum_absolute_difference/Solution.kt)| Easy | Array, Sorting | 507 | 75.00
1755+
| 1195 |[Fizz Buzz Multithreaded](src/main/kotlin/g1101_1200/s1195_fizz_buzz_multithreaded/FizzBuzz.kt)| Medium || |
1756+
| 1192 |[Critical Connections in a Network](src/main/kotlin/g1101_1200/s1192_critical_connections_in_a_network/Solution.kt)| Hard | Depth_First_Search, Graph, Biconnected_Component | 1696 | 60.00
1757+
| 1191 |[K-Concatenation Maximum Sum](src/main/kotlin/g1101_1200/s1191_k_concatenation_maximum_sum/Solution.kt)| Medium | Array, Dynamic_Programming | 389 | 100.00
1758+
| 1190 |[Reverse Substrings Between Each Pair of Parentheses](src/main/kotlin/g1101_1200/s1190_reverse_substrings_between_each_pair_of_parentheses/Solution.kt)| Medium | String, Stack | 153 | 42.86
1759+
| 1189 |[Maximum Number of Balloons](src/main/kotlin/g1101_1200/s1189_maximum_number_of_balloons/Solution.kt)| Easy | String, Hash_Table, Counting | 136 | 98.31
1760+
| 1187 |[Make Array Strictly Increasing](src/main/kotlin/g1101_1200/s1187_make_array_strictly_increasing/Solution.kt)| Hard | Array, Dynamic_Programming, Binary_Search | 308 | 100.00
1761+
| 1186 |[Maximum Subarray Sum with One Deletion](src/main/kotlin/g1101_1200/s1186_maximum_subarray_sum_with_one_deletion/Solution.kt)| Medium | Array, Dynamic_Programming | 304 | 50.00
1762+
| 1185 |[Day of the Week](src/main/kotlin/g1101_1200/s1185_day_of_the_week/Solution.kt)| Easy | Math | 127 | 50.00
1763+
| 1184 |[Distance Between Bus Stops](src/main/kotlin/g1101_1200/s1184_distance_between_bus_stops/Solution.kt)| Easy | Array | 164 | 57.14
1764+
| 1179 |[Reformat Department Table](src/main/kotlin/g1101_1200/s1179_reformat_department_table/script.sql)| Easy | Database | 815 | 90.95
1765+
| 1178 |[Number of Valid Words for Each Puzzle](src/main/kotlin/g1101_1200/s1178_number_of_valid_words_for_each_puzzle/Solution.kt)| Hard | Array, String, Hash_Table, Bit_Manipulation, Trie | 675 | 100.00
1766+
| 1177 |[Can Make Palindrome from Substring](src/main/kotlin/g1101_1200/s1177_can_make_palindrome_from_substring/Solution.kt)| Medium | String, Hash_Table, Bit_Manipulation, Prefix_Sum | 937 | 100.00
1767+
| 1175 |[Prime Arrangements](src/main/kotlin/g1101_1200/s1175_prime_arrangements/Solution.kt)| Easy | Math | 129 | 50.00
1768+
| 1172 |[Dinner Plate Stacks](src/main/kotlin/g1101_1200/s1172_dinner_plate_stacks/DinnerPlates.kt)| Hard | Hash_Table, Stack, Design, Heap_Priority_Queue | 1160 | 50.00
1769+
| 1171 |[Remove Zero Sum Consecutive Nodes from Linked List](src/main/kotlin/g1101_1200/s1171_remove_zero_sum_consecutive_nodes_from_linked_list/Solution.kt)| Medium | Hash_Table, Linked_List | 194 | 50.00
1770+
| 1170 |[Compare Strings by Frequency of the Smallest Character](src/main/kotlin/g1101_1200/s1170_compare_strings_by_frequency_of_the_smallest_character/Solution.kt)| Medium | Array, String, Hash_Table, Sorting, Binary_Search | 221 | 50.00
1771+
| 1169 |[Invalid Transactions](src/main/kotlin/g1101_1200/s1169_invalid_transactions/Solution.kt)| Medium | Array, String, Hash_Table, Sorting | 362 | 57.14
1772+
| 1163 |[Last Substring in Lexicographical Order](src/main/kotlin/g1101_1200/s1163_last_substring_in_lexicographical_order/Solution.kt)| Hard | String, Two_Pointers | 254 | 100.00
1773+
| 1162 |[As Far from Land as Possible](src/main/kotlin/g1101_1200/s1162_as_far_from_land_as_possible/Solution.kt)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix, Graph_Theory_I_Day_4_Matrix_Related_Problems | 362 | 81.25
1774+
| 1161 |[Maximum Level Sum of a Binary Tree](src/main/kotlin/g1101_1200/s1161_maximum_level_sum_of_a_binary_tree/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 445 | 87.50
1775+
| 1160 |[Find Words That Can Be Formed by Characters](src/main/kotlin/g1101_1200/s1160_find_words_that_can_be_formed_by_characters/Solution.kt)| Easy | Array, String, Hash_Table | 288 | 72.22
1776+
| 1158 |[Market Analysis I](src/main/kotlin/g1101_1200/s1158_market_analysis_i/script.sql)| Medium | Database, SQL_I_Day_9_Control_of_Flow | 2470 | 44.76
1777+
| 1157 |[Online Majority Element In Subarray](src/main/kotlin/g1101_1200/s1157_online_majority_element_in_subarray/MajorityChecker.kt)| Hard | Array, Binary_Search, Design, Segment_Tree, Binary_Indexed_Tree | 953 | 100.00
1778+
| 1156 |[Swap For Longest Repeated Character Substring](src/main/kotlin/g1101_1200/s1156_swap_for_longest_repeated_character_substring/Solution.kt)| Medium | String, Sliding_Window | 195 | 20.00
1779+
| 1155 |[Number of Dice Rolls With Target Sum](src/main/kotlin/g1101_1200/s1155_number_of_dice_rolls_with_target_sum/Solution.kt)| Medium | Dynamic_Programming | 158 | 80.95
1780+
| 1154 |[Day of the Year](src/main/kotlin/g1101_1200/s1154_day_of_the_year/Solution.kt)| Easy | String, Math | 317 | 70.00
17521781
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
17531782
| 1036 |[Escape a Large Maze](src/main/kotlin/g1001_1100/s1036_escape_a_large_maze/Solution.kt)| Hard | Array, Hash_Table, Depth_First_Search, Breadth_First_Search | 387 | 100.00
17541783
| 1035 |[Uncrossed Lines](src/main/kotlin/g1001_1100/s1035_uncrossed_lines/Solution.kt)| Medium | Array, Dynamic_Programming | 162 | 93.33
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1101_1200.s1154_day_of_the_year
2+
3+
// #Easy #String #Math #2023_05_25_Time_317_ms_(70.00%)_Space_59.3_MB_(20.00%)
4+
5+
class Solution {
6+
fun dayOfYear(date: String): Int {
7+
val monthDays = intArrayOf(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
8+
val dateArr = date.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
9+
val year = dateArr[0].toInt()
10+
val month = dateArr[1].toInt()
11+
val day = dateArr[2].toInt()
12+
var dayCount = 0
13+
val leapYear = year % 4 == 0 && year % 100 != 0 || year % 400 == 0
14+
for (i in 1 until month) {
15+
dayCount += monthDays[i]
16+
}
17+
dayCount += day
18+
if (leapYear && month > 2) {
19+
dayCount++
20+
}
21+
return dayCount
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
1154\. Day of the Year
2+
3+
Easy
4+
5+
Given a string `date` representing a [Gregorian calendar](https://en.wikipedia.org/wiki/Gregorian_calendar) date formatted as `YYYY-MM-DD`, return _the day number of the year_.
6+
7+
**Example 1:**
8+
9+
**Input:** date = "2019-01-09"
10+
11+
**Output:** 9
12+
13+
**Explanation:** Given date is the 9th day of the year in 2019.
14+
15+
**Example 2:**
16+
17+
**Input:** date = "2019-02-10"
18+
19+
**Output:** 41
20+
21+
**Constraints:**
22+
23+
* `date.length == 10`
24+
* `date[4] == date[7] == '-'`, and all other `date[i]`'s are digits
25+
* `date` represents a calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>th</sup>, 2019.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g1101_1200.s1155_number_of_dice_rolls_with_target_sum
2+
3+
// #Medium #Dynamic_Programming #2023_05_25_Time_158_ms_(80.95%)_Space_37.7_MB_(23.81%)
4+
5+
class Solution {
6+
private var memo: Array<IntArray> = arrayOf()
7+
8+
private var k = 0
9+
private fun dp(diceLeft: Int, targetLeft: Int): Int {
10+
if (diceLeft == 0) {
11+
return if (targetLeft == 0) {
12+
1
13+
} else 0
14+
}
15+
if (memo[diceLeft][targetLeft] == -1) {
16+
var res = 0
17+
for (i in 1..Math.min(k, targetLeft)) {
18+
res += dp(diceLeft - 1, targetLeft - i)
19+
val modulo = 1000000007
20+
res %= modulo
21+
}
22+
memo[diceLeft][targetLeft] = res
23+
}
24+
return memo[diceLeft][targetLeft]
25+
}
26+
27+
fun numRollsToTarget(n: Int, k: Int, target: Int): Int {
28+
this.k = k
29+
memo = Array(n + 1) { IntArray(target + 1) }
30+
for (i in memo) {
31+
i.fill(-1)
32+
}
33+
return dp(n, target)
34+
}
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1155\. Number of Dice Rolls With Target Sum
2+
3+
Medium
4+
5+
You have `n` dice and each die has `k` faces numbered from `1` to `k`.
6+
7+
Given three integers `n`, `k`, and `target`, return _the number of possible ways (out of the_ <code>k<sup>n</sup></code> _total ways)_ _to roll the dice so the sum of the face-up numbers equals_ `target`. Since the answer may be too large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 1, k = 6, target = 3
12+
13+
**Output:** 1
14+
15+
**Explanation:** You throw one die with 6 faces.
16+
17+
There is only one way to get a sum of 3.
18+
19+
**Example 2:**
20+
21+
**Input:** n = 2, k = 6, target = 7
22+
23+
**Output:** 6
24+
25+
**Explanation:** You throw two dice, each with 6 faces.
26+
27+
There are 6 ways to get a sum of 7: 1+6, 2+5, 3+4, 4+3, 5+2, 6+1.
28+
29+
**Example 3:**
30+
31+
**Input:** n = 30, k = 30, target = 500
32+
33+
**Output:** 222616187
34+
35+
**Explanation:** The answer must be returned modulo 10<sup>9</sup> + 7.
36+
37+
**Constraints:**
38+
39+
* `1 <= n, k <= 30`
40+
* `1 <= target <= 1000`
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package g1101_1200.s1156_swap_for_longest_repeated_character_substring
2+
3+
// #Medium #String #Sliding_Window #2023_05_25_Time_195_ms_(20.00%)_Space_38.9_MB_(20.00%)
4+
5+
class Solution {
6+
private class Pair(var character: Char, var count: Int)
7+
8+
fun maxRepOpt1(text: String): Int {
9+
val pairs: MutableList<Pair> = ArrayList()
10+
val map: MutableMap<Char, Int> = HashMap()
11+
// collect counts for each char-block
12+
var i = 0
13+
while (i < text.length) {
14+
val c = text[i]
15+
var count = 0
16+
while (i < text.length && text[i] == c) {
17+
count++
18+
i++
19+
}
20+
pairs.add(Pair(c, count))
21+
map[c] = map.getOrDefault(c, 0) + count
22+
}
23+
var max = 0
24+
// case 1, swap 1 item to the boundary of a consecutive cha-block to achieve possible max
25+
// length
26+
// we need total count to make sure whether a swap is possible!
27+
for (p in pairs) {
28+
val totalCount = map.getValue(p.character)
29+
max = if (totalCount > p.count) {
30+
Math.max(max, p.count + 1)
31+
} else {
32+
Math.max(max, p.count)
33+
}
34+
}
35+
// case 2, find xxxxYxxxxx pattern
36+
// we need total count to make sure whether a swap is possible!
37+
for (j in 1 until pairs.size - 1) {
38+
if (pairs[j - 1].character == pairs[j + 1].character &&
39+
pairs[j].count == 1
40+
) {
41+
val totalCount = map.getValue(pairs[j - 1].character)
42+
val groupSum = pairs[j - 1].count + pairs[j + 1].count
43+
max = if (totalCount > groupSum) {
44+
Math.max(max, groupSum + 1)
45+
} else {
46+
Math.max(max, groupSum)
47+
}
48+
}
49+
}
50+
return max
51+
}
52+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1156\. Swap For Longest Repeated Character Substring
2+
3+
Medium
4+
5+
You are given a string `text`. You can swap two of the characters in the `text`.
6+
7+
Return _the length of the longest substring with repeated characters_.
8+
9+
**Example 1:**
10+
11+
**Input:** text = "ababa"
12+
13+
**Output:** 3
14+
15+
**Explanation:** We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa" with length 3.
16+
17+
**Example 2:**
18+
19+
**Input:** text = "aaabaaa"
20+
21+
**Output:** 6
22+
23+
**Explanation:** Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa" with length 6.
24+
25+
**Example 3:**
26+
27+
**Input:** text = "aaaaa"
28+
29+
**Output:** 5
30+
31+
**Explanation:** No need to swap, longest repeated character substring is "aaaaa" with length is 5.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= text.length <= 2 * 10<sup>4</sup></code>
36+
* `text` consist of lowercase English characters only.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package g1101_1200.s1157_online_majority_element_in_subarray
2+
3+
// #Hard #Array #Binary_Search #Design #Segment_Tree #Binary_Indexed_Tree
4+
// #2023_05_25_Time_953_ms_(100.00%)_Space_139_MB_(100.00%)
5+
6+
class MajorityChecker(arr: IntArray) {
7+
private val valToInd: MutableMap<Int, MutableList<Int>>
8+
private val bitCount: Array<IntArray>
9+
10+
init {
11+
valToInd = HashMap()
12+
bitCount = Array(arr.size + 1) { IntArray(NUM_OF_BITS) }
13+
for (i in arr.indices) {
14+
var `val` = arr[i]
15+
val indList = valToInd.computeIfAbsent(`val`) { _: Int? -> ArrayList() }
16+
indList.add(i)
17+
for (j in 0 until NUM_OF_BITS) {
18+
bitCount[i + 1][j] = bitCount[i][j] + (`val` and 1)
19+
`val` = `val` shr 1
20+
}
21+
}
22+
}
23+
24+
fun query(left: Int, right: Int, threshold: Int): Int {
25+
var candidateVal = 0
26+
for (i in NUM_OF_BITS - 1 downTo 0) {
27+
val curBit = if (bitCount[right + 1][i] - bitCount[left][i] >= threshold) 1 else 0
28+
candidateVal = (candidateVal shl 1) + curBit
29+
}
30+
val indList: List<Int>? = valToInd[candidateVal]
31+
if (indList == null || indList.size < threshold) {
32+
return -1
33+
}
34+
var indOfLeft = indList.binarySearch(left)
35+
if (indOfLeft < 0) {
36+
indOfLeft = -indOfLeft - 1
37+
}
38+
var indOfRight = indList.binarySearch(right)
39+
if (indOfRight < 0) {
40+
indOfRight = -indOfRight - 2
41+
}
42+
return if (indOfRight - indOfLeft + 1 >= threshold) {
43+
candidateVal
44+
} else {
45+
-1
46+
}
47+
}
48+
49+
companion object {
50+
private const val NUM_OF_BITS = 15
51+
}
52+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
1157\. Online Majority Element In Subarray
2+
3+
Hard
4+
5+
Design a data structure that efficiently finds the **majority element** of a given subarray.
6+
7+
The **majority element** of a subarray is an element that occurs `threshold` times or more in the subarray.
8+
9+
Implementing the `MajorityChecker` class:
10+
11+
* `MajorityChecker(int[] arr)` Initializes the instance of the class with the given array `arr`.
12+
* `int query(int left, int right, int threshold)` returns the element in the subarray `arr[left...right]` that occurs at least `threshold` times, or `-1` if no such element exists.
13+
14+
**Example 1:**
15+
16+
**Input** ["MajorityChecker", "query", "query", "query"] [[[1, 1, 2, 2, 1, 1]], [0, 5, 4], [0, 3, 3], [2, 3, 2]]
17+
18+
**Output:** [null, 1, -1, 2]
19+
20+
**Explanation:**
21+
22+
MajorityChecker majorityChecker = new MajorityChecker([1, 1, 2, 2, 1, 1]);
23+
majorityChecker.query(0, 5, 4); // return 1
24+
majorityChecker.query(0, 3, 3); // return -1
25+
majorityChecker.query(2, 3, 2); // return 2
26+
27+
**Constraints:**
28+
29+
* <code>1 <= arr.length <= 2 * 10<sup>4</sup></code>
30+
* <code>1 <= arr[i] <= 2 * 10<sup>4</sup></code>
31+
* `0 <= left <= right < arr.length`
32+
* `threshold <= right - left + 1`
33+
* `2 * threshold > right - left + 1`
34+
* At most <code>10<sup>4</sup></code> calls will be made to `query`.

0 commit comments

Comments
 (0)