Skip to content

Commit ba99a20

Browse files
committed
Median of Two Sorted Arrays:
1 parent 9525b97 commit ba99a20

File tree

7 files changed

+232
-90
lines changed

7 files changed

+232
-90
lines changed

Easy/Two_Sum/Two_Sum.go

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,60 @@
1+
package Two_Sum
2+
13
// Runtime: 76 ms, faster than 8.22% of Go online submissions for Two Sum.
24
// Memory Usage: 2.9 MB, less than 100.00% of Go online submissions for Two Sum.
35
// 16/12/2019
46
// DD/MM/YY
57

68
func in(target int, list []int) bool {
7-
for _, i := range list {
8-
if target == i {
9-
return true
10-
}
11-
}
12-
return false
9+
for _, i := range list {
10+
if target == i {
11+
return true
12+
}
13+
}
14+
return false
1315
}
1416

1517
func count(target int, list []int) int {
16-
total := 0
17-
for _, i := range list {
18-
if i == target {
19-
total ++
20-
}
21-
}
22-
return total
18+
total := 0
19+
for _, i := range list {
20+
if i == target {
21+
total++
22+
}
23+
}
24+
return total
2325
}
2426

2527
func indice(target int, list []int) []int {
26-
indices := make([]int, 0, 0)
27-
for i := 0; i < len(list); i++ {
28-
if list[i] == target {
29-
indices = append(indices, i)
30-
}
31-
}
32-
return indices
28+
indices := make([]int, 0, 0)
29+
for i := 0; i < len(list); i++ {
30+
if list[i] == target {
31+
indices = append(indices, i)
32+
}
33+
}
34+
return indices
3335
}
3436

3537
func indexOf(target int, list []int) int {
36-
for k, v := range list {
37-
if target == v {
38-
return k
39-
}
40-
}
41-
return -1
38+
for k, v := range list {
39+
if target == v {
40+
return k
41+
}
42+
}
43+
return -1
4244
}
4345

4446
func twoSum(nums []int, target int) []int {
45-
for _, i := range nums {
46-
if in(target-i, nums) {
47-
if count(target-i, nums) >= 2 {
48-
indices := indice(target-i, nums)
49-
return []int {indices[0], indices[1]}
50-
} else if indexOf(i, nums) == indexOf(target-i, nums) {
51-
continue
52-
} else {
53-
return []int {indexOf(i, nums), indexOf(target-i, nums)}
54-
}
55-
}
56-
}
57-
return nil
58-
}
47+
for _, i := range nums {
48+
if in(target-i, nums) {
49+
if count(target-i, nums) >= 2 {
50+
indices := indice(target-i, nums)
51+
return []int{indices[0], indices[1]}
52+
} else if indexOf(i, nums) == indexOf(target-i, nums) {
53+
continue
54+
} else {
55+
return []int{indexOf(i, nums), indexOf(target-i, nums)}
56+
}
57+
}
58+
}
59+
return nil
60+
}

Easy/Two_Sum/Two_Sum.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
#16/12/2019
44
#DD/MM/YY
55

