-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
2,250 additions
and
1,694 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
problems/average-salary-excluding-the-minimum-and-maximum-salary/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
21 changes: 21 additions & 0 deletions
21
problems/average-salary-excluding-the-minimum-and-maximum-salary/average.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
33 changes: 33 additions & 0 deletions
33
problems/average-salary-excluding-the-minimum-and-maximum-salary/average.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
68 changes: 68 additions & 0 deletions
68
problems/longest-subarray-of-1s-after-deleting-one-element/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
38 changes: 38 additions & 0 deletions
38
problems/longest-subarray-of-1s-after-deleting-one-element/longestSubarray.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
57 changes: 57 additions & 0 deletions
57
problems/longest-subarray-of-1s-after-deleting-one-element/longestSubarray.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.