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
49 changes: 49 additions & 0 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[![](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)

## 116\. Populating Next Right Pointers in Each Node

Medium

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:

struct Node { int val; Node \*left; Node \*right; Node \*next; }

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

Initially, all next pointers are set to `NULL`.

**Example 1:**

![](https://assets.leetcode.com/uploads/2019/02/14/116_sample.png)

**Input:** root = [1,2,3,4,5,6,7]

**Output:** [1,#,2,3,#,4,5,6,7,#]

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

**Example 2:**

**Input:** root = []

**Output:** []

**Constraints:**

* The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.
* `-1000 <= Node.val <= 1000`

**Follow-up:**

* You may only use constant extra space.
* The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.

## Solution

```kotlin
import com_github_leetcode.left_right.Node

/*
* Definition for a Node.
* class Node(var `val`: Int) {
* var left: Node? = null
* var right: Node? = null
* var next: Node? = null
* }
*/

class Solution {
fun connect(root: Node?): Node? {
if (root == null) {
return null
}
if (root.left != null && root.right != null) {
root.left!!.next = root.right
}
if (root.next != null && root.right != null) {
root.right!!.next = root.next!!.left
}
connect(root.left)
connect(root.right)
return root
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
[![](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)

## 117\. Populating Next Right Pointers in Each Node II

Medium

Given a binary tree

struct Node { int val; Node \*left; Node \*right; Node \*next; }

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

Initially, all next pointers are set to `NULL`.

**Example 1:**

![](https://assets.leetcode.com/uploads/2019/02/15/117_sample.png)

**Input:** root = [1,2,3,4,5,null,7]

**Output:** [1,#,2,3,#,4,5,7,#]

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

**Example 2:**

**Input:** root = []

**Output:** []

**Constraints:**

* The number of nodes in the tree is in the range `[0, 6000]`.
* `-100 <= Node.val <= 100`

**Follow-up:**

* You may only use constant extra space.
* The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.

## Solution

```kotlin
import com_github_leetcode.left_right.Node
import java.util.LinkedList
import java.util.Queue

/*
* Definition for a Node.
* class Node(var `val`: Int) {
* var left: Node? = null
* var right: Node? = null
* var next: Node? = null
* }
*/
class Solution {
fun connect(root: Node?): Node? {

if (root == null) return null

val bfsQueue: Queue<Node> = LinkedList()

bfsQueue.offer(root)
root.next = null

var temp: Node?
var prev: Node?

while (!bfsQueue.isEmpty()) {

val size = bfsQueue.size
prev = null

for (j in 0 until size) {

temp = bfsQueue.poll()
if (prev != null) prev.next = temp
if (temp!!.left != null) bfsQueue.offer(temp.left)
if (temp.right != null) bfsQueue.offer(temp.right)
prev = temp
}
}

return root
}
}
```
51 changes: 51 additions & 0 deletions src/main/kotlin/g0101_0200/s0118_pascals_triangle/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
[![](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)

## 118\. Pascal's Triangle

Easy

Given an integer `numRows`, return the first numRows of **Pascal's triangle**.

In **Pascal's triangle**, each number is the sum of the two numbers directly above it as shown:

![](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)

**Example 1:**

**Input:** numRows = 5

**Output:** [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

**Example 2:**

**Input:** numRows = 1

**Output:** [[1]]

**Constraints:**

* `1 <= numRows <= 30`

## Solution

```kotlin
class Solution {
fun generate(numRows: Int): List<List<Int>> {
val output: MutableList<List<Int>> = ArrayList()
for (i in 0 until numRows) {
val currRow: MutableList<Int> = ArrayList()
for (j in 0..i) {
if (j == 0 || j == i || i <= 1) {
currRow.add(1)
} else {
val currCell = output[i - 1][j - 1] + output[i - 1][j]
currRow.add(currCell)
}
}
output.add(currRow)
}
return output
}
}
```
68 changes: 68 additions & 0 deletions src/main/kotlin/g0101_0200/s0119_pascals_triangle_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
[![](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)

## 119\. Pascal's Triangle II

Easy

Given an integer `rowIndex`, return the <code>rowIndex<sup>th</sup></code> (**0-indexed**) row of the **Pascal's triangle**.

In **Pascal's triangle**, each number is the sum of the two numbers directly above it as shown:

![](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)

**Example 1:**

**Input:** rowIndex = 3

**Output:** [1,3,3,1]

**Example 2:**

**Input:** rowIndex = 0

**Output:** [1]

**Example 3:**

**Input:** rowIndex = 1

**Output:** [1,1]

**Constraints:**

* `0 <= rowIndex <= 33`

**Follow up:** Could you optimize your algorithm to use only `O(rowIndex)` extra space?

## Solution

```kotlin
class Solution {
fun getRow(rowIndex: Int): List<Int> {
val buffer = IntArray(rowIndex + 1)
buffer[0] = 1
computeRow(buffer, 1)
// Copy buffer to List of Integer.
val ans: MutableList<Int> = ArrayList(buffer.size)
for (j in buffer) {
ans.add(j)
}
return ans
}

private fun computeRow(buffer: IntArray, k: Int) {
if (k >= buffer.size) {
return
}
var previous = buffer[0]
for (i in 1 until k) {
val tmp = previous + buffer[i]
previous = buffer[i]
buffer[i] = tmp
}
buffer[k] = 1
computeRow(buffer, k + 1)
}
}
```
80 changes: 80 additions & 0 deletions src/main/kotlin/g0101_0200/s0120_triangle/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[![](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)

## 120\. Triangle

Medium

Given a `triangle` array, return _the minimum path sum from top to bottom_.

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.

**Example 1:**

**Input:** triangle = \[\[2],[3,4],[6,5,7],[4,1,8,3]]

**Output:** 11

**Explanation:** The triangle looks like:

<ins>2</ins>

<ins>3</ins> 4

6 <ins>5</ins> 7

4 <ins>1</ins> 8 3

The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).

**Example 2:**

**Input:** triangle = \[\[-10]]

**Output:** -10

**Constraints:**

* `1 <= triangle.length <= 200`
* `triangle[0].length == 1`
* `triangle[i].length == triangle[i - 1].length + 1`
* <code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code>

**Follow up:** Could you do this using only `O(n)` extra space, where `n` is the total number of rows in the triangle?

## Solution

```kotlin
import java.util.Arrays

class Solution {
fun minimumTotal(triangle: List<List<Int>>?): Int {
if (triangle == null || triangle.isEmpty()) {
return 0
}
val dp = Array(triangle.size) { IntArray(triangle[triangle.size - 1].size) }
for (temp in dp) {
Arrays.fill(temp, -10001)
}
return dfs(triangle, dp, 0, 0)
}

private fun dfs(triangle: List<List<Int>>, dp: Array<IntArray>, row: Int, col: Int): Int {
if (row >= triangle.size) {
return 0
}
if (dp[row][col] != -10001) {
return dp[row][col]
}
val sum = (
triangle[row][col] +
Math.min(
dfs(triangle, dp, row + 1, col),
dfs(triangle, dp, row + 1, col + 1)
)
)
dp[row][col] = sum
return sum
}
}
```
Loading