Skip to content

Commit 06232af

Browse files
authored
Merge pull request #6 from LuaanNguyen/daily-leetcode-1368
Daily LeetCode 1368
2 parents 48e6a6a + 14703ff commit 06232af

File tree

3 files changed

+146
-12
lines changed

3 files changed

+146
-12
lines changed

README.md

+13-12
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,19 @@ go test ./easy/0001_two_sum
2828

2929
## Solutions (Continue Updating...)
3030

31-
| Leetcode ID | Title & Solution | Coefficient Of Difficulty | Remarks |
32-
| :-----------------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------- | :-----------------------: | :----------------------------------------------: |
33-
| [0001](https://leetcode.com/problems/two-sum/description/) | [Two Sum](/easy/0001_two_sum) | Easy | _`HashTable`_ _`Array`_ |
34-
| [0392](https://leetcode.com/problems/is-subsequence/description/) | [Is Subsequence](/easy/0392_is_subsequence) | Easy | _`Two Pointers`_ _`String`_ |
35-
| [3042](https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/description/) | [Count Prefix and Suffix Pairs I](/easy/3042_count_prefix_and_sufix_pairs_I) | Easy | _`Array`_ _`String`_ |
36-
| [2185](https://leetcode.com/problems/counting-words-with-a-given-prefix/description/) | [Counting Words With a Given Prefix](/easy/2185_counting_words_with_a_given_string) | Easy | _`Array`_ _`String`_ _`String Matching`_ |
37-
| [1400](https://leetcode.com/problems/construct-k-palindrome-strings/description/?envType=daily-question&envId=2025-01-11) | [Construct K Palindrome Strings](/medium/1400_construct_k_palindrome_strings) | Medium | _`HashTable`_ _`String`_ _`Greedy`_ _`Counting`_ |
38-
| [2116](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/description/?envType=daily-question&envId=2025-01-12) | [Check if a Parentheses String Can Be Valid](/medium/2116_check_if_a_parentheses_string_can_be_valid) | Medium | _`Stack`_ _`String`_ _`Greedy`_ |
39-
| [3223](https://leetcode.com/problems/minimum-length-of-string-after-operations/description/?envType=daily-question&envId=2025-01-13) | [Minimum Length of String After Operations](/medium/2116_check_if_a_parentheses_string_can_be_valid) | Medium | _`HashTable`_ _`String`_ _`Counting`_ |
40-
| [2657](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/description/?envType=daily-question&envId=2025-01-14) | [Find the Prefix Common Array of Two Arrays](/medium/2657_find_the_prefix_common_array_of_two_arrays) | Medium | _`HashTable`_ _`Array`_ |
41-
| [2429](https://leetcode.com/problems/minimize-xor/description/?envType=daily-question&envId=2025-01-15) | [Minimize XOR](/medium/2429_minimize_XOR) | Medium | _`Bit Manipulation`_ |
42-
| [2425](https://leetcode.com/problems/bitwise-xor-of-all-pairings/description/?envType=daily-question&envId=2025-01-16) | [Bitwise XOR of All Pairings](/medium/2429_minimize_XOR) | Medium | _`Bit Manipulation`_ _`Brain Teaser`_ |
31+
| Leetcode ID | Title & Solution | Coefficient Of Difficulty | Remarks |
32+
| :-----------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------- | :-----------------------: | :----------------------------------------------------------------------------------------------: |
33+
| [0001](https://leetcode.com/problems/two-sum/description/) | [Two Sum](/easy/0001_two_sum) | Easy | _`HashTable`_ _`Array`_ |
34+
| [0392](https://leetcode.com/problems/is-subsequence/description/) | [Is Subsequence](/easy/0392_is_subsequence) | Easy | _`Two Pointers`_ _`String`_ |
35+
| [3042](https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/description/) | [Count Prefix and Suffix Pairs I](/easy/3042_count_prefix_and_sufix_pairs_I) | Easy | _`Array`_ _`String`_ |
36+
| [2185](https://leetcode.com/problems/counting-words-with-a-given-prefix/description/) | [Counting Words With a Given Prefix](/easy/2185_counting_words_with_a_given_string) | Easy | _`Array`_ _`String`_ _`String Matching`_ |
37+
| [1400](https://leetcode.com/problems/construct-k-palindrome-strings/description/?envType=daily-question&envId=2025-01-11) | [Construct K Palindrome Strings](/medium/1400_construct_k_palindrome_strings) | Medium | _`HashTable`_ _`String`_ _`Greedy`_ _`Counting`_ |
38+
| [2116](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/description/?envType=daily-question&envId=2025-01-12) | [Check if a Parentheses String Can Be Valid](/medium/2116_check_if_a_parentheses_string_can_be_valid) | Medium | _`Stack`_ _`String`_ _`Greedy`_ |
39+
| [3223](https://leetcode.com/problems/minimum-length-of-string-after-operations/description/?envType=daily-question&envId=2025-01-13) | [Minimum Length of String After Operations](/medium/2116_check_if_a_parentheses_string_can_be_valid) | Medium | _`HashTable`_ _`String`_ _`Counting`_ |
40+
| [2657](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/description/?envType=daily-question&envId=2025-01-14) | [Find the Prefix Common Array of Two Arrays](/medium/2657_find_the_prefix_common_array_of_two_arrays) | Medium | _`HashTable`_ _`Array`_ |
41+
| [2429](https://leetcode.com/problems/minimize-xor/description/?envType=daily-question&envId=2025-01-15) | [Minimize XOR](/medium/2429_minimize_XOR) | Medium | _`Bit Manipulation`_ |
42+
| [2425](https://leetcode.com/problems/bitwise-xor-of-all-pairings/description/?envType=daily-question&envId=2025-01-16) | [Bitwise XOR of All Pairings](/medium/2429_minimize_XOR) | Medium | _`Bit Manipulation`_ _`Brain Teaser`_ |
43+
| [1368](https://leetcode.com/problems/minimum-cost-to-make-at-least-one-valid-path-in-a-grid/?envType=daily-question&envId=2025-01-18) | [Minimum Cost to Make at Least One Valid Path in a Grid](/hard/1368_minimum_cost_to_make_at_least_one_valid_path_in_a_grid) | Hard | _`Breadth First Search`_ _`Graph`_ `Heap(Priority Queue)` _`Matrix`_ _`Shortest Path`_ _`Array`_ |
4344

4445
## License 🪪
4546

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package minimum_cost_to_make_at_least_one_valid_path_in_a_grid
2+
3+
import (
4+
"container/heap"
5+
"math"
6+
)
7+
8+
func minCost(grid [][]int) int {
9+
directions := [][]int {
10+
{0, 1},
11+
{0, -1},
12+
{1, 0},
13+
{-1, 0},
14+
}
15+
16+
ROWS := len(grid)
17+
COLS := len(grid[0])
18+
19+
minCost := make([][]int, ROWS)
20+
21+
for r := range minCost{
22+
minCost[r] = make([]int, COLS)
23+
for c := range minCost[r] {
24+
minCost[r][c] = math.MaxInt
25+
}
26+
}
27+
minCost[0][0] = 0
28+
29+
q := &PriorityQueue{}
30+
heap.Init(q)
31+
heap.Push(q, &Node{row: 0, col: 0, cost: 0})
32+
33+
for q.Len() > 0 {
34+
current := heap.Pop(q).(*Node)
35+
row, col, currCost := current.row, current.col, current.cost
36+
37+
if currCost > minCost[row][col] {
38+
continue
39+
}
40+
41+
for dirIdx, dir := range directions {
42+
dr, dc := dir[0], dir[1]
43+
newRow, newCol := row+dr, col+dc
44+
45+
cost := 0
46+
if grid[row][col] != dirIdx+1 {
47+
cost = 1
48+
}
49+
50+
if newRow >= 0 && newRow < ROWS && newCol >= 0 && newCol < COLS &&
51+
currCost+cost < minCost[newRow][newCol] {
52+
minCost[newRow][newCol] = currCost + cost
53+
if cost == 1 {
54+
heap.Push(q, &Node{row: newRow, col: newCol, cost: currCost + cost})
55+
} else {
56+
heap.Push(q, &Node{row: newRow, col: newCol, cost: currCost})
57+
}
58+
}
59+
}
60+
}
61+
62+
return minCost[ROWS -1][COLS -1]
63+
}
64+
65+
66+
type Node struct {
67+
row, col int
68+
cost int
69+
}
70+
71+
type PriorityQueue []*Node
72+
73+
func (pq PriorityQueue) Len() int { return len(pq) }
74+
func (pq PriorityQueue) Less(i, j int) bool { return pq[i].cost < pq[j].cost }
75+
func (pq PriorityQueue) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
76+
func (pq *PriorityQueue) Push(x interface{}) { *pq = append(*pq, x.(*Node)) }
77+
func (pq *PriorityQueue) Pop() interface{} {
78+
old := *pq
79+
n := len(old)
80+
item := old[n-1]
81+
*pq = old[:n-1]
82+
return item
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package minimum_cost_to_make_at_least_one_valid_path_in_a_grid
2+
3+
import "testing"
4+
5+
6+
func TestMinCost(t *testing.T) {
7+
tests := []struct {
8+
name string
9+
grid [][]int
10+
expectedResult int
11+
} {
12+
{
13+
name: "Test Case 1",
14+
grid: [][]int {
15+
{1,1,1,1},
16+
{2,2,2,2},
17+
{1,1,1,1},
18+
{2,2,2,2},
19+
},
20+
expectedResult: 3,
21+
},
22+
{
23+
name: "Test Case 2",
24+
grid: [][]int {
25+
{1,1,3},
26+
{3,2,2},
27+
{1,1,4},
28+
},
29+
expectedResult: 0,
30+
},
31+
{
32+
name: "Test Case 3",
33+
grid: [][]int {
34+
{1,2},
35+
{4,3},
36+
},
37+
expectedResult: 1,
38+
},
39+
}
40+
41+
42+
for _, test := range tests {
43+
t.Run(test.name, func(t *testing.T) {
44+
got := minCost(test.grid)
45+
if got != test.expectedResult {
46+
t.Errorf("minCost(%v) = %d; want = %d", test.grid, got, test.expectedResult)
47+
}
48+
})
49+
}
50+
}

0 commit comments

Comments
 (0)