-
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
22 changed files
with
746 additions
and
2 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,52 @@ | ||
# Angle Between Hands of a Clock | ||
|
||
LeetCode #: [1344](https://leetcode.com/problems/angle-between-hands-of-a-clock/) | ||
|
||
Difficulty: Medium | ||
|
||
Topic: Math. | ||
|
||
## Problem | ||
|
||
Given two numbers, `hour` and `minutes`. Return the smaller angle (in sexagesimal units) formed between the `hour` and the `minute` hand. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input: hour = 12, minutes = 30 | ||
Output: 165 | ||
``` | ||
|
||
Example 2: | ||
|
||
```text | ||
Input: hour = 3, minutes = 30 | ||
Output: 75 | ||
``` | ||
|
||
Example 3: | ||
|
||
```text | ||
Input: hour = 3, minutes = 15 | ||
Output: 7.5 | ||
``` | ||
|
||
Example 4: | ||
|
||
```text | ||
Input: hour = 4, minutes = 50 | ||
Output: 155 | ||
``` | ||
|
||
Example 5: | ||
|
||
```text | ||
Input: hour = 12, minutes = 0 | ||
Output: 0 | ||
``` | ||
|
||
Constraints: | ||
|
||
* `1 <= hour <= 12` | ||
* `0 <= minutes <= 59` | ||
* Answers within `10^-5` of the actual value will be accepted as correct. |
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,10 @@ | ||
const angleClock = (hour, minutes) => { | ||
const minuteAngle = 6 * minutes | ||
const hourAngle = ((hour === 12) ? 0 : hour * 30) + (minutes / 60 * 30) | ||
|
||
const angleDiff = Math.abs(hourAngle - minuteAngle) | ||
|
||
return (angleDiff < 180) ? angleDiff : 360 - angleDiff | ||
} | ||
|
||
module.exports = angleClock |
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,64 @@ | ||
const angleClock = require('./solution') | ||
|
||
test('Example 1', () => { | ||
const hour = 12 | ||
const minutes = 30 | ||
|
||
const result = angleClock(hour, minutes) | ||
|
||
expect(result).toBe(165) | ||
}) | ||
|
||
test('Example 2', () => { | ||
const hour = 3 | ||
const minutes = 30 | ||
|
||
const result = angleClock(hour, minutes) | ||
|
||
expect(result).toBe(75) | ||
}) | ||
|
||
test('Example 3', () => { | ||
const hour = 3 | ||
const minutes = 15 | ||
|
||
const result = angleClock(hour, minutes) | ||
|
||
expect(result).toBe(7.5) | ||
}) | ||
|
||
test('Example 4', () => { | ||
const hour = 4 | ||
const minutes = 50 | ||
|
||
const result = angleClock(hour, minutes) | ||
|
||
expect(result).toBe(155) | ||
}) | ||
|
||
test('Example 5', () => { | ||
const hour = 12 | ||
const minutes = 0 | ||
|
||
const result = angleClock(hour, minutes) | ||
|
||
expect(result).toBe(0) | ||
}) | ||
|
||
test('7 hour 45 min', () => { | ||
const hour = 7 | ||
const minutes = 45 | ||
|
||
const result = angleClock(hour, minutes) | ||
|
||
expect(result).toBe(37.5) | ||
}) | ||
|
||
test('2 hour 50 min', () => { | ||
const hour = 2 | ||
const minutes = 50 | ||
|
||
const result = angleClock(hour, minutes) | ||
|
||
expect(result).toBe(145) | ||
}) |
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,46 @@ | ||
# Check If N and Its Double Exist | ||
|
||
LeetCode #: [1346](https://leetcode.com/problems/check-if-n-and-its-double-exist/) | ||
|
||
Difficulty: Easy | ||
|
||
Topic: Array. | ||
|
||
## Problem | ||
|
||
Given an array `arr` of integers, check if there exists two integers `N` and `M` such that `N` is the double of `M` ( i.e. `N = 2 * M`). | ||
|
||
More formally check if there exists two indices `i` and `j` such that : | ||
|
||
* `i != j` | ||
* `0 <= i, j < arr.length` | ||
* `arr[i] == 2 * arr[j]` | ||
|
||
Example 1: | ||
|
||
```text | ||
Input: arr = [10,2,5,3] | ||
Output: true | ||
Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5. | ||
``` | ||
|
||
Example 2: | ||
|
||
```text | ||
Input: arr = [7,1,14,11] | ||
Output: true | ||
Explanation: N = 14 is the double of M = 7,that is, 14 = 2 * 7. | ||
``` | ||
|
||
Example 3: | ||
|
||
```text | ||
Input: arr = [3,1,7,11] | ||
Output: false | ||
Explanation: In this case does not exist N and M, such that N = 2 * M. | ||
``` | ||
|
||
Constraints: | ||
|
||
* `2 <= arr.length <= 500` | ||
* `-10^3 <= arr[i] <= 10^3` |
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,20 @@ | ||
const sortBy = require('lodash/sortBy') | ||
const sortedIndexOf = require('lodash/sortedIndexOf') | ||
|
||
const checkIfExist = (num) => { | ||
const sorted = sortBy(num) | ||
|
||
for (let i = 0; i < sorted.length - 1; i++) { | ||
const element = sorted[i] | ||
const other = (element >= 0) ? element * 2 : element / 2 | ||
const slice = sorted.slice(i + 1) | ||
|
||
if (sortedIndexOf(slice, other) >= 0) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
module.exports = checkIfExist |
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 checkIfExist = require('./solution') | ||
|
||
test('Example 1', () => { | ||
const arr = [10, 2, 5, 3] | ||
|
||
const result = checkIfExist(arr) | ||
|
||
expect(result).toBe(true) | ||
}) | ||
|
||
test('Example 2', () => { | ||
const arr = [7, 1, 14, 11] | ||
|
||
const result = checkIfExist(arr) | ||
|
||
expect(result).toBe(true) | ||
}) | ||
|
||
test('Example 3', () => { | ||
const arr = [3, 1, 7, 11] | ||
|
||
const result = checkIfExist(arr) | ||
|
||
expect(result).toBe(false) | ||
}) | ||
|
||
test('should handle negative numbers too: -20 is double of -10, should return true', () => { | ||
const arr = [-10, 12, -20, -8, 15] | ||
|
||
const result = checkIfExist(arr) | ||
|
||
expect(result).toBe(true) | ||
}) |
59 changes: 59 additions & 0 deletions
59
problems/minimum-number-of-steps-to-make-two-strings-anagram/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,59 @@ | ||
# Minimum Number of Steps to Make Two Strings Anagram | ||
|
||
LeetCode #: [1347](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/) | ||
|
||
Difficulty: Medium | ||
|
||
Topic: String. | ||
|
||
## Problem | ||
|
||
Given two equal-size strings `s` and `t`. In one step you can choose any character of `t` and replace it with another character. | ||
|
||
Return the minimum number of steps to make `t` an anagram of `s`. | ||
|
||
An Anagram of a string is a string that contains the same characters with a different (or the same) ordering. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input: s = "bab", t = "aba" | ||
Output: 1 | ||
Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s. | ||
``` | ||
|
||
Example 2: | ||
|
||
```text | ||
Input: s = "leetcode", t = "practice" | ||
Output: 5 | ||
Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s. | ||
``` | ||
|
||
Example 3: | ||
|
||
```text | ||
Input: s = "anagram", t = "mangaar" | ||
Output: 0 | ||
Explanation: "anagram" and "mangaar" are anagrams. | ||
``` | ||
|
||
Example 4: | ||
|
||
```text | ||
Input: s = "xxyyzz", t = "xxyyzz" | ||
Output: 0 | ||
``` | ||
|
||
Example 5: | ||
|
||
```text | ||
Input: s = "friend", t = "family" | ||
Output: 4 | ||
``` | ||
|
||
Constraints: | ||
|
||
* `1 <= s.length <= 50000` | ||
* `s.length == t.length` | ||
* `s` and `t` contain lower-case English letters only. |
35 changes: 35 additions & 0 deletions
35
problems/minimum-number-of-steps-to-make-two-strings-anagram/solution.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,35 @@ | ||
const getMap = (str) => { | ||
const map = str.split('') | ||
.reduce((acc, val) => { | ||
if (!acc[val]) { | ||
acc[val] = 1 | ||
} else { | ||
acc[val] += 1 | ||
} | ||
|
||
return acc | ||
}, {}) | ||
|
||
return map | ||
} | ||
|
||
const minSteps = (s, t) => { | ||
const base = getMap(s) | ||
const compare = getMap(t) | ||
|
||
let count = 0 | ||
const keys = Object.keys(base) | ||
for (let i = 0; i < keys.length; i++) { | ||
const char = keys[i] | ||
|
||
if (!compare[char]) { | ||
count += base[char] | ||
} else { | ||
count += Math.max(0, base[char] - compare[char]) | ||
} | ||
} | ||
|
||
return count | ||
} | ||
|
||
module.exports = minSteps |
Oops, something went wrong.