Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/cxjwin/go-algocasts
Browse files Browse the repository at this point in the history
  • Loading branch information
smart committed May 25, 2019
2 parents 9bd1945 + 129dac9 commit 41b320b
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
90 changes: 90 additions & 0 deletions leetcode/leetcode493/reverse_pairs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package leetcode493

func reversePairsBruteForce(nums []int) int {
if nums == nil || len(nums) == 0 {
return 0
}

n := len(nums)
count := 0

for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if nums[i] > nums[j]*2 {
count++
}
}
}

return count
}

func merge(nums []int, low int, mid int, high int) {
m, n := mid-low+1, high-mid
a1 := make([]int, m)
for i := 0; i < m; i++ {
a1[i] = nums[low+i]
}
a2 := make([]int, n)
for i := 0; i < n; i++ {
a2[i] = nums[mid+1+i]
}

i, j, k := 0, 0, low
for i < m && j < n {
if a1[i] < a2[j] {
nums[k] = a1[i]
i++
} else {
nums[k] = a2[j]
j++
}
k++
}
for i < m {
nums[k] = a1[i]
i++
k++
}
for j < n {
nums[k] = a2[j]
j++
k++
}
}

func count(nums []int, low int, mid int, high int) int {
cnt := 0
i, j := low, mid+1
for i <= mid && j <= high {
if nums[i] <= nums[j]*2 {
i++
} else {
cnt += (mid - i + 1)
j++
}
}
return cnt
}

func sortAndCount(nums []int, low int, high int) int {
if low >= high {
return 0
}

cnt := 0
mid := low + (high-low)/2
cnt += sortAndCount(nums, low, mid)
cnt += sortAndCount(nums, mid+1, high)
cnt += count(nums, low, mid, high)
merge(nums, low, mid, high)

return cnt
}

func reversePairs(nums []int) int {
if nums == nil || len(nums) == 0 {
return 0
}
return sortAndCount(nums, 0, len(nums)-1)
}
14 changes: 14 additions & 0 deletions leetcode/leetcode493/reverse_pairs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package leetcode493

import "testing"

func TestReversePairs(t *testing.T) {
nums := []int{1, 3, 2, 3, 1}
if reversePairsBruteForce(nums) != 2 {
t.Error("a : 1,3,2,3,1 -> 2")
}

if reversePairs(nums) != 2 {
t.Error("b : 1,3,2,3,1 -> 2")
}
}

0 comments on commit 41b320b

Please sign in to comment.