Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added tasks 454-480.
  • Loading branch information
javadev committed Jan 2, 2023
commit 45931aad046032593f5cc0d389de4879feef94f4
311 changes: 169 additions & 142 deletions README.md

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions src/main/kotlin/g0401_0500/s0454_4sum_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 454\. 4Sum II

Medium

Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:

* `0 <= i, j, k, l < n`
* `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`

**Example 1:**

**Input:** nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]

**Output:** 2

**Explanation:** The two tuples are:

1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0

2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0

**Example 2:**

**Input:** nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]

**Output:** 1

**Constraints:**

* `n == nums1.length`
* `n == nums2.length`
* `n == nums3.length`
* `n == nums4.length`
* `1 <= n <= 200`
* <code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code>

## Solution

```kotlin
class Solution {
fun fourSumCount(nums1: IntArray, nums2: IntArray, nums3: IntArray, nums4: IntArray): Int {
var count = 0
val map: MutableMap<Int, Int> = HashMap()
for (k in nums3) {
for (i in nums4) {
val sum = k + i
map[sum] = map.getOrDefault(sum, 0) + 1
}
}
for (k in nums1) {
for (i in nums2) {
val m = -(k + i)
count += map.getOrDefault(m, 0)
}
}
return count
}
}
```
54 changes: 54 additions & 0 deletions src/main/kotlin/g0401_0500/s0455_assign_cookies/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 455\. Assign Cookies

Easy

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.

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.

**Example 1:**

**Input:** g = [1,2,3], s = [1,1]

**Output:** 1

**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.

**Example 2:**

**Input:** g = [1,2], s = [1,2,3]

**Output:** 2

**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.

**Constraints:**

* <code>1 <= g.length <= 3 * 10<sup>4</sup></code>
* <code>0 <= s.length <= 3 * 10<sup>4</sup></code>
* <code>1 <= g[i], s[j] <= 2<sup>31</sup> - 1</code>

## Solution

```kotlin
class Solution {
fun findContentChildren(g: IntArray, s: IntArray): Int {
g.sort()
s.sort()
var result = 0
var i = 0
var j = 0
while (i < g.size && j < s.size) {
if (s[j] >= g[i]) {
result++
i++
}
j++
}
return result
}
}
```
71 changes: 71 additions & 0 deletions src/main/kotlin/g0401_0500/s0456_132_pattern/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 456\. 132 Pattern

Medium

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]`.

Return `true` _if there is a **132 pattern** in_ `nums`_, otherwise, return_ `false`_._

**Example 1:**

**Input:** nums = [1,2,3,4]

**Output:** false

**Explanation:** There is no 132 pattern in the sequence.

**Example 2:**

**Input:** nums = [3,1,4,2]

**Output:** true

**Explanation:** There is a 132 pattern in the sequence: [1, 4, 2].

**Example 3:**

**Input:** nums = [-1,3,2,0]

**Output:** true

**Explanation:** There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].

**Constraints:**

* `n == nums.length`
* <code>1 <= n <= 2 * 10<sup>5</sup></code>
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>

## Solution

```kotlin
import java.util.Deque
import java.util.LinkedList

class Solution {
/*
* It scans only once, this is the power of using correct data structure.
* It goes from the right to the left.
* It keeps pushing elements into the stack,
* but it also keeps poping elements out of the stack as long as the current element is bigger than this number.
*/
fun find132pattern(nums: IntArray): Boolean {
val stack: Deque<Int> = LinkedList()
var s3 = Int.MIN_VALUE
for (i in nums.indices.reversed()) {
if (nums[i] < s3) {
return true
} else {
while (!stack.isEmpty() && nums[i] > stack.peek()) {
s3 = Math.max(s3, stack.pop())
}
}
stack.push(nums[i])
}
return false
}
}
```
100 changes: 100 additions & 0 deletions src/main/kotlin/g0401_0500/s0457_circular_array_loop/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 457\. Circular Array Loop

Medium

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`:

