diff --git a/solution/2000-2099/2091.Removing Minimum and Maximum From Array/README.md b/solution/2000-2099/2091.Removing Minimum and Maximum From Array/README.md index 595c9d5bb16e3..42be4bc198a3b 100644 --- a/solution/2000-2099/2091.Removing Minimum and Maximum From Array/README.md +++ b/solution/2000-2099/2091.Removing Minimum and Maximum From Array/README.md @@ -82,6 +82,23 @@ ``` +### **TypeScript** + +```ts +function minimumDeletions(nums: number[]): number { + const n = nums.length; + if (n == 1) return 1; + let i = nums.indexOf(Math.min(...nums)); + let j = nums.indexOf(Math.max(...nums)); + let left = Math.min(i, j); + let right = Math.max(i, j); + // 左右 left + 1 + n - right + // 两个都是左边 left + 1 + right - left = right + 1 + // 都是右边 n - right + right - left = n - left + return Math.min(left + 1 + n - right, right + 1, n - left); +}; +``` + ### **...** ``` diff --git a/solution/2000-2099/2091.Removing Minimum and Maximum From Array/README_EN.md b/solution/2000-2099/2091.Removing Minimum and Maximum From Array/README_EN.md index 9d06ce991b412..837570a13aaf9 100644 --- a/solution/2000-2099/2091.Removing Minimum and Maximum From Array/README_EN.md +++ b/solution/2000-2099/2091.Removing Minimum and Maximum From Array/README_EN.md @@ -72,6 +72,23 @@ We can remove it with 1 deletion. ``` +### **TypeScript** + +```ts +function minimumDeletions(nums: number[]): number { + const n = nums.length; + if (n == 1) return 1; + let i = nums.indexOf(Math.min(...nums)); + let j = nums.indexOf(Math.max(...nums)); + let left = Math.min(i, j); + let right = Math.max(i, j); + // 左右 left + 1 + n - right + // 两个都是左边 left + 1 + right - left = right + 1 + // 都是右边 n - right + right - left = n - left + return Math.min(left + 1 + n - right, right + 1, n - left); +}; +``` + ### **...** ``` diff --git a/solution/2000-2099/2091.Removing Minimum and Maximum From Array/Solution.ts b/solution/2000-2099/2091.Removing Minimum and Maximum From Array/Solution.ts new file mode 100644 index 0000000000000..be3a63d9f4afd --- /dev/null +++ b/solution/2000-2099/2091.Removing Minimum and Maximum From Array/Solution.ts @@ -0,0 +1,12 @@ +function minimumDeletions(nums: number[]): number { + const n = nums.length; + if (n == 1) return 1; + let i = nums.indexOf(Math.min(...nums)); + let j = nums.indexOf(Math.max(...nums)); + let left = Math.min(i, j); + let right = Math.max(i, j); + // 左右 left + 1 + n - right + // 两个都是左边 left + 1 + right - left = right + 1 + // 都是右边 n - right + right - left = n - left + return Math.min(left + 1 + n - right, right + 1, n - left); +}; \ No newline at end of file