-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #949 from 0xff-dev/220
Add solution and test-cases for problem 220
- Loading branch information
Showing
3 changed files
with
62 additions
and
25 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,35 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
import "sort" | ||
|
||
func Solution(nums []int, indexDiff int, valueDiff int) bool { | ||
indies := make([]int, len(nums)) | ||
for i := range nums { | ||
indies[i] = i | ||
} | ||
sort.Slice(indies, func(i, j int) bool { | ||
a, b := nums[indies[i]], nums[indies[j]] | ||
if a == b { | ||
return indies[i] < indies[j] | ||
} | ||
return a < b | ||
}) | ||
|
||
start, end := 0, 1 | ||
for ; end < len(nums); end++ { | ||
v := nums[indies[end]] - nums[indies[start]] | ||
if v > valueDiff { | ||
start++ | ||
end = start | ||
continue | ||
} | ||
i := indies[end] - indies[start] | ||
if i < 0 { | ||
i = -i | ||
} | ||
if i <= indexDiff { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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