Skip to content

Commit 87d63d3

Browse files
author
openset
committed
Add: ans
1 parent 09bd670 commit 87d63d3

File tree

132 files changed

+1711
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+1711
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Write your MySQL query statement below
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package adding_two_negabinary_numbers
2+
3+
func addNegabinary(A, B []int) []int {
4+
i, j := len(A)-1, len(B)-1
5+
res := make([]int, 0, i+j)
6+
carry := 0
7+
for i >= 0 || j >= 0 || carry != 0 {
8+
if i >= 0 {
9+
carry += A[i]
10+
i--
11+
}
12+
if j >= 0 {
13+
carry += B[j]
14+
j--
15+
}
16+
res = append(res, carry&1)
17+
carry = -(carry >> 1)
18+
}
19+
20+
res = reverse(res)
21+
22+
// cut leading zero
23+
i, end := 0, len(res)-1
24+
for i < end && res[i] == 0 {
25+
i++
26+
}
27+
return res[i:]
28+
}
29+
30+
func reverse(A []int) []int {
31+
i, j := 0, len(A)-1
32+
for i < j {
33+
A[i], A[j] = A[j], A[i]
34+
i++
35+
j--
36+
}
37+
return A
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package best_sightseeing_pair
2+
3+
func maxScoreSightseeingPair(A []int) int {
4+
a := A[0] // a is A[i]+i
5+
res := 0
6+
for j := 1; j < len(A); j++ {
7+
res = max(res, a+A[j]-j)
8+
a = max(a, A[j]+j)
9+
}
10+
return res
11+
}
12+
13+
func max(a, b int) int {
14+
if a > b {
15+
return a
16+
}
17+
return b
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package binary_tree_cameras
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package brace_expansion_ii
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package broken_calculator

problems/building-h2o/building_h2o.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
3+
class H2O:
4+
def __init__(self):
5+
pass
6+
7+
8+
def hydrogen(self, releaseHydrogen: 'Callable[[], None]') -> None:
9+
10+
# releaseHydrogen() outputs "H". Do not change or remove this line.
11+
releaseHydrogen()
12+
13+
14+
def oxygen(self, releaseOxygen: 'Callable[[], None]') -> None:
15+
16+
# releaseOxygen() outputs "O". Do not change or remove this line.
17+
releaseOxygen()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package capacity_to_ship_packages_within_d_days
2+
3+
func shipWithinDays(weights []int, D int) int {
4+
lo, hi := 0, 0
5+
for _, w := range weights {
6+
lo = max(lo, w)
7+
hi += w
8+
}
9+
10+
for lo < hi {
11+
mid := (lo + hi) >> 1
12+
days, cur := 1, 0
13+
for _, w := range weights {
14+
if cur+w > mid {
15+
days++
16+
cur = 0
17+
}
18+
cur += w
19+
}
20+
if days > D {
21+
lo = mid + 1
22+
} else {
23+
hi = mid
24+
}
25+
}
26+
27+
return lo
28+
}
29+
30+
func max(a, b int) int {
31+
if a > b {
32+
return a
33+
}
34+
return b
35+
}

problems/car-pooling/car_pooling.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package car_pooling
2+
3+
func carPooling(trips [][]int, capacity int) bool {
4+
location := [1001]int{}
5+
for _, t := range trips {
6+
num, start, end := t[0], t[1], t[2]
7+
location[start] += num
8+
location[end] -= num
9+
}
10+
11+
p := 0
12+
for _, c := range location {
13+
p += c
14+
if p > capacity {
15+
return false
16+
}
17+
}
18+
return true
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package check_if_word_is_valid_after_substitutions
2+
3+
func isValid(S string) bool {
4+
bs := []byte(S)
5+
stack := make([]byte, len(S))
6+
top := -1
7+
8+
for _, b := range bs {
9+
top++
10+
stack[top] = b
11+
switch top {
12+
case 0:
13+
if b != 'a' {
14+
return false
15+
}
16+
case 1:
17+
default:
18+
if b == 'c' &&
19+
stack[top-1] == 'b' &&
20+
stack[top-2] == 'a' {
21+
top -= 3
22+
}
23+
}
24+
}
25+
26+
return top == -1
27+
}

problems/clone-graph/clone_graph.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
# Definition for a Node.
5+
class Node:
6+
def __init__(self, val, neighbors):
7+
self.val = val
8+
self.neighbors = neighbors
9+
"""
10+
class Solution:
11+
def cloneGraph(self, node: 'Node') -> 'Node':
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package clumsy_factorial
2+
3+
var answers = []int{0, 1, 2, 6, 7}
4+
5+
func clumsy(N int) int {
6+
if N <= 4 {
7+
return answers[N]
8+
}
9+
a := N*(N-1)/(N-2) + (N - 3)
10+
b := (N - 4) * max(1, N-5) / max(1, N-6)
11+
return a - 2*b + clumsy(N-4)
12+
}
13+
14+
func max(a, b int) int {
15+
if a > b {
16+
return a
17+
}
18+
return b
19+
}
20+
21+
// go to look O(1) solution
22+
// https://leetcode.com/problems/clumsy-factorial/discuss/252279/You-never-think-of-this-amazing-O(1)-solution
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package coloring_a_border
2+
3+
var dx = []int{0, 0, 1, -1}
4+
var dy = []int{1, -1, 0, 0}
5+
6+
func colorBorder(grid [][]int, x0 int, y0 int, color int) [][]int {
7+
m, n := len(grid), len(grid[0])
8+
c := grid[x0][y0]
9+
10+
isBorder := func(x, y int) bool {
11+
return x == 0 || x == m-1 ||
12+
y == 0 || y == n-1 ||
13+
grid[x][y-1] != c ||
14+
grid[x][y+1] != c ||
15+
grid[x-1][y] != c ||
16+
grid[x+1][y] != c
17+
}
18+
19+
connected := make([][2]int, 1, 1024)
20+
hasSeen := [51][51]bool{}
21+
22+
connected[0] = [2]int{x0, y0}
23+
hasSeen[x0][y0] = true
24+
25+
borders := make([][2]int, 0, 1024)
26+
for len(connected) > 0 {
27+
size := len(connected)
28+
for i := 0; i < size; i++ {
29+
p := connected[i]
30+
if isBorder(p[0], p[1]) {
31+
borders = append(borders, p)
32+
}
33+
for k := 0; k < 4; k++ {
34+
x, y := p[0]+dx[k], p[1]+dy[k]
35+
if 0 <= x && x < m &&
36+
0 <= y && y < n &&
37+
grid[x][y] == c &&
38+
!hasSeen[x][y] {
39+
connected = append(connected, [2]int{x, y})
40+
hasSeen[x][y] = true
41+
}
42+
}
43+
}
44+
connected = connected[size:]
45+
}
46+
47+
for _, b := range borders {
48+
grid[b[0]][b[1]] = color
49+
}
50+
51+
return grid
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
# Definition for a QuadTree node.
5+
class Node:
6+
def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):
7+
self.val = val
8+
self.isLeaf = isLeaf
9+
self.topLeft = topLeft
10+
self.topRight = topRight
11+
self.bottomLeft = bottomLeft
12+
self.bottomRight = bottomRight
13+
"""
14+
class Solution:
15+
def construct(self, grid: List[List[int]]) -> 'Node':
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package convert_to_base_2
2+
3+
func baseNeg2(N int) string {
4+
if N == 0 {
5+
return "0"
6+
}
7+
8+
B := make([]byte, 0, 30)
9+
for N > 0 {
10+
switch N & 3 {
11+
case 0, 1:
12+
B = append(B, byte(N&1)+'0', '0')
13+
default: // 2,3
14+
B = append(B, byte(N&1)+'0', '1')
15+
N += 4
16+
// 2^(2n)-2^(2n-1) == 2^(2n-1)
17+
// 所以,把 N 转换成二进制后,每在奇数位上看见 1 就在其左边的偶数位上加 1
18+
// 就可以转换成以 -2 为基的二进制了
19+
}
20+
N >>= 2
21+
}
22+
23+
swap(B)
24+
25+
if B[0] == '0' { // no lead 0 except for N is 0
26+
B = B[1:]
27+
}
28+
29+
return string(B)
30+
}
31+
32+
func swap(B []byte) {
33+
i, j := 0, len(B)-1
34+
for i < j {
35+
B[i], B[j] = B[j], B[i]
36+
i++
37+
j--
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
# Definition for a Node.
5+
class Node:
6+
def __init__(self, val, next, random):
7+
self.val = val
8+
self.next = next
9+
self.random = random
10+
"""
11+
class Solution:
12+
def copyRandomList(self, head: 'Node') -> 'Node':
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package corporate_flight_bookings
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Write your MySQL query statement below
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Write your MySQL query statement below
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Write your MySQL query statement below
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Write your MySQL query statement below
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package delete_nodes_and_return_forest
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Write your MySQL query statement below
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Write your MySQL query statement below
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package distribute_candies_to_people
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package distribute_coins_in_binary_tree
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package duplicate_zeros
2+
3+
func duplicateZeros(A []int) {
4+
n := len(A)
5+
//
6+
count := 0
7+
for i := 0; i < n; i++ {
8+
if A[i] == 0 {
9+
count++
10+
}
11+
}
12+
// copy A[i] to A[j]
13+
copy := func(i, j int) {
14+
if j < n {
15+
A[j] = A[i]
16+
}
17+
}
18+
//
19+
i, j := n-1, n+count-1
20+
for i < j {
21+
copy(i, j)
22+
if A[i] == 0 {
23+
j--
24+
copy(i, j)
25+
}
26+
i--
27+
j--
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Write your MySQL query statement below

0 commit comments

Comments
 (0)