-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package day198 | ||
|
||
import ( | ||
"sort" | ||
) | ||
|
||
// LargestSubsetPairs finds the largest subset | ||
// such that every pair of elements in the subset (i, j) | ||
// satisfies either i % j = 0 or j % i = 0. | ||
// Runs in O(N^2) time | ||
func LargestSubsetPairs(nums []int) []int { | ||
sort.Slice(nums, func(i, j int) bool { | ||
return nums[i] < nums[j] | ||
}) | ||
dp := make([]int, len(nums)) | ||
dp[len(dp)-1] = 1 | ||
var largestI, largestVal int | ||
for i := len(dp) - 2; i >= 0; i-- { | ||
m := 0 | ||
for j := i + 1; j < len(dp); j++ { | ||
if nums[j]%nums[i] == 0 { | ||
m = max(m, dp[j]) | ||
} | ||
} | ||
dp[i] = 1 + m | ||
if dp[i] > largestVal { | ||
largestI = i | ||
largestVal = dp[i] | ||
} | ||
} | ||
result := make([]int, 0, largestVal) | ||
for i := largestI; i < len(nums); i++ { | ||
if nums[i]%nums[largestI] == 0 { | ||
result = append(result, nums[i]) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func max(a, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package day198 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var testcases = []struct { | ||
nums, subset []int | ||
}{ | ||
{[]int{3, 5, 10, 20, 21}, []int{5, 10, 20}}, | ||
{[]int{1, 3, 6, 24}, []int{1, 3, 6, 24}}, | ||
} | ||
|
||
func TestLargestSubsetPairs(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
input := append([]int{}, tc.nums...) | ||
if result := LargestSubsetPairs(input); !reflect.DeepEqual(result, tc.subset) { | ||
t.Errorf("Expected %v, got %v", tc.subset, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkLargestSubsetPairs(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
input := append([]int{}, tc.nums...) | ||
LargestSubsetPairs(input) | ||
} | ||
} | ||
} |