-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution and test-cases for problem 900
- Loading branch information
Showing
3 changed files
with
66 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,38 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
type RLEIterator struct { | ||
source []int | ||
index int | ||
} | ||
|
||
func Constructor900(encoding []int) RLEIterator { | ||
rle := RLEIterator{source: make([]int, 0), index: 0} | ||
for i := 0; i < len(encoding)-1; i += 2 { | ||
if encoding[i] == 0 { | ||
continue | ||
} | ||
rle.source = append(rle.source, encoding[i], encoding[i+1]) | ||
} | ||
return rle | ||
} | ||
|
||
func (this *RLEIterator) Next(n int) int { | ||
for ; this.index < len(this.source)-1 && this.source[this.index] < n; this.index += 2 { | ||
n -= this.source[this.index] | ||
this.source[this.index] = 0 | ||
} | ||
if this.index == len(this.source) { | ||
return -1 | ||
} | ||
this.source[this.index] -= n | ||
return this.source[this.index+1] | ||
} | ||
|
||
func Solution(encodings []int, input []int) []int { | ||
c := Constructor900(encodings) | ||
ans := make([]int, len(input)) | ||
for i, n := range input { | ||
ans[i] = c.Next(n) | ||
} | ||
return ans | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters