Skip to content

Add solution and test-cases for problem 3355 #1210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
45 changes: 32 additions & 13 deletions leetcode/3301-3400/3355.Zero-Array-Transformation-I/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
# [3355.Zero Array Transformation I][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given an integer array `nums` of length n and a 2D array `queries`, where `queries[i] = [li, ri]`.

For each `queries[i]`:

- Select a subset of indices within the range `[li, ri]` in nums.
- Decrement the values at the selected indices by 1.

A **Zero Array** is an array where all elements are equal to 0.

Return `true` if it is possible to transform `nums` into a **Zero Array** after processing all the queries sequentially, otherwise return `false`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Input: nums = [1,0,1], queries = [[0,2]]

## 题意
> ...
Output: true

## 题解
Explanation:

### 思路1
> ...
Zero Array Transformation I
```go
For i = 0:
Select the subset of indices as [0, 2] and decrement the values at these indices by 1.
The array will become [0, 0, 0], which is a Zero Array.
```

**Example 2:**

```
Input: nums = [4,3,2,1], queries = [[1,3],[0,2]]

Output: false

Explanation:

For i = 0:
Select the subset of indices as [1, 2, 3] and decrement the values at these indices by 1.
The array will become [4, 2, 1, 0].
For i = 1:
Select the subset of indices as [0, 1, 2] and decrement the values at these indices by 1.
The array will become [3, 1, 0, 0], which is not a Zero Array.
```

## 结语

Expand Down
22 changes: 20 additions & 2 deletions leetcode/3301-3400/3355.Zero-Array-Transformation-I/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int, queries [][]int) bool {
deltaArray := make([]int, len(nums)+1)
for _, query := range queries {
left := query[0]
right := query[1]
deltaArray[left] += 1
deltaArray[right+1] -= 1
}
operationCounts := make([]int, len(deltaArray))
currentOperations := 0
for i, delta := range deltaArray {
currentOperations += delta
operationCounts[i] = currentOperations
}
for i := 0; i < len(nums); i++ {
if operationCounts[i] < nums[i] {
return false
}
}
return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
inputs []int
queries [][]int
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 0, 1}, [][]int{{0, 2}}, true},
{"TestCase2", []int{4, 3, 2, 1}, [][]int{{1, 3}, {0, 2}}, false},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.queries)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.queries)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading