Skip to content

Commit 5bf0b85

Browse files
authored
Added tasks 454, 455, 456, 457.
1 parent 4c1f406 commit 5bf0b85

File tree

13 files changed

+1066
-690
lines changed

13 files changed

+1066
-690
lines changed

README.md

Lines changed: 695 additions & 690 deletions
Large diffs are not rendered by default.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0401_0500.s0454_4sum_ii
2+
3+
// #Medium #Top_Interview_Questions #Array #Hash_Table
4+
// #2022_12_26_Time_660_ms_(85.71%)_Space_48.4_MB_(82.86%)
5+
6+
class Solution {
7+
fun fourSumCount(nums1: IntArray, nums2: IntArray, nums3: IntArray, nums4: IntArray): Int {
8+
var count = 0
9+
val map: MutableMap<Int, Int> = HashMap()
10+
for (k in nums3) {
11+
for (i in nums4) {
12+
val sum = k + i
13+
map[sum] = map.getOrDefault(sum, 0) + 1
14+
}
15+
}
16+
for (k in nums1) {
17+
for (i in nums2) {
18+
val m = -(k + i)
19+
count += map.getOrDefault(m, 0)
20+
}
21+
}
22+
return count
23+
}
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
454\. 4Sum II
2+
3+
Medium
4+
5+
Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:
6+
7+
* `0 <= i, j, k, l < n`
8+
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
9+
10+
**Example 1:**
11+
12+
**Input:** nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
13+
14+
**Output:** 2
15+
16+
**Explanation:** The two tuples are:
17+
18+
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
19+
20+
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
21+
22+
**Example 2:**
23+
24+
**Input:** nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
25+
26+
**Output:** 1
27+
28+
**Constraints:**
29+
30+
* `n == nums1.length`
31+
* `n == nums2.length`
32+
* `n == nums3.length`
33+
* `n == nums4.length`
34+
* `1 <= n <= 200`
35+
* <code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0401_0500.s0455_assign_cookies
2+
3+
// #Easy #Array #Sorting #Greedy #2022_12_26_Time_260_ms_(96.67%)_Space_37.7_MB_(100.00%)
4+
5+
class Solution {
6+
fun findContentChildren(g: IntArray, s: IntArray): Int {
7+
g.sort()
8+
s.sort()
9+
var result = 0
10+
var i = 0
11+
var j = 0
12+
while (i < g.size && j < s.size) {
13+
if (s[j] >= g[i]) {
14+
result++
15+
i++
16+
}
17+
j++
18+
}
19+
return result
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
455\. Assign Cookies
2+
3+
Easy
4+
5+
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.
6+
7+
Each child `i` has a greed factor `g[i]`, which is the minimum size of a cookie that the child will be content with; and each cookie `j` has a size `s[j]`. If `s[j] >= g[i]`, we can assign the cookie `j` to the child `i`, and the child `i` will be content. Your goal is to maximize the number of your content children and output the maximum number.
8+
9+
**Example 1:**
10+
11+
**Input:** g = [1,2,3], s = [1,1]
12+
13+
**Output:** 1
14+
15+
**Explanation:** You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content. You need to output 1.
16+
17+
**Example 2:**
18+
19+
**Input:** g = [1,2], s = [1,2,3]
20+
21+
**Output:** 2
22+
23+
**Explanation:** You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. You have 3 cookies and their sizes are big enough to gratify all of the children, You need to output 2.
24+
25+
**Constraints:**
26+
27+
* <code>1 <= g.length <= 3 * 10<sup>4</sup></code>
28+
* <code>0 <= s.length <= 3 * 10<sup>4</sup></code>
29+
* <code>1 <= g[i], s[j] <= 2<sup>31</sup> - 1</code>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0401_0500.s0456_132_pattern
2+
3+
// #Medium #Array #Binary_Search #Stack #Ordered_Set #Monotonic_Stack #Udemy_Arrays
4+
// #2022_12_26_Time_434_ms_(100.00%)_Space_69.2_MB_(77.78%)
5+
6+
import java.util.Deque
7+
import java.util.LinkedList
8+
9+
class Solution {
10+
/*
11+
* It scans only once, this is the power of using correct data structure.
12+
* It goes from the right to the left.
13+
* It keeps pushing elements into the stack,
14+
* but it also keeps poping elements out of the stack as long as the current element is bigger than this number.
15+
*/
16+
fun find132pattern(nums: IntArray): Boolean {
17+
val stack: Deque<Int> = LinkedList()
18+
var s3 = Int.MIN_VALUE
19+
for (i in nums.indices.reversed()) {
20+
if (nums[i] < s3) {
21+
return true
22+
} else {
23+
while (!stack.isEmpty() && nums[i] > stack.peek()) {
24+
s3 = Math.max(s3, stack.pop())
25+
}
26+
}
27+
stack.push(nums[i])
28+
}
29+
return false
30+
}
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
456\. 132 Pattern
2+
3+
Medium
4+
5+
Given an array of `n` integers `nums`, a **132 pattern** is a subsequence of three integers `nums[i]`, `nums[j]` and `nums[k]` such that `i < j < k` and `nums[i] < nums[k] < nums[j]`.
6+
7+
Return `true` _if there is a **132 pattern** in_ `nums`_, otherwise, return_ `false`_._
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,3,4]
12+
13+
**Output:** false
14+
15+
**Explanation:** There is no 132 pattern in the sequence.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [3,1,4,2]
20+
21+
**Output:** true
22+
23+
**Explanation:** There is a 132 pattern in the sequence: [1, 4, 2].
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [-1,3,2,0]
28+
29+
**Output:** true
30+
31+
**Explanation:** There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
32+
33+
**Constraints:**
34+
35+
* `n == nums.length`
36+
* <code>1 <= n <= 2 * 10<sup>5</sup></code>
37+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g0401_0500.s0457_circular_array_loop
2+
3+
// #Medium #Array #Hash_Table #Two_Pointers #2022_12_26_Time_143_ms_(100.00%)_Space_34_MB_(66.67%)
4+
5+
class Solution {
6+
fun circularArrayLoop(arr: IntArray): Boolean {
7+
val n = arr.size
8+
val map: MutableMap<Int, Int> = HashMap()
9+
// arr[i]%n==0 (cycle)
10+
for (start in 0 until n) {
11+
if (map.containsKey(start)) {
12+
// check if already visited
13+
continue
14+
}
15+
var curr = start
16+
// Check if the current index is valid
17+
while (isValidCycle(start, curr, arr)) {
18+
// marking current index visited and set value as start of loop
19+
map[curr] = start
20+
// steps to jump;
21+
val jump = arr[curr] % n
22+
// Jumping x steps backwards is same as jumping (n-x) steps forward
23+
// going to next index;
24+
curr = (curr + jump + n) % n
25+
if (map.containsKey(curr)) {
26+
// value already processed
27+
if (map[curr] == start) {
28+
// If equal to start then we have found a loop
29+
return true
30+
}
31+
// Else we can break since this has already been visited hence we will get the
32+
// same result as before
33+
break
34+
}
35+
}
36+
}
37+
return false
38+
}
39+
40+
private fun isValidCycle(start: Int, curr: Int, arr: IntArray): Boolean {
41+
return (
42+
(arr[start] <= 0 || arr[curr] >= 0) &&
43+
(arr[start] >= 0 || arr[curr] <= 0) && arr[curr] % arr.size != 0
44+
)
45+
}
46+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
457\. Circular Array Loop
2+
3+
Medium
4+
5+
You are playing a game involving a **circular** array of non-zero integers `nums`. Each `nums[i]` denotes the number of indices forward/backward you must move if you are located at index `i`:
6+
7+
* If `nums[i]` is positive, move `nums[i]` steps **forward**, and
8+
* If `nums[i]` is negative, move `nums[i]` steps **backward**.
9+
10+
Since the array is **circular**, you may assume that moving forward from the last element puts you on the first element, and moving backwards from the first element puts you on the last element.
11+
12+
A **cycle** in the array consists of a sequence of indices `seq` of length `k` where:
13+
14+
* Following the movement rules above results in the repeating index sequence `seq[0] -> seq[1] -> ... -> seq[k - 1] -> seq[0] -> ...`
15+
* Every `nums[seq[j]]` is either **all positive** or **all negative**.
16+
* `k > 1`
17+
18+
Return `true` _if there is a **cycle** in_ `nums`_, or_ `false` _otherwise_.
19+
20+
**Example 1:**
21+
22+
**Input:** nums = [2,-1,1,2,2]
23+
24+
**Output:** true
25+
26+
**Explanation:** There is a cycle from index 0 -> 2 -> 3 -> 0 -> ... The cycle's length is 3.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [-1,2]
31+
32+
**Output:** false
33+
34+
**Explanation:** The sequence from index 1 -> 1 -> 1 -> ... is not a cycle because the sequence's length is 1. By definition the sequence's length must be strictly greater than 1 to be a cycle.
35+
36+
**Example 3:**
37+
38+
**Input:** nums = [-2,1,-1,-2,-2]
39+
40+
**Output:** false
41+
42+
**Explanation:** The sequence from index 1 -> 2 -> 1 -> ... is not a cycle because nums[1] is positive, but nums[2] is negative. Every nums[seq[j]] must be either all positive or all negative.
43+
44+
**Constraints:**
45+
46+
* `1 <= nums.length <= 5000`
47+
* `-1000 <= nums[i] <= 1000`
48+
* `nums[i] != 0`
49+
50+
**Follow up:** Could you solve it in `O(n)` time complexity and `O(1)` extra space complexity?
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g0401_0500.s0454_4sum_ii
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun fourSumCount() {
10+
assertThat(
11+
Solution()
12+
.fourSumCount(intArrayOf(1, 2), intArrayOf(-2, -1), intArrayOf(-1, 2), intArrayOf(0, 2)),
13+
equalTo(2)
14+
)
15+
}
16+
17+
@Test
18+
fun fourSumCount2() {
19+
assertThat(
20+
Solution()
21+
.fourSumCount(intArrayOf(0), intArrayOf(0), intArrayOf(0), intArrayOf(0)),
22+
equalTo(1)
23+
)
24+
}
25+
}

0 commit comments

Comments
 (0)