Skip to content

Commit 6127b6c

Browse files
committed
Add daily 1765. Map of Highest Peak
1 parent 37b05f8 commit 6127b6c

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ go test ./easy/0001_two_sum
3030

3131
| Leetcode ID | Title & Solution | Coefficient Of Difficulty | Remarks |
3232
| :-----------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------- | :-----------------------: | :----------------------------------------------------------------------------------------------: |
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`_ |
3533
| [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`_ |
3634
| [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`_ |
3735
| [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`_ |
@@ -45,6 +43,7 @@ go test ./easy/0001_two_sum
4543
| [407](https://leetcode.com/problems/trapping-rain-water-ii/?envType=daily-question&envId=2025-01-19) | [Trapping Rain Water II](/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`_ |
4644
| [2661](https://leetcode.com/problems/first-completely-painted-row-or-column/description/?envType=daily-question&envId=2025-01-20) | [First Completely Painter Row or Column](/medium/2661_first_completely_painted_row_or_column) | medium | _`Matrix`_ _`HashTable`_ _`Array`_ |
4745
| [2017](https://leetcode.com/problems/grid-game/) | [Grid Game](/medium/2017_grid_game) | medium | _`Matrix`_ _`Prefix Sum`_ |
46+
| [1765](https://leetcode.com/problems/map-of-highest-peak/description/?envType=daily-question&envId=2025-01-22) | [Map of Highest Peak](/medium/1765_map_of_highest_peak) | medium | _`Graph`_ _`Breadth-First Search`_ |
4847

4948
## License 🪪
5049

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package map_of_highest_peak
2+
3+
func highestPeak(isWater [][]int) [][]int {
4+
rows := len(isWater)
5+
cols := len(isWater[0])
6+
directions := [][2]int{
7+
{1, 0}, {-1, 0}, {0, 1}, {0, -1},
8+
}
9+
10+
// Initialize result grid with -1 for land and 0 for water
11+
res := make([][]int, rows)
12+
for i := range res {
13+
res[i] = make([]int, cols)
14+
for j := range res[i] {
15+
res[i][j] = -1
16+
}
17+
}
18+
19+
// Initialize queue for BFS
20+
type point struct{ r, c int }
21+
q := []point{}
22+
23+
// Add all water cells to the queue and mark them as height 0
24+
for r := 0; r < rows; r++ {
25+
for c := 0; c < cols; c++ {
26+
if isWater[r][c] == 1 {
27+
res[r][c] = 0
28+
q = append(q, point{r, c})
29+
}
30+
}
31+
}
32+
33+
// Perform BFS
34+
for len(q) > 0 {
35+
curr := q[0]
36+
q = q[1:]
37+
for _, dir := range directions {
38+
nr, nc := curr.r+dir[0], curr.c+dir[1]
39+
if nr >= 0 && nr < rows && nc >= 0 && nc < cols && res[nr][nc] == -1 {
40+
res[nr][nc] = res[curr.r][curr.c] + 1
41+
q = append(q, point{nr, nc})
42+
}
43+
}
44+
}
45+
46+
return res
47+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package map_of_highest_peak
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestHighestPeek(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
isWater [][]int
11+
expectedResult [][]int
12+
} {
13+
{
14+
name: "Test Case 1",
15+
isWater: [][]int {
16+
{0,1},
17+
{0,0},
18+
},
19+
expectedResult: [][]int {
20+
{1,0},
21+
{2,1},
22+
},
23+
},
24+
{
25+
name: "Test Case 2",
26+
isWater: [][]int {
27+
{0,0,1},
28+
{1,0,0},
29+
{0,0,0},
30+
},
31+
expectedResult: [][]int {
32+
{1,1,0},
33+
{0,1,1},
34+
{1,2,2},
35+
},
36+
},
37+
}
38+
39+
for _, test := range tests {
40+
t.Run(test.name, func(t *testing.T) {
41+
got := highestPeak(test.isWater)
42+
if !equal2DSlices(got, test.expectedResult) {
43+
t.Errorf("highestPeek(%v) = %v; want = %v", test.isWater, got ,test.expectedResult)
44+
}
45+
})
46+
}
47+
}
48+
49+
func equal2DSlices(a, b [][]int) bool {
50+
if len(a) != len(b) {
51+
return false
52+
}
53+
for i := range a {
54+
if len(a[i]) != len(b[i]) {
55+
return false
56+
}
57+
for j := range a[i] {
58+
if a[i][j] != b[i][j] {
59+
return false
60+
}
61+
}
62+
}
63+
return true
64+
}

0 commit comments

Comments
 (0)