Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a5856b8

Browse files
Add MoTSArrays to LeetCode → Completed
1 parent 91cad1d commit a5856b8

File tree

6 files changed

+88
-11
lines changed

6 files changed

+88
-11
lines changed
File renamed without changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# ProbName - Notes
2+
3+
1. concat arrays
4+
2. sort THAT new array
5+
3. get median of array
6+
7+
### 2 Cases
8+
9+
- amount of numbers in array is even
10+
- get index of middle value
11+
- return array[middleValue]
12+
- amount of numbers in array is odd
13+
- get middle two values
14+
- add them together, then divide by 2 and return that value
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// ===================================
2+
// Median of Two Sorted Arrays - Solution
3+
// ===================================
4+
5+
function findMedianSortedArrays(nums1, nums2) {
6+
// combine and sort both arrays in one method chain
7+
let combinedAndSortedArr = nums1.concat(nums2).sort();
8+
9+
// 2 CASES
10+
// #1 - if the amount of numbers in the combined arrays is odd...
11+
if (combinedAndSortedArr.length % 2 !== 0) {
12+
// return combinedAndSortedArr[middleValue]
13+
let middleValue = Math.floor(combinedAndSortedArr.length / 2);
14+
15+
// ...return the middle value of the array
16+
return combinedAndSortedArr[middleValue];
17+
}
18+
// #2 - amount of numbers is even
19+
else {
20+
// take the two middle values of the array...
21+
let numOne = combinedAndSortedArr[combinedAndSortedArr.length / 2];
22+
let numTwo = combinedAndSortedArr[combinedAndSortedArr.length / 2 - 1];
23+
24+
// ... and return their sum divided by 2
25+
return (numOne + numTwo) / 2;
26+
}
27+
}
28+
29+
// nums1 = [1, 2]
30+
// nums2 = [3]
31+
// findMedianSortedArrays(nums1, nums2); => 2 [CORRECT]
32+
33+
// nums3 = [5, 6]
34+
// nums4 = [7, 8]
35+
// findMedianSortedArrays(nums3, nums4); => 6.5 [CORRECT]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/description/)
2+
3+
There are two sorted arrays nums1 and nums2 of size m and n respectively.
4+
5+
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
6+
7+
You may assume nums1 and nums2 cannot be both empty.
8+
9+
Examples:
10+
11+
```
12+
nums1 = [1, 3]
13+
nums2 = [2]
14+
15+
The median is 2.0
16+
17+
nums1 = [1, 2]
18+
nums2 = [3, 4]
19+
20+
The median is (2 + 3)/2 = 2.5
21+
```
22+
23+
---
24+
25+
| Started | Last revisited | Completed |
26+
| ----------- | -------------- | ----------- |
27+
| 07-Sep-2018 | 07-Sep-2018 | 07-Sep-2018 |

‎LeetCode/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
## Completed
44

5-
| Problem | Completed | Code Link |
6-
| ------------------------------- | ----------- | ------------------------------- |
7-
| Longest Non-repeating Substring | 07-Sep-2018 | [Problem](https://git.io/fAz0T) |
8-
| Two Sum | 06-Sep-2018 | [Problem](https://git.io/fAzUw) |
5+
| Problem | Completed | Code Link |
6+
| ------------------------------- | ----------- | ----------------------------------- |
7+
| Median of Two Sorted Arrays | 07-Sep-2018 | [Problem](Update link after commit) |
8+
| Longest Non-repeating Substring | 07-Sep-2018 | [Problem](https://git.io/fAz0T) |
9+
| Two Sum | 06-Sep-2018 | [Problem](https://git.io/fAzUw) |
910

1011
## Need to Revisit
1112

‎README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ My notes and answers as JS files are also available to show my answer and steps
88

99
### Last 5 Completed
1010

11-
| Problem | Source | Completed | Prompt & Code |
12-
| ------------------------------- | ------ | ----------- | ------------------------------- |
13-
| Longest Non-repeating Substring | LEET | 07-Sep-2018 | [Problem](https://git.io/fAz0T) |
14-
| Two Sum | LEET | 06-Sep-2018 | [Problem](https://git.io/fAzUw) |
15-
| #76: Title Case [Easy] | DAILY | 05-Sep-2018 | [Problem](https://git.io/fAREH) |
16-
| All String Combinations | W3R | 04-Sep-2018 | [Problem](https://git.io/fARtq) |
17-
| #6 - Sum square difference | EULER | 16-Jun-2016 | [Prob 6](https://git.io/fARtl) |
11+
| Problem | Source | Completed | Prompt & Code |
12+
| ------------------------------- | ------ | ----------- | ----------------------------------- |
13+
| Median of Two Sorted Arrays | LEET | 07-Sep-2018 | [Problem](Update link after commit) |
14+
| Longest Non-repeating Substring | LEET | 07-Sep-2018 | [Problem](https://git.io/fAzuK) |
15+
| Two Sum | LEET | 06-Sep-2018 | [Problem](https://git.io/fAzUw) |
16+
| #76: Title Case [Easy] | DAILY | 05-Sep-2018 | [Problem](https://git.io/fAREH) |
17+
| All String Combinations | W3R | 04-Sep-2018 | [Problem](https://git.io/fARtq) |
1818

1919
### Last 5 Revisited
2020

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /