Skip to content

Commit 2f06503

Browse files
committed
Added tasks 149-209
1 parent ff9f6dd commit 2f06503

File tree

16 files changed

+1033
-0
lines changed

16 files changed

+1033
-0
lines changed

README.md

+55
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
## 149\. Max Points on a Line
5+
6+
Hard
7+
8+
Given an array of `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the **X-Y** plane, return _the maximum number of points that lie on the same straight line_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg)
13+
14+
**Input:** points = \[\[1,1],[2,2],[3,3]]
15+
16+
**Output:** 3
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg)
21+
22+
**Input:** points = \[\[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
23+
24+
**Output:** 4
25+
26+
**Constraints:**
27+
28+
* `1 <= points.length <= 300`
29+
* `points[i].length == 2`
30+
* <code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code>
31+
* All the `points` are **unique**.
32+
33+
## Solution
34+
35+
```typescript
36+
function maxPoints(points: number[][]): number {
37+
if (points.length <= 2) {
38+
return points.length
39+
}
40+
const map = new Map<number, number>()
41+
let result: number = 0
42+
for (let i = 0; i < points.length; i++) {
43+
const [x0, y0] = points[i]
44+
for (let j = i + 1; j < points.length; j++) {
45+
const [x1, y1] = points[j]
46+
let m: number
47+
if (x0 === x1) {
48+
m = Number.MAX_VALUE
49+
} else if (y0 === y1) {
50+
m = 0
51+
} else {
52+
m = (y0 - y1) / (x0 - x1)
53+
}
54+
const nextM: number = map.has(m) ? map.get(m) + 1 : 2
55+
map.set(m, nextM)
56+
result = Math.max(result, nextM)
57+
}
58+
map.clear()
59+
}
60+
return result
61+
}
62+
63+
export { maxPoints }
64+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
## 150\. Evaluate Reverse Polish Notation
5+
6+
Medium
7+
8+
Evaluate the value of an arithmetic expression in [Reverse Polish Notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation).
9+
10+
Valid operators are `+`, `-`, `*`, and `/`. Each operand may be an integer or another expression.
11+
12+
**Note** that division between two integers should truncate toward zero.
13+
14+
It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
15+
16+
**Example 1:**
17+
18+
**Input:** tokens = ["2","1","+","3","\*"]
19+
20+
**Output:** 9
21+
22+
**Explanation:** ((2 + 1) \* 3) = 9
23+
24+
**Example 2:**
25+
26+
**Input:** tokens = ["4","13","5","/","+"]
27+
28+
**Output:** 6
29+
30+
**Explanation:** (4 + (13 / 5)) = 6
31+
32+
**Example 3:**
33+
34+
**Input:** tokens = ["10","6","9","3","+","-11","\*","/","\*","17","+","5","+"]
35+
36+
**Output:** 22
37+
38+
**Explanation:**
39+
40+
((10 \* (6 / ((9 + 3) \* -11))) + 17) + 5
41+
= ((10 \* (6 / (12 \* -11))) + 17) + 5
42+
= ((10 \* (6 / -132)) + 17) + 5
43+
= ((10 \* 0) + 17) + 5
44+
= (0 + 17) + 5
45+
= 17 + 5
46+
= 22
47+
48+
**Constraints:**
49+
50+
* <code>1 <= tokens.length <= 10<sup>4</sup></code>
51+
* `tokens[i]` is either an operator: `"+"`, `"-"`, `"*"`, or `"/"`, or an integer in the range `[-200, 200]`.
52+
53+
## Solution
54+
55+
```typescript
56+
function evalRPN(tokens: string[]): number {
57+
const numberStack: number[] = []
58+
const isOperator = (val: string): boolean => val === '+' || val === '-' || val === '*' || val === '/'
59+
for (let token of tokens) {
60+
if (isOperator(token)) {
61+
const right = numberStack.pop() as number
62+
const left = numberStack.pop() as number
63+
64+
if (token === '+') numberStack.push(left + right)
65+
else if (token === '-') numberStack.push(left - right)
66+
else if (token === '*') numberStack.push(left * right)
67+
else if (token === '/') {
68+
const result = left / right
69+
numberStack.push(result < 0 ? Math.ceil(result) : Math.floor(result))
70+
}
71+
} else numberStack.push(+token)
72+
}
73+
return numberStack.pop() || 0
74+
}
75+
76+
export { evalRPN }
77+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
## 151\. Reverse Words in a String
5+
6+
Medium
7+
8+
Given an input string `s`, reverse the order of the **words**.
9+
10+
A **word** is defined as a sequence of non-space characters. The **words** in `s` will be separated by at least one space.
11+
12+
Return _a string of the words in reverse order concatenated by a single space._
13+
14+
**Note** that `s` may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "the sky is blue"
19+
20+
**Output:** "blue is sky the"
21+
22+
**Example 2:**
23+
24+
**Input:** s = " hello world "
25+
26+
**Output:** "world hello"
27+
28+
**Explanation:** Your reversed string should not contain leading or trailing spaces.
29+
30+
**Example 3:**
31+
32+
**Input:** s = "a good example"
33+
34+
**Output:** "example good a"
35+
36+
**Explanation:** You need to reduce multiple spaces between two words to a single space in the reversed string.
37+
38+
**Example 4:**
39+
40+
**Input:** s = " Bob Loves Alice "
41+
42+
**Output:** "Alice Loves Bob"
43+
44+
**Example 5:**
45+
46+
**Input:** s = "Alice does not even like bob"
47+
48+
**Output:** "bob like even not does Alice"
49+
50+
**Constraints:**
51+
52+
* <code>1 <= s.length <= 10<sup>4</sup></code>
53+
* `s` contains English letters (upper-case and lower-case), digits, and spaces `' '`.
54+
* There is **at least one** word in `s`.
55+
56+
**Follow-up:** If the string data type is mutable in your language, can you solve it **in-place** with `O(1)` extra space?
57+
58+
## Solution
59+
60+
```typescript
61+
function reverseWords(s: string): string {
62+
return s
63+
?.trim()
64+
?.replaceAll(/\s{2,}/g, ' ')
65+
?.split(' ')
66+
?.reverse()
67+
.join(' ')
68+
}
69+
70+
export { reverseWords }
71+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
## 162\. Find Peak Element
5+
6+
Medium
7+
8+
A peak element is an element that is strictly greater than its neighbors.
9+
10+
Given an integer array `nums`, find a peak element, and return its index. If the array contains multiple peaks, return the index to **any of the peaks**.
11+
12+
You may imagine that `nums[-1] = nums[n] = -∞`.
13+
14+
You must write an algorithm that runs in `O(log n)` time.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,2,3,1]
19+
20+
**Output:** 2
21+
22+
**Explanation:** 3 is a peak element and your function should return the index number 2.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [1,2,1,3,5,6,4]
27+
28+
**Output:** 5
29+
30+
**Explanation:** Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.
31+
32+
**Constraints:**
33+
34+
* `1 <= nums.length <= 1000`
35+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
36+
* `nums[i] != nums[i + 1]` for all valid `i`.
37+
38+
## Solution
39+
40+
```typescript
41+
function findPeakElement(nums: number[]): number {
42+
let start = 0
43+
let end = nums.length - 1
44+
while (start < end) {
45+
const mid = start + Math.floor((end - start) / 2)
46+
if (nums[mid + 1] > nums[mid]) {
47+
start = mid + 1
48+
} else {
49+
end = mid
50+
}
51+
}
52+
return start
53+
}
54+
55+
export { findPeakElement }
56+
```
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+
## 167\. Two Sum II - Input Array Is Sorted
5+
6+
Easy
7+
8+
Given a **1-indexed** array of integers `numbers` that is already **_sorted in non-decreasing order_**, find two numbers such that they add up to a specific `target` number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index<sub>2</sub>]</code> where <code>1 <= index<sub>1</sub> < index<sub>2</sub> <= numbers.length</code>.
9+
10+
Return _the indices of the two numbers,_ <code>index<sub>1</sub></code> _and_ <code>index<sub>2</sub></code>_, **added by one** as an integer array_ <code>[index<sub>1</sub>, index<sub>2</sub>]</code> _of length 2._
11+
12+
The tests are generated such that there is **exactly one solution**. You **may not** use the same element twice.
13+
14+
**Example 1:**
15+
16+
**Input:** numbers = [2,7,11,15], target = 9
17+
18+
**Output:** [1,2]
19+
20+
**Explanation:** The sum of 2 and 7 is 9. Therefore, index<sub>1</sub> = 1, index<sub>2</sub> = 2. We return [1, 2].
21+
22+
**Example 2:**
23+
24+
**Input:** numbers = [2,3,4], target = 6
25+
26+
**Output:** [1,3]
27+
28+
**Explanation:** The sum of 2 and 4 is 6. Therefore index<sub>1</sub> = 1, index<sub>2</sub> = 3. We return [1, 3].
29+
30+
**Example 3:**
31+
32+
**Input:** numbers = [\-1,0], target = -1
33+
34+
**Output:** [1,2]
35+
36+
**Explanation:** The sum of -1 and 0 is -1. Therefore index<sub>1</sub> = 1, index<sub>2</sub> = 2. We return [1, 2].
37+
38+
**Constraints:**
39+
40+
* <code>2 <= numbers.length <= 3 * 10<sup>4</sup></code>
41+
* `-1000 <= numbers[i] <= 1000`
42+
* `numbers` is sorted in **non-decreasing order**.
43+
* `-1000 <= target <= 1000`
44+
* The tests are generated such that there is **exactly one solution**.
45+
46+
## Solution
47+
48+
```typescript
49+
function twoSum(numbers: number[], target: number): number[] {
50+
let i = 0
51+
let j = numbers.length - 1
52+
while (i < j) {
53+
const sum = numbers[i] + numbers[j]
54+
if (sum === target) {
55+
return [i + 1, j + 1]
56+
} else if (sum < target) {
57+
i++
58+
} else {
59+
j--
60+
}
61+
}
62+
return []
63+
}
64+
65+
export { twoSum }
66+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
## 172\. Factorial Trailing Zeroes
5+
6+
Medium
7+
8+
Given an integer `n`, return _the number of trailing zeroes in_ `n!`.
9+
10+
Note that `n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1`.
11+
12+
**Example 1:**
13+
14+
**Input:** n = 3
15+
16+
**Output:** 0
17+
18+
**Explanation:** 3! = 6, no trailing zero.
19+
20+
**Example 2:**
21+
22+
**Input:** n = 5
23+
24+
**Output:** 1
25+
26+
**Explanation:** 5! = 120, one trailing zero.
27+
28+
**Example 3:**
29+
30+
**Input:** n = 0
31+
32+
**Output:** 0
33+
34+
**Constraints:**
35+
36+
* <code>0 <= n <= 10<sup>4</sup></code>
37+
38+
**Follow up:** Could you write a solution that works in logarithmic time complexity?
39+
40+
## Solution
41+
42+
```typescript
43+
function trailingZeroes(n: number): number {
44+
let base = 5
45+
let count = 0
46+
while (n >= base) {
47+
count += Math.floor(n / base)
48+
base *= 5
49+
}
50+
return count
51+
}
52+
53+
export { trailingZeroes }
54+
```

0 commit comments

Comments
 (0)