Skip to content

Commit a64b07f

Browse files
merge-intervals
1 parent 5959af5 commit a64b07f

9 files changed

+663
-0
lines changed

divide-two-integers.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func divide(dividend int, divisor int) int {
8+
9+
if dividend == divisor {
10+
return 1
11+
}
12+
13+
if divisor == 1 {
14+
return dividend
15+
}
16+
17+
sgn := 1
18+
19+
if (dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0) {
20+
sgn = -1
21+
}
22+
23+
if dividend < 0 {
24+
dividend = -dividend
25+
}
26+
27+
if divisor < 0 {
28+
divisor = -divisor
29+
}
30+
31+
if dividend < divisor {
32+
return 0
33+
}
34+
35+
ans := 0
36+
temp := 0
37+
38+
for i := uint32(31); ; i-- {
39+
if temp+(divisor<<i) <= dividend {
40+
temp += divisor << i
41+
ans = ans | (1 << i)
42+
}
43+
44+
if i == 0 {
45+
break
46+
}
47+
}
48+
49+
if sgn == -1 {
50+
ans = -ans
51+
}
52+
53+
if ans >= 1<<31 {
54+
ans = (1 << 31) - 1
55+
}
56+
57+
return ans
58+
}
59+
60+
func main() {
61+
62+
tests := [][3]int{{1, 2, 0}, {10, 3, 3}, {5, 3, 1}, {0, -5, 0}, {-2147483648, -1, 2147483647}, {-2147483647, 2, -2147483647 / 2}}
63+
64+
for _, test := range tests {
65+
dividend, divisor, expAns := test[0], test[1], test[2]
66+
67+
ans := divide(dividend, divisor)
68+
69+
if ans != expAns {
70+
fmt.Printf("test failed: %d / %d should have been %d. Was %d.\n", dividend, divisor, expAns, ans)
71+
}
72+
}
73+
74+
}

group-anagrams.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
type ByRune []rune
9+
10+
func (b ByRune) Len() int { return len(b) }
11+
func (b ByRune) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
12+
func (b ByRune) Less(i, j int) bool { return b[i] < b[j] }
13+
14+
func groupAnagrams(strs []string) [][]string {
15+
anagrams := make(map[string][]int)
16+
17+
for i, s := range strs {
18+
runes := ByRune(s)
19+
sort.Sort(runes)
20+
normalized := string(runes)
21+
22+
idxs := anagrams[normalized]
23+
idxs = append(idxs, i)
24+
anagrams[normalized] = idxs
25+
}
26+
27+
result := make([][]string, 0, 10)
28+
29+
for _, idxs := range anagrams {
30+
vals := make([]string, len(idxs))
31+
32+
for i, idx := range idxs {
33+
vals[i] = strs[idx]
34+
}
35+
36+
result = append(result, vals)
37+
}
38+
39+
return result
40+
}
41+
42+
func main() {
43+
44+
var a ByRune = []rune("acb")
45+
46+
sort.Sort(a)
47+
48+
fmt.Printf("a: %v\n", string(a))
49+
50+
input := []string{"eat", "tea", "tan", "ate", "nat", "bat"}
51+
ans := groupAnagrams(input)
52+
53+
fmt.Printf("ans: %v\n", ans)
54+
55+
}

