Skip to content

Commit 323fa1a

Browse files
author
Partho Biswas
committed
689. Maximum Sum of 3 Non-Overlapping Subarrays
+ 670. Maximum Swap
1 parent ef0c88a commit 323fa1a

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ I have solved quite a number of problems from several topics. See the below tabl
188188
|75| **[166. Fraction to Recurring Decimal](https://tinyurl.com/ycweccew)** | [Python](https://tinyurl.com/wu6rdaw/166_Fraction_to_Recurring_Decimal.py), [Swift](https://tinyurl.com/wuja3c4/166_Fraction_to_Recurring_Decimal.swift) | [Art 1](https://tinyurl.com/ycuah9vj) | Medium | Hate this problem. Why do companies ask this shit!!! |
189189
|75| **[311. Sparse Matrix Multiplication](https://tinyurl.com/y9lapcjx)** | [Python](https://tinyurl.com/wu6rdaw/311_Sparse_Matrix_Multiplication.py), [Swift](https://tinyurl.com/wuja3c4/311_Sparse_Matrix_Multiplication.swift) | [Art 1](https://tinyurl.com/ycv24vc4), [Art 2](https://tinyurl.com/ycmqcfw9), [Art 3](https://tinyurl.com/y9az7nef), **[Art 4](https://tinyurl.com/y84lwkya)** | Medium | Very tricky |
190190
|76| **[896. Monotonic Array](https://tinyurl.com/y8a95fb6)** | [Python](https://tinyurl.com/wu6rdaw/896_Monotonic_Array.py), [Swift](https://tinyurl.com/wuja3c4/896_Monotonic_Array.swift) | --- | Eassy | |
191+
|77| **[670. Maximum Swap](https://tinyurl.com/y2zhdd33)** | [Python](https://tinyurl.com/wu6rdaw/670_Maximum_Swap.py), [Swift](https://tinyurl.com/wuja3c4/670_Maximum_Swap.swift) | --- | Medium | |
191192

192193

193194
</p>
@@ -726,6 +727,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
726727
|53| **[1477. Find Two Non-overlapping Sub-arrays Each With Target Sum](https://tinyurl.com/ybte7ydl)** | [Python](https://tinyurl.com/wu6rdaw/1477_Find_Two_Non_overlapping_Sub_arrays_Each_With_Target_Sum.py), [Swift](https://tinyurl.com/wuja3c4/1477_Find_Two_Non_overlapping_Sub_arrays_Each_With_Target_Sum.swift)| [Art 1](https://tinyurl.com/y7rshs2j), [Art 2](https://tinyurl.com/y8x7cgxl) | Medium | **Very interesting and deceiving and tricky problem. An unique combination of sliding window and DP** |
727728
|54| **[368. Largest Divisible Subset](https://tinyurl.com/ya8sv5jr)** | [Python](https://tinyurl.com/wu6rdaw/368_Largest_Divisible_Subset.py), [Swift](https://tinyurl.com/wuja3c4/368_Largest_Divisible_Subset.swift)| Official Solution | Medium | **Interesting problem.** |
728729
|55| **[523. Continuous Subarray Sum](https://tinyurl.com/y6ce49ln)** | [Python](https://tinyurl.com/wu6rdaw/523_Continuous_Subarray_Sum.py), [Swift](https://tinyurl.com/wuja3c4/523_Continuous_Subarray_Sum.swift)| **[Art 1](https://tinyurl.com/yb3ya47s)** | Medium | **Tricky to see the DP** |
730+
|56| **[689. Maximum Sum of 3 Non-Overlapping Subarrays](https://tinyurl.com/y9nlm8tg)** | [Python](https://tinyurl.com/wu6rdaw/689_Maximum_Sum_of_3_Non_Overlapping_Subarrays.py), [Swift](https://tinyurl.com/wuja3c4/689_Maximum_Sum_of_3_Non_Overlapping_Subarrays.swift)| **[Art 1](https://tinyurl.com/__)** | Hard | **Mainly, tricly array index manipulation. TODO: need to solve it in DP and sliding window approach** |
729731

730732

731733
</p>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1+
import Foundation
2+
class Solution {
3+
func maximumSwap(_ num: Int) -> Int {
4+
var digits: [Int] = String(num).compactMap{ $0.wholeNumberValue }
5+
print("Digits: \(digits)")
6+
let sortedDigits: [Int] = digits.sorted {$0 > $1}
7+
print("Sorted: \(sortedDigits)")
8+
var numIdxMap = [Int:Int]()
9+
for (idx, item) in digits.enumerated() {
10+
numIdxMap[item] = idx
11+
}
12+
var swapIdx = 0
13+
while swapIdx < digits.count {
14+
if digits[swapIdx] != sortedDigits[swapIdx] {
15+
let targetIdx = numIdxMap[sortedDigits[swapIdx]]!
16+
print("targetIdx: \(targetIdx), swapIdx: \(targetIdx)")
17+
digits.swapAt(targetIdx, targetIdx)
18+
// (digits[swapIdx], digits[swapIdx]) = (digits[targetIdx], digits[swapIdx])
19+
break
20+
}
21+
}
22+
print("Digits: \(digits)")
23+
return Int(digits.reduce("") { $0 + String($1) }) ?? num
24+
}
25+
}
126

27+
/*
28+
29+
12345678
30+
*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Foundation
2+
class Solution {
3+
func maximumSwap(_ num: Int) -> Int {
4+
var digits: [Int] = String(num).compactMap{ $0.wholeNumberValue }
5+
print("Digits: \(digits)")
6+
let sortedDigits: [Int] = digits.sorted {$0 > $1}
7+
print("Sorted: \(sortedDigits)")
8+
var numIdxMap = [Int:Int]()
9+
for (idx, item) in digits.enumerated() {
10+
numIdxMap[item] = idx
11+
}
12+
var swapIdx = 0
13+
while swapIdx < digits.count {
14+
if digits[swapIdx] != sortedDigits[swapIdx] {
15+
let targetIdx = numIdxMap[sortedDigits[swapIdx]]!
16+
print("targetIdx: \(targetIdx), swapIdx: \(swapIdx)")
17+
digits.swapAt(targetIdx, swapIdx)
18+
break
19+
}
20+
swapIdx += 1
21+
}
22+
return Int(digits.reduce("") { $0 + String($1) }) ?? num
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Brute force approach, doing an exustive search through whole state space tree, recursively
2+
// Time limit exceeded
3+
import Foundation
4+
class Solution {
5+
func maxSumOfThreeSubarrays(_ nums: [Int], _ k: Int) -> [Int] {
6+
var sumIdxDict = [Int:[Int]]()
7+
var currentPath = [Int]()
8+
maxSumOfThreeSubarraysHelper(nums, k, 0, 0, 0, currentPath, &sumIdxDict)
9+
return sumIdxDict[sumIdxDict.keys.max()!]!
10+
}
11+
12+
func maxSumOfThreeSubarraysHelper(_ nums: [Int], _ k: Int, _ startingIdx: Int, _ currentArrayCount: Int, _ currentSum: Int, _ currentPath: [Int], _ sumIdxDict: inout [Int:[Int]]) {
13+
if currentArrayCount >= 3 {
14+
guard let previousPath = sumIdxDict[currentSum] else {
15+
sumIdxDict[currentSum] = currentPath.sorted()
16+
return
17+
}
18+
if currentPath.lexicographicallyPrecedes(previousPath) {
19+
sumIdxDict[currentSum] = currentPath.sorted()
20+
}
21+
return
22+
}
23+
if startingIdx > (nums.count - k + 1) {
24+
return
25+
}
26+
for i in startingIdx..<(nums.count - k + 1) {
27+
let subarray = Array(nums[i..<(i+k)])
28+
maxSumOfThreeSubarraysHelper(nums, k, (i+k), currentArrayCount + 1, (currentSum + subarray.reduce(0, +)), currentPath + [i], &sumIdxDict)
29+
}
30+
}
31+
}
32+
33+
34+
//-----------------------------------------------------------------------------------------------------------------------------------------
35+
36+
37+
import Foundation
38+
class Solution {
39+
func maxSumOfThreeSubarrays(_ nums: [Int], _ k: Int) -> [Int] {
40+
41+
}
42+
}
43+
44+
//-----------------------------------------------------------------------------------------------------------------------------------------
45+
46+
/*
47+
Input: [1,2,1,2,6,7,5,1], 2
48+
Output: [0, 3, 5]
49+
Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
50+
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.
51+
52+
53+
map = <totalSum:[starting indices of 3 arr]>
54+
backtracking with resursion, can be optimised with a memo. possible DP at last to try
55+
1, 2, 1,2,6,7,5 ,1
56+
57+
sumIdxDict: [
58+
5: [0, 1, 3],
59+
18: [0, 4, 6],
60+
10: [0, 2, 5],
61+
62+
15: [0, 3, 5],
63+
64+
9: [0, 2, 4],
65+
8: [0, 2, 6],
66+
4: [0, 0, 2],
67+
13: [0, 3, 6]]
68+
69+
*/
70+

0 commit comments

Comments
 (0)