Skip to content

Commit 296a539

Browse files
authored
Added tasks 116-150.
1 parent 72836a2 commit 296a539

File tree

25 files changed

+1953
-0
lines changed

25 files changed

+1953
-0
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
## 116\. Populating Next Right Pointers in Each Node
5+
6+
Medium
7+
8+
You are given a **perfect binary tree** where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
9+
10+
struct Node { int val; Node \*left; Node \*right; Node \*next; }
11+
12+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to `NULL`.
13+
14+
Initially, all next pointers are set to `NULL`.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2019/02/14/116_sample.png)
19+
20+
**Input:** root = [1,2,3,4,5,6,7]
21+
22+
**Output:** [1,#,2,3,#,4,5,6,7,#]
23+
24+
**Explanation:** Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
25+
26+
**Example 2:**
27+
28+
**Input:** root = []
29+
30+
**Output:** []
31+
32+
**Constraints:**
33+
34+
* The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.
35+
* `-1000 <= Node.val <= 1000`
36+
37+
**Follow-up:**
38+
39+
* You may only use constant extra space.
40+
* The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.
41+
42+
## Solution
43+
44+
```kotlin
45+
import com_github_leetcode.left_right.Node
46+
47+
/*
48+
* Definition for a Node.
49+
* class Node(var `val`: Int) {
50+
* var left: Node? = null
51+
* var right: Node? = null
52+
* var next: Node? = null
53+
* }
54+
*/
55+
56+
class Solution {
57+
fun connect(root: Node?): Node? {
58+
if (root == null) {
59+
return null
60+
}
61+
if (root.left != null && root.right != null) {
62+
root.left!!.next = root.right
63+
}
64+
if (root.next != null && root.right != null) {
65+
root.right!!.next = root.next!!.left
66+
}
67+
connect(root.left)
68+
connect(root.right)
69+
return root
70+
}
71+
}
72+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
## 117\. Populating Next Right Pointers in Each Node II
5+
6+
Medium
7+
8+
Given a binary tree
9+
10+
struct Node { int val; Node \*left; Node \*right; Node \*next; }
11+
12+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to `NULL`.
13+
14+
Initially, all next pointers are set to `NULL`.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2019/02/15/117_sample.png)
19+
20+
**Input:** root = [1,2,3,4,5,null,7]
21+
22+
**Output:** [1,#,2,3,#,4,5,7,#]
23+
24+
**Explanation:** Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
25+
26+
**Example 2:**
27+
28+
**Input:** root = []
29+
30+
**Output:** []
31+
32+
**Constraints:**
33+
34+
* The number of nodes in the tree is in the range `[0, 6000]`.
35+
* `-100 <= Node.val <= 100`
36+
37+
**Follow-up:**
38+
39+
* You may only use constant extra space.
40+
* The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.
41+
42+
## Solution
43+
44+
```kotlin
45+
import com_github_leetcode.left_right.Node
46+
import java.util.LinkedList
47+
import java.util.Queue
48+
49+
/*
50+
* Definition for a Node.
51+
* class Node(var `val`: Int) {
52+
* var left: Node? = null
53+
* var right: Node? = null
54+
* var next: Node? = null
55+
* }
56+
*/
57+
class Solution {
58+
fun connect(root: Node?): Node? {
59+
60+
if (root == null) return null
61+
62+
val bfsQueue: Queue<Node> = LinkedList()
63+
64+
bfsQueue.offer(root)
65+
root.next = null
66+
67+
var temp: Node?
68+
var prev: Node?
69+
70+
while (!bfsQueue.isEmpty()) {
71+
72+
val size = bfsQueue.size
73+
prev = null
74+
75+
for (j in 0 until size) {
76+
77+
temp = bfsQueue.poll()
78+
if (prev != null) prev.next = temp
79+
if (temp!!.left != null) bfsQueue.offer(temp.left)
80+
if (temp.right != null) bfsQueue.offer(temp.right)
81+
prev = temp
82+
}
83+
}
84+
85+
return root
86+
}
87+
}
88+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
## 118\. Pascal's Triangle
5+
6+
Easy
7+
8+
Given an integer `numRows`, return the first numRows of **Pascal's triangle**.
9+
10+
In **Pascal's triangle**, each number is the sum of the two numbers directly above it as shown:
11+
12+
![](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
13+
14+
**Example 1:**
15+
16+
**Input:** numRows = 5
17+
18+
**Output:** [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
19+
20+
**Example 2:**
21+
22+
**Input:** numRows = 1
23+
24+
**Output:** [[1]]
25+
26+
**Constraints:**
27+
28+
* `1 <= numRows <= 30`
29+
30+
## Solution
31+
32+
```kotlin
33+
class Solution {
34+
fun generate(numRows: Int): List<List<Int>> {
35+
val output: MutableList<List<Int>> = ArrayList()
36+
for (i in 0 until numRows) {
37+
val currRow: MutableList<Int> = ArrayList()
38+
for (j in 0..i) {
39+
if (j == 0 || j == i || i <= 1) {
40+
currRow.add(1)
41+
} else {
42+
val currCell = output[i - 1][j - 1] + output[i - 1][j]
43+
currRow.add(currCell)
44+
}
45+
}
46+
output.add(currRow)
47+
}
48+
return output
49+
}
50+
}
51+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
## 119\. Pascal's Triangle II
5+
6+
Easy
7+
8+
Given an integer `rowIndex`, return the <code>rowIndex<sup>th</sup></code> (**0-indexed**) row of the **Pascal's triangle**.
9+
10+
In **Pascal's triangle**, each number is the sum of the two numbers directly above it as shown:
11+
12+
![](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
13+
14+
**Example 1:**
15+
16+
**Input:** rowIndex = 3
17+
18+
**Output:** [1,3,3,1]
19+
20+
**Example 2:**
21+
22+
**Input:** rowIndex = 0
23+
24+
**Output:** [1]
25+
26+
**Example 3:**
27+
28+
**Input:** rowIndex = 1
29+
30+
**Output:** [1,1]
31+
32+
**Constraints:**
33+
34+
* `0 <= rowIndex <= 33`
35+
36+
**Follow up:** Could you optimize your algorithm to use only `O(rowIndex)` extra space?
37+
38+
## Solution
39+
40+
```kotlin
41+
class Solution {
42+
fun getRow(rowIndex: Int): List<Int> {
43+
val buffer = IntArray(rowIndex + 1)
44+
buffer[0] = 1
45+
computeRow(buffer, 1)
46+
// Copy buffer to List of Integer.
47+
val ans: MutableList<Int> = ArrayList(buffer.size)
48+
for (j in buffer) {
49+
ans.add(j)
50+
}
51+
return ans
52+
}
53+
54+
private fun computeRow(buffer: IntArray, k: Int) {
55+
if (k >= buffer.size) {
56+
return
57+
}
58+
var previous = buffer[0]
59+
for (i in 1 until k) {
60+
val tmp = previous + buffer[i]
61+
previous = buffer[i]
62+
buffer[i] = tmp
63+
}
64+
buffer[k] = 1
65+
computeRow(buffer, k + 1)
66+
}
67+
}
68+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
## 120\. Triangle
5+
6+
Medium
7+
8+
Given a `triangle` array, return _the minimum path sum from top to bottom_.
9+
10+
For each step, you may move to an adjacent number of the row below. More formally, if you are on index `i` on the current row, you may move to either index `i` or index `i + 1` on the next row.
11+
12+
**Example 1:**
13+
14+
**Input:** triangle = \[\[2],[3,4],[6,5,7],[4,1,8,3]]
15+
16+
**Output:** 11
17+
18+
**Explanation:** The triangle looks like:
19+
20+
<ins>2</ins>
21+
22+
<ins>3</ins> 4
23+
24+
6 <ins>5</ins> 7
25+
26+
4 <ins>1</ins> 8 3
27+
28+
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
29+
30+
**Example 2:**
31+
32+
**Input:** triangle = \[\[-10]]
33+
34+
**Output:** -10
35+
36+
**Constraints:**
37+
38+
* `1 <= triangle.length <= 200`
39+
* `triangle[0].length == 1`
40+
* `triangle[i].length == triangle[i - 1].length + 1`
41+
* <code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code>
42+
43+
**Follow up:** Could you do this using only `O(n)` extra space, where `n` is the total number of rows in the triangle?
44+
45+
## Solution
46+
47+
```kotlin
48+
import java.util.Arrays
49+
50+
class Solution {
51+
fun minimumTotal(triangle: List<List<Int>>?): Int {
52+
if (triangle == null || triangle.isEmpty()) {
53+
return 0
54+
}
55+
val dp = Array(triangle.size) { IntArray(triangle[triangle.size - 1].size) }
56+
for (temp in dp) {
57+
Arrays.fill(temp, -10001)
58+
}
59+
return dfs(triangle, dp, 0, 0)
60+
}
61+
62+
private fun dfs(triangle: List<List<Int>>, dp: Array<IntArray>, row: Int, col: Int): Int {
63+
if (row >= triangle.size) {
64+
return 0
65+
}
66+
if (dp[row][col] != -10001) {
67+
return dp[row][col]
68+
}
69+
val sum = (
70+
triangle[row][col] +
71+
Math.min(
72+
dfs(triangle, dp, row + 1, col),
73+
dfs(triangle, dp, row + 1, col + 1)
74+
)
75+
)
76+
dp[row][col] = sum
77+
return sum
78+
}
79+
}
80+
```

0 commit comments

Comments
 (0)