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
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Write your MySQL query statement below
Lines changed: 38 additions & 0 deletions
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+
}
Lines changed: 18 additions & 0 deletions
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package binary_tree_cameras
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package brace_expansion_ii
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package broken_calculator

problems/building-h2o/building_h2o.py

Lines changed: 17 additions & 0 deletions
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()
Lines changed: 35 additions & 0 deletions
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

Lines changed: 19 additions & 0 deletions
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+
}
Lines changed: 27 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)