Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions go/127-Word-Ladder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
func ladderLength(beginWord string, endWord string, wordList []string) int {
wordMap := getWordMap(wordList, beginWord)
que := []string{beginWord}
depth := 0
for len(que) > 0 {
depth++

qlen := len(que)
for i := 0; i < qlen; i++ {
word := que[0]
que = que[1:]

candidates := getCandidates(word)
for _, candidate := range candidates {
if _, ok := wordMap[candidate]; ok {
if candidate == endWord {
return depth + 1
}

delete(wordMap, candidate)
que = append(que, candidate)
}
}
}
}
return 0
}

func getWordMap(wordList []string, beginWord string) map[string]int {
wordMap := make(map[string]int)
for i, word := range wordList {
if _, ok := wordMap[word]; !ok {
if word != beginWord {
wordMap[word] = i
}
}
}
return wordMap
}

func getCandidates(word string) []string {
var res []string
for i := 0; i < 26; i++ {
for j := 0; j < len(word); j++ {
if word[j] != byte(int('a')+i) {
res = append(res, word[:j]+string(int('a')+i)+word[j+1:])
}
}
}
return res
}
60 changes: 60 additions & 0 deletions go/130-Surrounded-Regions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
func solve(board [][]byte) {
n := len(board)
m := len(board[0])

// If board have less than 3 size in any direction: nothing to do, because all cells located on borders
if n < 3 || m < 3 {
return
}

// Go and check left and right borders of the board
for row := 0; row < n; row++ {
if board[row][0] == 'O' {
dfs(row, 0, n, m, board)
}
if board[row][m - 1] == 'O' {
dfs(row, m - 1, n, m, board)
}
}

// Same for check up and down borders of the board
// Since corners (0,0) and (n - 1, m - 1) where checked in previous cycle, skip them in this one
for col := 1; col < m - 1; col++ {
if board[0][col] == 'O' {
dfs(0, col, n, m, board)
}
if board[n - 1][col] == 'O' {
dfs(n - 1, col, n, m, board)
}
}

// Follow through the whole board and flip all 'R' cells back into 'O' and all 'O' cell to 'X'
// since they're unreacheable from the board located 'O' cell if any
for row := 0; row < n; row++ {
for col := 0; col < m; col++ {
if board[row][col] == 'O' {
board[row][col] = 'X'
} else if board[row][col] == 'R' {
board[row][col] = 'O'
}
}
}
}

// DFS to look for the next 'O' cell upper, lower, to the right and to the left of current coordinates
// If 'O' cell is found, recursevly mark this cell as 'R' which is mean REACHED
func dfs(row int, col int, n int, m int, board [][]byte) {
board[row][col] = 'R'
if row > 0 && board[row - 1][col] == 'O' {
dfs(row - 1, col, n, m, board)
}
if row < n - 1 && board[row + 1][col] == 'O' {
dfs(row + 1, col, n, m, board)
}
if col > 0 && board[row][col - 1] == 'O' {
dfs(row, col - 1, n, m, board)
}
if col < m - 1 && board[row][col + 1] == 'O' {
dfs(row, col + 1, n, m, board)
}
}
22 changes: 22 additions & 0 deletions go/139-Word-Break.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
func wordBreak(s string, wordDict []string) bool {
dp := make([]bool, len(s) + 1)
dp[0] = true

for i := range s {
if !dp[i] {
continue
}

for _, word := range wordDict {
if i + len(word) > len(s) {
continue
}

if s[i:i+len(word)] == word {
dp[i+len(word)] = true
}
}
}

return dp[len(s)]
}
29 changes: 29 additions & 0 deletions go/152-Maximum-Product-Subarray.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
func maxProduct(nums []int) int {
tmpMin, tmpMax, ans := nums[0], nums[0], nums[0]

for i := 1; i < len(nums);i++{
tmpMin, tmpMax =
min(nums[i], min(nums[i]*tmpMax, nums[i]*tmpMin)),
max(nums[i], max(nums[i]*tmpMax, nums[i]*tmpMin))
ans = max(ans, tmpMax)
}


return ans
}

func max(a,b int)int{
if a < b{
return b
}

return a
}

