Skip to content

Commit fee3f27

Browse files
authored
Added tasks 1952-2000
1 parent 56755cd commit fee3f27

File tree

118 files changed

+4397
-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.

118 files changed

+4397
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
16741674

16751675
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
16761676
|-|-|-|-|-|-
1677+
| 1965 |[Employees With Missing Information](src/main/kotlin/g1901_2000/s1965_employees_with_missing_information/script.sql)| Easy | Database | 1355 | 23.96
16771678
| 1795 |[Rearrange Products Table](src.save/main/kotlin/g1701_1800/s1795_rearrange_products_table/script.sql)| Easy | Database | 1027 | 67.57
16781679
| 0608 |[Tree Node](src.save/main/kotlin/g0601_0700/s0608_tree_node/script.sql)| Medium | LeetCode_Curated_SQL_70, Database | 794 | 48.38
16791680
| 0176 |[Second Highest Salary](src.save/main/kotlin/g0101_0200/s0176_second_highest_salary/script.sql)| Medium | Database | 219 | 92.54
@@ -1837,6 +1838,45 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
18371838

18381839
| # | Title | Difficulty | Tag | Time, ms | Time, %
18391840
|------|----------------|-------------|-------------|----------|---------
1841+
| 2000 |[Reverse Prefix of Word](src/main/kotlin/g1901_2000/s2000_reverse_prefix_of_word/Solution.kt)| Easy | String, Two_Pointers | 164 | 31.25
1842+
| 1998 |[GCD Sort of an Array](src/main/kotlin/g1901_2000/s1998_gcd_sort_of_an_array/Solution.kt)| Hard | Array, Math, Sorting, Union_Find | 437 | 100.00
1843+
| 1997 |[First Day Where You Have Been in All the Rooms](src/main/kotlin/g1901_2000/s1997_first_day_where_you_have_been_in_all_the_rooms/Solution.kt)| Medium | Array, Dynamic_Programming | 572 | 100.00
1844+
| 1996 |[The Number of Weak Characters in the Game](src/main/kotlin/g1901_2000/s1996_the_number_of_weak_characters_in_the_game/Solution.kt)| Medium | Array, Sorting, Greedy, Stack, Monotonic_Stack | 1234 | 100.00
1845+
| 1995 |[Count Special Quadruplets](src/main/kotlin/g1901_2000/s1995_count_special_quadruplets/Solution.kt)| Easy | Array, Enumeration | 146 | 100.00
1846+
| 1994 |[The Number of Good Subsets](src/main/kotlin/g1901_2000/s1994_the_number_of_good_subsets/Solution.kt)| Hard | Array, Dynamic_Programming, Math, Bit_Manipulation, Bitmask | 737 | 100.00
1847+
| 1993 |[Operations on Tree](src/main/kotlin/g1901_2000/s1993_operations_on_tree/LockingTree.kt)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Tree, Design | 1117 | 100.00
1848+
| 1992 |[Find All Groups of Farmland](src/main/kotlin/g1901_2000/s1992_find_all_groups_of_farmland/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 768 | 100.00
1849+
| 1991 |[Find the Middle Index in Array](src/main/kotlin/g1901_2000/s1991_find_the_middle_index_in_array/Solution.kt)| Easy | Array, Prefix_Sum | 142 | 100.00
1850+
| 1987 |[Number of Unique Good Subsequences](src/main/kotlin/g1901_2000/s1987_number_of_unique_good_subsequences/Solution.kt)| Hard | String, Dynamic_Programming | 248 | 100.00
1851+
| 1986 |[Minimum Number of Work Sessions to Finish the Tasks](src/main/kotlin/g1901_2000/s1986_minimum_number_of_work_sessions_to_finish_the_tasks/Solution.kt)| Medium | Array, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask | 153 | 100.00
1852+
| 1985 |[Find the Kth Largest Integer in the Array](src/main/kotlin/g1901_2000/s1985_find_the_kth_largest_integer_in_the_array/Solution.kt)| Medium | Array, String, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Quickselect | 397 | 100.00
1853+
| 1984 |[Minimum Difference Between Highest and Lowest of K Scores](src/main/kotlin/g1901_2000/s1984_minimum_difference_between_highest_and_lowest_of_k_scores/Solution.kt)| Easy | Array, Sorting, Sliding_Window | 227 | 83.33
1854+
| 1982 |[Find Array Given Subset Sums](src/main/kotlin/g1901_2000/s1982_find_array_given_subset_sums/Solution.kt)| Hard | Array, Divide_and_Conquer | 640 | 100.00
1855+
| 1981 |[Minimize the Difference Between Target and Chosen Elements](src/main/kotlin/g1901_2000/s1981_minimize_the_difference_between_target_and_chosen_elements/Solution.kt)| Medium | Array, Dynamic_Programming, Matrix | 588 | 100.00
1856+
| 1980 |[Find Unique Binary String](src/main/kotlin/g1901_2000/s1980_find_unique_binary_string/Solution.kt)| Medium | Array, String, Backtracking | 186 | 50.00
1857+
| 1979 |[Find Greatest Common Divisor of Array](src/main/kotlin/g1901_2000/s1979_find_greatest_common_divisor_of_array/Solution.kt)| Easy | Array, Math, Number_Theory | 172 | 100.00
1858+
| 1977 |[Number of Ways to Separate Numbers](src/main/kotlin/g1901_2000/s1977_number_of_ways_to_separate_numbers/Solution.kt)| Hard | String, Dynamic_Programming, Suffix_Array | 199 | 100.00
1859+
| 1976 |[Number of Ways to Arrive at Destination](src/main/kotlin/g1901_2000/s1976_number_of_ways_to_arrive_at_destination/Solution.kt)| Medium | Dynamic_Programming, Graph, Topological_Sort, Shortest_Path | 282 | 100.00
1860+
| 1975 |[Maximum Matrix Sum](src/main/kotlin/g1901_2000/s1975_maximum_matrix_sum/Solution.kt)| Medium | Array, Greedy, Matrix | 535 | 100.00
1861+
| 1974 |[Minimum Time to Type Word Using Special Typewriter](src/main/kotlin/g1901_2000/s1974_minimum_time_to_type_word_using_special_typewriter/Solution.kt)| Easy | String, Greedy | 139 | 71.43
1862+
| 1971 |[Find if Path Exists in Graph](src/main/kotlin/g1901_2000/s1971_find_if_path_exists_in_graph/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Graph | 949 | 86.89
1863+
| 1970 |[Last Day Where You Can Still Cross](src/main/kotlin/g1901_2000/s1970_last_day_where_you_can_still_cross/Solution.kt)| Hard | Array, Depth_First_Search, Breadth_First_Search, Binary_Search, Matrix, Union_Find | 703 | 100.00
1864+
| 1969 |[Minimum Non-Zero Product of the Array Elements](src/main/kotlin/g1901_2000/s1969_minimum_non_zero_product_of_the_array_elements/Solution.kt)| Medium | Math, Greedy, Recursion | 131 | 100.00
1865+
| 1968 |[Array With Elements Not Equal to Average of Neighbors](src/main/kotlin/g1901_2000/s1968_array_with_elements_not_equal_to_average_of_neighbors/Solution.kt)| Medium | Array, Sorting, Greedy | 973 | 83.33
1866+
| 1967 |[Number of Strings That Appear as Substrings in Word](src/main/kotlin/g1901_2000/s1967_number_of_strings_that_appear_as_substrings_in_word/Solution.kt)| Easy | String | 157 | 100.00
1867+
| 1965 |[Employees With Missing Information](src/main/kotlin/g1901_2000/s1965_employees_with_missing_information/script.sql)| Easy | Database, SQL_I_Day_4_Union_and_Select | 1355 | 23.96
1868+
| 1964 |[Find the Longest Valid Obstacle Course at Each Position](src/main/kotlin/g1901_2000/s1964_find_the_longest_valid_obstacle_course_at_each_position/Solution.kt)| Hard | Array, Binary_Search, Binary_Indexed_Tree | 846 | 40.00
1869+
| 1963 |[Minimum Number of Swaps to Make the String Balanced](src/main/kotlin/g1901_2000/s1963_minimum_number_of_swaps_to_make_the_string_balanced/Solution.kt)| Medium | String, Greedy, Two_Pointers, Stack | 291 | 53.85
1870+
| 1962 |[Remove Stones to Minimize the Total](src/main/kotlin/g1901_2000/s1962_remove_stones_to_minimize_the_total/Solution.kt)| Medium | Array, Heap_Priority_Queue | 888 | 68.42
1871+
| 1961 |[Check If String Is a Prefix of Array](src/main/kotlin/g1901_2000/s1961_check_if_string_is_a_prefix_of_array/Solution.kt)| Easy | Array, String | 162 | 100.00
1872+
| 1960 |[Maximum Product of the Length of Two Palindromic Substrings](src/main/kotlin/g1901_2000/s1960_maximum_product_of_the_length_of_two_palindromic_substrings/Solution.kt)| Hard | String, Hash_Function, Rolling_Hash | 247 | 100.00
1873+
| 1959 |[Minimum Total Space Wasted With K Resizing Operations](src/main/kotlin/g1901_2000/s1959_minimum_total_space_wasted_with_k_resizing_operations/Solution.kt)| Medium | Array, Dynamic_Programming | 225 | 100.00
1874+
| 1958 |[Check if Move is Legal](src/main/kotlin/g1901_2000/s1958_check_if_move_is_legal/Solution.kt)| Medium | Array, Matrix, Enumeration | 169 | 100.00
1875+
| 1957 |[Delete Characters to Make Fancy String](src/main/kotlin/g1901_2000/s1957_delete_characters_to_make_fancy_string/Solution.kt)| Easy | String | 377 | 50.00
1876+
| 1955 |[Count Number of Special Subsequences](src/main/kotlin/g1901_2000/s1955_count_number_of_special_subsequences/Solution.kt)| Hard | Array, Dynamic_Programming | 620 | 100.00
1877+
| 1954 |[Minimum Garden Perimeter to Collect Enough Apples](src/main/kotlin/g1901_2000/s1954_minimum_garden_perimeter_to_collect_enough_apples/Solution.kt)| Medium | Math, Binary_Search | 132 | 100.00
1878+
| 1953 |[Maximum Number of Weeks for Which You Can Work](src/main/kotlin/g1901_2000/s1953_maximum_number_of_weeks_for_which_you_can_work/Solution.kt)| Medium | Array, Greedy | 461 | 100.00
1879+
| 1952 |[Three Divisors](src/main/kotlin/g1901_2000/s1952_three_divisors/Solution.kt)| Easy | Math | 141 | 33.33
18401880
| 1948 |[Delete Duplicate Folders in System](src/main/kotlin/g1901_2000/s1948_delete_duplicate_folders_in_system/Solution.kt)| Hard | Array, String, Hash_Table, Trie, Hash_Function | 1420 | 100.00
18411881
| 1947 |[Maximum Compatibility Score Sum](src/main/kotlin/g1901_2000/s1947_maximum_compatibility_score_sum/Solution.kt)| Medium | Array, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask | 179 | 100.00
18421882
| 1946 |[Largest Number After Mutating Substring](src/main/kotlin/g1901_2000/s1946_largest_number_after_mutating_substring/Solution.kt)| Medium | Array, String, Greedy | 346 | 100.00
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g1901_2000.s1952_three_divisors
2+
3+
// #Easy #Math #2023_06_21_Time_141_ms_(33.33%)_Space_32.8_MB_(100.00%)
4+
5+
class Solution {
6+
fun isThree(n: Int): Boolean {
7+
var divisors = 0
8+
for (i in 1..n) {
9+
if (n % i == 0) {
10+
divisors++
11+
}
12+
}
13+
return divisors == 3
14+
}
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
1952\. Three Divisors
2+
3+
Easy
4+
5+
Given an integer `n`, return `true` _if_ `n` _has **exactly three positive divisors**. Otherwise, return_ `false`.
6+
7+
An integer `m` is a **divisor** of `n` if there exists an integer `k` such that `n = k * m`.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 2
12+
13+
**Output:** false
14+
15+
**Explantion:** 2 has only two divisors: 1 and 2.
16+
17+
**Example 2:**
18+
19+
**Input:** n = 4
20+
21+
**Output:** true
22+
23+
**Explantion:** 4 has three divisors: 1, 2, and 4.
24+
25+
**Constraints:**
26+
27+
* <code>1 <= n <= 10<sup>4</sup></code>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g1901_2000.s1953_maximum_number_of_weeks_for_which_you_can_work
2+
3+
// #Medium #Array #Greedy #2023_06_21_Time_461_ms_(100.00%)_Space_57.5_MB_(100.00%)
4+
5+
class Solution {
6+
fun numberOfWeeks(milestones: IntArray): Long {
7+
var sum: Long = 0
8+
var max: Long = 0
9+
for (m in milestones) {
10+
sum += m.toLong()
11+
max = Math.max(max, m.toLong())
12+
}
13+
return if (sum - max + 1 >= max) sum else 1 + 2 * (sum - max)
14+
}
15+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
1953\. Maximum Number of Weeks for Which You Can Work
2+
3+
Medium
4+
5+
There are `n` projects numbered from `0` to `n - 1`. You are given an integer array `milestones` where each `milestones[i]` denotes the number of milestones the <code>i<sup>th</sup></code> project has.
6+
7+
You can work on the projects following these two rules:
8+
9+
* Every week, you will finish **exactly one** milestone of **one** project. You **must** work every week.
10+
* You **cannot** work on two milestones from the same project for two **consecutive** weeks.
11+
12+
Once all the milestones of all the projects are finished, or if the only milestones that you can work on will cause you to violate the above rules, you will **stop working**. Note that you may not be able to finish every project's milestones due to these constraints.
13+
14+
Return _the **maximum** number of weeks you would be able to work on the projects without violating the rules mentioned above_.
15+
16+
**Example 1:**
17+
18+
**Input:** milestones = [1,2,3]
19+
20+
**Output:** 6
21+
22+
**Explanation:** One possible scenario is:
23+
24+
- During the 1<sup>st</sup> week, you will work on a milestone of project 0.
25+
26+
- During the 2<sup>nd</sup> week, you will work on a milestone of project 2.
27+
28+
- During the 3<sup>rd</sup> week, you will work on a milestone of project 1.
29+
30+
- During the 4<sup>th</sup> week, you will work on a milestone of project 2.
31+
32+
- During the 5<sup>th</sup> week, you will work on a milestone of project 1.
33+
34+
- During the 6<sup>th</sup> week, you will work on a milestone of project 2.
35+
36+
The total number of weeks is 6.
37+
38+
**Example 2:**
39+
40+
**Input:** milestones = [5,2,1]
41+
42+
**Output:** 7
43+
44+
**Explanation:** One possible scenario is:
45+
46+
- During the 1<sup>st</sup> week, you will work on a milestone of project 0.
47+
48+
- During the 2<sup>nd</sup> week, you will work on a milestone of project 1.
49+
50+
- During the 3<sup>rd</sup> week, you will work on a milestone of project 0.
51+
52+
- During the 4<sup>th</sup> week, you will work on a milestone of project 1.
53+
54+
- During the 5<sup>th</sup> week, you will work on a milestone of project 0.
55+
56+
- During the 6<sup>th</sup> week, you will work on a milestone of project 2.
57+
58+
- During the 7<sup>th</sup> week, you will work on a milestone of project 0.
59+
60+
The total number of weeks is 7. Note that you cannot work on the last milestone of project 0 on 8<sup>th</sup> week because it would violate the rules. Thus, one milestone in project 0 will remain unfinished.
61+
62+
**Constraints:**
63+
64+
* `n == milestones.length`
65+
* <code>1 <= n <= 10<sup>5</sup></code>
66+
* <code>1 <= milestones[i] <= 10<sup>9</sup></code>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g1901_2000.s1954_minimum_garden_perimeter_to_collect_enough_apples
2+
3+
// #Medium #Math #Binary_Search #2023_06_21_Time_132_ms_(100.00%)_Space_33_MB_(100.00%)
4+
5+
class Solution {
6+
fun minimumPerimeter(neededApples: Long): Long {
7+
var l: Long = 1
8+
var r: Long = 1000000
9+
var res = l
10+
while (l <= r) {
11+
val m = l + (r - l) / 2
12+
val isPossible = check(m, neededApples)
13+
if (isPossible) {
14+
res = m
15+
r = m - 1
16+
} else {
17+
l = m + 1
18+
}
19+
}
20+
return res * 8
21+
}
22+
23+
private fun check(len: Long, neededApples: Long): Boolean {
24+
val sum = len * (len + 1) / 2
25+
val applesPerQuadrant = 2 * len * sum
26+
val totalCount = 4 * sum + 4 * applesPerQuadrant
27+
return totalCount >= neededApples
28+
}
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1954\. Minimum Garden Perimeter to Collect Enough Apples
2+
3+
Medium
4+
5+
In a garden represented as an infinite 2D grid, there is an apple tree planted at **every** integer coordinate. The apple tree planted at an integer coordinate `(i, j)` has `|i| + |j|` apples growing on it.
6+
7+
You will buy an axis-aligned **square plot** of land that is centered at `(0, 0)`.
8+
9+
Given an integer `neededApples`, return _the **minimum perimeter** of a plot such that **at least**_ `neededApples` _apples are **inside or on** the perimeter of that plot_.
10+
11+
The value of `|x|` is defined as:
12+
13+
* `x` if `x >= 0`
14+
* `-x` if `x < 0`
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2019/08/30/1527_example_1_2.png)
19+
20+
**Input:** neededApples = 1
21+
22+
**Output:** 8
23+
24+
**Explanation:** A square plot of side length 1 does not contain any apples. However, a square plot of side length 2 has 12 apples inside (as depicted in the image above). The perimeter is 2 \* 4 = 8.
25+
26+
**Example 2:**
27+
28+
**Input:** neededApples = 13
29+
30+
**Output:** 16
31+
32+
**Example 3:**
33+
34+
**Input:** neededApples = 1000000000
35+
36+
**Output:** 5040
37+
38+
**Constraints:**
39+
40+
* <code>1 <= neededApples <= 10<sup>15</sup></code>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package g1901_2000.s1955_count_number_of_special_subsequences
2+
3+
// #Hard #Array #Dynamic_Programming #2023_06_21_Time_620_ms_(100.00%)_Space_56.3_MB_(100.00%)
4+
5+
class Solution {
6+
fun countSpecialSubsequences(nums: IntArray): Int {
7+
val mod = 1000000007
8+
val dp = intArrayOf(1, 0, 0, 0)
9+
for (n in nums) {
10+
dp[n + 1] = (dp[n] + 2 * dp[n + 1] % mod) % mod
11+
}
12+
return dp[3]
13+
}
14+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
1955\. Count Number of Special Subsequences
2+
3+
Hard
4+
5+
A sequence is **special** if it consists of a **positive** number of `0`s, followed by a **positive** number of `1`s, then a **positive** number of `2`s.
6+
7+
* For example, `[0,1,2]` and `[0,0,1,1,1,2]` are special.
8+
* In contrast, `[2,1,0]`, `[1]`, and `[0,1,2,0]` are not special.
9+
10+
Given an array `nums` (consisting of **only** integers `0`, `1`, and `2`), return _the **number of different subsequences** that are special_. Since the answer may be very large, **return it modulo** <code>10<sup>9</sup> + 7</code>.
11+
12+
A **subsequence** of an array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements. Two subsequences are **different** if the **set of indices** chosen are different.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [0,1,2,2]
17+
18+
**Output:** 3
19+
20+
**Explanation:** The special subsequences are bolded [**0**,**1**,**2**,2], [**0**,**1**,2,**2**], and [**0**,**1**,**2**,**2**].
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [2,2,0,0]
25+
26+
**Output:** 0
27+
28+
**Explanation:** There are no special subsequences in [2,2,0,0].
29+
30+
**Example 3:**
31+
32+
**Input:** nums = [0,1,2,0,1,2]
33+
34+
**Output:** 7
35+
36+
**Explanation:** The special subsequences are bolded:
37+
38+
- [**0**,**1**,**2**,0,1,2]
39+
40+
- [**0**,**1**,2,0,1,**2**]
41+
42+
- [**0**,**1**,**2**,0,1,**2**]
43+
44+
- [**0**,**1**,2,0,**1**,**2**]
45+
46+
- [**0**,1,2,**0**,**1**,**2**]
47+
48+
- [**0**,1,2,0,**1**,**2**]
49+
50+
- [0,1,2,**0**,**1**,**2**]
51+
52+
**Constraints:**
53+
54+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
55+
* `0 <= nums[i] <= 2`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g1901_2000.s1957_delete_characters_to_make_fancy_string
2+
3+
// #Easy #String #2023_06_21_Time_377_ms_(50.00%)_Space_42.9_MB_(87.50%)
4+
5+
class Solution {
6+
fun makeFancyString(s: String): String {
7+
val ans = StringBuilder()
8+
var c = 1
9+
ans.append(s[0])
10+
for (i in 1 until s.length) {
11+
if (s[i] == s[i - 1]) {
12+
c++
13+
} else {
14+
c = 1
15+
}
16+
if (c < 3) {
17+
ans.append(s[i])
18+
}
19+
}
20+
return ans.toString()
21+
}
22+
}

0 commit comments

Comments
 (0)