diff options
| author | DevSanti12 <santiagoleyva2013@gmail.com> | 2025-01-19 17:37:10 -0500 |
|---|---|---|
| committer | DevSanti12 <santiagoleyva2013@gmail.com> | 2025-01-19 17:37:10 -0500 |
| commit | 72cf87713e575f50e347d79f688476f4a105ab5f (patch) | |
| tree | b8c6b2cb8bbf1e815412cfb3026b88f291afc3f8 | |
| parent | 3aa4260caf45fc1b7deea5261df4ba7c71e02c6b (diff) | |
| download | perlweeklychallenge-club-72cf87713e575f50e347d79f688476f4a105ab5f.tar.gz perlweeklychallenge-club-72cf87713e575f50e347d79f688476f4a105ab5f.tar.bz2 perlweeklychallenge-club-72cf87713e575f50e347d79f688476f4a105ab5f.zip | |
Added perl challenges for 302
| -rw-r--r-- | challenge-302/santiago-leyva/perl/ch-01.pl | 80 | ||||
| -rw-r--r-- | challenge-302/santiago-leyva/perl/ch-02.pl | 50 |
2 files changed, 130 insertions, 0 deletions
diff --git a/challenge-302/santiago-leyva/perl/ch-01.pl b/challenge-302/santiago-leyva/perl/ch-01.pl new file mode 100644 index 0000000000..b2f58d3d84 --- /dev/null +++ b/challenge-302/santiago-leyva/perl/ch-01.pl @@ -0,0 +1,80 @@ +=begin +You are given an array of binary strings, @str, and two integers, $x and $y. + +Write a script to return the size of the largest subset of @str such that there are at most $x 0’s and $y 1’s in the subset. + +A set m is a subset of n if all elements of m are also elements of n. +Example 1 +Input: @str = ("10", "0001", "111001", "1", "0") + $x = 5 + $y = 3 +Output: 4 + +The largest subset with at most five 0's and three 1's: +("10", "0001", "1", "0") +Example 2 +Input: @str = ("10", "1", "0") + $x = 1 + $y = 1 +Output: 2 + +The largest subset with at most one 0's and one 1's: +("1", "0") +=cut + +use strict; +use warnings; + +sub findMaxForm { + my ($strs, $m, $n) = @_; + + # Get the size of the strs array + my $sz = scalar @$strs; + + # Initialize the DP table with 3D array + my @f; + for my $i (0..$sz) { + for my $j (0..$m) { + for my $k (0..$n) { + $f[$i][$j][$k] = 0; + } + } + } + + # Loop over all strings + for my $i (1..$sz) { + my $s = $strs->[$i - 1]; # Get the i-th string (1-indexed in loop) + my $a = ($s =~ tr/0//); # Count of '0's + my $b = ($s =~ tr/1//); # Count of '1's + + # Update the DP table + for my $j (0..$m) { + for my $k (0..$n) { + $f[$i][$j][$k] = $f[$i - 1][$j][$k]; # Not including the current string + if ($j >= $a && $k >= $b) { + # Include the current string if we have enough 0's and 1's + $f[$i][$j][$k] = max($f[$i][$j][$k], $f[$i - 1][$j - $a][$k - $b] + 1); + } + } + } + } + + return $f[$sz][$m][$n]; # Return the result stored in the bottom-right corner of the DP table +} + +# Helper function to get the maximum of two values +sub max { + my ($a, $b) = @_; + return $a > $b ? $a : $b; +} + +# Example usage +my @str1 = ("10", "0001", "111001", "1", "0"); +my $m1 = 5; +my $n1 = 3; +print findMaxForm(\@str1, $m1, $n1), "\n"; # Expected Output: 4 + +my @str2 = ("10", "1", "0"); +my $m2 = 1; +my $n2 = 1; +print findMaxForm(\@str2, $m2, $n2), "\n"; # Expected Output: 2
\ No newline at end of file diff --git a/challenge-302/santiago-leyva/perl/ch-02.pl b/challenge-302/santiago-leyva/perl/ch-02.pl new file mode 100644 index 0000000000..4f1102216c --- /dev/null +++ b/challenge-302/santiago-leyva/perl/ch-02.pl @@ -0,0 +1,50 @@ +=begin +You are given an array of integers, @ints. + +Write a script to find the minimum positive start value such that step by step sum is never less than one. + +Example 1 +Input: @ints = (-3, 2, -3, 4, 2) +Output: 5 + +For start value 5. +5 + (-3) = 2 +2 + (+2) = 4 +4 + (-3) = 1 +1 + (+4) = 5 +5 + (+2) = 7 +Example 2 +Input: @ints = (1, 2) +Output: 1 +Example 3 +Input: @ints = (1, -2, -3) +Output: 5 +=cut + +use strict; +use warnings; +use List::Util qw(min); + +my @nums = ([-3, 2, -3, 4, 2],[1, 2],[1, -2, -3]); + +foreach(@nums){ + my @arr = @$_; + my $result = findStart(\@arr); + print "For ",join(" ",@arr)," the start value is -> $result \n"; +} + +sub findStart { + my @A = @$_; + + my $s = 0; # Initialize the cumulative sum + my $t = 'inf'; # Initialize the minimum sum encountered + + for my $i (@A) { + $s += $i; # Update the cumulative sum + #print "$s\n"; + $t = min($t, $s); # Track the minimum cumulative sum + print "$t\n"; + } + + return $t < 0 ? 1 - $t : 1; # Return max(1, 1 - minimum cumulative sum) +}
\ No newline at end of file |
