Skip to content

Commit

Permalink
Merge pull request #12 from arunsathiya/solve/154-find-minimum-in-rot…
Browse files Browse the repository at this point in the history
…ated-sorted-array-2

new(problem): 154. Find Minimum In Rotated Sorted Array 2
  • Loading branch information
arunsathiya authored Dec 24, 2023
2 parents 4c404a7 + 6d31ce8 commit 572ff2a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 10 deletions.
28 changes: 22 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,41 @@ on:
- "**.go"

jobs:
test:
diff:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
ref: ${{ github.head_ref }}
- id: set-matrix
run: |
echo "::set-output name=matrix::$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep '.go$' | xargs -I {} dirname {} | uniq | jq -R -s -c 'split("\n")[:-1]')"
changed_dirs=$(git diff --name-only origin/${{ github.base_ref }} | grep '.go$' | xargs -I {} dirname {} | uniq | awk '{print "\"" $0 "\""}' | jq -R -s -c 'split("\n")[:-1]')
echo "Changed directories: $changed_dirs"
echo "::set-output name=matrix::$changed_dirs"
build:
needs: test
test:
needs: diff
runs-on: ubuntu-latest
if: needs.diff.outputs.matrix != '[]'
strategy:
fail-fast: false
matrix:
directory: ${{fromJson(needs.test.outputs.matrix)}}
dir: ${{fromJson(needs.diff.outputs.matrix)}}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: "^1.12.1"
- run: go test ./${{ matrix.directory }}
- name: Run tests in changed directories
run: |
for dir in "${{ matrix.dir }}"; do
if [ -d "$dir" ]; then
echo "Running tests in $dir"
(cd "$dir" && go test ./...)
else
echo "No Go files in $dir"
fi
done
4 changes: 2 additions & 2 deletions 153-find-minimum-in-rotated-sorted-array/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

func findMin(nums []int) int {
func findMin1(nums []int) int {
left, right := 0, len(nums)-1
for left < right {
if nums[left] < nums[right] {
return nums[left]
}
mid := (left + right) / 2
mid := left + (right-left)/2
if nums[mid] >= nums[left] {
left = mid + 1
} else {
Expand Down
4 changes: 2 additions & 2 deletions 153-find-minimum-in-rotated-sorted-array/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import "testing"

func TestFindMin(t *testing.T) {
func TestFindMin1(t *testing.T) {
tests := []struct {
name string
input []int
Expand Down Expand Up @@ -31,7 +31,7 @@ func TestFindMin(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := findMin(test.input)
output := findMin1(test.input)
if output != test.expected {
t.Errorf("Expected %d, got %d", test.expected, output)
}
Expand Down
19 changes: 19 additions & 0 deletions 154-find-minimum-in-rotated-sorted-array-ii/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

func findMin2(nums []int) int {
left, right := 0, len(nums)-1
for left < right {
if nums[left] < nums[right] {
return nums[left]
}
mid := left + (right-left)/2
if nums[mid] > nums[right] {
left = mid + 1
} else if nums[mid] < nums[right] {
right = mid
} else {
right--
}
}
return nums[left]
}
37 changes: 37 additions & 0 deletions 154-find-minimum-in-rotated-sorted-array-ii/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"testing"
)

func TestFindMin2(t *testing.T) {
tests := []struct {
name string
input []int
expected int
}{
{
name: "test case from leetcode",
input: []int{1, 3, 5},
expected: 1,
},
{
name: "second test case from leetcode",
input: []int{2, 2, 2, 0, 1},
expected: 0,
},
{
name: "third test case from leetcode",
input: []int{10, 1, 10, 10, 10},
expected: 1,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := findMin2(test.input)
if output != test.expected {
t.Errorf("Expected %d, got %d", test.expected, output)
}
})
}
}

0 comments on commit 572ff2a

Please sign in to comment.