Skip to content

Commit 2635d82

Browse files
Up to sliding window maximum
1 parent d7c1a8d commit 2635d82

File tree

5 files changed

+652
-0
lines changed

5 files changed

+652
-0
lines changed

longest-absolute-file-path.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func indexOfRune(chars []rune, char rune) int {
9+
for i, c := range chars {
10+
if c == char {
11+
return i
12+
}
13+
}
14+
15+
return -1
16+
}
17+
18+
func nextToken(chars []rune) string {
19+
if len(chars) == 0 {
20+
return ""
21+
}
22+
23+
if chars[0] == '\\' {
24+
wspChars := []rune{'\\', chars[1]}
25+
26+
return string(wspChars)
27+
}
28+
29+
if chars[0] == '\n' || chars[0] == '\t' {
30+
wspChars := []rune{'\\', chars[1]}
31+
32+
return string(wspChars)
33+
}
34+
35+
slashPos := indexOfRune(chars, '\\')
36+
37+
if slashPos == -1 {
38+
return string(chars)
39+
}
40+
41+
return string(chars[:slashPos])
42+
}
43+
44+
func nestingLevel(str string) int {
45+
chars := []rune(str)
46+
47+
for i, c := range chars {
48+
if c != '\t' {
49+
return i
50+
}
51+
}
52+
53+
return 0
54+
}
55+
56+
func max(a, b int) int {
57+
if a > b {
58+
return a
59+
}
60+
61+
return b
62+
}
63+
64+
func lengthLongestPath(input string) int {
65+
lines := strings.Split(input, "\n")
66+
dirStack := []string{}
67+
dirLen := 0
68+
maxPathLen := 0
69+
70+
for _, line := range lines {
71+
nstLvl := nestingLevel(line)
72+
73+
for nstLvl < len(dirStack) {
74+
dirLen -= len(dirStack[len(dirStack)-1])
75+
dirStack = dirStack[:len(dirStack)-1]
76+
}
77+
78+
trimmed := strings.Trim(line, "\t")
79+
80+
if strings.Index(line, ".") != -1 {
81+
// file
82+
// Add len(dirStack) to account for the file component separator ('/')
83+
pathLen := dirLen + len(dirStack) + len(trimmed)
84+
maxPathLen = max(maxPathLen, pathLen)
85+
} else {
86+
// dir
87+
dirLen += len(trimmed)
88+
dirStack = append(dirStack, trimmed)
89+
}
90+
}
91+
92+
return maxPathLen
93+
}
94+
95+
func testNextToken() {
96+
tests := []string{
97+
"abcd",
98+
"a\\nb\\n\\tc",
99+
"dir\\n\\tsubdir1\\n\\tsubdir2\\n\\t\\tfile.ext",
100+
"a\nb\n\tc",
101+
}
102+
103+
for _, test := range tests {
104+
testChars := []rune(test)
105+
106+
for token := nextToken(testChars); token != ""; {
107+
fmt.Printf("token: %s\n", token)
108+
109+
testChars = testChars[len(token):]
110+
token = nextToken(testChars)
111+
}
112+
}
113+
}
114+
115+
type testCase struct {
116+
path string
117+
longestPathLength int
118+
}
119+
120+
func test() {
121+
testCases := []testCase{
122+
testCase{path: "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext", longestPathLength: 20},
123+
testCase{path: "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext", longestPathLength: 32},
124+
testCase{path: "dir\n file.txt", longestPathLength: 12},
125+
}
126+
127+
for i, tc := range testCases {
128+
result := lengthLongestPath(tc.path)
129+
130+
if result != tc.longestPathLength {
131+
fmt.Printf("Test number %d FAILED. Expected: %d; got: %d\n", i, tc.longestPathLength, result)
132+
} else {
133+
fmt.Printf("Test number %d PASSED\n", i)
134+
}
135+
}
136+
}
137+
138+
func main() {
139+
test()
140+
}

