Skip to content

Commit 619e40b

Browse files
committed
Add backspace string compare and maximum ascending subarray sum
1 parent 171e10d commit 619e40b

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

Easy/backspace-string-compare.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Code
2+
====
3+
4+
```go
5+
func backspaceCompare(s string, t string) bool {
6+
sol := []string{}
7+
strs := []string{s, t}
8+
9+
for _, str := range strs {
10+
newStr := ""
11+
for _, c := range str {
12+
if string(c) == "#" {
13+
if len(newStr) > 0 {
14+
newStr = newStr[:len(newStr)-1]
15+
}
16+
} else {
17+
newStr = newStr + string(c)
18+
}
19+
}
20+
21+
sol = append(sol, newStr)
22+
}
23+
24+
return sol[0] == sol[1]
25+
}
26+
```
27+
28+
Solution in mind
29+
================
30+
31+
- We iterate through characters of a string and maintain a new string (`newStr`) which has the updated string post backspaces.
32+
33+
- If a `#` is encountered as the character, we simply check if `newStr` has any characters in it and remove the last character. If empty, we simply continue.
34+
35+
- In the case where the encountered character is not `#`, we simply append it as a suffix to `newStr`
36+
37+
- After both strings have been parsed, we compare the new strings for equality.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Code
2+
====
3+
4+
```go
5+
func sum(nums []int) int {
6+
sum := 0
7+
for _, num := range nums {
8+
sum += num
9+
}
10+
11+
return sum
12+
}
13+
14+
func maxAscendingSum(nums []int) int {
15+
window := []int{}
16+
windowSum := 0
17+
prevNum := 0
18+
19+
for _, num := range nums {
20+
if num > prevNum {
21+
window = append(window, num)
22+
prevNum = num
23+
} else {
24+
s := sum(window)
25+
if s > windowSum {
26+
windowSum = s
27+
}
28+
prevNum = num
29+
window = []int{num}
30+
}
31+
}
32+
33+
s := sum(window)
34+
if s > windowSum {
35+
windowSum = s
36+
}
37+
38+
return windowSum
39+
}
40+
```
41+
42+
Solution in mind
43+
================
44+
45+
- We iterate through elements of the `nums` array, while keeping track of `window` and `prevNum`. Here, `window` holds the subsequence of the array that is ascending.
46+
47+
- As and when we encounter a number, we check if it is greater than the previous encountered number. If it is, the `window` grows in size (we append to `window`).
48+
49+
- If it is not, we calculate the sum of the existing window and check if it is greater than any previously encountered sum (`windowSum`) and update the sum accordingly.
50+
51+
- After updating the window sum, we reset the window to have only the latest encountered number in it, as the window is no longer increasing.
52+
53+
- After completion of iterations through elements, we return windowSum

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Link to profile, [bhargavsnv100](https://leetcode.com/bhargavsnv100/)
77

88
| [Easy](#easy) | [Medium](#medium) | [Hard](#hard) |
99
|---------------|-------------------|---------------|
10-
| 42 | 69 | 12 |
10+
| 44 | 69 | 12 |
1111

1212
Problems finished
1313
=================
@@ -16,6 +16,7 @@ Easy
1616
----
1717

1818
- [average-of-levels-in-binary-tree.md](Easy/average-of-levels-in-binary-tree.md)
19+
- [backspace-string-compare.md](Easy/backspace-string-compare.md)
1920
- [best-time-to-buy-and-sell-stock.md](Easy/best-time-to-buy-and-sell-stock.md)
2021
- [binary-tree-level-order-traversal-ii.md](Easy/binary-tree-level-order-traversal-ii.md)
2122
- [binary-tree-paths.md](Easy/binary-tree-paths.md)
@@ -32,6 +33,7 @@ Easy
3233
- [license-key-formatting.md](Easy/license-key-formatting.md)
3334
- [longest-common-prefix.md](Easy/longest-common-prefix.md)
3435
- [majority-element.md](Easy/majority-element.md)
36+
- [maximum-ascending-subarray-sum.md](Easy/maximum-ascending-subarray-sum.md)
3537
- [maximum-depth-of-binary-tree.md](Easy/maximum-depth-of-binary-tree.md)
3638
- [maximum-depth-of-n-ary-tree.md](Easy/maximum-depth-of-n-ary-tree.md)
3739
- [merge-sorted-array.md](Easy/merge-sorted-array.md)

0 commit comments

Comments
 (0)