Skip to content

Commit

Permalink
2023/12/31
Browse files Browse the repository at this point in the history
  • Loading branch information
baowj-678 committed Dec 31, 2023
1 parent 036598a commit 8ad4e31
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 0 deletions.
25 changes: 25 additions & 0 deletions leetcode/1154.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import "strconv"

func dayOfYear(date string) int {
year, _ := strconv.Atoi(date[:4])
month, _ := strconv.Atoi(date[5:7])
day, _ := strconv.Atoi(date[8:])
monthDays := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
if year%4 == 0 {
monthDays[1] = 29
}
if year%100 == 0 {
monthDays[1] = 28
}
if year%400 == 0 {
monthDays[1] = 29
}
res := 0
for i := 0; i < month-1; i++ {
res += monthDays[i]
}
res += day
return res
}
50 changes: 50 additions & 0 deletions leetcode/1349.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

func maxStudents(seats [][]byte) int {
l := len(seats[0])
dp := make([]int, 1<<l)
for _, seat := range seats {
s := byte(0)
for _, tmp := range seat {
switch tmp {
case '.':
s = (s << 1) | 1
case '#':
s = s << 1
}
}
newDp := make([]int, 1<<l)

for iInt := 0; iInt < 1<<l; iInt++ {
i := byte(iInt)
if (i^s)&i != 0 || i&(i<<1) != 0 || i&(i>>1) != 0 {
continue
}
add := 0
for k := 0; k < l; k++ {
if (1<<k)&i != 0 {
add += 1
}
}

for jInt := 0; jInt < 1<<l; jInt++ {
j := byte(jInt)
if i&(j<<1) != 0 || i&(j>>1) != 0 {
continue
}

if dp[j]+add > newDp[i] {
newDp[i] = dp[j] + add
}
}
}
dp = newDp
}
res := 0
for _, x := range dp {
if x > res {
res = x
}
}
return res
}
33 changes: 33 additions & 0 deletions leetcode/1660.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

func isWinner(player1 []int, player2 []int) int {
score1, score2 := 0, 0
lastIdx := -10
for i := 0; i < len(player1); i++ {
if i-lastIdx <= 2 {
score1 += player1[i]
}
score1 += player1[i]
if player1[i] == 10 {
lastIdx = i
}
}

lastIdx = -10
for i := 0; i < len(player2); i++ {
if i-lastIdx <= 2 {
score2 += player2[i]
}
score2 += player2[i]
if player2[i] == 10 {
lastIdx = i
}
}

if score1 > score2 {
return 1
} else if score2 > score1 {
return 2
}
return 0
}
39 changes: 39 additions & 0 deletions leetcode/1671.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
)

func minimumMountainRemovals(nums []int) int {
res := 0
leftUp := make([]int, len(nums))
for i := 0; i < len(nums); i++ {
leftUp[i] = 1
for j := i - 1; j >= 0; j-- {
if nums[j] < nums[i] && leftUp[i] < leftUp[j]+1 {
leftUp[i] = leftUp[j] + 1
}
}
}
rightUp := make([]int, len(nums))
for i := len(nums) - 1; i >= 0; i-- {
rightUp[i] = 1
for j := i + 1; j < len(nums); j++ {
if nums[j] < nums[i] && rightUp[j]+1 > rightUp[i] {
rightUp[i] = rightUp[j] + 1
}
}
}

for i := 0; i < len(nums); i++ {
if leftUp[i] > 1 && rightUp[i] > 1 && leftUp[i]+rightUp[i]-1 > res {
res = leftUp[i] + rightUp[i] - 1
}
}
return len(nums) - res
}

func main() {
nums := []int{23, 47, 63, 72, 81, 99, 88, 55, 21, 33, 32}
fmt.Println(minimumMountainRemovals(nums))
}
23 changes: 23 additions & 0 deletions leetcode/1726.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import "sort"

func tupleSameProduct(nums []int) int {
cnt := map[int]int{}
sort.Ints(nums)
for i := 0; i < len(nums); i++ {
for j := i + 1; j < len(nums); j++ {
s := nums[i] * nums[j]
if v, ok := cnt[s]; ok {
cnt[s] = v + 1
} else {
cnt[s] = 1
}
}
}
res := 0
for _, v := range cnt {
res += v * (v - 1) * 4
}
return res
}
51 changes: 51 additions & 0 deletions leetcode/2316.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