* If `nums[i]` is positive, move `nums[i]` steps **forward**, and
* If `nums[i]` is negative, move `nums[i]` steps **backward**.

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.

A **cycle** in the array consists of a sequence of indices `seq` of length `k` where:

* Following the movement rules above results in the repeating index sequence `seq[0] -> seq[1] -> ... -> seq[k - 1] -> seq[0] -> ...`
* Every `nums[seq[j]]` is either **all positive** or **all negative**.
* `k > 1`

Return `true` _if there is a **cycle** in_ `nums`_, or_ `false` _otherwise_.

**Example 1:**

**Input:** nums = [2,-1,1,2,2]

**Output:** true

**Explanation:** There is a cycle from index 0 -> 2 -> 3 -> 0 -> ... The cycle's length is 3.

**Example 2:**

**Input:** nums = [-1,2]

**Output:** false

**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.

**Example 3:**

**Input:** nums = [-2,1,-1,-2,-2]

**Output:** false

**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.

**Constraints:**

* `1 <= nums.length <= 5000`
* `-1000 <= nums[i] <= 1000`
* `nums[i] != 0`

**Follow up:** Could you solve it in `O(n)` time complexity and `O(1)` extra space complexity?

## Solution

```kotlin
class Solution {
fun circularArrayLoop(arr: IntArray): Boolean {
val n = arr.size
val map: MutableMap<Int, Int> = HashMap()
// arr[i]%n==0 (cycle)
for (start in 0 until n) {
if (map.containsKey(start)) {
// check if already visited
continue
}
var curr = start
// Check if the current index is valid
while (isValidCycle(start, curr, arr)) {
// marking current index visited and set value as start of loop
map[curr] = start
// steps to jump;
val jump = arr[curr] % n
// Jumping x steps backwards is same as jumping (n-x) steps forward
// going to next index;
curr = (curr + jump + n) % n
if (map.containsKey(curr)) {
// value already processed
if (map[curr] == start) {
// If equal to start then we have found a loop
return true
}
// Else we can break since this has already been visited hence we will get the
// same result as before
break
}
}
}
return false
}

private fun isValidCycle(start: Int, curr: Int, arr: IntArray): Boolean {
return (
(arr[start] <= 0 || arr[curr] >= 0) &&
(arr[start] >= 0 || arr[curr] <= 0) && arr[curr] % arr.size != 0
)
}
}
```
62 changes: 62 additions & 0 deletions src/main/kotlin/g0401_0500/s0458_poor_pigs/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 458\. Poor Pigs

Hard

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.

You can feed the pigs according to these steps:

1. Choose some live pigs to feed.
2. For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time.
3. Wait for `minutesToDie` minutes. You may **not** feed any other pigs during this time.
4. After `minutesToDie` minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.
5. Repeat this process until you run out of time.

Given `buckets`, `minutesToDie`, and `minutesToTest`, return _the **minimum** number of pigs needed to figure out which bucket is poisonous within the allotted time_.

**Example 1:**

**Input:** buckets = 1000, minutesToDie = 15, minutesToTest = 60

**Output:** 5

**Example 2:**

**Input:** buckets = 4, minutesToDie = 15, minutesToTest = 15

**Output:** 2

**Example 3:**

**Input:** buckets = 4, minutesToDie = 15, minutesToTest = 30

**Output:** 2

**Constraints:**

* `1 <= buckets <= 1000`
* `1 <= minutesToDie <= minutesToTest <= 100`

## Solution

```kotlin
@Suppress("NAME_SHADOWING")
class Solution {
fun poorPigs(buckets: Int, minutesToDie: Int, minutesToTest: Int): Int {
var buckets = buckets
if (buckets-- == 1) {
return 0
}
val base = minutesToTest / minutesToDie + 1
var count = 0
while (buckets > 0) {
buckets /= base
count++
}
return count
}
}
```
Loading