6-
def twoSum(nums, target):
7-
for i in nums:
8-
if target-i in nums:
9-
if nums.count(target-i) >= 2:
10-
indices = [index for index, x in enumerate(nums) if x == target-i]
11-
return [indices[0], indices[1]]
12-
elif nums.index(i) == nums.index(target-i): continue
13-
else: return [nums.index(i), nums.index(target-i)]
6+
class Solution:
7+
def twoSum(self, nums, target):
8+
for i in nums:
9+
if target-i in nums:
10+
if nums.count(target-i) >= 2:
11+
indices = [index for index, x in enumerate(nums) if x == target-i]
12+
return [indices[0], indices[1]]
13+
elif nums.index(i) == nums.index(target-i): continue
14+
else: return [nums.index(i), nums.index(target-i)]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package Median_of_Two_Sorted_arrays
2+
3+
// Runtime: 12 ms, faster than 89.45% of Go online submissions for Median of Two Sorted Arrays.
4+
// Memory Usage: 5.9 MB, less than 10.00% of Go online submissions for Median of Two Sorted Arrays.
5+
// 02/14/2020
6+
// MM/DD/YY
7+
// Suprisingly, shell sort has the fastest runtime for this method out of other sorting algorithms, such as
8+
// quicksort, merge sort, and merging (from merge sort)
9+
10+
func shell_sort(array []int) []int {
11+
gap := len(array) / 2
12+
for gap >= 1 {
13+
for i := gap; i < len(array); i++ { //iterate through array, starting from gap
14+
comparator := array[i] //make comparisons with this
15+
var output int //for accessing x outside the array
16+
var index int //for negative indexes
17+
18+
for x := i; x > gap-2; x -= gap { //iterate throguh array with gap as the step
19+
output = x //to access x outside the loop
20+
if x-gap < 0 { //in case of negative index
21+
index = len(array) - x - gap
22+
} else {
23+
index = x - gap
24+
}
25+
26+
if array[index] <= comparator { //break when correct spot is found
27+
break
28+
} else { //otherwise, move elements forward to make space
29+
array[x] = array[index]
30+
}
31+
}
32+
array[output] = comparator //insert comparator in the correct spot
33+
}
34+
gap /= 2 //increment the gap
35+
}
36+
return array
37+
}
38+
39+
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
40+
array := append(nums1, nums2...)
41+
array = shell_sort(array)
42+
if len(array)%2 == 1 {
43+
return float64(array[len(array)/2])
44+
} else {
45+
return float64(array[len(array)/2]+array[len(array)/2-1]) / 2
46+
}
47+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Runtime: 104 ms, faster than 92.06% of JavaScript online submissions for Median of Two Sorted Arrays.
2+
// Memory Usage: 39.2 MB, less than 72.34% of JavaScript online submissions for Median of Two Sorted Arrays.
3+
// 02/14/2020
4+
// MM/DD/YY
5+
6+
/*
7+
* @param {number[]} nums1
8+
* @param {number[]} nums2
9+
* @return {number}
10+
*/
11+
12+
function merge(array1, array2) {
13+
let array = []
14+
let first = 0
15+
let second = 0
16+
17+
while (first < array1.length && second < array2.length) {
18+
if (array1[first] > array2[second]) {
19+
array.push(array2[second])
20+
second++
21+
} else if (array1[first] < array2[second]) {
22+
array.push(array1[first])
23+
first++
24+
} else {
25+
array.push(array2[second])
26+
array.push(array1[first])
27+
second++
28+
first++
29+
}
30+
}
31+
32+
while (first < array1.length) {
33+
array.push(array1[first])
34+
first++
35+
}
36+
37+
while (second < array2.length) {
38+
array.push(array2[second])
39+
second++
40+
}
41+
42+
return array
43+
}
44+
45+
var findMedianSortedArrays = function(nums1, nums2) {
46+
let array = merge(nums1, nums2)
47+
if (array.length % 2 === 1) {
48+
return array[Math.floor(array.length/2)]
49+
} else {
50+
return (array[Math.floor(array.length/2)] + array[Math.floor(array.length/2)-1]) / 2
51+
}
52+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#Runtime: 92 ms, faster than 84.09% of Python3 online submissions for Median of Two Sorted Arrays.
2+
#Memory Usage: 12.7 MB, less than 100.00% of Python3 online submissions for Median of Two Sorted Arrays.
3+
#02/14/2020
4+
#MM/DD/YY
5+
6+
class Solution:
7+
#from merge sort
8+
@staticmethod
9+
def merge(array1, array2): #merge two arrays
10+
array = [] #merged array
11+
first = second = 0 #starting points
12+
13+
while first < len(array1) and second < len(array2):
14+
if array1[first] > array2[second]:
15+
array.append(array2[second])
16+
second += 1
17+
elif array1[first] < array2[second]:
18+
array.append(array1[first])
19+
first += 1
20+
else:
21+
array.append(array1[first])
22+
array.append(array2[second])
23+
first += 1
24+
second += 1
25+
26+
while first < len(array1):
27+
array.append(array1[first])
28+
first += 1
29+
30+
while second < len(array2):
31+
array.append(array2[second])
32+
second += 1
33+
34+
return array
35+
36+
def findMedianSortedArrays(self, nums1, nums2):
37+
array = self.merge(nums1, nums2)
38+
if len(array) % 2 == 1:
39+
return float(array[len(array)//2])
40+
else:
41+
return (array[len(array)//2] + array[len(array)//2-1]) / 2

Medium/Add_Two_Numbers/Add_Two_Numbers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package Add_Two_Numbers
2+
13
// Runtime: 12 ms, faster than 65.70% of Go online submissions for Add Two Numbers.
24
// Memory Usage: 5.9 MB, less than 7.32% of Go online submissions for Add Two Numbers.
35
// 17/12/2019
@@ -11,8 +13,6 @@
1113
* }
1214
*/
1315

14-
package Add_Two_Numbers
15-
1616
import (
1717
"strconv"
1818
"math/big"
Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,53 @@
1+
package lengthOfLongestSubstring
2+
13
// Runtime: 20 ms, faster than 31.05% of Go online submissions for Longest Substring Without Repeating Characters.
24
// Memory Usage: 6.4 MB, less than 20.00% of Go online submissions for Longest Substring Without Repeating Characters.
35
// 13/02/2020
46
// DD/MM/YY
57

6-
package lengthOfLongestSubstring
7-
88
import "strings"
99

1010
func contains(target string, list []string) bool {
11-
for _, i := range list {
12-
if target == i {
13-
return true
14-
}
15-
}
16-
return false
11+
for _, i := range list {
12+
if target == i {
13+
return true
14+
}
15+
}
16+
return false
1717
}
1818

1919
func set(arr []string) []string {
20-
all := make([]string, 0, 0)
21-
for _, elem := range arr {
22-
if !contains(elem, all) {
23-
all = append(all, elem)
24-
}
25-
}
26-
return all
20+
all := make([]string, 0, 0)
21+
for _, elem := range arr {
22+
if !contains(elem, all) {
23+
all = append(all, elem)
24+
}
25+
}
26+
return all
2727
}
2828

29-
3029
func lengthOfLongestSubstring(s string) int {
31-
letters := set(strings.Split(s, ""))
32-
longest := 0
33-
if len(letters) <= 2 {
34-
return len(letters)
35-
} else {
36-
for index, letter := range s {
37-
subString := append(make([]string, 0, 0), string(letter))
38-
for _, subLetter := range s[index+1:]{
39-
if !contains(string(subLetter), subString) {
40-
subString = append(subString, string(subLetter))
41-
} else {
42-
break
43-
}
44-
}
45-
if len(subString) > longest {
46-
longest = len(subString)
47-
}
48-
if longest == len(letters) || index >= len(s)-longest {
49-
break
50-
}
51-
}
52-
}
53-
return longest
54-
}
30+
letters := set(strings.Split(s, ""))
31+
longest := 0
32+
if len(letters) <= 2 {
33+
return len(letters)
34+
} else {
35+
for index, letter := range s {
36+
subString := append(make([]string, 0, 0), string(letter))
37+
for _, subLetter := range s[index+1:] {
38+
if !contains(string(subLetter), subString) {
39+
subString = append(subString, string(subLetter))
40+
} else {
41+
break
42+
}
43+
}
44+
if len(subString) > longest {
45+
longest = len(subString)
46+
}
47+
if longest == len(letters) || index >= len(s)-longest {
48+
break
49+
}
50+
}
51+
}
52+
return longest
53+
}

0 commit comments

Comments
 (0)