Skip to content

Updated readme #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Added files
  • Loading branch information
javadev committed Oct 1, 2023
commit 09386719d99ef51b99195c7d5cdbf84981410735
81 changes: 81 additions & 0 deletions src/main/ts/g0001_0100/s0039_combination_sum/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)

## 39\. Combination Sum

Medium

Given an array of **distinct** integers `candidates` and a target integer `target`, return _a list of all **unique combinations** of_ `candidates` _where the chosen numbers sum to_ `target`_._ You may return the combinations in **any order**.

The **same** number may be chosen from `candidates` an **unlimited number of times**. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

It is **guaranteed** that the number of unique combinations that sum up to `target` is less than `150` combinations for the given input.

**Example 1:**

**Input:** candidates = [2,3,6,7], target = 7

**Output:** [[2,2,3],[7]]

**Explanation:**

2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.

**Example 2:**

**Input:** candidates = [2,3,5], target = 8

**Output:** [[2,2,2,2],[2,3,3],[3,5]]

**Example 3:**

**Input:** candidates = [2], target = 1

**Output:** []

**Example 4:**

**Input:** candidates = [1], target = 1

**Output:** [[1]]

**Example 5:**

**Input:** candidates = [1], target = 2

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

**Constraints:**

* `1 <= candidates.length <= 30`
* `1 <= candidates[i] <= 200`
* All elements of `candidates` are **distinct**.
* `1 <= target <= 500`

## Solution

```typescript
function combinationSum(candidates: number[], target: number): number[][] {
const result: number[][] = []
const path: number[] = []

const comFunct = (index: number, sum: number) => {
if (sum === target) {
result.push([...path])
return
}
if (sum > target) return
for (let i = index; i < candidates.length; i++) {
path.push(candidates[i])
comFunct(i, sum + candidates[i])
path.pop()
}
}
comFunct(0, 0)
return result
}

export { combinationSum }
```
57 changes: 57 additions & 0 deletions src/main/ts/g0001_0100/s0041_first_missing_positive/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)

## 41\. First Missing Positive

Hard

Given an unsorted integer array `nums`, return the smallest missing positive integer.

You must implement an algorithm that runs in `O(n)` time and uses constant extra space.

**Example 1:**

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

**Output:** 3

**Example 2:**

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

**Output:** 2

**Example 3:**

**Input:** nums = [7,8,9,11,12]

**Output:** 1

**Constraints:**

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

## Solution

```typescript
function firstMissingPositive(nums: number[]): number {
let i = 0
while (i < nums.length) {
let correctIndex = nums[i] - 1
if (nums[i] > 0 && nums[i] <= nums.length && nums[i] !== nums[correctIndex]) {
;[nums[i], nums[correctIndex]] = [nums[correctIndex], nums[i]]
} else {
i++
}
}
for (let i = 0; i < nums.length; i++) {
if (nums[i] !== i + 1) {
return i + 1
}
}
return nums.length + 1
}

export { firstMissingPositive }
```
56 changes: 56 additions & 0 deletions src/main/ts/g0001_0100/s0042_trapping_rain_water/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)

## 42\. Trapping Rain Water

Hard

Given `n` non-negative integers representing an elevation map where the width of each bar is `1`, compute how much water it can trap after raining.

**Example 1:**

![](https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png)

**Input:** height = [0,1,0,2,1,0,1,3,2,1,2,1]

**Output:** 6

**Explanation:** The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.

**Example 2:**

**Input:** height = [4,2,0,3,2,5]

**Output:** 9

**Constraints:**

* `n == height.length`
* <code>1 <= n <= 2 * 10<sup>4</sup></code>
* <code>0 <= height[i] <= 10<sup>5</sup></code>

## Solution

```typescript
function trap(height: number[]): number {
let result = 0
let left = 1
let right = height.length - 2
let leftMax = height[left - 1]
let rightMax = height[right + 1]
while (left <= right) {
if (leftMax < rightMax) {
leftMax = Math.max(leftMax, height[left])
result += leftMax - height[left]
left++
} else {
rightMax = Math.max(rightMax, height[right])
result += rightMax - height[right]
right--
}
}
return result
}

export { trap }
```
75 changes: 75 additions & 0 deletions src/main/ts/g0001_0100/s0045_jump_game_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)

## 45\. Jump Game II

Medium

Given an array of non-negative integers `nums`, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps.

You can assume that you can always reach the last index.

**Example 1:**

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

**Output:** 2

**Explanation:** The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.

**Example 2:**

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

**Output:** 2

**Constraints:**

* <code>1 <= nums.length <= 10<sup>4</sup></code>
* `0 <= nums[i] <= 1000`

## Solution

```typescript
function jump(nums: number[]): number { //NOSONAR
let minJmp = new Array(nums.length)
if (nums.length === 1) return 0
let prevIndex = 0
minJmp[prevIndex] = 0
while (prevIndex < nums.length - 1) {
let nextMaxJmpTo = nums[prevIndex] + prevIndex
let prevIndexJmp = minJmp[prevIndex]

let farthestJumpVal = -1
let farthestJumpIndex = -1
for (let i = nextMaxJmpTo; ; i--) {
if (i >= nums.length) {
continue
}
if (i === nums.length - 1) {
return prevIndexJmp + 1
}
if (minJmp[i] != undefined) {
break
}
minJmp[i] = prevIndexJmp + 1
let curmaxTo = nums[i] + i
if (farthestJumpVal < curmaxTo) {
farthestJumpVal = curmaxTo
farthestJumpIndex = i
}
}
if (farthestJumpIndex === -1) {
return -1
}
prevIndex = farthestJumpIndex
}
return minJmp[nums.length - 1]
}

export { jump }
```
54 changes: 54 additions & 0 deletions src/main/ts/g0001_0100/s0046_permutations/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)

## 46\. Permutations

Medium

Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.

**Example 1:**

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

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

**Example 2:**

**Input:** nums = [0,1]

**Output:** [[0,1],[1,0]]

**Example 3:**

**Input:** nums = [1]

**Output:** [[1]]

**Constraints:**

* `1 <= nums.length <= 6`
* `-10 <= nums[i] <= 10`
* All the integers of `nums` are **unique**.

## Solution

```typescript
function permute(nums: number[]): number[][] {
const result: number[][] = []
const permuteRecursive = (nums: number[], index: number, result: number[][]) => {
if (index >= nums.length) {
result.push([...nums])
}
for (let i = index; i < nums.length; i++) {
;[nums[index], nums[i]] = [nums[i], nums[index]]
permuteRecursive(nums, index + 1, result)
;[nums[index], nums[i]] = [nums[i], nums[index]]
}
}
permuteRecursive(nums, 0, result)
return result
}

export { permute }
```
Loading