Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
50f6bb8
Add solution and test-cases for problem 3493
0xff-dev Nov 8, 2026
538a368
Add solution and test-cases for problem 1606
0xff-dev Nov 9, 2026
acca834
Add solution and test-cases for problem 598
0xff-dev Nov 10, 2026
f042f39
Add solution and test-cases for problem 3789
0xff-dev Nov 11, 2026
7348866
Add solution and test-cases for problem 3945
0xff-dev Nov 12, 2026
546c0a0
Add solution and test-cases for problem 3420
0xff-dev Nov 13, 2026
5ee6141
Add solution and test-cases for problem 3364
0xff-dev Nov 14, 2026
e64a5ea
Add solution and test-cases for problem 908
0xff-dev Nov 15, 2026
c15b85c
Add solution and test-cases for problem 2243
0xff-dev Nov 16, 2026
42ccb8f
Add solution and test-cases for problem 2911
0xff-dev Nov 17, 2026
a5f82ec
Add solution and test-cases for problem 3818
0xff-dev Nov 18, 2026
0814cfe
Add solution and test-cases for problem 2567
0xff-dev Nov 19, 2026
8a4b1d9
Add solution and test-cases for problem 3041
0xff-dev Nov 20, 2026
26fd021
Add solution and test-cases for problem 3331
0xff-dev Nov 21, 2026
ade74fa
Add solution and test-cases for problem 2124
0xff-dev Nov 22, 2026
996186f
Add solution and test-cases for problem 3309
0xff-dev Nov 23, 2026
1dd81c1
Add solution and test-cases for problem 3411
0xff-dev Nov 24, 2026
be15b63
Add solution and test-cases for problem 3934
0xff-dev Nov 25, 2026
55666cb
Add solution and test-cases for problem 3360
0xff-dev Nov 26, 2026
5f43dc1
Add solution and test-cases for problem 3946
0xff-dev Nov 27, 2026
ee2e2c5
Add solution and test-cases for problem 3947
0xff-dev Nov 28, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
# [1606.Find Servers That Handled Most Number of Requests][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You have k servers numbered from `0` to `k-1` that are being used to handle multiple requests simultaneously. Each server has infinite computational capacity but **cannot handle more than one request at a time**. The requests are assigned to servers according to a specific algorithm:

- The `ith` (0-indexed) request arrives.
- If all servers are busy, the request is dropped (not handled at all).
- If the `(i % k)th` server is available, assign the request to that server.
- Otherwise, assign the request to the next available server (wrapping around the list of servers and starting from 0 if necessary). For example, if the `ith` server is busy, try to assign the request to the `(i+1)th` server, then the `(i+2)th` server, and so on.

You are given a **strictly increasing** array `arrival` of positive integers, where `arrival[i]` represents the arrival time of the `ith` request, and another array `load`, where `load[i]` represents the load of the `ith` request (the time it takes to complete). Your goal is to find the **busiest server(s)**. A server is considered **busiest** if it handled the most number of requests successfully among all the servers.

Return a list containing the IDs (0-indexed) of the **busiest server(s)**. You may return the IDs in any order.

**Example 1:**

**Example 1:**
![1](./1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: k = 3, arrival = [1,2,3,4,5], load = [5,2,3,3,3]
Output: [1]
Explanation:
All of the servers start out available.
The first 3 requests are handled by the first 3 servers in order.
Request 3 comes in. Server 0 is busy, so it's assigned to the next available server, which is 1.
Request 4 comes in. It cannot be handled since all servers are busy, so it is dropped.
Servers 0 and 2 handled one request each, while server 1 handled two requests. Hence server 1 is the busiest server.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Find Servers That Handled Most Number of Requests
```go
```
Input: k = 3, arrival = [1,2,3,4], load = [1,2,1,2]
Output: [0]
Explanation:
The first 3 requests are handled by first 3 servers.
Request 3 comes in. It is handled by server 0 since the server is available.
Server 0 handled two requests, while servers 1 and 2 handled one request each. Hence server 0 is the busiest server.
```

**Example 3:**

```
Input: k = 3, arrival = [1,2,3,4], load = [1,2,1,2]
Output: [0]
Explanation:
The first 3 requests are handled by first 3 servers.
Request 3 comes in. It is handled by server 0 since the server is available.
Server 0 handled two requests, while servers 1 and 2 handled one request each. Hence server 0 is the busiest server.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,85 @@
package Solution

func Solution(x bool) bool {
import (
"container/heap"

"github.com/emirpasic/gods/trees/redblacktree"

Check failure on line 6 in leetcode/1601-1700/1606.Find-Servers-That-Handled-Most-Number-of-Requests/Solution.go

View workflow job for this annotation

GitHub Actions / Test Solution Cases

no required module provides package github.com/emirpasic/gods/trees/redblacktree; to add it:
)

type BusyServer struct {
releaseTime int
serverId int
}

type MinHeap []BusyServer

func (h MinHeap) Len() int { return len(h) }
func (h MinHeap) Less(i, j int) bool { return h[i].releaseTime < h[j].releaseTime }
func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *MinHeap) Push(x interface{}) {
*h = append(*h, x.(BusyServer))
}
func (h *MinHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}

func Solution(k int, arrival []int, load []int) []int {
available := redblacktree.NewWithIntComparator()
for i := 0; i < k; i++ {
available.Put(i, true)
}

busyHeap := &MinHeap{}
heap.Init(busyHeap)

requestCount := make([]int, k)
maxRequests := 0

for i := 0; i < len(arrival); i++ {
currTime := arrival[i]
duration := load[i]

for busyHeap.Len() > 0 && (*busyHeap)[0].releaseTime <= currTime {
item := heap.Pop(busyHeap).(BusyServer)
available.Put(item.serverId, true)
}

if available.Size() == 0 {
continue
}

target := i % k
var serverId int

node, found := available.Ceiling(target)
if found {
serverId = node.Key.(int)
} else {
serverId = available.Left().Key.(int)
}

available.Remove(serverId)
heap.Push(busyHeap, BusyServer{
releaseTime: currTime + duration,
serverId: serverId,
})

requestCount[serverId]++
if requestCount[serverId] > maxRequests {
maxRequests = requestCount[serverId]
}
}

var result []int
for i := 0; i < k; i++ {
if requestCount[i] == maxRequests {
result = append(result, i)
}
}

return result
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
k int
arrival []int
load []int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 3, []int{1, 2, 3, 4, 5}, []int{5, 2, 3, 3, 3}, []int{1}},
{"TestCase2", 3, []int{1, 2, 3, 4}, []int{1, 2, 1, 2}, []int{0}},
{"TestCase3", 3, []int{1, 2, 3}, []int{10, 12, 11}, []int{0, 1, 2}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.k, c.arrival, c.load)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.k, c.arrival, c.load)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
# [2124.Check if All A&#39;s Appears Before All B&#39;s][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
Given a string `s` consisting of **only** the characters `'a'` and `'b'`, return `true` if **every** `'a'` appears before **every** `'b'` in the string. Otherwise, return `false`.


