Skip to content

Commit e6aea2f

Browse files
author
openset
committed
Add: programs
1 parent 87d63d3 commit e6aea2f

File tree

13 files changed

+445
-1
lines changed

13 files changed

+445
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package binary_string_with_substrings_representing_1_to_n
2+
3+
import "strings"
4+
5+
func queryString(S string, N int) bool {
6+
end := N >> 1
7+
for N >= end {
8+
if !strings.Contains(S, binaryString(N)) {
9+
return false
10+
}
11+
N--
12+
}
13+
return true
14+
}
15+
16+
func binaryString(n int) string {
17+
B := make([]byte, 0, 30)
18+
for n > 0 {
19+
B = append(B, byte(n&1)+'0')
20+
n >>= 1
21+
}
22+
swap(B)
23+
return string(B)
24+
}
25+
26+
func swap(B []byte) {
27+
i, j := 0, len(B)-1
28+
for i < j {
29+
B[i], B[j] = B[j], B[i]
30+
i++
31+
j--
32+
}
33+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package defanging_an_ip_address
2+
3+
import "strings"
4+
5+
func defangIPaddr(address string) string {
6+
return strings.Replace(address, ".", "[.]", -1)
7+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package distant_barcodes
2+
3+
import "sort"
4+
5+
func rearrangeBarcodes(A []int) []int {
6+
n := len(A)
7+
8+
count := [10001]int{}
9+
for _, a := range A {
10+
count[a]++
11+
}
12+
13+
sort.Slice(A, func(i int, j int) bool {
14+
if count[A[i]] == count[A[j]] {
15+
return A[i] < A[j]
16+
}
17+
return count[A[i]] > count[A[j]]
18+
})
19+
20+
res := make([]int, n)
21+
i := 0
22+
for _, a := range A {
23+
res[i] = a
24+
i += 2
25+
if i >= n {
26+
i = 1
27+
}
28+
}
29+
30+
return res
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package flip_columns_for_maximum_number_of_equal_rows
2+
3+
import "strings"
4+
5+
func maxEqualRowsAfterFlips(A [][]int) int {
6+
count := make(map[string]int, 300)
7+
var sb strings.Builder
8+
for _, r := range A {
9+
r0 := r[0]
10+
for _, x := range r {
11+
sb.WriteByte(byte(x ^ r0 + '0'))
12+
}
13+
count[sb.String()]++
14+
sb.Reset()
15+
}
16+
17+
res := 0
18+
19+
for _, c := range count {
20+
res = max(res, c)
21+
}
22+
23+
return res
24+
}
25+
26+
func max(a, b int) int {
27+
if a > b {
28+
return a
29+
}
30+
return b
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package greatest_common_divisor_of_strings
2+
3+
import "strings"
4+
5+
func gcdOfStrings(s1, s2 string) string {
6+
l1, l2 := len(s1), len(s2)
7+
d := gcd(max(l1, l2), min(l1, l2))
8+
p := s2[:d]
9+
if s1 == strings.Repeat(p, l1/d) &&
10+
s2 == strings.Repeat(p, l2/d) {
11+
return p
12+
}
13+
return ""
14+
}
15+
16+
// a >= b
17+
func gcd(a, b int) int {
18+
if b == 0 {
19+
return a
20+
}
21+
return gcd(b, a%b)
22+
}
23+
24+
func min(a, b int) int {
25+
if a < b {
26+
return a
27+
}
28+
return b
29+
}
30+
31+
func max(a, b int) int {
32+
if a > b {
33+
return a
34+
}
35+
return b
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package largest_values_from_labels
2+
3+
import "sort"
4+
5+
func largestValsFromLabels(values []int, labels []int, num_wanted int, use_limit int) int {
6+
for i, v := range values {
7+
values[i] = v<<16 + labels[i]
8+
}
9+
10+
sort.Slice(values, func(i int, j int) bool {
11+
return values[i] > values[j]
12+
})
13+
14+
count := [20001]int{}
15+
res := 0
16+
for _, v := range values {
17+
if count[v&0xFFFF] == use_limit {
18+
continue
19+
}
20+
res += v >> 16
21+
count[v&0xFFFF]++
22+
num_wanted--
23+
if num_wanted == 0 {
24+
break
25+
}
26+
}
27+
28+
return res
29+
}

problems/max-consecutive-ones-iii/max_consecutive_ones_iii.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package max_consecutive_ones_iii
22

3-
// ref: https://leetcode.com/problems/max-consecutive-ones-iii/discuss/247564/JavaC%2B%2BPython-Sliding-Window
43
func longestOnes(A []int, K int) int {
54
left, right := 0, 0
65
for right = range A {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package moving_stones_until_consecutive_ii
2+
3+
import "sort"
4+
5+
func numMovesStonesII(stones []int) []int {
6+
sort.Ints(stones)
7+
return []int{low(stones), high(stones)}
8+
}
9+
10+
func high(s []int) int {
11+
n := len(s)
12+
return max(s[n-1]-s[1], s[n-2]-s[0]) - n + 2
13+
}
14+
15+
func low(s []int) int {
16+
n := len(s)
17+
// corner case
18+
if (s[n-2]-s[0] == n-2 && s[n-1]-s[n-2] > 2) ||
19+
(s[n-1]-s[1] == n-2 && s[1]-s[0] > 2) {
20+
return 2
21+
}
22+
// sliding window is s[i:j]
23+
width, i, j := 0, 0, 1
24+
for ; j < n; j++ {
25+
if s[j]-s[i] < n {
26+
continue
27+
}
28+
width = max(width, j-i)
29+
i = j
30+
}
31+
width = max(width, j-i)
32+
// finally, all stone move into maxWidth windows
33+
// so need move n-width stones
34+
return n - width
35+
}
36+
37+
func max(a, b int) int {
38+
if a > b {
39+
return a
40+
}
41+
return b
42+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package occurrences_after_bigram
2+
3+
import "strings"
4+
5+
func findOcurrences(text string, first string, second string) []string {
6+
words := strings.Split(text, " ")
7+
n := len(words)
8+
res := make([]string, 0, n)
9+
for i := 0; i+2 < n; i++ {
10+
if words[i] == first &&
11+
words[i+1] == second {
12+
res = append(res, words[i+2])
13+
}
14+
}
15+
return res
16+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package shortest_common_supersequence
2+
3+
import "strings"
4+
5+
func shortestCommonSupersequence(A, B string) string {
6+
m, n := len(A), len(B)
7+
// 解题思路,
8+
// 先求出 A 和 B 的 LCS,
9+
// 然后,在 LCS 上添加缺少的字母
10+
// 利用 dp 求解 LCS ,
11+
// dp[i][j]=k 表示 A[:i] 与 B[:j] 的 LCS 的长度为 k
12+
// 在递归过程中,会出现三种情况:
13+
// 1. A[i]=B[j], 则 dp[i][j]= dp[i-1][j-1]+1
14+
// 2. A[i]!=B[j] 且 dp[i-1][j] >= dp[i][j-1],则 dp[i][j]=dp[i-1][j]
15+
// 3. A[i]!=B[j] 且 dp[i-1][j] < dp[i][j-1],则 dp[i][j]=dp[i][j+1]
16+
17+
dp := [1001][1001]int{}
18+
b := [1001][1001]int{} // 记录哪种情况发生了,以便添加字母
19+
20+
for i := 1; i <= m; i++ {
21+
for j := 1; j <= n; j++ {
22+
if A[i-1] == B[j-1] {
23+
dp[i][j] = dp[i-1][j-1] + 1
24+
b[i][j] = 1
25+
} else if dp[i-1][j] >= dp[i][j-1] {
26+
dp[i][j] = dp[i-1][j]
27+
b[i][j] = 2
28+
} else {
29+
dp[i][j] = dp[i][j-1]
30+
b[i][j] = 3
31+
}
32+
}
33+
}
34+
35+
var sb strings.Builder
36+
var dfs func(int, int)
37+
dfs = func(i, j int) {
38+
if i == 0 {
39+
sb.WriteString(B[:j])
40+
return
41+
}
42+
43+
if j == 0 {
44+
sb.WriteString(A[:i])
45+
return
46+
}
47+
48+
switch b[i][j] {
49+
case 1:
50+
dfs(i-1, j-1)
51+
sb.WriteByte(A[i-1])
52+
case 2:
53+
dfs(i-1, j)
54+
sb.WriteByte(A[i-1])
55+
case 3:
56+
dfs(i, j-1)
57+
sb.WriteByte(B[j-1])
58+
}
59+
}
60+
61+
dfs(m, n)
62+
63+
return sb.String()
64+
}

0 commit comments

Comments
 (0)