Skip to content

Commit d9daf2c

Browse files
authored
Added tasks 4-22
1 parent fa6e4b4 commit d9daf2c

File tree

31 files changed

+927
-1
lines changed

31 files changed

+927
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g0001_0100.s0004_median_of_two_sorted_arrays
2+
3+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
4+
// #Big_O_Time_O(log(min(N,M)))_Space_O(1)
5+
6+
object Solution {
7+
@SuppressWarnings(Array("scala:S3776"))
8+
def findMedianSortedArrays(nums1: Array[Int], nums2: Array[Int]): Double = {
9+
if (nums2.length < nums1.length) {
10+
findMedianSortedArrays(nums2, nums1)
11+
} else {
12+
val n1 = nums1.length
13+
val n2 = nums2.length
14+
var cut1: Int = 0
15+
var cut2: Int = 0
16+
var low: Int = 0
17+
var high: Int = n1
18+
19+
while (low <= high) {
20+
cut1 = (low + high) / 2
21+
cut2 = (n1 + n2 + 1) / 2 - cut1
22+
val l1 = if (cut1 == 0) Int.MinValue else nums1(cut1 - 1)
23+
val l2 = if (cut2 == 0) Int.MinValue else nums2(cut2 - 1)
24+
val r1 = if (cut1 == n1) Int.MaxValue else nums1(cut1)
25+
val r2 = if (cut2 == n2) Int.MaxValue else nums2(cut2)
26+
27+
if (l1 <= r2 && l2 <= r1) {
28+
if ((n1 + n2) % 2 == 0) {
29+
return (math.max(l1, l2) + math.min(r1, r2)) / 2.0
30+
}
31+
return math.max(l1, l2)
32+
} else if (l1 > r2) {
33+
high = cut1 - 1
34+
} else {
35+
low = cut1 + 1
36+
}
37+
}
38+
0.0
39+
}
40+
}
41+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
4\. Median of Two Sorted Arrays
2+
3+
Hard
4+
5+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
6+
7+
The overall run time complexity should be `O(log (m+n))`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums1 = [1,3], nums2 = [2]
12+
13+
**Output:** 2.00000
14+
15+
**Explanation:** merged array = [1,2,3] and median is 2.
16+
17+
**Example 2:**
18+
19+
**Input:** nums1 = [1,2], nums2 = [3,4]
20+
21+
**Output:** 2.50000
22+
23+
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
24+
25+
**Example 3:**
26+
27+
**Input:** nums1 = [0,0], nums2 = [0,0]
28+
29+
**Output:** 0.00000
30+
31+
**Example 4:**
32+
33+
**Input:** nums1 = [], nums2 = [1]
34+
35+
**Output:** 1.00000
36+
37+
**Example 5:**
38+
39+
**Input:** nums1 = [2], nums2 = []
40+
41+
**Output:** 2.00000
42+
43+
**Constraints:**
44+
45+
* `nums1.length == m`
46+
* `nums2.length == n`
47+
* `0 <= m <= 1000`
48+
* `0 <= n <= 1000`
49+
* `1 <= m + n <= 2000`
50+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g0001_0100.s0005_longest_palindromic_substring
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
4+
// #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming
5+
// #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
6+
7+
object Solution {
8+
def longestPalindrome(s: String): String = {
9+
val newStr = new Array[Char](s.length * 2 + 1)
10+
newStr(0) = '#'
11+
for (i <- 0 until s.length) {
12+
newStr(2 * i + 1) = s.charAt(i)
13+
newStr(2 * i + 2) = '#'
14+
}
15+
16+
val dp = new Array[Int](newStr.length)
17+
var friendCenter = 0
18+
var friendRadius = 0
19+
var lpsCenter = 0
20+
var lpsRadius = 0
21+
22+
for (i <- 0 until newStr.length) {
23+
dp(i) =
24+
if (friendCenter + friendRadius > i)
25+
Math.min(dp(friendCenter * 2 - i), (friendCenter + friendRadius) - i)
26+
else
27+
1
28+
29+
while (i + dp(i) < newStr.length
30+
&& i - dp(i) >= 0
31+
&& newStr(i + dp(i)) == newStr(i - dp(i))) {
32+
dp(i) += 1
33+
}
34+
35+
if (friendCenter + friendRadius < i + dp(i)) {
36+
friendCenter = i
37+
friendRadius = dp(i)
38+
}
39+
40+
if (lpsRadius < dp(i)) {
41+
lpsCenter = i
42+
lpsRadius = dp(i)
43+
}
44+
}
45+
46+
s.substring((lpsCenter - lpsRadius + 1) / 2, (lpsCenter + lpsRadius - 1) / 2)
47+
}
48+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
5\. Longest Palindromic Substring
2+
3+
Medium
4+
5+
Given a string `s`, return _the longest palindromic substring_ in `s`.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "babad"
10+
11+
**Output:** "bab" **Note:** "aba" is also a valid answer.
12+
13+
**Example 2:**
14+
15+
**Input:** s = "cbbd"
16+
17+
**Output:** "bb"
18+
19+
**Example 3:**
20+
21+
**Input:** s = "a"
22+
23+
**Output:** "a"
24+
25+
**Example 4:**
26+
27+
**Input:** s = "ac"
28+
29+
**Output:** "a"
30+
31+
**Constraints:**
32+
33+
* `1 <= s.length <= 1000`
34+
* `s` consist of only digits and English letters.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0001_0100.s0010_regular_expression_matching
2+
3+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming #Recursion
4+
// #Udemy_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n)
5+
6+
object Solution {
7+
private var cache: Array[Array[Option[Boolean]]] = Array.ofDim[Option[Boolean]](0, 0)
8+
9+
def isMatch(s: String, p: String): Boolean = {
10+
cache = Array.ofDim[Option[Boolean]](s.length + 1, p.length + 1)
11+
isMatch(s, p, 0, 0)
12+
}
13+
14+
private def isMatch(s: String, p: String, i: Int, j: Int): Boolean = {
15+
if (j == p.length) {
16+
return i == s.length
17+
}
18+
var result: Boolean = false
19+
if (cache(i).isDefinedAt(j) && cache(i)(j) != null) {
20+
return cache(i)(j).get
21+
}
22+
val firstMatch = i < s.length && (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.')
23+
if (j + 1 < p.length && p.charAt(j + 1) == '*') {
24+
result = (firstMatch && isMatch(s, p, i + 1, j)) || isMatch(s, p, i, j + 2)
25+
} else {
26+
result = firstMatch && isMatch(s, p, i + 1, j + 1)
27+
}
28+
cache(i)(j) = Some(result)
29+
result
30+
}
31+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
10\. Regular Expression Matching
2+
3+
Hard
4+
5+
Given an input string `s` and a pattern `p`, implement regular expression matching with support for `'.'` and `'*'` where:
6+
7+
* `'.'` Matches any single character.
8+
* `'*'` Matches zero or more of the preceding element.
9+
10+
The matching should cover the **entire** input string (not partial).
11+
12+
**Example 1:**
13+
14+
**Input:** s = "aa", p = "a"
15+
16+
**Output:** false
17+
18+
**Explanation:** "a" does not match the entire string "aa".
19+
20+
**Example 2:**
21+
22+
**Input:** s = "aa", p = "a\*"
23+
24+
**Output:** true
25+
26+
**Explanation:** '\*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
27+
28+
**Example 3:**
29+
30+
**Input:** s = "ab", p = ".\*"
31+
32+
**Output:** true
33+
34+
**Explanation:** ".\*" means "zero or more (\*) of any character (.)".
35+
36+
**Example 4:**
37+
38+
**Input:** s = "aab", p = "c\*a\*b"
39+
40+
**Output:** true
41+
42+
**Explanation:** c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".
43+
44+
**Example 5:**
45+
46+
**Input:** s = "mississippi", p = "mis\*is\*p\*."
47+
48+
**Output:** false
49+
50+
**Constraints:**
51+
52+
* `1 <= s.length <= 20`
53+
* `1 <= p.length <= 30`
54+
* `s` contains only lowercase English letters.
55+
* `p` contains only lowercase English letters, `'.'`, and `'*'`.
56+
* It is guaranteed for each appearance of the character `'*'`, there will be a previous valid character to match.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0001_0100.s0011_container_with_most_water
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Greedy #Two_Pointers
4+
// #Algorithm_II_Day_4_Two_Pointers #Big_O_Time_O(n)_Space_O(1)
5+
6+
object Solution {
7+
def maxArea(height: Array[Int]): Int = {
8+
var maxArea = -1
9+
var left = 0
10+
var right = height.length - 1
11+
12+
while (left < right) {
13+
if (height(left) < height(right)) {
14+
maxArea = maxArea.max(height(left) * (right - left))
15+
left += 1
16+
} else {
17+
maxArea = maxArea.max(height(right) * (right - left))
18+
right -= 1
19+
}
20+
}
21+
22+
maxArea
23+
}
24+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
11\. Container With Most Water
2+
3+
Medium
4+
5+
Given `n` non-negative integers <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code> , where each represents a point at coordinate <code>(i, a<sub>i</sub>)</code>. `n` vertical lines are drawn such that the two endpoints of the line `i` is at <code>(i, a<sub>i</sub>)</code> and `(i, 0)`. Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.
6+
7+
**Notice** that you may not slant the container.
8+
9+
**Example 1:**
10+
11+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg)
12+
13+
**Input:** height = [1,8,6,2,5,4,8,3,7]
14+
15+
**Output:** 49
16+
17+
**Explanation:** The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
18+
19+
**Example 2:**
20+
21+
**Input:** height = [1,1]
22+
23+
**Output:** 1
24+
25+
**Example 3:**
26+
27+
**Input:** height = [4,3,2,1,4]
28+
29+
**Output:** 16
30+
31+
**Example 4:**
32+
33+
**Input:** height = [1,2,1]
34+
35+
**Output:** 2
36+
37+
**Constraints:**
38+
39+
* `n == height.length`
40+
* <code>2 <= n <= 10<sup>5</sup></code>
41+
* <code>0 <= height[i] <= 10<sup>4</sup></code>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0001_0100.s0015_3sum
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting #Two_Pointers
4+
// #Data_Structure_II_Day_1_Array #Algorithm_II_Day_3_Two_Pointers #Udemy_Two_Pointers
5+
// #Big_O_Time_O(n*log(n))_Space_O(n^2) #2023_10_29_Time_701_ms_(98.81%)_Space_66.7_MB_(88.10%)
6+
7+
object Solution {
8+
@SuppressWarnings(Array("scala:S3776"))
9+
def threeSum(nums: Array[Int]): List[List[Int]] = {
10+
val sortedNums = nums.sorted
11+
var result: List[List[Int]] = List()
12+
for (i <- 0 until sortedNums.length - 2) {
13+
if (i == 0 || (i > 0 && sortedNums(i) != sortedNums(i - 1))) {
14+
var left = i + 1
15+
var right = sortedNums.length - 1
16+
val target = -sortedNums(i)
17+
while (left < right) {
18+
if (sortedNums(left) + sortedNums(right) == target) {
19+
result = List(sortedNums(i), sortedNums(left), sortedNums(right)) :: result
20+
while (left < right && sortedNums(left) == sortedNums(left + 1))
21+
left += 1
22+
while (left < right && sortedNums(right) == sortedNums(right - 1))
23+
right -= 1
24+
25+
left += 1
26+
right -= 1
27+
} else if (sortedNums(left) + sortedNums(right) < target)
28+
left += 1
29+
else
30+
right -= 1
31+
}
32+
}
33+
}
34+
result
35+
}
36+
}

0 commit comments

Comments
 (0)