Skip to content

Commit 4a8e4e8

Browse files
authored
Added tasks 2011-2050
1 parent 35a3f11 commit 4a8e4e8

File tree

97 files changed

+3975
-5
lines changed

Some content is hidden

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

97 files changed

+3975
-5
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g2001_2100.s2011_final_value_of_variable_after_performing_operations
2+
3+
// #Easy #Array #String #Simulation #2023_06_23_Time_178_ms_(44.55%)_Space_37.3_MB_(66.34%)
4+
5+
class Solution {
6+
fun finalValueAfterOperations(operations: Array<String>): Int {
7+
var xValue = 0
8+
for (word in operations) {
9+
if (word.contains("+")) {
10+
xValue++
11+
} else {
12+
xValue--
13+
}
14+
}
15+
return xValue
16+
}
17+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2011\. Final Value of Variable After Performing Operations
2+
3+
Easy
4+
5+
There is a programming language with only **four** operations and **one** variable `X`:
6+
7+
* `++X` and `X++` **increments** the value of the variable `X` by `1`.
8+
* `--X` and `X--` **decrements** the value of the variable `X` by `1`.
9+
10+
Initially, the value of `X` is `0`.
11+
12+
Given an array of strings `operations` containing a list of operations, return _the **final** value of_ `X` _after performing all the operations_.
13+
14+
**Example 1:**
15+
16+
**Input:** operations = ["--X","X++","X++"]
17+
18+
**Output:** 1
19+
20+
**Explanation:** The operations are performed as follows:
21+
22+
Initially, X = 0.
23+
24+
--X: X is decremented by 1, X = 0 - 1 = -1.
25+
26+
X++: X is incremented by 1, X = -1 + 1 = 0.
27+
28+
X++: X is incremented by 1, X = 0 + 1 = 1.
29+
30+
**Example 2:**
31+
32+
**Input:** operations = ["++X","++X","X++"]
33+
34+
**Output:** 3
35+
36+
**Explanation:** The operations are performed as follows:
37+
38+
Initially, X = 0.
39+
40+
++X: X is incremented by 1, X = 0 + 1 = 1.
41+
42+
++X: X is incremented by 1, X = 1 + 1 = 2.
43+
44+
X++: X is incremented by 1, X = 2 + 1 = 3.
45+
46+
**Example 3:**
47+
48+
**Input:** operations = ["X++","++X","--X","X--"]
49+
50+
**Output:** 0
51+
52+
**Explanation:** The operations are performed as follows:
53+
54+
Initially, X = 0.
55+
56+
X++: X is incremented by 1, X = 0 + 1 = 1.
57+
58+
++X: X is incremented by 1, X = 1 + 1 = 2.
59+
60+
--X: X is decremented by 1, X = 2 - 1 = 1.
61+
62+
X--: X is decremented by 1, X = 1 - 1 = 0.
63+
64+
**Constraints:**
65+
66+
* `1 <= operations.length <= 100`
67+
* `operations[i]` will be either `"++X"`, `"X++"`, `"--X"`, or `"X--"`.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g2001_2100.s2012_sum_of_beauty_in_the_array
2+
3+
// #Medium #Array #2023_06_23_Time_511_ms_(100.00%)_Space_56.5_MB_(50.00%)
4+
5+
class Solution {
6+
fun sumOfBeauties(nums: IntArray): Int {
7+
val maxArr = IntArray(nums.size)
8+
maxArr[0] = nums[0]
9+
for (i in 1 until nums.size - 1) {
10+
maxArr[i] = Math.max(maxArr[i - 1], nums[i])
11+
}
12+
val minArr = IntArray(nums.size)
13+
minArr[nums.size - 1] = nums[nums.size - 1]
14+
for (i in nums.size - 2 downTo 0) {
15+
minArr[i] = Math.min(minArr[i + 1], nums[i])
16+
}
17+
var sum = 0
18+
for (i in 1 until nums.size - 1) {
19+
if (nums[i] > maxArr[i - 1] && nums[i] < minArr[i + 1]) {
20+
sum += 2
21+
} else if (nums[i] > nums[i - 1] && nums[i] < nums[i + 1]) {
22+
sum += 1
23+
}
24+
}
25+
return sum
26+
}
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2012\. Sum of Beauty in the Array
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums`. For each index `i` (`1 <= i <= nums.length - 2`) the **beauty** of `nums[i]` equals:
6+
7+
* `2`, if `nums[j] < nums[i] < nums[k]`, for **all** `0 <= j < i` and for **all** `i < k <= nums.length - 1`.
8+
* `1`, if `nums[i - 1] < nums[i] < nums[i + 1]`, and the previous condition is not satisfied.
9+
* `0`, if none of the previous conditions holds.
10+
11+
Return _the **sum of beauty** of all_ `nums[i]` _where_ `1 <= i <= nums.length - 2`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3]
16+
17+
**Output:** 2
18+
19+
**Explanation:** For each index i in the range 1 <= i <= 1: - The beauty of nums[1] equals 2.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [2,4,6,4]
24+
25+
**Output:** 1
26+
27+
**Explanation:** For each index i in the range 1 <= i <= 2:
28+
29+
- The beauty of nums[1] equals 1.
30+
31+
- The beauty of nums[2] equals 0.
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [3,2,1]
36+
37+
**Output:** 0
38+
39+
**Explanation:** For each index i in the range 1 <= i <= 1: - The beauty of nums[1] equals 0.
40+
41+
**Constraints:**
42+
43+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
44+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package g2001_2100.s2013_detect_squares
2+
3+
// #Medium #Array #Hash_Table #Design #Counting
4+
// #2023_06_23_Time_511_ms_(100.00%)_Space_61.4_MB_(80.00%)
5+
6+
class DetectSquares {
7+
private val map: MutableMap<Int, IntArray>
8+
9+
init {
10+
map = HashMap()
11+
}
12+
13+
fun add(point: IntArray) {
14+
val x = point[0]
15+
val y = point[1]
16+
val hash = x * MUL + y
17+
if (map.containsKey(hash)) {
18+
map[hash]!![2]++
19+
} else {
20+
map[hash] = intArrayOf(x, y, 1)
21+
}
22+
}
23+
24+
fun count(point: IntArray): Int {
25+
var ans = 0
26+
val x = point[0]
27+
val y = point[1]
28+
for ((_, diap) in map) {
29+
val x1 = diap[0]
30+
val y1 = diap[1]
31+
val num = diap[2]
32+
if (Math.abs(x - x1) == Math.abs(y - y1) && x != x1 && y != y1) {
33+
val p1hash = x * MUL + y1
34+
val p2hash = x1 * MUL + y
35+
if (map.containsKey(p1hash) && map.containsKey(p2hash)) {
36+
ans += map[p1hash]!![2] * map[p2hash]!![2] * num
37+
}
38+
}
39+
}
40+
return ans
41+
}
42+
43+
companion object {
44+
private const val MUL = 1002
45+
}
46+
}
47+
/*
48+
* Your DetectSquares object will be instantiated and called as such:
49+
* var obj = DetectSquares()
50+
* obj.add(point)
51+
* var param_2 = obj.count(point)
52+
*/
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2013\. Detect Squares
2+
3+
Medium
4+
5+
You are given a stream of points on the X-Y plane. Design an algorithm that:
6+
7+
* **Adds** new points from the stream into a data structure. **Duplicate** points are allowed and should be treated as different points.
8+
* Given a query point, **counts** the number of ways to choose three points from the data structure such that the three points and the query point form an **axis-aligned square** with **positive area**.
9+
10+
An **axis-aligned square** is a square whose edges are all the same length and are either parallel or perpendicular to the x-axis and y-axis.
11+
12+
Implement the `DetectSquares` class:
13+
14+
* `DetectSquares()` Initializes the object with an empty data structure.
15+
* `void add(int[] point)` Adds a new point `point = [x, y]` to the data structure.
16+
* `int count(int[] point)` Counts the number of ways to form **axis-aligned squares** with point `point = [x, y]` as described above.
17+
18+
**Example 1:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/09/01/image.png)
21+
22+
**Input** ["DetectSquares", "add", "add", "add", "count", "count", "add", "count"] [[], [[3, 10]], [[11, 2]], [[3, 2]], [[11, 10]], [[14, 8]], [[11, 2]], [[11, 10]]]
23+
24+
**Output:** [null, null, null, null, 1, 0, null, 2]
25+
26+
**Explanation:**
27+
28+
DetectSquares detectSquares = new DetectSquares();
29+
30+
detectSquares.add([3, 10]);
31+
32+
detectSquares.add([11, 2]);
33+
34+
detectSquares.add([3, 2]);
35+
36+
detectSquares.count([11, 10]); // return 1. You can choose:
37+
// - The first, second, and third points
38+
39+
detectSquares.count([14, 8]); // return 0. The query point cannot form a square with any points in the data structure.
40+
41+
detectSquares.add([11, 2]); // Adding duplicate points is allowed.
42+
43+
detectSquares.count([11, 10]); // return 2. You can choose: // - The first, second, and third points // - The first, third, and fourth points
44+
45+
**Constraints:**
46+
47+
* `point.length == 2`
48+
* `0 <= x, y <= 1000`
49+
* At most `3000` calls **in total** will be made to `add` and `count`.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g2001_2100.s2014_longest_subsequence_repeated_k_times
2+
3+
// #Hard #String #Greedy #Backtracking #Counting #Enumeration
4+
// #2023_06_23_Time_333_ms_(100.00%)_Space_39_MB_(100.00%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
fun longestSubsequenceRepeatedK(s: String, k: Int): String {
9+
val ca = s.toCharArray()
10+
val freq = CharArray(26)
11+
for (value in ca) {
12+
++freq[value.code - 'a'.code]
13+
}
14+
val cand: Array<ArrayList<String>?> = arrayOfNulls(8)
15+
cand[1] = ArrayList()
16+
var ans = ""
17+
for (i in 0..25) {
18+
if (freq[i].code >= k) {
19+
ans = "" + ('a'.code + i).toChar()
20+
cand[1]?.add(ans)
21+
}
22+
}
23+
for (i in 2..7) {
24+
cand[i] = ArrayList()
25+
for (prev in cand[i - 1]!!) {
26+
for (c in cand[1]!!) {
27+
val next = prev + c
28+
if (isSubsequenceRepeatedK(ca, next, k)) {
29+
ans = next
30+
cand[i]?.add(ans)
31+
}
32+
}
33+
}
34+
}
35+
return ans
36+
}
37+
38+
private fun isSubsequenceRepeatedK(ca: CharArray, t: String, k: Int): Boolean {
39+
var k = k
40+
val ta = t.toCharArray()
41+
val n = ca.size
42+
val m = ta.size
43+
var i = 0
44+
while (k-- > 0) {
45+
var j = 0
46+
while (i < n && j < m) {
47+
if (ca[i] == ta[j]) {
48+
j++
49+
}
50+
i++
51+
}
52+
if (j != m) {
53+
return false
54+
}
55+
}
56+
return true
57+
}
58+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2014\. Longest Subsequence Repeated k Times
2+
3+
Hard
4+
5+
You are given a string `s` of length `n`, and an integer `k`. You are tasked to find the **longest subsequence repeated** `k` times in string `s`.
6+
7+
A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
8+
9+
A subsequence `seq` is **repeated** `k` times in the string `s` if `seq * k` is a subsequence of `s`, where `seq * k` represents a string constructed by concatenating `seq` `k` times.
10+
11+
* For example, `"bba"` is repeated `2` times in the string `"bababcba"`, because the string `"bbabba"`, constructed by concatenating `"bba"` `2` times, is a subsequence of the string `"**b**a**bab**c**ba**"`.
12+
13+
Return _the **longest subsequence repeated**_ `k` _times in string_ `s`_. If multiple such subsequences are found, return the **lexicographically largest** one. If there is no such subsequence, return an **empty** string_.
14+
15+
**Example 1:**
16+
17+
![example 1](https://assets.leetcode.com/uploads/2021/08/30/longest-subsequence-repeat-k-times.png)
18+
19+
**Input:** s = "letsleetcode", k = 2
20+
21+
**Output:** "let"
22+
23+
**Explanation:** There are two longest subsequences repeated 2 times: "let" and "ete". "let" is the lexicographically largest one.
24+
25+
**Example 2:**
26+
27+
**Input:** s = "bb", k = 2
28+
29+
**Output:** "b"
30+
31+
**Explanation:** The longest subsequence repeated 2 times is "b".
32+
33+
**Example 3:**
34+
35+
**Input:** s = "ab", k = 2
36+
37+
**Output:** ""
38+
39+
**Explanation:** There is no subsequence repeated 2 times. Empty string is returned.
40+
41+
**Constraints:**
42+
43+
* `n == s.length`
44+
* `2 <= n, k <= 2000`
45+
* `2 <= n < k * 8`
46+
* `s` consists of lowercase English letters.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g2001_2100.s2016_maximum_difference_between_increasing_elements
2+
3+
// #Easy #Array #2023_06_23_Time_140_ms_(100.00%)_Space_35_MB_(100.00%)
4+
5+
class Solution {
6+
fun maximumDifference(nums: IntArray): Int {
7+
var mini = nums[0]
8+
var ans = -1
9+
for (i in 0 until nums.size - 1) {
10+
if (nums[i] < mini) {
11+
mini = nums[i]
12+
}
13+
if (nums[i + 1] - mini > ans) {
14+
ans = nums[i + 1] - mini
15+
}
16+
}
17+
return if (ans <= 0) -1 else ans
18+
}
19+
}

0 commit comments

Comments
 (0)