Skip to content

Commit

Permalink
2023/9/15
Browse files Browse the repository at this point in the history
  • Loading branch information
baowj-678 committed Sep 15, 2023
1 parent 54f3d1d commit 10d1a5a
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 36 deletions.
36 changes: 36 additions & 0 deletions leetcode/1222.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @author: baowj
* @time: 2023/9/14 10:03
*/
package main

func queensAttacktheKing(queens [][]int, king []int) [][]int {
const Queen, King, Attack = 1, 2, 3
board := [8][8]int{}
for _, q := range queens {
board[q[0]][q[1]] = Queen
}
board[king[0]][king[1]] = King

directs := [][2]int{{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}}
for _, direct := range directs {
i, j := king[0]+direct[0], king[1]+direct[1]
for 0 <= i && i < 8 && 0 <= j && j < 8 {
if board[i][j] == Queen {
board[i][j] = Attack
break
}
i += direct[0]
j += direct[1]
}
}
var res [][]int
for i := 0; i < 8; i++ {
for j := 0; j < 8; j++ {
if board[i][j] == Attack {
res = append(res, []int{i, j})
}
}
}
return res
}
5 changes: 5 additions & 0 deletions leetcode/1263.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* @author: baowj
* @time: 2023/5/17 11:17
*/
package main
14 changes: 7 additions & 7 deletions leetcode/1385.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
package main

func findTheDistanceValue(arr1 []int, arr2 []int, d int) int {
abs1385 := func(x int) int {
if x < 0 {
x = -x
}
return x
}

res := 0
for _, x := range arr1 {
for _, y := range arr2 {
Expand All @@ -17,10 +24,3 @@ func findTheDistanceValue(arr1 []int, arr2 []int, d int) int {
}
return res
}

func abs1385(x int) int {
if x < 0 {
x = -x
}
return x
}
15 changes: 7 additions & 8 deletions leetcode/1417.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
)

func reformat(s string) string {
abs1417 := func(x int, y int) int {
if x > y {
return x - y
} else {
return y - x
}
}
alphaP, numP := 0, 0
for i := 0; i < len(s); i++ {
if unicode.IsDigit(rune(s[i])) {
Expand Down Expand Up @@ -54,11 +61,3 @@ func reformat(s string) string {
}
return string(res)
}

func abs1417(x int, y int) int {
if x > y {
return x - y
} else {
return y - x
}
}
14 changes: 7 additions & 7 deletions leetcode/1779.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ package main
import "math"

func nearestValidPoint(x int, y int, points [][]int) int {
abs1779 := func(x int) int {
if x < 0 {
return -x
}
return x
}

dis := math.MaxInt
index := 0
for i, point := range points {
Expand All @@ -21,10 +28,3 @@ func nearestValidPoint(x int, y int, points [][]int) int {
}
return index
}

func abs1779[T int | float64](x T) T {
if x < 0 {
return -x
}
return x
}
13 changes: 6 additions & 7 deletions leetcode/1802.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
package main

func maxValue(n int, index int, maxSum int) int {
AbsInt1802 := func(x int) int {
if x < 0 {
return -x
}
return x
}
res := 1
maxSum -= n
if maxSum == 0 {
Expand Down Expand Up @@ -64,10 +70,3 @@ func maxValue(n int, index int, maxSum int) int {
res += maxSum / n
return res
}

func AbsInt1802(x int) int {
if x < 0 {
return -x
}
return x
}
51 changes: 51 additions & 0 deletions leetcode/2596.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @author: baowj
* @time: 2023/9/14 10:15
*/
package main

import "sort"

func checkValidGrid(grid [][]int) bool {
n := len(grid)
positions := make([][3]int, 0, n*n)
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
positions = append(positions, [3]int{grid[i][j], i, j})
}
}
sort.Slice(positions, func(i, j int) bool {
return positions[i][0] < positions[j][0]
})

absInt := func(x int) int {
if x < 0 {
x = -x
}
return x
}
check := func(a, b []int) bool {
x := absInt(a[0] - b[0])
y := absInt(a[1] - b[1])
if (x == 2 && y == 1) || (x == 1 && y == 2) {
return true
}
return false
}
tmp := positions[0][1:]
if tmp[0] != 0 || tmp[1] != 0 {
return false
}
for _, p := range positions[1:] {
if !check(tmp, p[1:]) {
return false
}
tmp[0], tmp[1] = p[1], p[2]
}
return true
}

func main() {
grid := [][]int{{0, 11, 16, 5, 20}, {17, 4, 19, 10, 15}, {12, 1, 8, 21, 6}, {3, 18, 23, 14, 9}, {24, 13, 2, 7, 22}}
checkValidGrid(grid)
}
13 changes: 6 additions & 7 deletions leetcode/658.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ package main
import "fmt"

func findClosestElements(arr []int, k int, x int) []int {
AbsInt := func(x int) int {
if x < 0 {
return -x
}
return x
}
start, end := 0, k-1
for i := k; i < len(arr); i++ {
if AbsInt(arr[i]-x) < AbsInt(arr[end]-x) {
Expand All @@ -22,13 +28,6 @@ func findClosestElements(arr []int, k int, x int) []int {
return arr[start : end+1]
}

func AbsInt(x int) int {
if x < 0 {
return -x
}
return x
}

func main() {
arr := []int{1, 2, 3, 4, 5}
fmt.Println(findClosestElements(arr, 4, -1))
Expand Down
35 changes: 35 additions & 0 deletions leetcode/LCP 50.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @author: baowj
* @time: 2023/9/15 9:50
*/
package main

import "math"

func giveGem(gem []int, operations [][]int) int {
minFn := func(x, y int) int {
if x > y {
x = y
}
return x
}

maxFn := func(x, y int) int {
if x < y {
x = y
}
return x
}

for _, op := range operations {
gem[op[1]] += gem[op[0]] / 2
gem[op[0]] -= gem[op[0]] / 2
}

min, max := math.MaxInt, 0
for _, x := range gem {
min = minFn(min, x)
max = maxFn(max, x)
}
return max - min
}

0 comments on commit 10d1a5a

Please sign in to comment.