Skip to content

Commit a76296a

Browse files
Cori1109S Sathish Babu
and
S Sathish Babu
committed
Add solution and test cases to problem 68, 124 (#191)
* ✨ Add solution and test-cases for Problem 51 * ✨ Add solution and test cases to problem 68 * ✨ Add solution and test cses to problem 124 Co-authored-by: S Sathish Babu <[email protected]>
1 parent 3d52ec6 commit a76296a

File tree

7 files changed

+163
-30
lines changed

7 files changed

+163
-30
lines changed
Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func Solution(words []string, k int) []string {
6+
result := make([]string, 0);
7+
currWords := make([]string, 0);
8+
currWordsLen := 0;
9+
for _, word := range words {
10+
if currWordsLen + len(currWords) + len(word) > k {
11+
if len(currWords) == 1 {
12+
result = append(result, currWords[0] + strings.Repeat(" ", k - currWordsLen));
13+
} else {
14+
spaces := k - currWordsLen
15+
spacesBetween, extras := spaces / (len(currWords) - 1), spaces % (len(currWords) - 1);
16+
for i := 0; i < extras; i++ {
17+
currWords[i] += " ";
18+
}
19+
result = append(result, strings.Join(currWords, strings.Repeat(" ", spacesBetween)));
20+
}
21+
currWords = make([]string, 0);
22+
currWordsLen = 0;
23+
}
24+
currWords = append(currWords, word);
25+
currWordsLen += len(word);
26+
}
27+
result = append(result, strings.Join(currWords, " ") + strings.Repeat(" ", k - currWordsLen - len(currWords) + 1));
28+
return result;
529
}

leetcode/1-100/0068.Text-Justification/Solution_test.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,38 @@ func TestSolution(t *testing.T) {
99
// 测试用例
1010
cases := []struct {
1111
name string
12-
inputs bool
13-
expect bool
12+
words []string
13+
k int
14+
expect []string
1415
}{
15-
{"TestCacse 1", true, true},
16-
{"TestCacse 1", true, true},
17-
{"TestCacse 1", false, false},
16+
{"TestCase", []string{"This", "is", "an", "example", "of", "text", "justification."}, 16, []string{
17+
"This is an",
18+
"example of text",
19+
"justification. ",
20+
}},
21+
{"TestCase", []string{"What","must","be","acknowledgment","shall","be"}, 16, []string{
22+
"What must be",
23+
"acknowledgment ",
24+
"shall be ",
25+
}},
26+
{"TestCase", []string{"Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"}, 20,
27+
[]string{
28+
"Science is what we",
29+
"understand well",
30+
"enough to explain to",
31+
"a computer. Art is",
32+
"everything else we",
33+
"do ",
34+
}},
1835
}
1936

2037
// 开始测试
2138
for _, c := range cases {
2239
t.Run(c.name, func(t *testing.T) {
23-
ret := Solution(c.inputs)
40+
ret := Solution(c.words, c.k)
2441
if !reflect.DeepEqual(ret, c.expect) {
25-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26-
c.expect, ret, c.inputs)
42+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
43+
c.expect, ret, c.words, c.k)
2744
}
2845
})
2946
}

leetcode/1-100/0068.text-justification.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,54 @@
22

33
## Description
44

5-
Given two binary strings, return their sum \(also a binary string\).
5+
Given an array of words and a width _maxWidth_, format the text such that each line has exactly _maxWidth_ characters and is fully (left and right) justified.
66

7-
The input strings are both **non-empty** and contains only characters `1` or `0`.
7+
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces `' '` when necessary so that each line has exactly _maxWidth_ characters.
8+
9+
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
10+
11+
For the last line of text, it should be left justified and no **extra** space is inserted between words.
812

913
**Example 1:**
1014

1115
```text
12-
Input: a = "11", b = "1"
13-
Output: "100"
16+
Input: words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
17+
Output:
18+
[
19+
"This is an",
20+
"example of text",
21+
"justification. "
22+
]
1423
```
1524

1625
**Example 2:**
1726

1827
```text
19-
Input: a = "1010", b = "1011"
20-
Output: "10101"
28+
Input: words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
29+
Output:
30+
[
31+
"What must be",
32+
"acknowledgment ",
33+
"shall be "
34+
]
35+
```
36+
37+
**Example 3:**
38+
39+
```text
40+
Input: words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"], maxWidth = 20
41+
Output:
42+
[
43+
"Science is what we",
44+
"understand well",
45+
"enough to explain to",
46+
"a computer. Art is",
47+
"everything else we",
48+
"do "
49+
]
2150
```
2251

23-
**Tags:** Math, String
52+
**Tags:** String, Simulation
2453

2554
## 题意
2655

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "math"
4+
5+
func Solution(root *TreeNode) int {
6+
res := math.MinInt32;
7+
helper(root, &res);
8+
return res
9+
}
10+
11+
func helper(root *TreeNode, res *int) int {
12+
if root == nil {
13+
return 0;
14+
}
15+
left := helper(root.Left, res);
16+
right := helper(root.Right, res);
17+
*res = max(*res, root.Val, left + root.Val, root.Val + right, left + root.Val + right);
18+
return max(left + root.Val, root.Val + right, root.Val);
519
}
20+
21+
func max(nums ...int) int {
22+
maxVal := nums[0];
23+
for i := 1; i < len(nums); i++ {
24+
if nums[i] > maxVal {
25+
maxVal = nums[i];
26+
}
27+
}
28+
return maxVal;
29+
}

leetcode/101-200/0124.Binary-Tree-Maximum-Path-Sum/Solution_test.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,32 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
root *TreeNode
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase", &TreeNode{
17+
Val: 1,
18+
Left: &TreeNode{Val: 2},
19+
Right: &TreeNode{Val: 3},
20+
}, 6},
21+
{"TestCase", &TreeNode{
22+
-10,
23+
&TreeNode{Val: 9},
24+
&TreeNode{
25+
20,
26+
&TreeNode{Val: 15},
27+
&TreeNode{Val: 7},
28+
},
29+
}, 42},
1930
}
2031

2132
// 开始测试
2233
for i, c := range cases {
2334
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
35+
got := Solution(c.root)
2536
if !reflect.DeepEqual(got, c.expect) {
2637
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
38+
c.expect, got, c.root)
2839
}
2940
})
3041
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Solution
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}

leetcode/101-200/0124.binary-tree-maximum-path-sum.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
11
# 0124.Binary-Tree-Maximum-Path-Sum
22

3-
> \[!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)
4-
53
## Description
64

5+
A **path** in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence **at most once**. Note that the path does not need to pass through the root.
6+
7+
The **path sum** of a path is the sum of the node's values in the path.
8+
9+
Given the `root` of a binary tree, return _the maximum **path sum** of any path_.
10+
11+
**Example 1:**
12+
13+
```text
14+
1
15+
/ \
16+
2 3
17+
Input: root = [1,2,3]
18+
Output: 6
19+
```
20+
721
**Example 1:**
822

923
```text
10-
Input: a = "11", b = "1"
11-
Output: "100"
24+
-10
25+
/ \
26+
9 20
27+
/ \
28+
15 7
29+
Input: root = [-10,9,20,null,null,15,7]
30+
Output: 42
1231
```
1332

33+
**Tags:** Dynamic-Programming, Tree, DFS, BinaryTree
34+
1435
## 题意
1536

1637
> ...

0 commit comments

Comments
 (0)