Skip to content

Commit eea057a

Browse files
authored
Added tasks 11-25
1 parent 9036231 commit eea057a

File tree

11 files changed

+890
-92
lines changed

11 files changed

+890
-92
lines changed

README.md

+122-92
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[![](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)
2+
[![](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)
3+
4+
## 11\. Container With Most Water
5+
6+
Medium
7+
8+
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.
9+
10+
**Notice** that you may not slant the container.
11+
12+
**Example 1:**
13+
14+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg)
15+
16+
**Input:** height = [1,8,6,2,5,4,8,3,7]
17+
18+
**Output:** 49
19+
20+
**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.
21+
22+
**Example 2:**
23+
24+
**Input:** height = [1,1]
25+
26+
**Output:** 1
27+
28+
**Example 3:**
29+
30+
**Input:** height = [4,3,2,1,4]
31+
32+
**Output:** 16
33+
34+
**Example 4:**
35+
36+
**Input:** height = [1,2,1]
37+
38+
**Output:** 2
39+
40+
**Constraints:**
41+
42+
* `n == height.length`
43+
* <code>2 <= n <= 10<sup>5</sup></code>
44+
* <code>0 <= height[i] <= 10<sup>4</sup></code>
45+
46+
## Solution
47+
48+
```typescript
49+
function maxArea(height: number[]): number {
50+
let maxArea = -1
51+
let left = 0
52+
let right = height.length - 1
53+
while (left < right) {
54+
if (height[left] < height[right]) {
55+
maxArea = Math.max(maxArea, height[left] * (right - left))
56+
left++
57+
} else {
58+
maxArea = Math.max(maxArea, height[right] * (right - left))
59+
right--
60+
}
61+
}
62+
return maxArea
63+
}
64+
65+
export { maxArea }
66+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[![](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)
2+
[![](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)
3+
4+
## 15\. 3Sum
5+
6+
Medium
7+
8+
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`.
9+
10+
Notice that the solution set must not contain duplicate triplets.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [-1,0,1,2,-1,-4]
15+
16+
**Output:** [[-1,-1,2],[-1,0,1]]
17+
18+
**Example 2:**
19+
20+
**Input:** nums = []
21+
22+
**Output:** []
23+
24+
**Example 3:**
25+
26+
**Input:** nums = [0]
27+
28+
**Output:** []
29+
30+
**Constraints:**
31+
32+
* `0 <= nums.length <= 3000`
33+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
34+
35+
## Solution
36+
37+
```typescript
38+
function threeSum(nums: number[]): number[][] { //NOSONAR
39+
nums.sort((a, b) => a - b)
40+
const len = nums.length
41+
const result: number[][] = []
42+
let l: number
43+
let r: number
44+
for (let i = 0; i < len - 2; i++) {
45+
l = i + 1
46+
r = len - 1
47+
while (r > l) {
48+
const sum = nums[i] + nums[l] + nums[r]
49+
if (sum < 0) {
50+
l++
51+
} else if (sum > 0) {
52+
r--
53+
} else {
54+
const list: number[] = [nums[i], nums[l], nums[r]]
55+
result.push(list)
56+
while (l < r && nums[l + 1] === nums[l]) {
57+
l++
58+
}
59+
while (r > l && nums[r - 1] === nums[r]) {
60+
r--
61+
}
62+
l++
63+
r--
64+
}
65+
}
66+
while (i < len - 1 && nums[i + 1] === nums[i]) {
67+
i++ //NOSONAR
68+
}
69+
}
70+
return result
71+
}
72+
73+
export { threeSum }
74+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
[![](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)
2+
[![](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)
3+
4+
## 17\. Letter Combinations of a Phone Number
5+
6+
Medium
7+
8+
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**.
9+
10+
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
11+
12+
![](https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png)
13+
14+
**Example 1:**
15+
16+
**Input:** digits = "23"
17+
18+
**Output:** ["ad","ae","af","bd","be","bf","cd","ce","cf"]
19+
20+
**Example 2:**
21+
22+
**Input:** digits = ""
23+
24+
**Output:** []
25+
26+
**Example 3:**
27+
28+
**Input:** digits = "2"
29+
30+
**Output:** ["a","b","c"]
31+
32+
**Constraints:**
33+
34+
* `0 <= digits.length <= 4`
35+
* `digits[i]` is a digit in the range `['2', '9']`.
36+
37+
## Solution
38+
39+
```typescript
40+
function letterCombinations(digits: string): string[] {
41+
if (digits.length === 0) {
42+
return []
43+
}
44+
const letters: string[] = ['', '', 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']
45+
const ans: string[] = []
46+
const sb: string[] = []
47+
findCombinations(0, digits, letters, sb, ans)
48+
return ans
49+
}
50+
51+
function findCombinations(start: number, nums: string, letters: string[], curr: string[], ans: string[]): void {
52+
if (curr.length === nums.length) {
53+
ans.push(curr.join(''))
54+
return
55+
}
56+
for (let i = start; i < nums.length; i++) {
57+
const n = parseInt(nums.charAt(i))
58+
for (let j = 0; j < letters[n].length; j++) {
59+
const ch = letters[n].charAt(j)
60+
curr.push(ch)
61+
findCombinations(i + 1, nums, letters, curr, ans)
62+
curr.pop()
63+
}
64+
}
65+
}
66+
67+
export { letterCombinations }
68+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[![](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)
2+
[![](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)
3+
4+
## 19\. Remove Nth Node From End of List
5+
6+
Medium
7+
8+
Given the `head` of a linked list, remove the `nth` node from the end of the list and return its head.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg)
13+
14+
**Input:** head = [1,2,3,4,5], n = 2
15+
16+
**Output:** [1,2,3,5]
17+
18+
**Example 2:**
19+
20+
**Input:** head = [1], n = 1
21+
22+
**Output:** []
23+
24+
**Example 3:**
25+
26+
**Input:** head = [1,2], n = 1
27+
28+
**Output:** [1]
29+
30+
**Constraints:**
31+
32+
* The number of nodes in the list is `sz`.
33+
* `1 <= sz <= 30`
34+
* `0 <= Node.val <= 100`
35+
* `1 <= n <= sz`
36+
37+
**Follow up:** Could you do this in one pass?
38+
39+
## Solution
40+
41+
```typescript
42+
import { ListNode } from '../../com_github_leetcode/listnode'
43+
44+
let localN: number
45+
46+
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
47+
localN = n
48+
const dummy = new ListNode(0)
49+
dummy.next = head
50+
removeNth(dummy)
51+
return dummy.next
52+
}
53+
54+
function removeNth(node: ListNode | null): void {
55+
if (!node || !node.next) { //NOSONAR
56+
return
57+
}
58+
removeNth(node.next)
59+
localN--
60+
61+
if (localN === 0) {
62+
node.next = node.next?.next || null //NOSONAR
63+
}
64+
}
65+
66+
export { removeNthFromEnd }
67+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
[![](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)
2+
[![](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)
3+
4+
## 20\. Valid Parentheses
5+
6+
Easy
7+
8+
Given a string `s` containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid.
9+
10+
An input string is valid if:
11+
12+
1. Open brackets must be closed by the same type of brackets.
13+
2. Open brackets must be closed in the correct order.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "()"
18+
19+
**Output:** true
20+
21+
**Example 2:**
22+
23+
**Input:** s = "()[]{}"
24+
25+
**Output:** true
26+
27+
**Example 3:**
28+
29+
**Input:** s = "(]"
30+
31+
**Output:** false
32+
33+
**Example 4:**
34+
35+
**Input:** s = "([)]"
36+
37+
**Output:** false
38+
39+
**Example 5:**
40+
41+
**Input:** s = "{[]}"
42+
43+
**Output:** true
44+
45+
**Constraints:**
46+
47+
* <code>1 <= s.length <= 10<sup>4</sup></code>
48+
* `s` consists of parentheses only `'()[]{}'`.
49+
50+
## Solution
51+
52+
```typescript
53+
function isValid(s: string): boolean {
54+
const stack: string[] = []
55+
for (let i = 0; i < s.length; i++) {
56+
const c = s.charAt(i)
57+
if (c === '(' || c === '[' || c === '{') {
58+
stack.push(c)
59+
} else if (
60+
(c === ')' && stack.length > 0 && stack[stack.length - 1] === '(') ||
61+
(c === '}' && stack.length > 0 && stack[stack.length - 1] === '{') ||
62+
(c === ']' && stack.length > 0 && stack[stack.length - 1] === '[')
63+
) {
64+
stack.pop()
65+
} else {
66+
return false
67+
}
68+
}
69+
return stack.length === 0
70+
}
71+
72+
export { isValid }
73+
```

0 commit comments

Comments
 (0)