Skip to content

Commit

Permalink
Merge branch 'release/1.17.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ecgan committed Feb 9, 2020
2 parents 09a5d86 + 8265c6c commit e7b7b1e
Show file tree
Hide file tree
Showing 22 changed files with 746 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ Folder structure in this repository: https://<span></span>github.com/ecgan/leetc
| 1313 | [Decompress Run-Length Encoded List](/problems/decompress-run-length-encoded-list) | Easy | Array |
| 1314 | [Matrix Block Sum](/problems/matrix-block-sum) | Medium | Dynamic programming |
| 1317 | [Convert Integer to the Sum of Two No-Zero Integers](/problems/convert-integer-to-the-sum-of-two-no-zero-integers) | Easy | Math |
| 1342 | [Number of Steps to Reduce a Number to Zero](/problems/number-of-steps-to-reduce-a-number-to-zero) | Easy | Bit manipulation |
| 1343 | [Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold](/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold) | Medium | Array |
| 1344 | [Angle Between Hands of a Clock](/problems/angle-between-hands-of-a-clock) | Medium | Math |
| 1346 | [Check If N and Its Double Exist](/problems/check-if-n-and-its-double-exist) | Easy | Array |
| 1347 | [Minimum Number of Steps to Make Two Strings Anagram](/problems/minimum-number-of-steps-to-make-two-strings-anagram) | Medium | String |
| 1348 | [Tweet Counts Per Frequency](/problems/tweet-counts-per-frequency) | Medium | Design |

## Questions / Issues

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leetcode",
"version": "1.16.0",
"version": "1.17.0",
"description": "My solutions for LeetCode problems.",
"main": "index.js",
"scripts": {
Expand Down
52 changes: 52 additions & 0 deletions problems/angle-between-hands-of-a-clock/README.md
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.
10 changes: 10 additions & 0 deletions problems/angle-between-hands-of-a-clock/solution.js
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
64 changes: 64 additions & 0 deletions problems/angle-between-hands-of-a-clock/solution.test.js
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)
})
46 changes: 46 additions & 0 deletions problems/check-if-n-and-its-double-exist/README.md
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`
20 changes: 20 additions & 0 deletions problems/check-if-n-and-its-double-exist/solution.js
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
33 changes: 33 additions & 0 deletions problems/check-if-n-and-its-double-exist/solution.test.js
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)
})
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.
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
Loading

0 comments on commit e7b7b1e

Please sign in to comment.