Skip to content

Commit f07ed57

Browse files
authored
Added tasks 454-480.
1 parent df4bcca commit f07ed57

File tree

25 files changed

+1962
-142
lines changed

25 files changed

+1962
-142
lines changed

README.md

Lines changed: 169 additions & 142 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 454\. 4Sum II
5+
6+
Medium
7+
8+
Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:
9+
10+
* `0 <= i, j, k, l < n`
11+
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
12+
13+
**Example 1:**
14+
15+
**Input:** nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
16+
17+
**Output:** 2
18+
19+
**Explanation:** The two tuples are:
20+
21+
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
22+
23+
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
24+
25+
**Example 2:**
26+
27+
**Input:** nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
28+
29+
**Output:** 1
30+
31+
**Constraints:**
32+
33+
* `n == nums1.length`
34+
* `n == nums2.length`
35+
* `n == nums3.length`
36+
* `n == nums4.length`
37+
* `1 <= n <= 200`
38+
* <code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code>
39+
40+
## Solution
41+
42+
```kotlin
43+
class Solution {
44+
fun fourSumCount(nums1: IntArray, nums2: IntArray, nums3: IntArray, nums4: IntArray): Int {
45+
var count = 0
46+
val map: MutableMap<Int, Int> = HashMap()
47+
for (k in nums3) {
48+
for (i in nums4) {
49+
val sum = k + i
50+
map[sum] = map.getOrDefault(sum, 0) + 1
51+
}
52+
}
53+
for (k in nums1) {
54+
for (i in nums2) {
55+
val m = -(k + i)
56+
count += map.getOrDefault(m, 0)
57+
}
58+
}
59+
return count
60+
}
61+
}
62+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 455\. Assign Cookies
5+
6+
Easy
7+
8+
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.
9+
10+
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.
11+
12+
**Example 1:**
13+
14+
**Input:** g = [1,2,3], s = [1,1]
15+
16+
**Output:** 1
17+
18+
**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.
19+
20+
**Example 2:**
21+
22+
**Input:** g = [1,2], s = [1,2,3]
23+
24+
**Output:** 2
25+
26+
**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.
27+
28+
**Constraints:**
29+
30+
* <code>1 <= g.length <= 3 * 10<sup>4</sup></code>
31+
* <code>0 <= s.length <= 3 * 10<sup>4</sup></code>
32+
* <code>1 <= g[i], s[j] <= 2<sup>31</sup> - 1</code>
33+
34+
## Solution
35+
36+
```kotlin
37+
class Solution {
38+
fun findContentChildren(g: IntArray, s: IntArray): Int {
39+
g.sort()
40+
s.sort()
41+
var result = 0
42+
var i = 0
43+
var j = 0
44+
while (i < g.size && j < s.size) {
45+
if (s[j] >= g[i]) {
46+
result++
47+
i++
48+
}
49+
j++
50+
}
51+
return result
52+
}
53+
}
54+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 456\. 132 Pattern
5+
6+
Medium
7+
8+
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]`.
9+
10+
Return `true` _if there is a **132 pattern** in_ `nums`_, otherwise, return_ `false`_._
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [1,2,3,4]
15+
16+
**Output:** false
17+
18+
**Explanation:** There is no 132 pattern in the sequence.
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [3,1,4,2]
23+
24+
**Output:** true
25+
26+
**Explanation:** There is a 132 pattern in the sequence: [1, 4, 2].
27+
28+
**Example 3:**
29+
30+
**Input:** nums = [-1,3,2,0]
31+
32+
**Output:** true
33+
34+
**Explanation:** There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
35+
36+
**Constraints:**
37+
38+
* `n == nums.length`
39+
* <code>1 <= n <= 2 * 10<sup>5</sup></code>
40+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
41+
42+
## Solution
43+
44+
```kotlin
45+
import java.util.Deque
46+
import java.util.LinkedList
47+
48+
class Solution {
49+
/*
50+
* It scans only once, this is the power of using correct data structure.
51+
* It goes from the right to the left.
52+
* It keeps pushing elements into the stack,
53+
* but it also keeps poping elements out of the stack as long as the current element is bigger than this number.
54+
*/
55+
fun find132pattern(nums: IntArray): Boolean {
56+
val stack: Deque<Int> = LinkedList()
57+
var s3 = Int.MIN_VALUE
58+
for (i in nums.indices.reversed()) {
59+
if (nums[i] < s3) {
60+
return true
61+
} else {
62+
while (!stack.isEmpty() && nums[i] > stack.peek()) {
63+
s3 = Math.max(s3, stack.pop())
64+
}
65+
}
66+
stack.push(nums[i])
67+
}
68+
return false
69+
}
70+
}
71+
```
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 457\. Circular Array Loop
5+
6+
Medium
7+
8+
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`:
9+
10+
* If `nums[i]` is positive, move `nums[i]` steps **forward**, and
11+
* If `nums[i]` is negative, move `nums[i]` steps **backward**.
12+
13+
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.
14+
15+
A **cycle** in the array consists of a sequence of indices `seq` of length `k` where:
16+
17+
* Following the movement rules above results in the repeating index sequence `seq[0] -> seq[1] -> ... -> seq[k - 1] -> seq[0] -> ...`
18+
* Every `nums[seq[j]]` is either **all positive** or **all negative**.
19+
* `k > 1`
20+
21+
Return `true` _if there is a **cycle** in_ `nums`_, or_ `false` _otherwise_.
22+
23+
**Example 1:**
24+
25+
**Input:** nums = [2,-1,1,2,2]
26+
27+
**Output:** true
28+
29+
**Explanation:** There is a cycle from index 0 -> 2 -> 3 -> 0 -> ... The cycle's length is 3.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [-1,2]
34+
35+
**Output:** false
36+
37+
**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.
38+
39+
**Example 3:**
40+
41+
**Input:** nums = [-2,1,-1,-2,-2]
42+
43+
**Output:** false
44+
45+
**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.
46+
47+
**Constraints:**
48+
49+
* `1 <= nums.length <= 5000`
50+
* `-1000 <= nums[i] <= 1000`
51+
* `nums[i] != 0`
52+
53+
**Follow up:** Could you solve it in `O(n)` time complexity and `O(1)` extra space complexity?
54+
55+
## Solution
56+
57+
```kotlin
58+
class Solution {
59+
fun circularArrayLoop(arr: IntArray): Boolean {
60+
val n = arr.size
61+
val map: MutableMap<Int, Int> = HashMap()
62+
// arr[i]%n==0 (cycle)
63+
for (start in 0 until n) {
64+
if (map.containsKey(start)) {
65+
// check if already visited
66+
continue
67+
}
68+
var curr = start
69+
// Check if the current index is valid
70+
while (isValidCycle(start, curr, arr)) {
71+
// marking current index visited and set value as start of loop
72+
map[curr] = start
73+
// steps to jump;
74+
val jump = arr[curr] % n
75+
// Jumping x steps backwards is same as jumping (n-x) steps forward
76+
// going to next index;
77+
curr = (curr + jump + n) % n
78+
if (map.containsKey(curr)) {
79+
// value already processed
80+
if (map[curr] == start) {
81+
// If equal to start then we have found a loop
82+
return true
83+
}
84+
// Else we can break since this has already been visited hence we will get the
85+
// same result as before
86+
break
87+
}
88+
}
89+
}
90+
return false
91+
}
92+
93+
private fun isValidCycle(start: Int, curr: Int, arr: IntArray): Boolean {
94+
return (
95+
(arr[start] <= 0 || arr[curr] >= 0) &&
96+
(arr[start] >= 0 || arr[curr] <= 0) && arr[curr] % arr.size != 0
97+
)
98+
}
99+
}
100+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 458\. Poor Pigs
5+
6+
Hard
7+
8+
There are `buckets` buckets of liquid, where **exactly one** of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have `minutesToTest` minutes to determine which bucket is poisonous.
9+
10+
You can feed the pigs according to these steps:
11+
12+
1. Choose some live pigs to feed.
13+
2. For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time.
14+
3. Wait for `minutesToDie` minutes. You may **not** feed any other pigs during this time.
15+
4. After `minutesToDie` minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.
16+
5. Repeat this process until you run out of time.
17+
18+
Given `buckets`, `minutesToDie`, and `minutesToTest`, return _the **minimum** number of pigs needed to figure out which bucket is poisonous within the allotted time_.
19+
20+
**Example 1:**
21+
22+
**Input:** buckets = 1000, minutesToDie = 15, minutesToTest = 60
23+
24+
**Output:** 5
25+
26+
**Example 2:**
27+
28+
**Input:** buckets = 4, minutesToDie = 15, minutesToTest = 15
29+
30+
**Output:** 2
31+
32+
**Example 3:**
33+
34+
**Input:** buckets = 4, minutesToDie = 15, minutesToTest = 30
35+
36+
**Output:** 2
37+
38+
**Constraints:**
39+
40+
* `1 <= buckets <= 1000`
41+
* `1 <= minutesToDie <= minutesToTest <= 100`
42+
43+
## Solution
44+
45+
```kotlin
46+
@Suppress("NAME_SHADOWING")
47+
class Solution {
48+
fun poorPigs(buckets: Int, minutesToDie: Int, minutesToTest: Int): Int {
49+
var buckets = buckets
50+
if (buckets-- == 1) {
51+
return 0
52+
}
53+
val base = minutesToTest / minutesToDie + 1
54+
var count = 0
55+
while (buckets > 0) {
56+
buckets /= base
57+
count++
58+
}
59+
return count
60+
}
61+
}
62+
```

0 commit comments

Comments
 (0)