diff --git a/content/posts/leetcode.md b/content/posts/leetcode.md index 3dd0bcd..1a05c99 100644 --- a/content/posts/leetcode.md +++ b/content/posts/leetcode.md @@ -9533,3 +9533,380 @@ func luckyNumbers (matrix [][]int) []int { return result } ``` + +## day145 2024-07-20 +### 1605. Find Valid Matrix Given Row and Column Sums +You are given two arrays rowSum and colSum of non-negative integers where rowSum[i] is the sum of the elements in the ith row and colSum[j] is the sum of the elements of the jth column of a 2D matrix. In other words, you do not know the elements of the matrix, but you do know the sums of each row and column. + +Find any matrix of non-negative integers of size rowSum.length x colSum.length that satisfies the rowSum and colSum requirements. + +Return a 2D array representing any matrix that fulfills the requirements. It's guaranteed that at least one matrix that fulfills the requirements exists. + +![0720Jr3skSqkPy0Y](https://testingcf.jsdelivr.net/gh/game-loader/picbase@master/uPic/0720Jr3skSqkPy0Y.png) + +### 题解 +本题使用贪心算法, 在填充每一行的数字时, 每次都取当前位置行和和列和的较小值. 再分别从行和和列和中减去当前取的值. 继续填充下一个位置的数字. 这样可以保证填充的每个数字都不会使当前行和列超过和的限制(实际按这个思路只要取小于等于二者较小值即可), 直接取二者中的较小值可以保证二者中的一个变为0. 避免了后续继续填充时可能会因行或列的和的限制而无法取得合适的数的问题(0对和没有影响). 而当和为0时只需将该位置填充为0即可. + +### 代码 +```go +func restoreMatrix(rowSum []int, colSum []int) [][]int { + result := [][]int{} + collen := len(colSum) + rowlen := len(rowSum) + for i:=0;ib存在一种后继关系. 可以将above和below的关系视为有向图中的一条边, 从above指向below. 但本题中可能会出现环, 出现环即产生了冲突, 无法进行拓扑排序. 此时无解, 因此先判断由两个条件数组构建的有向图是否包含环, 包含环则无解, 不包含环继续进行拓扑排序, 得到拓扑排序后的数组, 按照数组中数对应的下标将其放入对应的行或者列. 注意对行进行拓扑排序得到的结果和对列进行拓扑排序得到的结果二者并不冲突. 所以分别排序并按顺序填入数组就能得到正确答案. + +### 代码 +```go +func buildMatrix(k int, rowConditions [][]int, colConditions [][]int) [][]int { + // 定义检查环和拓扑排序的辅助函数 + hasCycle := func(graph [][]int, k int) bool { + inDegree := make([]int, k+1) + for _, edge := range graph { + for _,value := range edge{ + inDegree[value]++ + } + } + + queue := []int{} + for i := 1; i <= k; i++ { + if inDegree[i] == 0 { + queue = append(queue, i) + } + } + + visited := 0 + for len(queue) > 0 { + node := queue[0] + queue = queue[1:] + visited++ + for _, neighbor := range graph[node] { + inDegree[neighbor]-- + if inDegree[neighbor] == 0 { + queue = append(queue, neighbor) + } + } + } + return visited != k + } + + topologicalSort := func(graph [][]int, k int) []int { + inDegree := make([]int, k+1) + for _, edge := range graph { + for _,value := range edge{ + inDegree[value]++ + } + } + + queue := []int{} + for i := 1; i <= k; i++ { + if inDegree[i] == 0 { + queue = append(queue, i) + } + } + + order := []int{} + for len(queue) > 0 { + node := queue[0] + queue = queue[1:] + order = append(order, node) + for _, neighbor := range graph[node] { + inDegree[neighbor]-- + if inDegree[neighbor] == 0 { + queue = append(queue, neighbor) + } + } + } + if len(order) != k { + return nil + } + return order + } + + // 构建行和列的有向图 + rowGraph := make([][]int, k+1) + colGraph := make([][]int, k+1) + for i := 0; i <= k; i++ { + rowGraph[i] = []int{} + colGraph[i] = []int{} + } + for _, cond := range rowConditions { + rowGraph[cond[0]] = append(rowGraph[cond[0]], cond[1]) + } + for _, cond := range colConditions { + colGraph[cond[0]] = append(colGraph[cond[0]], cond[1]) + } + + // 检查是否存在环 + if hasCycle(rowGraph, k) || hasCycle(colGraph, k) { + return [][]int{} + } + + // 获取拓扑排序 + rowOrder := topologicalSort(rowGraph, k) + colOrder := topologicalSort(colGraph, k) + if rowOrder == nil || colOrder == nil { + return [][]int{} + } + + // 构建结果矩阵 + matrix := make([][]int, k) + for i := range matrix { + matrix[i] = make([]int, k) + } + + rowPos := make(map[int]int) + colPos := make(map[int]int) + for i, num := range rowOrder { + rowPos[num] = i + } + for i, num := range colOrder { + colPos[num] = i + } + + for num := 1; num <= k; num++ { + matrix[rowPos[num]][colPos[num]] = num + } + + return matrix +} +``` + +## day147 2024-07-22 +### 2418. Sort the People +You are given an array of strings names, and an array heights that consists of distinct positive integers. Both arrays are of length n. + +For each index i, names[i] and heights[i] denote the name and height of the ith person. + +Return names sorted in descending order by the people's heights. + +![0722oZxFqSgIHFvB](https://testingcf.jsdelivr.net/gh/game-loader/picbase@master/uPic/0722oZxFqSgIHFvB.png) + +### 题解 +本题先将人名和身高绑定成一个结构体, 再根据身高排序, 思路是比较简单, 因此手动实现快排用于身高排序. + +### 代码 +```go +func sortPeople(names []string, heights []int) []string { + type people struct{ + name string + height int + } + peoples := []people{} + for i,name := range names{ + peoples = append(peoples, people{name, heights[i]}) + } + var quicksort func(sortpeople []people) + quicksort = func(sortpeople []people){ + if len(sortpeople) <= 1{ + return + } + flag := sortpeople[0] + i := 1 + j := len(sortpeople)-1 + for ii{ + j-- + } + sortpeople[i],sortpeople[j] = sortpeople[j],sortpeople[i] + } + i++ + } + if sortpeople[j].height > sortpeople[0].height{ + sortpeople[0],sortpeople[j] = sortpeople[j],sortpeople[0] + } + quicksort(sortpeople[0:j]) + quicksort(sortpeople[j:]) + return + } + quicksort(peoples) + result := []string{} + for _,value := range peoples{ + result = append(result, value.name) + } + return result +} +``` + +## day148 2024-07-23 +### 1636. Sort Array by Increasing Frequency +Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order. + +Return the sorted array. + +![0723bKEQAXFqTMve](https://testingcf.jsdelivr.net/gh/game-loader/picbase@master/uPic/0723bKEQAXFqTMve.png) + +### 题解 +本题可使用计数排序, 先使用map统计各个数字出现的次数, 再直接对原始数组进行排序, 首先根据出现的次数排序, 如果两个数字出现次数相同则根据数字大小排序, 将数字更大的放在前面, 这里只需根据上面的条件写好go内置的sort函数即可完成排序. 最后返回结果. + +### 代码 +```go +func frequencySort(nums []int) []int { + mapp:= make(map[int]int) + n:= len(nums) + if n==1{ + return nums + } + for i:=0;inums[j] + } + return mapp[nums[i]] 0 { + sortedArray = append(sortedArray, heap.Pop(h).(int)) + } + return sortedArray +} +```