Skip to content

Commit 224c865

Browse files
committed
31
1 parent f89266d commit 224c865

File tree

7 files changed

+85
-15
lines changed

7 files changed

+85
-15
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ Number | Difficulty | Asked By
4747
[#28](problem028) | MEDIUM | Palantir
4848
[#29](problem029) | EASY | Amazon
4949
[#30](problem030) | MEDIUM | Facebook
50-
[#31](problem031) | MEDIUM | Facebook
50+
[#31](problem031) | EASY | Google
5151

problem028/problem028.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func Justify(words []string, lineLength int) []string {
1616
wordIndex++
1717
} else {
1818
justifiedWords = append(justifiedWords, justifyLine(lineSizeLeft, wordsInline, spaceCounts))
19-
justifiedWords, lineSizeLeft, wordsInline, spaceCounts = initValues(words, lineLength)
19+
_, lineSizeLeft, wordsInline, spaceCounts = initValues(words, lineLength)
2020
}
2121
}
2222
justifiedWords = append(justifiedWords, justifyLine(lineSizeLeft, wordsInline, spaceCounts))
@@ -25,10 +25,10 @@ func Justify(words []string, lineLength int) []string {
2525
}
2626

2727
func justifyLine(lineSizeLeft int, wordsInline []string, spaceCounts []int) string {
28+
if len(wordsInline) == 1 {
29+
spaceCounts = append(spaceCounts, 0)
30+
}
2831
for lineSize := 0; lineSize < lineSizeLeft; lineSize++ {
29-
if len(wordsInline) == 1 {
30-
spaceCounts = append(spaceCounts, 0)
31-
}
3232
spaceCounts[lineSize%len(spaceCounts)]++
3333
}
3434
if len(wordsInline) > 1 {

problem028/problem028_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ func TestJustify(t *testing.T) {
1010
result := Justify(words, 16)
1111
if !reflect.DeepEqual(result, []string{"the quick brown", "fox jumps over", "the lazy dog"}) {
1212
t.Fail()
13+
t.Log(result)
1314
}
1415
}

problem030/problem030.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package problem030
22

33
func MeasureTrappedWater(wallHeights []int) int {
44
trappedWater := 0
5-
leftCursor, rightCursor := 0, len(wallHeights) - 1
5+
leftCursor, rightCursor := 0, len(wallHeights)-1
66
leftMaxHeight, rightMaxHeight := 0, 0
77
for leftCursor <= rightCursor {
88
if leftHeight, rightHeight := wallHeights[leftCursor], wallHeights[rightCursor]; leftHeight < rightHeight {
@@ -22,6 +22,5 @@ func MeasureTrappedWater(wallHeights []int) int {
2222
}
2323
}
2424

25-
2625
return trappedWater
2726
}

problem031/README.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
## Daily Coding Problem: Problem #30 [Medium]
1+
## Daily Coding Problem: Problem #31 [EASY]
22

33
Good morning! Here's your coding interview problem for today.
44

5-
This problem was asked by Facebook.
5+
This problem was asked by Google.
66

7-
You are given an array of non-negative integers that represents a two-dimensional elevation map where each element is unit-width wall and the integer is the height. Suppose it will rain and all spots between two walls get filled up.
7+
The edit distance between two strings refers to the minimum number of character insertions, deletions, and substitutions required to change one string to the other. For example, the edit distance between “kitten” and “sitting” is three: substitute the “k” for “s”, substitute the “e” for “i”, and append a “g”.
88

9-
Compute how many units of water remain trapped on the map in O(N) time and O(1) space.
10-
11-
For example, given the input [2, 1, 2], we can hold 1 unit of water in the middle.
12-
13-
Given the input [3, 0, 1, 3, 0, 5], we can hold 3 units in the first index, 2 in the second, and 3 in the fourth index (we cannot hold 5 since it would run off to the left), so we can trap 8 units of water.
9+
Given two strings, compute the edit distance between them.

problem031/problem031.go

+54
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
11
package problem031
2+
3+
func minInt(nums ...int) int {
4+
minimum := nums[0]
5+
for _, num := range nums {
6+
if num < minimum {
7+
minimum = num
8+
}
9+
}
10+
return minimum
11+
}
12+
13+
func MeasureEditDistance(value1 string, value2 string) int {
14+
// make sure width > height of the matrix
15+
if len(value1) < len(value2) {
16+
value1, value2 = value2, value1
17+
}
18+
19+
matrixWidth, matrixHeight := len(value1)+1, len(value2)+1
20+
distanceMatrix := make([][]int, matrixHeight, matrixHeight)
21+
for rowIndex := range distanceMatrix {
22+
distanceMatrix[rowIndex] = make([]int, matrixWidth, matrixWidth)
23+
}
24+
25+
for initialRow, initialColumn := 0, 0; initialRow < matrixHeight && initialColumn < matrixWidth; {
26+
for row, column := initialRow, initialColumn; 0 <= row && row < matrixHeight && 0 <= column && column < matrixWidth; {
27+
switch {
28+
case row == 0:
29+
distanceMatrix[row][column] = column
30+
case column == 0:
31+
distanceMatrix[row][column] = row
32+
default:
33+
if value2[row-1] == value1[column-1] {
34+
distanceMatrix[row][column] = distanceMatrix[row-1][column-1]
35+
} else {
36+
distanceMatrix[row][column] = 1 + minInt(
37+
distanceMatrix[row-1][column],
38+
distanceMatrix[row-1][column-1],
39+
distanceMatrix[row][column-1],
40+
)
41+
}
42+
}
43+
row++
44+
column--
45+
}
46+
47+
if initialColumn < matrixWidth-1 {
48+
initialColumn++
49+
} else {
50+
initialRow++
51+
}
52+
}
53+
54+
return distanceMatrix[matrixHeight-1][matrixWidth-1]
55+
}

problem031/problem031_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
11
package problem031
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMeasureEditDistance(t *testing.T) {
8+
var editDistance int
9+
10+
editDistance = MeasureEditDistance("sitting", "kitten")
11+
if editDistance != 3 {
12+
t.Fail()
13+
t.Log(editDistance)
14+
}
15+
16+
editDistance = MeasureEditDistance("Saturday", "Sunday")
17+
if editDistance != 3 {
18+
t.Fail()
19+
t.Log(editDistance)
20+
}
21+
}

0 commit comments

Comments
 (0)