**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "aaabbb"
Output: true
Explanation:
The 'a's are at indices 0, 1, and 2, while the 'b's are at indices 3, 4, and 5.
Hence, every 'a' appears before every 'b' and we return true.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Check if All A&#39;s Appears Before All B&#39;s
```go
```
Input: s = "abab"
Output: false
Explanation:
There is an 'a' at index 2 and a 'b' at index 1.
Hence, not every 'a' appears before every 'b' and we return false.
```

**Example 3:**

```
Input: s = "bbb"
Output: true
Explanation:
There are no 'a's, hence, every 'a' appears before every 'b' and we return true.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string) bool {
lastA, earliestB := -1, 1000000007
for i := range s {
if s[i] == 'a' {
lastA = i
continue
}
earliestB = min(earliestB, i)
}
return lastA < earliestB
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
inputs string
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "aaabbb", true},
{"TestCase2", "abab", false},
{"TestCase3", "bbb", true},
}

// 开始测试
Expand Down
41 changes: 27 additions & 14 deletions leetcode/2201-2300/2243.Calculate-Digit-Sum-of-a-String/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
# [2243.Calculate Digit Sum of a String][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are given a string `s` consisting of digits and an integer `k`.

A **round** can be completed if the length of `s` is greater than `k`. In one round, do the following:

- **Divide** `s` into **consecutive groups** of size `k` such that the first `k` characters are in the first group, the next `k` characters are in the second group, and so on. **Note** that the size of the last group can be smaller than `k`.
- **Replace** each group of `s` with a string representing the sum of all its digits. For example, `"346"` is replaced with `"13"` because `3 + 4 + 6 = 13`.
- **Merge** consecutive groups together to form a new string. If the length of the string is greater than `k`, repeat from step `1`.

Return `s` after all rounds have been completed.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "11111222223", k = 3
Output: "135"
Explanation:
- For the first round, we divide s into groups of size 3: "111", "112", "222", and "23".
​​​​​Then we calculate the digit sum of each group: 1 + 1 + 1 = 3, 1 + 1 + 2 = 4, 2 + 2 + 2 = 6, and 2 + 3 = 5.
So, s becomes "3" + "4" + "6" + "5" = "3465" after the first round.
- For the second round, we divide s into "346" and "5".
Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5.
So, s becomes "13" + "5" = "135" after second round.
Now, s.length <= k, so we return "135" as the answer.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Calculate Digit Sum of a String
```go
```

Input: s = "00000000", k = 3
Output: "000"
Explanation:
We divide s into "000", "000", and "00".
Then we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0.
s becomes "0" + "0" + "0" = "000", whose length is equal to k, so we return "000".
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
package Solution

func Solution(x bool) bool {
return x
import (
"strconv"
"strings"
)

func Solution(s string, k int) string {
var buf strings.Builder

replace := func(src string) string {
buf.Reset()
base := 0
for i := range src {
if i%k == 0 {
if i != 0 {
_new := strconv.Itoa(base)
buf.WriteString(_new)
}
base = 0
}
base += int(src[i] - '0')
}
_new := strconv.Itoa(base)
buf.WriteString(_new)
return buf.String()
}

for len(s) > k {
s = replace(s)
}
return s
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
k int
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "11111222223", 3, "135"},
{"TestCase2", "00000000", 3, "000"},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.k)
}
})
}
Expand Down
Loading
Loading