Skip to content

Commit

Permalink
feat: Solution for Problem Statement #06 - Image Overlap
Browse files Browse the repository at this point in the history
  • Loading branch information
milanpanchal committed Sep 7, 2020
1 parent 8ad95f3 commit 5ba7a74
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ Learning & Solving Problems on LeetCode
* [210_CourseSchedule-II](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/210_CourseSchedule-II.playground/Contents.swift)
* [211_AddAndSearchWord_DataStructureDesign](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/211_AddAndSearchWord_DataStructureDesign.playground/Contents.swift)
* [215_KthLargestElementInAnArray](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/215_KthLargestElementInAnArray.playground/Contents.swift)

* [220_ContainsDuplicate-III](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/220_ContainsDuplicate-III.playground/Contents.swift)

* [222_CountCompleteTreeNodes](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/222_CountCompleteTreeNodes.playground/Contents.swift)

* [258_AddDigits](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/258_AddDigits.playground/Contents.swift)
* [260_SingleNumber-III](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/260_SingleNumber-III.playground/Contents.swift)
* [264_UglyNumber-II](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/264_UglyNumber-II.playground/Contents.swift)
Expand Down Expand Up @@ -92,6 +96,8 @@ Learning & Solving Problems on LeetCode

* [450_DeleteNodeInABST](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/450_DeleteNodeInABST.playground/Contents.swift)

* [459_RepeatedSubstringPattern](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/459_RepeatedSubstringPattern.playground/Contents.swift)

* [463_IslandPerimeter](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/463_IslandPerimeter.playground/Contents.swift)

* [470_ImplementRand10()UsingRand7()](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/470_ImplementRand10()UsingRand7().playground/Contents.swift)
Expand All @@ -112,8 +118,12 @@ Learning & Solving Problems on LeetCode

* [709_ToLowerCase](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/709_ToLowerCase.playground/Contents.swift)

* [763_PartitionLabels](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/763_PartitionLabels.playground/Contents.swift)

* [797_AllPathsFromSourceToTarget](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/797_AllPathsFromSourceToTarget.playground/Contents.swift)

* [835_Image_Overlap](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/835_Image_Overlap.playground/Contents.swift)

* [917_ReverseOnlyLetters](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/917_ReverseOnlyLetters.playground/Contents.swift)

* [952_LargestComponentSizeByCommonFactor](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/952_LargestComponentSizeByCommonFactor.playground.playground/Contents.swift)
Expand All @@ -132,6 +142,8 @@ Learning & Solving Problems on LeetCode

* [1032_StreamOfCharacters](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/1032_StreamOfCharacters.playground.playground/Contents.swift)

* [1305_AllElementsInTwoBinarySearchTrees](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/1305_AllElementsInTwoBinarySearchTrees.playground/Contents.swift)

* [1344_AngleBetweenHandsOfAClock](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/1344_AngleBetweenHandsOfAClock.playground/Contents.swift)

* [1470_ShuffleTheArray](https://github.com/milanpanchal/LeetCode/tree/master/Swift/Problems/1470_ShuffleTheArray.playground/Contents.swift)
Expand Down
70 changes: 70 additions & 0 deletions Swift/Problems/835_Image_Overlap.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*:
# 835. Image Overlap [Medium]
https://leetcode.com/problems/image-overlap/

---

### Problem Statement:
Two images `A` and `B` are given, represented as binary, square matrices of the same size. (A binary matrix has only 0s and 1s as values.)

We translate one image however we choose (sliding it left, right, up, or down any number of units), and place it on top of the other image. After, the overlap of this translation is the number of positions that have a 1 in both images.

(Note also that a translation does not include any kind of rotation.)

What is the largest possible overlap?


### Example:

```
Input: A = [[1,1,0],
[0,1,0],
[0,1,0]]
B = [[0,0,0],
[0,1,1],
[0,0,1]]
Output: 3
Explanation: We slide A to right by 1 unit and down by 1 unit.

```

### Constraints:
+ 1 <= A.length = A[0].length = B.length = B[0].length <= 30
+ 0 <= A[i][j], B[i][j] <= 1

*/


import UIKit

// 49 / 49 test cases passed.
// Status: Accepted
// Runtime: 172 ms
// Memory Usage: 20.9 MB

class Solution {

func largestOverlap(_ A: [[Int]], _ B: [[Int]]) -> Int {
return max(findMax(A, B), findMax(B, A))
}

private func findMax(_ a: [[Int]], _ b: [[Int]]) -> Int {
var result = 0
let aCount = a.count
for i1 in 0..<aCount {
for j1 in 0..<aCount {
var temp = 0
for i2 in i1..<aCount {
for j2 in j1..<aCount where a[i2 - i1][j2 - j1] == 1 && b[i2][j2] == 1 {
temp += 1
}
}
result = max(result, temp)
}
}
return result
}
}

let sol = Solution()
sol.largestOverlap([[1,1,0],[0,1,0],[0,1,0]], [[0,0,0],[0,1,1],[0,0,1]]) // 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
</playground>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*:
# 06. Image Overlap
https://leetcode.com/explore/challenge/card/september-leetcoding-challenge/554/week-1-september-1st-september-7th/3450/

---

### Problem Statement:
Two images `A` and `B` are given, represented as binary, square matrices of the same size. (A binary matrix has only 0s and 1s as values.)

We translate one image however we choose (sliding it left, right, up, or down any number of units), and place it on top of the other image. After, the overlap of this translation is the number of positions that have a 1 in both images.

(Note also that a translation does not include any kind of rotation.)

What is the largest possible overlap?


### Example:

```
Input: A = [[1,1,0],
[0,1,0],
[0,1,0]]
B = [[0,0,0],
[0,1,1],
[0,0,1]]
Output: 3
Explanation: We slide A to right by 1 unit and down by 1 unit.

```

### Constraints:
+ 1 <= A.length = A[0].length = B.length = B[0].length <= 30
+ 0 <= A[i][j], B[i][j] <= 1

*/


import UIKit

// 49 / 49 test cases passed.
// Status: Accepted
// Runtime: 172 ms
// Memory Usage: 20.9 MB

class Solution {

func largestOverlap(_ A: [[Int]], _ B: [[Int]]) -> Int {
return max(findMax(A, B), findMax(B, A))
}

private func findMax(_ a: [[Int]], _ b: [[Int]]) -> Int {
var result = 0
let aCount = a.count
for i1 in 0..<aCount {
for j1 in 0..<aCount {
var temp = 0
for i2 in i1..<aCount {
for j2 in j1..<aCount where a[i2 - i1][j2 - j1] == 1 && b[i2][j2] == 1 {
temp += 1
}
}
result = max(result, temp)
}
}
return result
}
}

let sol = Solution()
sol.largestOverlap([[1,1,0],[0,1,0],[0,1,0]], [[0,0,0],[0,1,1],[0,0,1]]) // 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
</playground>

0 comments on commit 5ba7a74

Please sign in to comment.