func min(a,b int)int{
if a > b{
return b
}

return a
}
42 changes: 42 additions & 0 deletions go/207-Course-Schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
func canFinish(numCourses int, prerequisites [][]int) bool {
visited := map[int]bool{}
adjList, indegree := getMaps(prerequisites)


q := []int{}
for i := 0; i < numCourses; i++ {
if indegree[i] == 0 {
q = append(q, i)
}
}

for len(q) > 0 {
course := q[0] // top
q = q[1:]

visited[course] = true
for _, conn := range adjList[course] {
indegree[conn]--

if indegree[conn] == 0 && !visited[conn] {
q = append(q, conn)
delete(indegree, conn)
}
}
}

return len(indegree) == 0
}

func getMaps(edges [][]int) (map[int][]int, map[int]int) {
adjList := map[int][]int{}
indegree := map[int]int{}

for _, edge := range edges {
s, e := edge[1], edge[0]
adjList[s] = append(adjList[s], e)

indegree[e]++
}
return adjList, indegree
}
51 changes: 51 additions & 0 deletions go/210-Course-Schedule-II.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
func findOrder(numCourses int, prerequisites [][]int) []int {
graph, incomings := buildMap(numCourses, prerequisites)

var starts []int
for i, incoming := range incomings {
if incoming == 0 {
starts = append(starts, i)
}
}

var res []int
var visitNum int
for _, start := range starts {
queue := []int{start}
res = append(res, start)
visitNum += 1
for len(queue) > 0 {
node := queue[0]
queue = queue[1:]
neighbors, ok := graph[node]
if ok {
for _, neighbor := range neighbors {
incomings[neighbor] -= 1
if incomings[neighbor] == 0 {
queue = append(queue, neighbor)
res = append(res, neighbor)
visitNum += 1
}
}
}
}
}
if visitNum == numCourses {
return res
}
return []int{}
}

func buildMap(numCourses int, prerequisites [][]int) (map[int][]int, []int) {
graph := make(map[int][]int)
incomings := make([]int, numCourses)
for _, preq := range prerequisites {
if _, ok := graph[preq[1]]; !ok {
graph[preq[1]] = []int{preq[0]}
} else {
graph[preq[1]] = append(graph[preq[1]], preq[0])
}
incomings[preq[0]] += 1
}
return graph, incomings
}
28 changes: 28 additions & 0 deletions go/261-Graph-Valid-Tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
func validTree(n int, edges [][]int) bool {
if len(edges) != n-1 {
return false
}

seen := make(map[int]bool)
g := make(map[int][]int)

for _, edge := range edges {
g[edge[0]] = append(g[edge[0]], edge[1])
g[edge[1]] = append(g[edge[1]], edge[0])
}

dfs(0, seen, g)

return len(seen) == n
}

func dfs(u int, seen map[int]bool, g map[int][]int) {
if _, ok := seen[u]; ok {
return
}
seen[u] = true

for _, v := range g[u] {
dfs(v, seen, g)
}
}
53 changes: 53 additions & 0 deletions go/286-Walls-And-Gates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import "math"

const (
empty = math.MaxInt32
gate = 0
)

var directions = [4][2]int{
{1, 0},
{-1, 0},
{0, 1},
{0, -1},
}

func wallsAndGates(rooms [][]int) {
if len(rooms) == 0 || len(rooms[0]) == 0 {
return
}

queue := findGatesCoordinates(rooms)

rowLength, columnLength := len(rooms), len(rooms[0])

for len(queue) > 0 {
coordinates := queue[0]
queue = queue[1:]
row, col := coordinates[0], coordinates[1]

for _, direction := range directions {
currentRow := row + direction[0]
currentCol := col + direction[1]
isOutOfBounds := currentRow < 0 || currentCol < 0 || currentRow >= rowLength || currentCol >= columnLength

if !isOutOfBounds && rooms[currentRow][currentCol] == empty {
rooms[currentRow][currentCol] = rooms[row][col] + 1
queue = append(queue, []int{currentRow, currentCol})
}
}
}
}

func findGatesCoordinates(rooms [][]int) [][]int {
queue := [][]int{}
for row := range rooms {
for col := range rooms[row] {
if rooms[row][col] == gate {
queue = append(queue, []int{row, col})
}
}
}

return queue
}
Loading