From f918be35eb4e6647c89b41490f8037db1be3abc3 Mon Sep 17 00:00:00 2001 From: YDZ Date: Fri, 7 Aug 2020 17:33:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=B0=9D=E8=AF=95=E4=BD=86?= =?UTF-8?q?=E6=9C=AA=E9=80=9A=E8=BF=87=E7=9A=84=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../85. Maximal Rectangle.go | 5 + .../85. Maximal Rectangle_test.go | 42 +++++++ .../132. Palindrome Partitioning II.go | 42 +++++++ .../132. Palindrome Partitioning II_test.go | 42 +++++++ .../316. Remove Duplicate Letters.go | 5 + .../316. Remove Duplicate Letters_test.go | 46 +++++++ .../352. Data Stream as Disjoint Intervals.go | 101 ++++++++++++++++ ... Data Stream as Disjoint Intervals_test.go | 21 ++++ .... Max Sum of Rectangle No Larger Than K.go | 112 ++++++++++++++++++ ... Sum of Rectangle No Larger Than K_test.go | 58 +++++++++ .../377. Combination Sum IV.go | 24 ++++ .../377. Combination Sum IV_test.go | 48 ++++++++ .../975. Odd Even Jump.go | 74 ++++++++++++ .../975. Odd Even Jump_test.go | 52 ++++++++ .../1044. Longest Duplicate Substring.go | 86 ++++++++++++++ .../1044. Longest Duplicate Substring_test.go | 46 +++++++ ...ith Sum Less than or Equal to Threshold.go | 48 ++++++++ ...um Less than or Equal to Threshold_test.go | 48 ++++++++ 18 files changed, 900 insertions(+) create mode 100644 leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle.go create mode 100644 leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle_test.go create mode 100644 leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II.go create mode 100644 leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II_test.go create mode 100644 leetcode/9990316.Remove-Duplicate-Letters/316. Remove Duplicate Letters.go create mode 100644 leetcode/9990316.Remove-Duplicate-Letters/316. Remove Duplicate Letters_test.go create mode 100644 leetcode/9990352.Data-Stream-as-Disjoint-Intervals/352. Data Stream as Disjoint Intervals.go create mode 100644 leetcode/9990352.Data-Stream-as-Disjoint-Intervals/352. Data Stream as Disjoint Intervals_test.go create mode 100644 leetcode/9990363.Max-Sum-of-Rectangle-No-Larger-Than-K/363. Max Sum of Rectangle No Larger Than K.go create mode 100644 leetcode/9990363.Max-Sum-of-Rectangle-No-Larger-Than-K/363. Max Sum of Rectangle No Larger Than K_test.go create mode 100644 leetcode/9990377.Combination-Sum-IV/377. Combination Sum IV.go create mode 100644 leetcode/9990377.Combination-Sum-IV/377. Combination Sum IV_test.go create mode 100644 leetcode/9990975.Odd-Even-Jump/975. Odd Even Jump.go create mode 100644 leetcode/9990975.Odd-Even-Jump/975. Odd Even Jump_test.go create mode 100644 leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring.go create mode 100644 leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring_test.go create mode 100644 leetcode/9991292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold.go create mode 100644 leetcode/9991292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_test.go diff --git a/leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle.go b/leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle.go new file mode 100644 index 000000000..1a98bd359 --- /dev/null +++ b/leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle.go @@ -0,0 +1,5 @@ +package leetcode + +func maximalRectangle(matrix [][]byte) int { + return 0 +} diff --git a/leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle_test.go b/leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle_test.go new file mode 100644 index 000000000..12853fbfc --- /dev/null +++ b/leetcode/9990085.Maximal-Rectangle/85. Maximal Rectangle_test.go @@ -0,0 +1,42 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question85 struct { + para85 + ans85 +} + +// para 是参数 +// one 代表第一个参数 +type para85 struct { + one [][]byte +} + +// ans 是答案 +// one 代表第一个答案 +type ans85 struct { + one int +} + +func Test_Problem85(t *testing.T) { + + qs := []question85{ + + question85{ + para85{[][]byte{[]byte{'1', '0', '1', '0', '0'}, []byte{'1', '0', '1', '1', '1'}, []byte{'1', '1', '1', '1', '1'}, []byte{'1', '0', '0', '1', '0'}}}, + ans85{6}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 85------------------------\n") + + for _, q := range qs { + _, p := q.ans85, q.para85 + fmt.Printf("【input】:%v 【output】:%v\n", p, maximalRectangle(p.one)) + } + fmt.Printf("\n\n\n") +} diff --git a/leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II.go b/leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II.go new file mode 100644 index 000000000..6ac3bc894 --- /dev/null +++ b/leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II.go @@ -0,0 +1,42 @@ +package leetcode + +func minCut(s string) int { + if s == "" { + return 0 + } + result := len(s) + current := make([]string, 0, len(s)) + dfs132(s, 0, current, &result) + return result +} + +func dfs132(s string, idx int, cur []string, result *int) { + start, end := idx, len(s) + if start == end { + *result = min(*result, len(cur)-1) + return + } + for i := start; i < end; i++ { + if isPal(s, start, i) { + dfs132(s, i+1, append(cur, s[start:i+1]), result) + } + } +} + +func min(a int, b int) int { + if a > b { + return b + } + return a +} + +func isPal(str string, s, e int) bool { + for s < e { + if str[s] != str[e] { + return false + } + s++ + e-- + } + return true +} diff --git a/leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II_test.go b/leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II_test.go new file mode 100644 index 000000000..1588e30aa --- /dev/null +++ b/leetcode/9990132.Palindrome-Partitioning-II/132. Palindrome Partitioning II_test.go @@ -0,0 +1,42 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question132 struct { + para132 + ans132 +} + +// para 是参数 +// one 代表第一个参数 +type para132 struct { + s string +} + +// ans 是答案 +// one 代表第一个答案 +type ans132 struct { + one int +} + +func Test_Problem132(t *testing.T) { + + qs := []question132{ + + question132{ + para132{"aab"}, + ans132{1}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 132------------------------\n") + + for _, q := range qs { + _, p := q.ans132, q.para132 + fmt.Printf("【input】:%v 【output】:%v\n", p, minCut(p.s)) + } + fmt.Printf("\n\n\n") +} diff --git a/leetcode/9990316.Remove-Duplicate-Letters/316. Remove Duplicate Letters.go b/leetcode/9990316.Remove-Duplicate-Letters/316. Remove Duplicate Letters.go new file mode 100644 index 000000000..df5f7a6bc --- /dev/null +++ b/leetcode/9990316.Remove-Duplicate-Letters/316. Remove Duplicate Letters.go @@ -0,0 +1,5 @@ +package leetcode + +func removeDuplicateLetters(s string) string { + return "" +} diff --git a/leetcode/9990316.Remove-Duplicate-Letters/316. Remove Duplicate Letters_test.go b/leetcode/9990316.Remove-Duplicate-Letters/316. Remove Duplicate Letters_test.go new file mode 100644 index 000000000..9768dbb35 --- /dev/null +++ b/leetcode/9990316.Remove-Duplicate-Letters/316. Remove Duplicate Letters_test.go @@ -0,0 +1,46 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question316 struct { + para316 + ans316 +} + +// para 是参数 +// one 代表第一个参数 +type para316 struct { + one string +} + +// ans 是答案 +// one 代表第一个答案 +type ans316 struct { + one string +} + +func Test_Problem316(t *testing.T) { + + qs := []question316{ + + question316{ + para316{"bcabc"}, + ans316{"abc"}, + }, + question316{ + para316{"cbacdcbc"}, + ans316{"acdb"}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 316------------------------\n") + + for _, q := range qs { + _, p := q.ans316, q.para316 + fmt.Printf("【input】:%v 【output】:%v\n", p, removeDuplicateLetters(p.one)) + } + fmt.Printf("\n\n\n") +} diff --git a/leetcode/9990352.Data-Stream-as-Disjoint-Intervals/352. Data Stream as Disjoint Intervals.go b/leetcode/9990352.Data-Stream-as-Disjoint-Intervals/352. Data Stream as Disjoint Intervals.go new file mode 100644 index 000000000..58b045b54 --- /dev/null +++ b/leetcode/9990352.Data-Stream-as-Disjoint-Intervals/352. Data Stream as Disjoint Intervals.go @@ -0,0 +1,101 @@ +package leetcode + +import ( + "github.com/halfrost/LeetCode-Go/structures" +) + +// Interval define +type Interval = structures.Interval + +// SummaryRanges define +type SummaryRanges struct { + intervals []Interval +} + +// Constructor352 define +func Constructor352() SummaryRanges { + return SummaryRanges{} +} + +// AddNum define +func (sr *SummaryRanges) AddNum(val int) { + if sr.intervals == nil { + sr.intervals = []Interval{ + Interval{ + Start: val, + End: val, + }, + } + return + } + + low, high := 0, len(sr.intervals)-1 + for low <= high { + mid := low + (high-low)>>1 + if sr.intervals[mid].Start <= val && val <= sr.intervals[mid].End { + return + } else if val < sr.intervals[mid].Start { + high = mid - 1 + } else { + low = mid + 1 + } + } + + if low == 0 { + if sr.intervals[0].Start-1 == val { + sr.intervals[0].Start-- + return + } + ni := Interval{Start: val, End: val} + sr.intervals = append(sr.intervals, ni) + copy(sr.intervals[1:], sr.intervals) + sr.intervals[0] = ni + return + } + + if low == len(sr.intervals) { + if sr.intervals[low-1].End+1 == val { + sr.intervals[low-1].End++ + return + } + sr.intervals = append(sr.intervals, Interval{Start: val, End: val}) + return + } + + if sr.intervals[low-1].End+1 < val && val < sr.intervals[low].Start-1 { + sr.intervals = append(sr.intervals, Interval{}) + copy(sr.intervals[low+1:], sr.intervals[low:]) + sr.intervals[low] = Interval{Start: val, End: val} + return + } + + if sr.intervals[low-1].End == val-1 && val+1 == sr.intervals[low].Start { + sr.intervals[low-1].End = sr.intervals[low].End + n := len(sr.intervals) + copy(sr.intervals[low:], sr.intervals[low+1:]) + sr.intervals = sr.intervals[:n-1] + return + } + + if sr.intervals[low-1].End == val-1 { + sr.intervals[low-1].End++ + } else { + sr.intervals[low].Start-- + } +} + +// GetIntervals define +func (sr *SummaryRanges) GetIntervals() [][]int { + intervals := [][]int{} + for _, interval := range sr.intervals { + intervals = append(intervals, []int{interval.Start, interval.End}) + } + return intervals +} + +/** + * Your SummaryRanges object will be instantiated and called as such: + * obj := Constructor(); + * obj.AddNum(val); + * param_2 := obj.GetIntervals(); + */ diff --git a/leetcode/9990352.Data-Stream-as-Disjoint-Intervals/352. Data Stream as Disjoint Intervals_test.go b/leetcode/9990352.Data-Stream-as-Disjoint-Intervals/352. Data Stream as Disjoint Intervals_test.go new file mode 100644 index 000000000..d92023c30 --- /dev/null +++ b/leetcode/9990352.Data-Stream-as-Disjoint-Intervals/352. Data Stream as Disjoint Intervals_test.go @@ -0,0 +1,21 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +func Test_Problem352(t *testing.T) { + obj := Constructor352() + fmt.Printf("obj = %v\n", obj) + obj.AddNum(1) + fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1,1] + obj.AddNum(3) + fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1,1] [3,3] + obj.AddNum(7) + fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1, 1], [3, 3], [7, 7] + obj.AddNum(2) + fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1, 3], [7, 7] + obj.AddNum(6) + fmt.Printf("Intervals = %v\n", obj.GetIntervals()) // [1, 3], [6, 7] +} diff --git a/leetcode/9990363.Max-Sum-of-Rectangle-No-Larger-Than-K/363. Max Sum of Rectangle No Larger Than K.go b/leetcode/9990363.Max-Sum-of-Rectangle-No-Larger-Than-K/363. Max Sum of Rectangle No Larger Than K.go new file mode 100644 index 000000000..7f1a6f3c3 --- /dev/null +++ b/leetcode/9990363.Max-Sum-of-Rectangle-No-Larger-Than-K/363. Max Sum of Rectangle No Larger Than K.go @@ -0,0 +1,112 @@ +package leetcode + +import ( + "math" +) + +// 解法一 二分搜索 +func maxSumSubmatrix(matrix [][]int, k int) int { + // 转换为前缀和 + for i := 0; i < len(matrix); i++ { + for j := 1; j < len(matrix[0]); j++ { + matrix[i][j] += matrix[i][j-1] + } + } + sum, absMax, absMaxFound := make([]int, len(matrix)), 0, false + for y1 := 0; y1 < len(matrix[0]); y1++ { + for y2 := y1; y2 < len(matrix[0]); y2++ { + for x := 0; x < len(matrix); x++ { + if y1 == 0 { + sum[x] = matrix[x][y2] + } else { + sum[x] = matrix[x][y2] - matrix[x][y1-1] + } + } + curMax := kadaneK(sum, k) + if !absMaxFound || curMax > absMax { + absMax = curMax + absMaxFound = true + } + } + } + return absMax +} + +func kadaneK(a []int, k int) int { + sum, sums, maxSoFar := 0, []int{}, math.MinInt32 + for _, v := range a { + // 第一次循环会先插入 0,因为 sum 有可能等于 k + sums = insertSort(sums, sum) + sum += v + pos := binarySearchOfKadane(sums, sum-k) + if pos < len(sums) && sum-sums[pos] > maxSoFar { + maxSoFar = sum - sums[pos] + } + } + return maxSoFar +} + +func binarySearchOfKadane(a []int, v int) int { + low, high := 0, len(a) + for low < high { + mid := low + (high-low)>>1 + if a[mid] < v { + low = mid + 1 + } else { + high = mid + } + } + return low +} + +func insertSort(a []int, v int) []int { + // 类似插入排序,将元素按照从小到大的顺序插入数组 + p := binarySearchOfKadane(a, v) + a = append(a, 0) + // 把 p 后面的元素全部往后移,把 p 位置空出来放 v + copy(a[p+1:], a[p:len(a)-1]) + a[p] = v + return a +} + +// 解法二 暴力解法,超时 +func maxSumSubmatrix1(matrix [][]int, k int) int { + minNum := math.MaxInt64 + for row := range matrix { + for col := 1; col < len(matrix[row]); col++ { + if matrix[row][col] < minNum { + minNum = matrix[row][col] + } + } + } + for row := range matrix { + for col := 1; col < len(matrix[row]); col++ { + matrix[row][col] += matrix[row][col-1] + } + } + for i := k; ; i-- { + if findSumSubmatrix(matrix, i) > 0 { + return i + } + } +} + +func findSumSubmatrix(matrix [][]int, target int) int { + m, n, res := len(matrix), len(matrix[0]), 0 + for i := 0; i < n; i++ { + for j := i; j < n; j++ { + counterMap, sum := make(map[int]int, m), 0 + counterMap[0] = 1 // 题目保证一定有解,所以这里初始化是 1 + for row := 0; row < m; row++ { + if i > 0 { + sum += matrix[row][j] - matrix[row][i-1] + } else { + sum += matrix[row][j] + } + res += counterMap[sum-target] + counterMap[sum]++ + } + } + } + return res +} diff --git a/leetcode/9990363.Max-Sum-of-Rectangle-No-Larger-Than-K/363. Max Sum of Rectangle No Larger Than K_test.go b/leetcode/9990363.Max-Sum-of-Rectangle-No-Larger-Than-K/363. Max Sum of Rectangle No Larger Than K_test.go new file mode 100644 index 000000000..09cfe8755 --- /dev/null +++ b/leetcode/9990363.Max-Sum-of-Rectangle-No-Larger-Than-K/363. Max Sum of Rectangle No Larger Than K_test.go @@ -0,0 +1,58 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question363 struct { + para363 + ans363 +} + +// para 是参数 +// one 代表第一个参数 +type para363 struct { + one [][]int + k int +} + +// ans 是答案 +// one 代表第一个答案 +type ans363 struct { + one int +} + +func Test_Problem363(t *testing.T) { + + qs := []question363{ + + question363{ + para363{[][]int{[]int{1, 0, 1}, []int{0, -2, 3}}, 2}, + ans363{2}, + }, + + question363{ + para363{[][]int{[]int{2, 2, -1}}, 0}, + ans363{-1}, + }, + + question363{ + para363{[][]int{[]int{5, -4, -3, 4}, []int{-3, -4, 4, 5}, []int{5, 1, 5, -4}}, 8}, + ans363{8}, + }, + + question363{ + para363{[][]int{[]int{5, -4, -3, 4}, []int{-3, -4, 4, 5}, []int{5, 1, 5, -4}}, 3}, + ans363{2}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 363------------------------\n") + + for _, q := range qs { + _, p := q.ans363, q.para363 + fmt.Printf("【input】:%v 【output】:%v\n", p, maxSumSubmatrix(p.one, p.k)) + } + fmt.Printf("\n\n\n") +} diff --git a/leetcode/9990377.Combination-Sum-IV/377. Combination Sum IV.go b/leetcode/9990377.Combination-Sum-IV/377. Combination Sum IV.go new file mode 100644 index 000000000..6d99c99ba --- /dev/null +++ b/leetcode/9990377.Combination-Sum-IV/377. Combination Sum IV.go @@ -0,0 +1,24 @@ +package leetcode + +func combinationSum4(nums []int, target int) int { + if len(nums) == 0 { + return 0 + } + c, res := []int{}, 0 + findcombinationSum4(nums, target, 0, c, &res) + return res +} + +func findcombinationSum4(nums []int, target, index int, c []int, res *int) { + if target <= 0 { + if target == 0 { + *res++ + } + return + } + for i := 0; i < len(nums); i++ { + c = append(c, nums[i]) + findcombinationSum4(nums, target-nums[i], i, c, res) + c = c[:len(c)-1] + } +} diff --git a/leetcode/9990377.Combination-Sum-IV/377. Combination Sum IV_test.go b/leetcode/9990377.Combination-Sum-IV/377. Combination Sum IV_test.go new file mode 100644 index 000000000..56de0654b --- /dev/null +++ b/leetcode/9990377.Combination-Sum-IV/377. Combination Sum IV_test.go @@ -0,0 +1,48 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question377 struct { + para377 + ans377 +} + +// para 是参数 +// one 代表第一个参数 +type para377 struct { + n []int + k int +} + +// ans 是答案 +// one 代表第一个答案 +type ans377 struct { + one int +} + +func Test_Problem377(t *testing.T) { + + qs := []question377{ + + question377{ + para377{[]int{1, 2, 3}, 4}, + ans377{7}, + }, + + question377{ + para377{[]int{1, 2, 3}, 32}, + ans377{7}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 377------------------------\n") + + for _, q := range qs { + _, p := q.ans377, q.para377 + fmt.Printf("【input】:%v 【output】:%v\n", p, combinationSum4(p.n, p.k)) + } + fmt.Printf("\n\n\n") +} diff --git a/leetcode/9990975.Odd-Even-Jump/975. Odd Even Jump.go b/leetcode/9990975.Odd-Even-Jump/975. Odd Even Jump.go new file mode 100644 index 000000000..05c97087f --- /dev/null +++ b/leetcode/9990975.Odd-Even-Jump/975. Odd Even Jump.go @@ -0,0 +1,74 @@ +package leetcode + +import ( + "fmt" +) + +func oddEvenJumps(A []int) int { + oddJumpMap, evenJumpMap, count, current, res := map[int]int{}, map[int]int{}, 1, 0, 0 + for i := 0; i < len(A); i++ { + for j := i + 1; j < len(A); j++ { + if v, ok := oddJumpMap[i]; ok { + if A[i] <= A[j] && A[j] <= A[v] { + if A[j] == A[v] && j < oddJumpMap[i] { + oddJumpMap[i] = j + } else if A[j] < A[v] { + oddJumpMap[i] = j + } + } + } else { + if A[i] <= A[j] { + oddJumpMap[i] = j + } + } + } + } + for i := 0; i < len(A); i++ { + for j := i + 1; j < len(A); j++ { + if v, ok := evenJumpMap[i]; ok { + if A[i] >= A[j] && A[j] >= A[v] { + if A[j] == A[v] && j < evenJumpMap[i] { + evenJumpMap[i] = j + } else if A[j] > A[v] { + evenJumpMap[i] = j + } + } + } else { + if A[i] >= A[j] { + evenJumpMap[i] = j + } + } + } + } + fmt.Printf("oddJumpMap = %v evenJumpMap = %v\n", oddJumpMap, evenJumpMap) + for i := 0; i < len(A); i++ { + count, current = 1, i + for { + if count%2 == 1 { + if v, ok := oddJumpMap[current]; ok { + if v == len(A)-1 { + res++ + break + } + current = v + count++ + } else { + break + } + } + if count%2 == 0 { + if v, ok := evenJumpMap[current]; ok { + if v == len(A)-1 { + res++ + break + } + current = v + count++ + } else { + break + } + } + } + } + return res + 1 +} diff --git a/leetcode/9990975.Odd-Even-Jump/975. Odd Even Jump_test.go b/leetcode/9990975.Odd-Even-Jump/975. Odd Even Jump_test.go new file mode 100644 index 000000000..c85653146 --- /dev/null +++ b/leetcode/9990975.Odd-Even-Jump/975. Odd Even Jump_test.go @@ -0,0 +1,52 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question975 struct { + para975 + ans975 +} + +// para 是参数 +// one 代表第一个参数 +type para975 struct { + A []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans975 struct { + one int +} + +func Test_Problem975(t *testing.T) { + + qs := []question975{ + + question975{ + para975{[]int{10, 13, 12, 14, 15}}, + ans975{2}, + }, + + question975{ + para975{[]int{2, 3, 1, 1, 4}}, + ans975{3}, + }, + + question975{ + para975{[]int{5, 1, 3, 4, 2}}, + ans975{3}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 975------------------------\n") + + for _, q := range qs { + _, p := q.ans975, q.para975 + fmt.Printf("【input】:%v 【output】:%v\n", p, oddEvenJumps(p.A)) + } + fmt.Printf("\n\n\n") +} diff --git a/leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring.go b/leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring.go new file mode 100644 index 000000000..1dd0f8cb4 --- /dev/null +++ b/leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring.go @@ -0,0 +1,86 @@ +package leetcode + +// 解法一 二分搜索 + Rabin-Karp +func longestDupSubstring(S string) string { + // low, high := 0, len(S) + // for low < high { + // mid := (low + high + 1) >> 1 + // if hasRepeated("", B, mid) { + // low = mid + // } else { + // high = mid - 1 + // } + // } + return "这个解法还有问题" +} + +// func hashSlice(arr []int, length int) []int { +// // hash 数组里面记录 arr 比 length 长出去部分的 hash 值 +// hash, pl, h := make([]int, len(arr)-length+1), 1, 0 +// for i := 0; i < length-1; i++ { +// pl *= primeRK +// } +// for i, v := range arr { +// h = h*primeRK + v +// if i >= length-1 { +// hash[i-length+1] = h +// h -= pl * arr[i-length+1] +// } +// } +// return hash +// } + +// func hasSamePrefix(A, B []int, length int) bool { +// for i := 0; i < length; i++ { +// if A[i] != B[i] { +// return false +// } +// } +// return true +// } + +// func hasRepeated(A, B []int, length int) bool { +// hs := hashSlice(A, length) +// hashToOffset := make(map[int][]int, len(hs)) +// for i, h := range hs { +// hashToOffset[h] = append(hashToOffset[h], i) +// } +// for i, h := range hashSlice(B, length) { +// if offsets, ok := hashToOffset[h]; ok { +// for _, offset := range offsets { +// if hasSamePrefix(A[offset:], B[i:], length) { +// return true +// } +// } +// } +// } +// return false +// } + +// 解法二 二分搜索 + 暴力匹配 +func longestDupSubstring1(S string) string { + res := "" + low, high := 0, len(S) + for low < high { + mid := low + (high-low)>>1 + if isDuplicate(mid, S, &res) { + low = mid + 1 + } else { + high = mid + } + } + return res +} + +func isDuplicate(length int, str string, res *string) bool { + visited := map[string]bool{} + for i := 0; i+length <= len(str); i++ { + subStr := str[i : i+length] + if visited[subStr] { + *res = subStr + return true + } + visited[subStr] = true + } + return false +} diff --git a/leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring_test.go b/leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring_test.go new file mode 100644 index 000000000..15b19927d --- /dev/null +++ b/leetcode/9991044.Longest-Duplicate-Substring/1044. Longest Duplicate Substring_test.go @@ -0,0 +1,46 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question1044 struct { + para1044 + ans1044 +} + +// para 是参数 +// one 代表第一个参数 +type para1044 struct { + one string +} + +// ans 是答案 +// one 代表第一个答案 +type ans1044 struct { + one string +} + +func Test_Problem1044(t *testing.T) { + + qs := []question1044{ + question1044{ + para1044{"banana"}, + ans1044{"ana"}, + }, + + question1044{ + para1044{"abcd"}, + ans1044{""}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 1044------------------------\n") + + for _, q := range qs { + _, p := q.ans1044, q.para1044 + fmt.Printf("【input】:%v 【output】:%v\n", p, longestDupSubstring(p.one)) + } + fmt.Printf("\n\n\n") +} diff --git a/leetcode/9991292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold.go b/leetcode/9991292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold.go new file mode 100644 index 000000000..90c05d815 --- /dev/null +++ b/leetcode/9991292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold.go @@ -0,0 +1,48 @@ +package leetcode + +func maxSideLength(mat [][]int, threshold int) int { + return 0 + // m, n, sum := len(mat), len(mat[0]), make([][]int, len(mat[0])+1, len(mat[0])+1) + // for i := range sum { + // sum[i] = make([]int, n+1, n+1) + // } + // for i := 0; i < m; i++ { + // for j := 0; j < n; j++ { + // sum[i+1][j+1] = sum[i][j+1] + sum[i+1][j] - sum[i][j] + mat[i][j] + // } + // } + // low, high := 0, min(m, n) + // for low < high { + // mid := low + (high-low)>>1 + // if !inThreshold(&sum, threshold, mid) { + // high = mid + 1 + // } else { + // low = mid + // } + // } + // if inThreshold(&sum, threshold, high) { + // return high + // } + // return low +} + +// func min(a int, b int) int { +// if a > b { +// return b +// } +// return a +// } + +// func inThreshold(sum *[][]int, threshold int, length int) bool { +// for i := 1; i < len(*sum); i++ { +// for j := 1; j < len((*sum)[0]); j++ { +// if i < length || j < length { +// continue +// } +// if (*sum)[i][j]+(*sum)[i-length][j-length]-(*sum)[i-length][j]-(*sum)[i][j-length] <= threshold { +// return true +// } +// } +// } +// return false +// } diff --git a/leetcode/9991292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_test.go b/leetcode/9991292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_test.go new file mode 100644 index 000000000..d731d5174 --- /dev/null +++ b/leetcode/9991292.Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold/1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_test.go @@ -0,0 +1,48 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question1292 struct { + para1292 + ans1292 +} + +// para 是参数 +// one 代表第一个参数 +type para1292 struct { + mat [][]int + threshold int +} + +// ans 是答案 +// one 代表第一个答案 +type ans1292 struct { + one int +} + +func Test_Problem1292(t *testing.T) { + + qs := []question1292{ + + question1292{ + para1292{[][]int{[]int{1, 1, 3, 2, 4, 3, 2}, []int{1, 1, 3, 2, 4, 3, 2}, []int{1, 1, 3, 2, 4, 3, 2}}, 4}, + ans1292{2}, + }, + + question1292{ + para1292{[][]int{[]int{2, 2, 2, 2, 2}, []int{2, 2, 2, 2, 2}, []int{2, 2, 2, 2, 2}, []int{2, 2, 2, 2, 2}, []int{2, 2, 2, 2, 2}}, 1}, + ans1292{0}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 1292------------------------\n") + + for _, q := range qs { + _, p := q.ans1292, q.para1292 + fmt.Printf("【input】:%v 【output】:%v\n", p, maxSideLength(p.mat, p.threshold)) + } + fmt.Printf("\n\n\n") +}