You are given an integer array deck
where deck[i]
represents the number written on the ith
card.
Partition the cards into one or more groups such that:
x
cards where x > 1
, andReturn true
if such partition is possible, or false
otherwise.
Example 1:
Input: deck = [1,2,3,4,4,3,2,1] Output: true Explanation: Possible partition [1,1],[2,2],[3,3],[4,4].
Example 2:
Input: deck = [1,1,1,2,2,2,3,3] Output: false Explanation: No possible partition.
Constraints:
1 <= deck.length <= 104
0 <= deck[i] < 104
program main
implicit none
integer, parameter :: n = 8
integer, dimension(n) :: deck = (/ 1, 2, 3, 4, 4, 3, 2, 1 /)
logical :: result
print '(L1)', solve(deck)
contains
function solve(deck) result(result)
implicit none
integer, intent(in) :: deck(:)
integer :: x, i, j
logical :: found
do x = 2, size(deck)
found = .false.
do i = 1, size(deck) - x + 1
if (all(deck(i:i+x-1) == deck(i))) then
found = .true.
exit
end if
end do
if (found) then
result = .true.
return
end if
end do
result = .false.
end function solve
end program main
temp.f95:12:39: 12 | function solve(deck) result(result) | 1 Error: Function result ‘result’ at (1) has no IMPLICIT type
module partition
implicit none
contains
function can_partition(deck) result(can_part)
integer, intent(in) :: deck(:)
integer :: i, j, x, y, n, sum
logical :: can_part
! Initialize variables
n = size(deck)
sum = sum(deck)
x = 0
y = 0
can_part = .false.
! Check if the sum of the elements in the array is even
if (mod(sum, 2) /= 0) then
return
end if
! Check if there is at least one element in the array that is greater than 1
do i = 1, n
if (deck(i) > 1) then
x = deck(i)
exit
end if
end do
if (x == 0) then
return
end if
! Check if there is at least one element in the array that is less than x
do i = 1, n
if (deck(i) < x) then
y = deck(i)
exit
end if
end do
if (y == 0) then
return
end if
! Check if the sum of the elements in the array is equal to the sum of the elements in the x group
do i = 1, n
if (deck(i) == x) then
j = i
do while (j <= n)
if (deck(j) /= x) then
exit
end if
j = j + 1
end do
if (mod(j - i, x) == 0) then
can_part = .true.
return
end if
end if
end do
end function can_partition
end module partition
program main
use partition
implicit none
integer, parameter :: n = 8
integer :: deck(n) = [1, 2, 3, 4, 4, 3, 2, 1]
logical :: can_part
can_part = can_partition(deck)
if (can_part) then
print *, "Partition possible"
else
print *, "Partition not possible"
end if
end program main
temp.f95:11:5: 11 | sum = sum(deck) | 1 Error: Unclassifiable statement at (1) temp.f95:63:7: 63 | use partition | 1 Fatal Error: Cannot open module file ‘partition.mod’ for reading at (1): No such file or directory compilation terminated.
import random
from bisect import bisect_left
class Solution:
def __init__(self, rects):
self.rects = rects
self.areas = []
self.total_area = 0
for rect in rects:
area = (rect[2] - rect[0] + 1) * (rect[3] - rect[1] + 1)
self.total_area += area
self.areas.append(self.total_area)
def pick(self):
random_area = random.randint(0, self.total_area - 1)
rect_index = bisect_left(self.areas, random_area + 1)
x = random.randint(self.rects[rect_index][0], self.rects[rect_index][2])
y = random.randint(self.rects[rect_index][1], self.rects[rect_index][3])
return [x, y]
The algorithm starts by initializing the data structures: input rectangles (rects), individual areas (areas), total area (total_area), and random number generator (Random for Python and JavaScript, Random for Java, default_random_engine for C++). For every input rectangle, it calculates its area and updates the areas and total_area.
To pick a random point inside a rectangle, the algorithm performs the following steps:
This algorithm ensures that any integer point inside the space covered by one of the given rectangles is equally likely to be returned.
#include <vector>
#include <random>
class Solution {
public:
std::vector<std::vector<int>> rects;
std::vector<int> areas;
int total_area;
std::default_random_engine generator;
Solution(std::vector<std::vector<int>>& rectangles) {
rects = rectangles;
total_area = 0;
for (const auto& rect : rects) {
int area = (rect[2] - rect[0] + 1) * (rect[3] - rect[1] + 1);
total_area += area;
areas.push_back(total_area);
}
}
std::vector<int> pick() {
std::uniform_int_distribution<int> area_distribution(0, total_area - 1);
int random_area = area_distribution(generator);
int rect_index = std::lower_bound(areas.begin(), areas.end(), random_area + 1) - areas.begin();
std::uniform_int_distribution<int> x_distribution(rects[rect_index][0], rects[rect_index][2]);
std::uniform_int_distribution<int> y_distribution(rects[rect_index][1], rects[rect_index][3]);
int x = x_distribution(generator);
int y = y_distribution(generator);
return {x, y};
}
};
The algorithm starts by initializing the data structures: input rectangles (rects), individual areas (areas), total area (total_area), and random number generator (Random for Python and JavaScript, Random for Java, default_random_engine for C++). For every input rectangle, it calculates its area and updates the areas and total_area.
To pick a random point inside a rectangle, the algorithm performs the following steps:
This algorithm ensures that any integer point inside the space covered by one of the given rectangles is equally likely to be returned.