paint-house-2.go

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func min2(a, b int) int {
6+
if a < b {
7+
return a
8+
}
9+
10+
return b
11+
}
12+
13+
// Assumption: all numbers in arr are non-negative
14+
func fillMinExceptAt(arr []int, minLeftScratch []int, minRightScratch []int) {
15+
// Populate minLeftScratch
16+
for i := range arr {
17+
if i == 0 {
18+
minLeftScratch[i] = -1
19+
20+
continue
21+
}
22+
23+
if i == 1 {
24+
minLeftScratch[i] = arr[i-1]
25+
26+
continue
27+
}
28+
29+
minLeftScratch[i] = min2(minLeftScratch[i-1], arr[i-1])
30+
}
31+
32+
// Populate minRightScratch
33+
for i := len(arr) - 1; i >= 0; i-- {
34+
if i == len(arr)-1 {
35+
minRightScratch[i] = -1
36+
37+
continue
38+
}
39+
40+
if i == len(arr)-2 {
41+
minRightScratch[i] = arr[i+1]
42+
43+
continue
44+
}
45+
46+
minRightScratch[i] = min2(minRightScratch[i+1], arr[i+1])
47+
}
48+
49+
for i := 0; i < len(arr); i++ {
50+
if minLeftScratch[i] == -1 {
51+
arr[i] = minRightScratch[i]
52+
53+
continue
54+
}
55+
56+
if minRightScratch[i] == -1 {
57+
arr[i] = minLeftScratch[i]
58+
59+
continue
60+
}
61+
62+
arr[i] = min2(minLeftScratch[i], minRightScratch[i])
63+
}
64+
}
65+
66+
func calcMin(costs [][]int, minCosts [][]int) int {
67+
k := len(costs[0])
68+
n := len(costs)
69+
lScratch := make([]int, k)
70+
rScratch := make([]int, k)
71+
72+
for i := n - 1; i >= 1; i-- {
73+
if i == n-1 {
74+
for j := 0; j < k; j++ {
75+
minCosts[i][j] = costs[i][j]
76+
}
77+
78+
fillMinExceptAt(minCosts[i], lScratch, rScratch)
79+
80+
continue
81+
}
82+
83+
for j := 0; j < k; j++ {
84+
minCosts[i][j] = costs[i][j] + minCosts[i+1][j]
85+
}
86+
87+
fillMinExceptAt(minCosts[i], lScratch, rScratch)
88+
}
89+
90+
// Handle final case of the first house
91+
var resultMinCost int
92+
93+
for j := 0; j < k; j++ {
94+
if n > 1 {
95+
minCosts[0][j] = costs[0][j] + minCosts[1][j]
96+
} else {
97+
minCosts[0][j] = costs[0][j]
98+
}
99+
100+
if j == 0 {
101+
resultMinCost = minCosts[0][j]
102+
103+
continue
104+
}
105+
106+
resultMinCost = min2(resultMinCost, minCosts[0][j])
107+
}
108+
109+
return resultMinCost
110+
}
111+
112+
func minCostII(costs [][]int) int {
113+
n := len(costs)
114+
115+
if n == 0 {
116+
return 0
117+
}
118+
119+
k := len(costs[0])
120+
minCosts := make([][]int, n)
121+
122+
for i := 0; i < n; i++ {
123+
minCosts[i] = make([]int, k)
124+
}
125+
126+
return calcMin(costs, minCosts)
127+
}
128+
129+
func test1() {
130+
test1 := [][]int{
131+
[]int{17, 2, 17},
132+
[]int{16, 16, 5},
133+
[]int{14, 3, 19},
134+
}
135+
136+
result := minCostII(test1)
137+
138+
fmt.Printf("result: %d\n", result)
139+
}
140+
141+
func test2() {
142+
test1 := [][]int{
143+
[]int{17, 2, 17},
144+
}
145+
146+
result := minCostII(test1)
147+
148+
fmt.Printf("result: %d\n", result)
149+
}
150+
151+
func test3() {
152+
test1 := [][]int{
153+
[]int{1, 5, 3},
154+
[]int{2, 9, 4},
155+
}
156+
157+
result := minCostII(test1)
158+
159+
fmt.Printf("result: %d\n", result)
160+
}
161+
162+
func testFillMinExcept() {
163+
// nums := []int{17, 2, 17}
164+
nums := []int{17, 17}
165+
lScratch := make([]int, len(nums))
166+
rScratch := make([]int, len(nums))
167+
168+
fillMinExceptAt(nums, lScratch, rScratch)
169+
170+
fmt.Printf("minExceptAt: %v\n", nums)
171+
}
172+
173+
func main() {
174+
test1()
175+
test2()
176+
test3()
177+
// testFillMinExcept()
178+
}

0 commit comments

Comments
 (0)