8000 solved 1089 Β· realtemirov/leetcode@9fe11b8 Β· GitHub
[go: up one dir, main page]

Skip to content

Commit 9fe11b8

Browse files
committed
solved 1089
1 parent f8dc094 commit 9fe11b8

File tree

7 files changed

+106
-63
lines changed

7 files changed

+106
-63
lines changed

β€ŽProblems/1089. Duplicate Zeros.md

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 1089. Duplicate Zeros
2+
3+
🟩 Easy
4+
5+
## Solution
6+
7+
My Solution
8+
9+
```go
10+
func duplicateZeros(arr []int) {
11+
zeros := 0
12+
13+
for _, v := range arr {
14+
if v == 0 {
15+
zeros++
16+
}
17+
}
18+
19+
for i := len(arr) - 1; i >= 0; i-- {
20+
if arr[i] == 0 {
21+
if zeros + i < len(arr) {
22+
arr[zeros + i] = 0
23+
}
24+
25+
if zeros - 1 + i < len(arr) {
26+
arr[zeros - 1 + i] = 0
27+
}
28+
29+
zeros--
30+
} else if i + zeros < len(arr) {
31+
arr[zeros + i] = arr[i]
32+
}
33+
}
34+
}
35+
```
36+
37+
![result](1089.png)
38+
39+
Leetcode: [link](https://leetcode.com/problems/duplicate-zeros/description/)

β€ŽProblems/1089/1089.png

35.5 KB
Loading

β€ŽProblems/1089/solution.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package problem1089
2+
3+
func DuplicateZeros(arr []int) {
4+
zeros := 0
5+
6+
for _, v := range arr {
7+
if v == 0 {
8+
zeros++
9+
}
10+
}
11+
12+
for i := len(arr) - 1; i >= 0; i-- {
13+
if arr[i] == 0 {
14+
if zeros+i < len(arr) {
15+
arr[zeros+i] = 0
16+
}
17+
18+
if zeros-1+i < len(arr) {
19+
arr[zeros-1+i] = 0
20+
}
21+
22+
zeros--
23+
} else if i+zeros < len(arr) {
24+
arr[zeros+i] = arr[i]
25+
}
26+
}
27+
}

β€ŽProblems/1089/solution_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package problem1089_test
2+
3+
import (
4+
"testing"
5+
6+
problem1089 "github.com/realtemirov/leetcode/Problems/1089"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestSolution(t *testing.T) {
11+
testCases := []struct {
12+
name string
13+
cases []int
14+
expected []int
15+
}{
16+
{
17+
name: "Test 1",
18+
cases: []int{1, 0, 2, 3, 0, 4, 5, 0},
19+
expected: []int{1, 0, 0, 2, 3, 0, 0, 4},
20+
},
21+
{
22+
name: "Test 2",
23+
cases: []int{1, 2, 3},
24+
expected: []int{1, 2, 3},
25+
},
26+
}
27+
28+
for _, tc := range testCases {
29+
t.Run(tc.name, func(t *testing.T) {
30+
problem1089.DuplicateZeros(tc.cases)
31+
result := tc.cases
32+
require.Equal(t, tc.expected, result, "expected: %v, result: %v", tc.expected, result)
33+
})
34+
}
35+
}

β€ŽProblems/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Problems
22

3-
> **Total:** `77` \
4-
> **Easy:** `54` \
3+
> **Total:** `78` \
4+
> **Easy:** `55` \
55
> **Medium:** `23` \
66
> **Hard:** `0`
77
@@ -60,6 +60,7 @@
6060
* 🟩 Easy - [976. Largest Perimeter Triangle](<./0976/976. Largest Perimeter Triangle.md>)
6161
* 🟩 Easy - [977. Squares of a Sorted Array](<./0977/977. Squares of a Sorted Array.md>)
6262
* 🟧 Medium - [1041. Robot Bounded In Circle](<./1041/1041. Robot Bounded In Circle.md>)
63+
* 🟩 Easy - [1089. Duplicate Zeros](<./1089/1089. Duplicate Zeros.md>)
6364
* 🟩 Easy - [1232. Check If It Is a Straight Line](<./1232/1232. Check If It Is a Straight Line.md>)
6465
* 🟩 Easy - [1275. Find Winner on a Tic Tac Toe Game](<./1275/1275. Find Winner on a Tic Tac Toe Game.md>)
6566
* 🟩 Easy - [1295. Find Numbers with Even Number of Digits](<./1295/1295. Find Numbers with Even Number of Digits.md>)

β€ŽSUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
* [976. Largest Perimeter Triangle](<./Problems/0976/976. Largest Perimeter Triangle.md>)
116116
* [977. Squares of a Sorted Array](<./Problems/0977/977. Squares of a Sorted Array.md>)
117117
* [1041. Robot Bounded In Circle](<./Problems/1041/1041. Robot Bounded In Circle.md>)
118+
* [1089. Duplicate Zeros](<./Problems/1089/1089. Duplicate Zeros.md>)
118119
* [1232. Check If It Is a Straight Line](<./Problems/1232/1232. Check If It Is a Straight Line.md>)
119120
* [1275. Find Winner on a Tic Tac Toe Game](<./Problems/1275/1275. Find Winner on a Tic Tac Toe Game.md>)
120121
* [1295. Find Numbers with Even Number of Digits](<./Problems/1295/1295. Find Numbers with Even Number of Digits.md>)
@@ -208,7 +209,7 @@
208209
* [1043. Partition Array for Maximum Sum](<Problems/1043. Partition Array for Maximum Sum.md>)
209210
* [1071. Greatest Common Divisor of Strings](<Problems/1071. Greatest Common Divisor of Strings.md>)
210211
* [1084. Sales Analysis III](<Problems/1084. Sales Analysis III.md>)
211-
* [1089. Duplicate Zeros](<Problems/1089. Duplicate Zeros.md>)
212+
212213
* [1141. User Activity for the Past 30 Days I](<Problems/1141. User Activity for the Past 30 Days I.md>)
213214
* [1148. Article Views I](<Problems/1148. Article Views I.md>)
214215
* [1160. Find Words That Can Be Formed by Characters](<Problems/1160. Find Words That Can Be Formed by Characters.md>)

0 commit comments

Comments
Β (0)
0