Skip to content

Commit

Permalink
Merge branch 'release/1.27.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ecgan committed Jul 18, 2020
2 parents 39e0a06 + 3aa1f91 commit 8583800
Show file tree
Hide file tree
Showing 12 changed files with 2,250 additions and 1,694 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ The listing below is sorted based on LeetCode #. If you are interested to see my
| 1476 | [Subrectangle Queries](/problems/subrectangle-queries) | Medium | Array |
| 1480 | [Running Sum of 1d Array](/problems/running-sum-of-1d-array) | Easy | Array |
| 1481 | [Least Number of Unique Integers after K Removals](/problems/least-number-of-unique-integers-after-k-removals) | Medium | Array, Sort |
| 1491 | [Average Salary Excluding the Minimum and Maximum Salary](/problems/average-salary-excluding-the-minimum-and-maximum-salary) | Easy | Array, Sort. |
| 1492 | [The kth Factor of n](/problems/the-kth-factor-of-n) | Medium | Math |
| 1493 | [Longest Subarray of 1's After Deleting One Element](/problems/longest-subarray-of-1s-after-deleting-one-element) | Medium | Array |

## Questions / Issues

Expand Down
3,518 changes: 1,826 additions & 1,692 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leetcode",
"version": "1.26.0",
"version": "1.27.0",
"description": "My solutions for LeetCode problems.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -34,6 +34,6 @@
"leettree": "^0.3.0"
},
"dependencies": {
"lodash": "^4.17.15"
"lodash": "^4.17.19"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Average Salary Excluding the Minimum and Maximum Salary

LeetCode #: [1491](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/)

Difficulty: Easy

Topic: Array, Sort.

## Problem

Given an array of unique integers salary where salary[i] is the salary of the employee i.

Return the average salary of employees excluding the minimum and maximum salary.

Example 1:

```text
Input: salary = [4000,3000,1000,2000]
Output: 2500.00000
Explanation: Minimum salary and maximum salary are 1000 and 4000 respectively.
Average salary excluding minimum and maximum salary is (2000+3000)/2= 2500
```

Example 2:

```text
Input: salary = [1000,2000,3000]
Output: 2000.00000
Explanation: Minimum salary and maximum salary are 1000 and 3000 respectively.
Average salary excluding minimum and maximum salary is (2000)/1= 2000
```

Example 3:

```text
Input: salary = [6000,5000,4000,3000,2000,1000]
Output: 3500.00000
```

Example 4:

```text
Input: salary = [8000,9000,2000,3000,6000,1000]
Output: 4750.00000
```

Constraints:

- `3 <= salary.length <= 100`
- `10^3 <= salary[i] <= 10^6`
- `salary[i]` is unique.
- Answers within `10^-5` of the actual value will be accepted as correct.

## Complexity Analysis

Time complexity: O(n)

Space complexiy: O(1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {number[]} salary
* @return {number}
*/
const average = function (salary) {
let max = Number.MIN_SAFE_INTEGER
let min = Number.MAX_SAFE_INTEGER
let sum = 0

for (let i = 0; i < salary.length; i++) {
sum += salary[i]
max = Math.max(max, salary[i])
min = Math.min(min, salary[i])
}

const average = (sum - max - min) / (salary.length - 2)

return average
}

module.exports = average
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const average = require('./average')

test('Example 1', () => {
const salary = [4000, 3000, 1000, 2000]

const result = average(salary)

expect(result).toBe(2500)
})

test('Example 2', () => {
const salary = [1000, 2000, 3000]

const result = average(salary)

expect(result).toBe(2000)
})

test('Example 3', () => {
const salary = [6000, 5000, 4000, 3000, 2000, 1000]

const result = average(salary)

expect(result).toBe(3500)
})

test('Example 4', () => {
const salary = [8000, 9000, 2000, 3000, 6000, 1000]

const result = average(salary)

expect(result).toBe(4750)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Longest Subarray of 1's After Deleting One Element

LeetCode #: [1493](https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/)

Difficulty: Medium

Topics: Array.

## Problem

Given a binary array `nums`, you should delete one element from it.

Return the size of the longest non-empty subarray containing only 1's in the resulting array.

Return 0 if there is no such subarray.

Example 1:

```text
Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
```

Example 2:

```text
Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
```

Example 3:

```text
Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.
```

Example 4:

```text
Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4
```

Example 5:

```text
Input: nums = [0,0,0]
Output: 0
```

Constraints:

- `1 <= nums.length <= 10^5`
- `nums[i]` is either `0` or `1`.

## Solution Explanation

Loop through all the numbers and keep track of the count in the groups of 1 and 0. You only need to keep track of the last two groups of 1 (`prevCount1` and `currCount1`) and last one group of 0 (`count0`). You can combine `prevCount1` and `currCount1` when `count0` is equal to 1.

## Complexity Analysis

Time complexity: O(n)

Space complexity: O(1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @param {number[]} nums
* @return {number}
*/
const longestSubarray = function (nums) {
let max = 0
let prevCount1 = 0
let currCount1 = 0
let count0 = 0

for (let i = 0; i < nums.length; i++) {
const currNum = nums[i]

if (currNum === 0) {
const prevNum = nums[i - 1] || 0

if (prevNum === 0) {
count0++
} else {
prevCount1 = currCount1
currCount1 = 0
count0 = 1
}
} else {
currCount1++
max = Math.max(max, (count0 === 1) ? (prevCount1 + currCount1) : currCount1)
}
}

const lastNum = nums[nums.length - 1]
if (lastNum === 1 && count0 === 0) {
return currCount1 - 1
}

return max
}

module.exports = longestSubarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const longestSubarray = require('./longestSubarray')

test('Example 1', () => {
const nums = [1, 1, 0, 1]

const result = longestSubarray(nums)

expect(result).toBe(3)
})

test('Example 2', () => {
const nums = [0, 1, 1, 1, 0, 1, 1, 0, 1]

const result = longestSubarray(nums)

expect(result).toBe(5)
})

test('Example 3', () => {
const nums = [1, 1, 1]

const result = longestSubarray(nums)

expect(result).toBe(2)
})

test('Example 4', () => {
const nums = [1, 1, 0, 0, 1, 1, 1, 0, 1]

const result = longestSubarray(nums)

expect(result).toBe(4)
})

test('Example 5', () => {
const nums = [0, 0, 0]

const result = longestSubarray(nums)

expect(result).toBe(0)
})

test('1 group of 1, end with 0', () => {
const nums = [1, 0]

const result = longestSubarray(nums)

expect(result).toBe(1)
})

test('1 group of 1, start with 0', () => {
const nums = [0, 1]

const result = longestSubarray(nums)

expect(result).toBe(1)
})
65 changes: 65 additions & 0 deletions problems/the-kth-factor-of-n/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# The kth Factor of n

LeetCode #: [1492](https://leetcode.com/problems/the-kth-factor-of-n/)

Difficulty: Medium

Topic: Math.

## Problem

Given two positive integers `n` and `k`.

A factor of an integer `n` is defined as an integer `i` where `n % i == 0`.

Consider a list of all factors of `n` sorted in ascending order, return the `kth` factor in this list or return -1 if `n` has less than `k` factors.

Example 1:

```text
Input: n = 12, k = 3
Output: 3
Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3.
```

Example 2:

```text
Input: n = 7, k = 2
Output: 7
Explanation: Factors list is [1, 7], the 2nd factor is 7.
```

Example 3:

```text
Input: n = 4, k = 4
Output: -1
Explanation: Factors list is [1, 2, 4], there is only 3 factors. We should return -1.
```

Example 4:

```text
Input: n = 1, k = 1
Output: 1
Explanation: Factors list is [1], the 1st factor is 1.
```

Example 5:

```text
Input: n = 1000, k = 3
Output: 4
Explanation: Factors list is [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 250, 500, 1000].
```

Constraints:

- `1 <= k <= n <= 1000`

## Complexity Analysis

Time complexity: O(sqrt(n))

Space complexity: O(factors of n)
Loading

0 comments on commit 8583800

Please sign in to comment.