merge-intervals.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
type Interval struct {
9+
Start int
10+
End int
11+
}
12+
13+
type ByStart []Interval
14+
15+
func (intvls ByStart) Len() int {
16+
return len(intvls)
17+
}
18+
19+
func (intvls ByStart) Swap(i, j int) {
20+
intvls[i], intvls[j] = intvls[j], intvls[i]
21+
}
22+
23+
func (intvls ByStart) Less(i, j int) bool {
24+
return intvls[i].Start < intvls[j].Start
25+
}
26+
27+
func intervalsIntersect(intvl1, intvl2 Interval) bool {
28+
return ((intvl1.Start >= intvl2.Start && intvl1.Start <= intvl2.End) || (intvl1.End >= intvl2.Start && intvl1.End <= intvl2.End))
29+
}
30+
31+
func max(a, b int) int {
32+
if a >= b {
33+
return a
34+
}
35+
36+
return b
37+
}
38+
39+
func merge(intervals []Interval) []Interval {
40+
sort.Sort(ByStart(intervals))
41+
42+
mergedIntervals := make([]Interval, 0, len(intervals))
43+
44+
if len(intervals) == 0 {
45+
return intervals
46+
}
47+
48+
var currInterval Interval
49+
50+
for i, interval := range intervals {
51+
if i == 0 {
52+
currInterval = interval
53+
54+
continue
55+
}
56+
57+
if intervalsIntersect(currInterval, interval) || intervalsIntersect(interval, currInterval) {
58+
currInterval.End = max(currInterval.End, interval.End)
59+
} else {
60+
mergedIntervals = append(mergedIntervals, currInterval)
61+
currInterval = interval
62+
}
63+
}
64+
65+
mergedIntervals = append(mergedIntervals, currInterval)
66+
67+
return mergedIntervals
68+
}
69+
70+
func test1() {
71+
intvls := []Interval{
72+
Interval{1, 3},
73+
Interval{2, 6},
74+
Interval{8, 10},
75+
Interval{15, 18},
76+
}
77+
78+
mergedIntervals := merge(intvls)
79+
80+
fmt.Printf("merged: %v\n", mergedIntervals)
81+
}
82+
83+
func test2() {
84+
intvls := []Interval{
85+
Interval{1, 4},
86+
Interval{4, 5},
87+
}
88+
89+
mergedIntervals := merge(intvls)
90+
91+
fmt.Printf("merged: %v\n", mergedIntervals)
92+
}
93+
94+
func test3() {
95+
intvls := []Interval{
96+
Interval{1, 4},
97+
}
98+
99+
mergedIntervals := merge(intvls)
100+
101+
fmt.Printf("merged: %v\n", mergedIntervals)
102+
}
103+
104+
func test4() {
105+
intvls := []Interval{}
106+
107+
mergedIntervals := merge(intvls)
108+
109+
fmt.Printf("merged: %v\n", mergedIntervals)
110+
}
111+
112+
func test5() {
113+
intvls := []Interval{
114+
Interval{1, 4},
115+
Interval{2, 3},
116+
}
117+
118+
mergedIntervals := merge(intvls)
119+
120+
fmt.Printf("merged: %v\n", mergedIntervals)
121+
}
122+
123+
func main() {
124+
125+
// intvls := ByStart([]interval{interval{4, 3}, interval{5, 2}, interval{1, 10}})
126+
127+
// sort.Sort(intvls)
128+
129+
// fmt.Printf("sorted: %v\n", intvls)
130+
131+
test1()
132+
test2()
133+
test3()
134+
test4()
135+
test5()
136+
}

multiply-strings.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
)
7+
8+
// multiplicand should only comprise of digits
9+
func multiplyByDigit(multiplicand []int, multiplier int, acc []int, accOffset int) {
10+
mulCarry := 0
11+
accCarry := 0
12+
13+
for i, mulDigit := range multiplicand {
14+
prod := mulDigit*multiplier + mulCarry
15+
mulCarry = prod / 10
16+
prod %= 10
17+
acc[accOffset+i] += prod + accCarry
18+
accCarry = acc[accOffset+i] / 10
19+
acc[accOffset+i] %= 10
20+
// fmt.Printf("prod: %d; mulCarry = %d; accCarry: %d\n", prod, mulCarry, accCarry)
21+
}
22+
23+
accCarry += mulCarry
24+
25+
for i := (accOffset + len(multiplicand)); accCarry != 0; i++ {
26+
acc[i] += accCarry
27+
accCarry = acc[i] / 10
28+
acc[i] %= 10
29+
}
30+
}
31+
32+
func multiply(num1 string, num2 string) string {
33+
num1Digits := make([]int, len(num1))
34+
num2Digits := make([]int, len(num2))
35+
digitRunes := map[int]rune{
36+
0: '0',
37+
1: '1',
38+
2: '2',
39+
3: '3',
40+
4: '4',
41+
5: '5',
42+
6: '6',
43+
7: '7',
44+
8: '8',
45+
9: '9',
46+
}
47+
48+
for i, n1 := range num1 {
49+
num1Digits[len(num1)-1-i] = int(byte(n1) - '0')
50+
}
51+
52+
for i, n2 := range num2 {
53+
num2Digits[len(num2)-1-i] = int(byte(n2) - '0')
54+
}
55+
56+
acc := make([]int, len(num1)+len(num2)+5, len(num1)+len(num2)+5)
57+
58+
for i, multiplierDigit := range num2Digits {
59+
multiplyByDigit(num1Digits, multiplierDigit, acc, i)
60+
}
61+
62+
prodBuf := bytes.NewBufferString("")
63+
64+
for i, skip := len(acc)-1, true; i >= 0; i-- {
65+
if acc[i] == 0 && skip {
66+
continue
67+
}
68+
69+
skip = false
70+
71+
prodBuf.WriteRune(digitRunes[acc[i]])
72+
}
73+
74+
// fmt.Printf("product: %v\n", acc)
75+
76+
ans := prodBuf.String()
77+
78+
if ans == "" {
79+
return "0"
80+
}
81+
82+
return prodBuf.String()
83+
}
84+
85+
func main() {
86+
fmt.Printf("17 * 35: %s\n", multiply("17", "35"))
87+
fmt.Printf("172 * 543: %s\n", multiply("172", "543"))
88+
fmt.Printf("99999 * 99999: %s\n", multiply("99999", "99999"))
89+
fmt.Printf("1 * 99999: %s\n", multiply("1", "99999"))
90+
fmt.Printf("99999 * 1: %s\n", multiply("99999", "1"))
91+
fmt.Printf("0 * 0: %s\n", multiply("0", "0"))
92+
}

0 commit comments

Comments
 (0)