Skip to content

Commit 8833331

Browse files
committedMar 28, 2025·
Updated readme
1 parent 623b514 commit 8833331

File tree

6 files changed

+491
-499
lines changed

6 files changed

+491
-499
lines changed
 

‎README.md

+447-447
Large diffs are not rendered by default.

‎src/main/ts/g0001_0100/s0045_jump_game_ii/readme.md

+13-32
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,21 @@ You can assume that you can always reach the last index.
3535
## Solution
3636

3737
```typescript
38-
function jump(nums: number[]): number { //NOSONAR
39-
let minJmp = new Array(nums.length)
40-
if (nums.length === 1) return 0
41-
let prevIndex = 0
42-
minJmp[prevIndex] = 0
43-
while (prevIndex < nums.length - 1) {
44-
let nextMaxJmpTo = nums[prevIndex] + prevIndex
45-
let prevIndexJmp = minJmp[prevIndex]
46-
47-
let farthestJumpVal = -1
48-
let farthestJumpIndex = -1
49-
for (let i = nextMaxJmpTo; ; i--) {
50-
if (i >= nums.length) {
51-
continue
52-
}
53-
if (i === nums.length - 1) {
54-
return prevIndexJmp + 1
55-
}
56-
if (minJmp[i] != undefined) {
57-
break
58-
}
59-
minJmp[i] = prevIndexJmp + 1
60-
let curmaxTo = nums[i] + i
61-
if (farthestJumpVal < curmaxTo) {
62-
farthestJumpVal = curmaxTo
63-
farthestJumpIndex = i
64-
}
38+
function jump(nums: number[]): number {
39+
let minJump = 0,
40+
farthest = 0,
41+
currentEnd = 0
42+
for (let i = 0; i < nums.length - 1; i++) {
43+
farthest = Math.max(farthest, i + nums[i])
44+
// If we've reached the end of the current jump range
45+
if (i === currentEnd) {
46+
minJump++
47+
currentEnd = farthest
48+
49+
if (currentEnd >= nums.length - 1) break
6550
}
66-
if (farthestJumpIndex === -1) {
67-
return -1
68-
}
69-
prevIndex = farthestJumpIndex
7051
}
71-
return minJmp[nums.length - 1]
52+
return minJump
7253
}
7354

7455
export { jump }

‎src/main/ts/g0001_0100/s0062_unique_paths/readme.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,15 @@ How many possible unique paths are there?
5353

5454
```typescript
5555
function uniquePaths(m: number, n: number): number {
56-
const factorialize = (x: number) => {
57-
if (x <= 1) return 1
58-
let res = x
59-
while (x > 1) {
60-
x--
61-
res *= x
56+
let aboveRow = Array(n).fill(1)
57+
for (let row = 1; row < m; row++) {
58+
let currentRow = Array(n).fill(1)
59+
for (let col = 1; col < n; col++) {
60+
currentRow[col] = currentRow[col - 1] + aboveRow[col]
6261
}
63-
return res
62+
aboveRow = currentRow
6463
}
65-
return factorialize(m + n - 2) / factorialize(m - 1) / factorialize(n - 1)
64+
return aboveRow[n - 1]
6665
}
6766

6867
export { uniquePaths }

‎src/main/ts/g0001_0100/s0078_subsets/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ The solution set **must not** contain duplicate subsets. Return the solution in
3030
## Solution
3131

3232
```typescript
33-
const subsets = (nums: number[]): number[][] => {
33+
function subsets(nums: number[]): number[][] {
3434
const sets: number[][] = [[]]
3535
for (const num of nums) sets.push(...sets.map((set) => [...set, num]))
3636
return sets

‎src/main/ts/g0101_0200/s0128_longest_consecutive_sequence/readme.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,19 @@ You must write an algorithm that runs in `O(n)` time.
3232

3333
```typescript
3434
function longestConsecutive(nums: number[]): number {
35-
const set = new Set(nums)
36-
let max = 0
37-
for (const num of nums) {
38-
if (set.has(num + 1)) continue
39-
let counter = 1,
40-
current = num
41-
while (set.has(--current)) counter++
42-
max = Math.max(counter, max)
35+
let sset = new Set(nums)
36+
let maxLen = 0
37+
for (let num of sset) {
38+
// check its start of the sequence
39+
if (!sset.has(num-1)) {
40+
let len = 0;
41+
while (sset.has(num+len)) {
42+
len += 1
43+
}
44+
maxLen = Math.max(maxLen, len)
45+
}
4346
}
44-
return max
47+
return maxLen
4548
}
4649

4750
export { longestConsecutive }

‎src/main/ts/g0201_0300/s0215_kth_largest_element_in_an_array/readme.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,17 @@ Note that it is the <code>k<sup>th</sup></code> largest element in the sorted or
3030

3131
```typescript
3232
function findKthLargest(nums: number[], k: number): number {
33-
nums.sort((prev, next) => next - prev)
34-
return nums[k - 1]
33+
const countingLen = 2e4 + 1
34+
const counting = new Int32Array(countingLen)
35+
for (const num of nums) {
36+
counting[num + 1e4]++;
37+
}
38+
for (let i = countingLen - 1; i >= 0; i--) {
39+
k -= counting[i]
40+
if (k <= 0) {
41+
return i - 1e4
42+
}
43+
}
3544
}
3645

3746
export { findKthLargest }

0 commit comments

Comments
 (0)
Please sign in to comment.