func countPairs2316(n int, edges [][]int) int64 {
parent := make([]int, n)
for i := range parent {
parent[i] = i
}
find := func(x int) int {
tmp := x
for parent[x] != x {
x = parent[x]
}
for parent[tmp] != x {
parent[tmp], tmp = x, parent[tmp]
}
return x
}

union := func(x, y int) {
x = find(x)
y = find(y)
parent[x] = y
}

for _, edge := range edges {
union(edge[0], edge[1])
}

m := map[int]int{}
for i := range parent {
m[find(i)]++
}

groups := make([]int, 0, len(m))
for _, v := range m {
groups = append(groups, v)
}
sums := make([]int, len(m))
s := 0
for i := len(groups) - 1; i >= 0; i-- {
sums[i] = s
s += groups[i]
}

res := int64(0)
for i, g1 := range groups {
res += int64(g1) * int64(sums[i])
}

return res
}
20 changes: 20 additions & 0 deletions leetcode/2706.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "math"

func buyChoco(prices []int, money int) int {
minMoneys := [2]int{math.MaxInt, math.MaxInt}
for _, price := range prices {
if price < minMoneys[0] {
minMoneys[1], minMoneys[0] = minMoneys[0], price
continue
}
if price < minMoneys[1] {
minMoneys[1] = price
}
}
if minMoneys[0]+minMoneys[1] > money {
return money
}
return money - minMoneys[0] - minMoneys[1]
}
41 changes: 41 additions & 0 deletions leetcode/2735.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"math"
)

func minCost(nums []int, x int) int64 {
minNums := make([]int, len(nums))
copy(minNums, nums)
min := func(x int, y int64) int {
if int64(x) < y {
return x
}
return int(y)
}

minInt64 := func(x, y int64) int64 {
if x < y {
return x
}
return y
}

res := int64(math.MaxInt64)

for i := 0; i < len(nums); i++ {
sum := int64(i * x)
for k := 0; k < len(nums); k++ {
minNums[k] = min(minNums[k], int64(nums[(k-i+len(nums))%len(nums)]))
sum += int64(minNums[k])
}
res = minInt64(res, sum)
}
return res
}

func main() {
nums := []int{20, 1, 15}
fmt.Println(minCost(nums, 5))
}
48 changes: 48 additions & 0 deletions leetcode/2866.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import "fmt"

func maximumSumOfHeights(maxHeights []int) int64 {
prefix := make([]int64, len(maxHeights))
suffix := make([]int64, len(maxHeights))
stack := [][2]int{}
for i, height := range maxHeights {
for len(stack) > 0 && stack[len(stack)-1][1] > height {
stack = stack[:len(stack)-1]
}
if len(stack) > 0 {
lastIdx := stack[len(stack)-1][0]
prefix[i] = prefix[lastIdx] + int64(height)*int64(i-lastIdx)
} else {
prefix[i] = int64(height) * int64(i+1)
}
stack = append(stack, [2]int{i, height})
}

stack = [][2]int{}
for i := len(maxHeights) - 1; i >= 0; i-- {
height := maxHeights[i]
for len(stack) > 0 && stack[len(stack)-1][1] > height {
stack = stack[:len(stack)-1]
}
if len(stack) > 0 {
lastIdx := stack[len(stack)-1][0]
suffix[i] = suffix[lastIdx] + int64(height)*int64(lastIdx-i)
} else {
suffix[i] = int64(height) * int64(len(maxHeights)-i)
}
stack = append(stack, [2]int{i, height})
}
res := int64(0)
for i := 0; i < len(maxHeights); i++ {
if prefix[i]+suffix[i]-int64(maxHeights[i]) > res {
res = prefix[i] + suffix[i] - int64(maxHeights[i])
}
}
return res
}

func main() {
nums := []int{5, 3, 4, 1, 1}
fmt.Println(maximumSumOfHeights(nums))
}

0 comments on commit 8ad4e31

Please sign in to comment.