Skip to content

Added tasks 11-25 #4

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 1 commit into from
Sep 26, 2023
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
214 changes: 122 additions & 92 deletions README.md

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions src/main/ts/g0001_0100/s0011_container_with_most_water/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[![](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)

## 11\. Container With Most Water

Medium

Given `n` non-negative integers <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code> , where each represents a point at coordinate <code>(i, a<sub>i</sub>)</code>. `n` vertical lines are drawn such that the two endpoints of the line `i` is at <code>(i, a<sub>i</sub>)</code> and `(i, 0)`. Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.

**Notice** that you may not slant the container.

**Example 1:**

![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg)

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

**Output:** 49

**Explanation:** The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

**Example 2:**

**Input:** height = [1,1]

**Output:** 1

**Example 3:**

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

**Output:** 16

**Example 4:**

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

**Output:** 2

**Constraints:**

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

## Solution

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

export { maxArea }
```
74 changes: 74 additions & 0 deletions src/main/ts/g0001_0100/s0015_3sum/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
[![](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)

## 15\. 3Sum

Medium

Given an integer array nums, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`.

Notice that the solution set must not contain duplicate triplets.

**Example 1:**

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

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

**Example 2:**

**Input:** nums = []

**Output:** []

**Example 3:**

**Input:** nums = [0]

**Output:** []

**Constraints:**

* `0 <= nums.length <= 3000`
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>

## Solution

```typescript
function threeSum(nums: number[]): number[][] { //NOSONAR
nums.sort((a, b) => a - b)
const len = nums.length
const result: number[][] = []
let l: number
let r: number
for (let i = 0; i < len - 2; i++) {
l = i + 1
r = len - 1
while (r > l) {
const sum = nums[i] + nums[l] + nums[r]
if (sum < 0) {
l++
} else if (sum > 0) {
r--
} else {
const list: number[] = [nums[i], nums[l], nums[r]]
result.push(list)
while (l < r && nums[l + 1] === nums[l]) {
l++
}
while (r > l && nums[r - 1] === nums[r]) {
r--
}
l++
r--
}
}
while (i < len - 1 && nums[i + 1] === nums[i]) {
i++ //NOSONAR
}
}
return result
}

export { threeSum }
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
[![](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)

## 17\. Letter Combinations of a Phone Number

Medium

Given a string containing digits from `2-9` inclusive, return all possible letter combinations that the number could represent. Return the answer in **any order**.

A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

![](https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png)

**Example 1:**

**Input:** digits = "23"

**Output:** ["ad","ae","af","bd","be","bf","cd","ce","cf"]

**Example 2:**

**Input:** digits = ""

**Output:** []

**Example 3:**

**Input:** digits = "2"

**Output:** ["a","b","c"]

**Constraints:**

* `0 <= digits.length <= 4`
* `digits[i]` is a digit in the range `['2', '9']`.

## Solution

```typescript
function letterCombinations(digits: string): string[] {
if (digits.length === 0) {
return []
}
const letters: string[] = ['', '', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
const ans: string[] = []
const sb: string[] = []
findCombinations(0, digits, letters, sb, ans)
return ans
}

function findCombinations(start: number, nums: string, letters: string[], curr: string[], ans: string[]): void {
if (curr.length === nums.length) {
ans.push(curr.join(''))
return
}
for (let i = start; i < nums.length; i++) {
const n = parseInt(nums.charAt(i))
for (let j = 0; j < letters[n].length; j++) {
const ch = letters[n].charAt(j)
curr.push(ch)
findCombinations(i + 1, nums, letters, curr, ans)
curr.pop()
}
}
}

export { letterCombinations }
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[![](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)

## 19\. Remove Nth Node From End of List

Medium

Given the `head` of a linked list, remove the `nth` node from the end of the list and return its head.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg)

**Input:** head = [1,2,3,4,5], n = 2

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

**Example 2:**

**Input:** head = [1], n = 1

**Output:** []

**Example 3:**

**Input:** head = [1,2], n = 1

**Output:** [1]

**Constraints:**

* The number of nodes in the list is `sz`.
* `1 <= sz <= 30`
* `0 <= Node.val <= 100`
* `1 <= n <= sz`

**Follow up:** Could you do this in one pass?

## Solution

```typescript
import { ListNode } from '../../com_github_leetcode/listnode'

let localN: number

function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
localN = n
const dummy = new ListNode(0)
dummy.next = head
removeNth(dummy)
return dummy.next
}

function removeNth(node: ListNode | null): void {
if (!node || !node.next) { //NOSONAR
return
}
removeNth(node.next)
localN--

if (localN === 0) {
node.next = node.next?.next || null //NOSONAR
}
}

export { removeNthFromEnd }
```
73 changes: 73 additions & 0 deletions src/main/ts/g0001_0100/s0020_valid_parentheses/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
[![](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)

## 20\. Valid Parentheses

Easy

Given a string `s` containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid.

An input string is valid if:

1. Open brackets must be closed by the same type of brackets.
2. Open brackets must be closed in the correct order.

**Example 1:**

**Input:** s = "()"

**Output:** true

**Example 2:**

**Input:** s = "()[]{}"

**Output:** true

**Example 3:**

**Input:** s = "(]"

**Output:** false

**Example 4:**

**Input:** s = "([)]"

**Output:** false

**Example 5:**

**Input:** s = "{[]}"

**Output:** true

**Constraints:**

* <code>1 <= s.length <= 10<sup>4</sup></code>
* `s` consists of parentheses only `'()[]{}'`.

## Solution

```typescript
function isValid(s: string): boolean {
const stack: string[] = []
for (let i = 0; i < s.length; i++) {
const c = s.charAt(i)
if (c === '(' || c === '[' || c === '{') {
stack.push(c)
} else if (
(c === ')' && stack.length > 0 && stack[stack.length - 1] === '(') ||
(c === '}' && stack.length > 0 && stack[stack.length - 1] === '{') ||
(c === ']' && stack.length > 0 && stack[stack.length - 1] === '[')
) {
stack.pop()
} else {
return false
}
}
return stack.length === 0
}

export { isValid }
```
Loading