Skip to content
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
916 changes: 458 additions & 458 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,5 @@ function checkIfInstanceOf(obj: any, classFunction: any): boolean {
return false
}

/*
* checkIfInstanceOf(new Date(), Date); // true
*/

export { checkIfInstanceOf }
```
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ declare global {
}
}

Array.prototype.last = function <T>(): T | -1 { //NOSONAR
return this.length ? this.at(-1) : -1
Array.prototype.last = function () { //NOSONAR
return this.length !== 0 ? this[this.length - 1] : -1
}

/*
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/g2601_2700/s2621_sleep/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ Given a positive integer `millis`, write an asynchronous function that sleeps fo

```typescript
async function sleep(millis: number): Promise<void> {
return new Promise<void>((resolve, reject) => {
setTimeout(resolve, millis)
})
await new Promise((resolve) => setTimeout(resolve, millis))
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ function reduce(nums: number[], fn: Fn, init: number): number {
nums.forEach((num) => {
accumulator = fn(accumulator, num)
})

return accumulator
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,11 @@ Evaluating from right to left ...
type F = (x: number) => number

function compose(functions: F[]): F {
const n = functions.length

return function (x) {
for (let i = n - 1; i >= 0; i--) {
const fn = functions[i]
x = fn(x)
if (functions.length == 0) return x
for (let ind = functions.length - 1; ind >= 0; ind--) {
x = functions[ind](x)
}

return x
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/main/kotlin/g2601_2700/s2637_promise_time_limit/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,13 @@ t = 1000
type Fn = (...params: any[]) => Promise<any>

function timeLimit(fn: Fn, t: number): Fn {
return async function (...args: any[]): Promise<any> {
const fns = fn(...args)
const timeLimitPromise = new Promise((_, reject) => {
return async function (...args) {
const timeout = new Promise<any>((_, reject) => {
setTimeout(() => {
reject(new Error('Time Limit Exceeded'))
reject('Time Limit Exceeded') //NOSONAR
}, t)
})

return Promise.race([fns, timeLimitPromise])
return Promise.race([fn(...args), timeout])
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,9 @@ A **multi-dimensional array** is a recursive data structure that contains both i
type MultidimensionalArray = (MultidimensionalArray | number)[]

function* inorderTraversal(arr: MultidimensionalArray): Generator<number, void, unknown> {
if (!Array.isArray(arr)) {
yield arr
return
}

for (let value of arr) {
yield* inorderTraversal(value as MultidimensionalArray)
}
for (const item of arr)
if (Array.isArray(item)) yield* inorderTraversal(item)
else yield item
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Return _the smallest index_ `i` _at which either a row or a column will be compl

**Example 1:**

![](image explanation for example 1)![image explanation for example 1](https://assets.leetcode.com/uploads/2023/01/18/grid1.jpg)
![image explanation for example 1](https://assets.leetcode.com/uploads/2023/01/18/grid1.jpg)

**Input:** arr = [1,3,4,2], mat = \[\[1,4],[2,3]]

Expand Down
22 changes: 7 additions & 15 deletions src/main/kotlin/g2601_2700/s2666_allow_one_function_call/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,17 @@ Given a function `fn`, return a new function that is identical to the original f
## Solution

```typescript
function once<T extends (...args: T[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T> | undefined {
let hasRun: boolean
return function (...args: T[]): ReturnType<T> | undefined {
if (!hasRun) {
hasRun = true
type Fn = (...args: any[]) => any

function once(fn: Fn): Fn {
let wasCalled = false
return function (...args) {
if (!wasCalled) {
wasCalled = true
return fn(...args)
} else {
return undefined
}
}
}

/*
* let fn = (a,b,c) => (a + b + c)
* let onceFn = once(fn)
*
* onceFn(1,2,3); // 6
* onceFn(2,3,6); // returns undefined without calling fn
*/

export { once }
```
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ Any arguments could be passed to the function but it should still always return
## Solution

```typescript
function createHelloWorld() {
return function (...args): string {
return 'Hello World'
}
}
const createHelloWorld = () => () => "Hello World";

/*
* const f = createHelloWorld();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,37 @@ It can be shown that it is the maximum number of moves that can be made.
```kotlin
class Solution {
fun maxMoves(grid: Array<IntArray>): Int {
val height = grid.size
val width = grid[0].size
val dp = Array(height) { IntArray(width) { Int.MIN_VALUE } }
var result = 0
for (i in 0 until height) {
dp[i][0] = 0
}
for (c in 1 until width) {
for (r in 0 until height) {
if (r > 0 && grid[r - 1][c - 1] < grid[r][c]) {
dp[r][c] = dp[r][c].coerceAtLeast(dp[r - 1][c - 1] + 1)
val h = grid.size
var dp1 = BooleanArray(h)
var dp2 = BooleanArray(h)
var rtn = 0
dp1.fill(true)
for (col in 1 until grid[0].size) {
var f = false
for (row in 0 until h) {
val pr = row - 1
val nr = row + 1
dp2[row] = false
if (pr >= 0 && dp1[pr] && grid[pr][col - 1] < grid[row][col]) {
dp2[row] = true
f = true
}
if (nr < h && dp1[nr] && grid[nr][col - 1] < grid[row][col]) {
dp2[row] = true
f = true
}
if (grid[r][c - 1] < grid[r][c]) dp[r][c] = dp[r][c].coerceAtLeast(dp[r][c - 1] + 1)
if (r < height - 1 && grid[r + 1][c - 1] < grid[r][c]) {
dp[r][c] = dp[r][c].coerceAtLeast(dp[r + 1][c - 1] + 1)
if (dp1[row] && grid[row][col - 1] < grid[row][col]) {
dp2[row] = true
f = true
}
result = result.coerceAtLeast(dp[r][c])
}
val t = dp1
dp1 = dp2
dp2 = t
if (!f) break
rtn++
}
return result
return rtn
}
}
```