Compare commits
3 commits
0f5f9e331c
...
7bdd2b5d01
| Author | SHA1 | Date | |
|---|---|---|---|
| 7bdd2b5d01 | |||
| 079c2d5dd5 | |||
| a7ff717b7a |
11 changed files with 4129 additions and 8 deletions
5
Makefile
5
Makefile
|
|
@ -1,4 +1,7 @@
|
||||||
lint:
|
lint:
|
||||||
golangci-lint run ./...
|
golangci-lint run ./...
|
||||||
|
|
||||||
.PHONY: lint
|
problemset:
|
||||||
|
bash ./query-problem-set.sh > problemset.jsonl
|
||||||
|
|
||||||
|
.PHONY: lint problemset
|
||||||
|
|
|
||||||
23
create.sh
23
create.sh
|
|
@ -1,21 +1,30 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
num=$1
|
id=$1
|
||||||
|
|
||||||
echo "$num" | grep -q "^[1-9][0-9]\{,4\}$" || {
|
echo "$id" | grep -q "^[1-9][0-9]\{,4\}$" || {
|
||||||
echo "Err: no valid number given" >&2
|
echo "Err: no valid problem ID provided." >&2
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
parent=$((num / 100))
|
parent=$((id / 100))
|
||||||
pdir="solutions/${parent}/q${num}"
|
pdir="solutions/${parent}/q${id}"
|
||||||
|
|
||||||
sol_file="${pdir}/solution.go"
|
sol_file="${pdir}/solution.go"
|
||||||
|
|
||||||
if [ -f "$sol_file" ]; then
|
if [ -f "$sol_file" ]; then
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Creating template for question No. $num" >&2
|
title_slug=$(jq -r "select(.id == $id) | .titleSlug" ./problemset.jsonl)
|
||||||
|
[ -n "$title_slug" ] || {
|
||||||
|
echo "Err: problem $id not found in the problem set." >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
echo -e "Creating template for problem \033[32m${id}: $title_slug\033[0m" >&2
|
||||||
mkdir -pv "$pdir"
|
mkdir -pv "$pdir"
|
||||||
echo "package q$num" > "$sol_file"
|
echo "// Package q$id implements solution for https://leetcode.com/problems/$title_slug/" > "$sol_file"
|
||||||
|
echo "package q$id" >> "$sol_file"
|
||||||
|
|
||||||
echo "Created $sol_file"
|
echo "Created $sol_file"
|
||||||
|
|
|
||||||
3851
problemset.jsonl
Normal file
3851
problemset.jsonl
Normal file
File diff suppressed because it is too large
Load diff
37
query-problem-set.sh
Normal file
37
query-problem-set.sh
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
query() {
|
||||||
|
page=$1
|
||||||
|
offset=$((page * 100))
|
||||||
|
|
||||||
|
# shellcheck disable=SC2016
|
||||||
|
curl -sSLf 'https://leetcode.com/graphql/' \
|
||||||
|
--compressed \
|
||||||
|
-H 'content-type: application/json' \
|
||||||
|
--data-raw '{
|
||||||
|
"query": "\n query problemsetQuestionListV2($filters: QuestionFilterInput, $limit: Int, $searchKeyword: String, $skip: Int, $sortBy: QuestionSortByInput, $categorySlug: String) {\n problemsetQuestionListV2(\n filters: $filters\n limit: $limit\n searchKeyword: $searchKeyword\n skip: $skip\n sortBy: $sortBy\n categorySlug: $categorySlug\n ) {\n questions {\n id\n titleSlug\n title\n translatedTitle\n questionFrontendId\n paidOnly\n difficulty\n topicTags {\n name\n slug\n nameTranslated\n }\n status\n isInMyFavorites\n frequency\n acRate\n contestPoint\n }\n totalLength\n finishedLength\n hasMore\n }\n}\n ",
|
||||||
|
"variables": {
|
||||||
|
"skip": '"$offset"',
|
||||||
|
"limit": 100,
|
||||||
|
"categorySlug": "all-code-essentials",
|
||||||
|
"filters": {"filterCombineType":"ALL","statusFilter":{"questionStatuses":[],"operator":"IS"},"difficultyFilter":{"difficulties":[],"operator":"IS"},"languageFilter":{"languageSlugs":[],"operator":"IS"},"topicFilter":{"topicSlugs":[],"operator":"IS"},"acceptanceFilter":{},"frequencyFilter":{},"frontendIdFilter":{},"lastSubmittedFilter":{},"publishedFilter":{},"companyFilter":{"companySlugs":[],"operator":"IS"},"positionFilter":{"positionSlugs":[],"operator":"IS"},"positionLevelFilter":{"positionLevelSlugs":[],"operator":"IS"},"contestPointFilter":{"contestPoints":[],"operator":"IS"},"premiumFilter":{"premiumStatus":[],"operator":"IS"}},
|
||||||
|
"searchKeyword": "",
|
||||||
|
"sortBy": {"sortField":"CUSTOM","sortOrder":"ASCENDING"},
|
||||||
|
"filtersV2": {"filterCombineType":"ALL","statusFilter":{"questionStatuses":[],"operator":"IS"},"difficultyFilter":{"difficulties":[],"operator":"IS"},"languageFilter":{"languageSlugs":[],"operator":"IS"},"topicFilter":{"topicSlugs":[],"operator":"IS"},"acceptanceFilter":{},"frequencyFilter":{},"frontendIdFilter":{},"lastSubmittedFilter":{},"publishedFilter":{},"companyFilter":{"companySlugs":[],"operator":"IS"},"positionFilter":{"positionSlugs":[],"operator":"IS"},"positionLevelFilter":{"positionLevelSlugs":[],"operator":"IS"},"contestPointFilter":{"contestPoints":[],"operator":"IS"},"premiumFilter":{"premiumStatus":[],"operator":"IS"}}
|
||||||
|
},
|
||||||
|
"operationName": "problemsetQuestionListV2"
|
||||||
|
}' | jq -c '.data.problemsetQuestionListV2.questions[] | { id, title, titleSlug, difficulty, paidOnly, tags: [ .topicTags[] | .slug ] }'
|
||||||
|
}
|
||||||
|
|
||||||
|
page=0
|
||||||
|
while true; do
|
||||||
|
echo -ne " Querying page $((page + 1)) ...\r" >&2
|
||||||
|
content=$(query $page)
|
||||||
|
lines=$(echo "$content" | wc -l)
|
||||||
|
echo "$content"
|
||||||
|
|
||||||
|
[ "$lines" -lt 100 ] && break
|
||||||
|
page=$((page + 1))
|
||||||
|
done | jq -s -c 'sort_by(.id) | .[]'
|
||||||
|
echo >&2
|
||||||
29
solutions/10/q1022/solution.go
Normal file
29
solutions/10/q1022/solution.go
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
package q1022
|
||||||
|
|
||||||
|
type TreeNode struct {
|
||||||
|
Val int
|
||||||
|
Left *TreeNode
|
||||||
|
Right *TreeNode
|
||||||
|
}
|
||||||
|
|
||||||
|
func sum(node *TreeNode, prefix int, ret *int) {
|
||||||
|
if node == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
num := prefix<<1 + node.Val
|
||||||
|
|
||||||
|
if node.Left == nil && node.Right == nil {
|
||||||
|
*ret += num
|
||||||
|
} else {
|
||||||
|
sum(node.Left, num, ret)
|
||||||
|
sum(node.Right, num, ret)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sumRootToLeaf(root *TreeNode) int {
|
||||||
|
ret := 0
|
||||||
|
sum(root, 0, &ret)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = sumRootToLeaf
|
||||||
26
solutions/13/q1356/solution.go
Normal file
26
solutions/13/q1356/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
package q1356
|
||||||
|
|
||||||
|
import "slices"
|
||||||
|
|
||||||
|
func count1s(n int) int {
|
||||||
|
c := 0
|
||||||
|
for n > 0 {
|
||||||
|
c++
|
||||||
|
n = n & (n - 1)
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func sortByBits(arr []int) []int {
|
||||||
|
for i := range arr {
|
||||||
|
arr[i] += count1s(arr[i]) << 16
|
||||||
|
}
|
||||||
|
slices.Sort(arr)
|
||||||
|
|
||||||
|
for i := range arr {
|
||||||
|
arr[i] &= 1<<16 - 1
|
||||||
|
}
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = sortByBits
|
||||||
25
solutions/14/q1404/solution.go
Normal file
25
solutions/14/q1404/solution.go
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
package q1404
|
||||||
|
|
||||||
|
func numSteps(s string) int {
|
||||||
|
steps := 0
|
||||||
|
carry := false
|
||||||
|
for i := len(s) - 1; i > 0; i-- {
|
||||||
|
switch {
|
||||||
|
case !carry && s[i] == '0':
|
||||||
|
steps++
|
||||||
|
case !carry && s[i] == '1':
|
||||||
|
steps += 2
|
||||||
|
carry = true
|
||||||
|
case carry && s[i] == '0':
|
||||||
|
steps += 2
|
||||||
|
default:
|
||||||
|
steps++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if carry {
|
||||||
|
steps++
|
||||||
|
}
|
||||||
|
return steps
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = numSteps
|
||||||
15
solutions/6/q693/solution.go
Normal file
15
solutions/6/q693/solution.go
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
package q693
|
||||||
|
|
||||||
|
func hasAlternatingBits(n int) bool {
|
||||||
|
last := -1
|
||||||
|
for n > 0 {
|
||||||
|
if last == n%2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
last = n % 2
|
||||||
|
n >>= 1
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = hasAlternatingBits
|
||||||
20
solutions/6/q696/solution.go
Normal file
20
solutions/6/q696/solution.go
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
package q696
|
||||||
|
|
||||||
|
func countBinarySubstrings(s string) int {
|
||||||
|
cnt := 0
|
||||||
|
last := byte('0')
|
||||||
|
lastLen, curLen := 0, 0
|
||||||
|
for i := range len(s) {
|
||||||
|
if s[i] != last {
|
||||||
|
last, lastLen = s[i], curLen
|
||||||
|
curLen = 0
|
||||||
|
}
|
||||||
|
curLen++
|
||||||
|
if curLen <= lastLen {
|
||||||
|
cnt++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cnt
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = countBinarySubstrings
|
||||||
84
solutions/7/q761/solution.go
Normal file
84
solutions/7/q761/solution.go
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
package q761
|
||||||
|
|
||||||
|
func toNumber(arrs ...[]byte) int {
|
||||||
|
r := 0
|
||||||
|
for i := range arrs {
|
||||||
|
for _, num := range arrs[i] {
|
||||||
|
r = r<<1 + int(num)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromNumber(arr []byte, num int) {
|
||||||
|
for i := len(arr) - 1; i >= 0; i-- {
|
||||||
|
arr[i] = byte(num % 2)
|
||||||
|
num >>= 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func move(arr []byte) int {
|
||||||
|
specials := [][2]int{}
|
||||||
|
specialsMap := make(map[int][]int, len(arr)+1)
|
||||||
|
val := toNumber(arr)
|
||||||
|
|
||||||
|
for i := range len(arr) - 1 {
|
||||||
|
if arr[i] == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
bal := 1
|
||||||
|
for j := i + 1; j < len(arr); j++ {
|
||||||
|
switch arr[j] {
|
||||||
|
case 0:
|
||||||
|
bal--
|
||||||
|
case 1:
|
||||||
|
bal++
|
||||||
|
}
|
||||||
|
|
||||||
|
if bal < 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if bal == 0 {
|
||||||
|
specials = append(specials, [2]int{i, j})
|
||||||
|
specialsMap[i] = append(specialsMap[i], j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range specials {
|
||||||
|
l1, r1 := specials[i][0], specials[i][1]
|
||||||
|
l2 := r1 + 1
|
||||||
|
for _, r2 := range specialsMap[l2] {
|
||||||
|
swapped := toNumber(arr[:l1], arr[l2:r2+1], arr[l1:l2], arr[r2+1:])
|
||||||
|
if swapped > val {
|
||||||
|
val = swapped
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeLargestSpecial(s string) string {
|
||||||
|
arr := []byte(s)
|
||||||
|
for i := range arr {
|
||||||
|
arr[i] -= '0'
|
||||||
|
}
|
||||||
|
|
||||||
|
val := toNumber(arr)
|
||||||
|
for {
|
||||||
|
newVal := move(arr)
|
||||||
|
if newVal > val {
|
||||||
|
val = newVal
|
||||||
|
fromNumber(arr, newVal)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range arr {
|
||||||
|
arr[i] += '0'
|
||||||
|
}
|
||||||
|
return string(arr)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = makeLargestSpecial
|
||||||
22
solutions/9/q918/solution.go
Normal file
22
solutions/9/q918/solution.go
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
package q918
|
||||||
|
|
||||||
|
func maxSubarraySumCircular(nums []int) int {
|
||||||
|
maxSum, minSum, maxPfx, minPfx := nums[0], nums[0], 0, 0
|
||||||
|
|
||||||
|
sum := 0
|
||||||
|
for _, num := range nums {
|
||||||
|
sum += num
|
||||||
|
maxSum = max(maxSum, sum-minPfx)
|
||||||
|
minSum = min(minSum, sum-maxPfx)
|
||||||
|
|
||||||
|
maxPfx = max(maxPfx, sum)
|
||||||
|
minPfx = min(minPfx, sum)
|
||||||
|
}
|
||||||
|
|
||||||
|
if sum == minSum {
|
||||||
|
return maxSum
|
||||||
|
}
|
||||||
|
return max(maxSum, sum-minSum)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = maxSubarraySumCircular
|
||||||
Loading…
Add table
Add a link
Reference in a new issue