Given an array of integers arr
, return true
if the number of occurrences of each value in the array is unique or false
otherwise.
Example 1:
Input: arr = [1,2,2,1,1,3] Output: true Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.
Example 2:
Input: arr = [1,2] Output: false
Example 3:
Input: arr = [-3,0,1,-3,1,1,1,-3,10,0] Output: true
Constraints:
1 <= arr.length <= 1000
-1000 <= arr[i] <= 1000
program main
implicit none
integer, parameter :: n = 10
integer, dimension(n) :: arr
logical :: result
! Example 1
arr = [1, 2, 2, 1, 1, 3]
write (*, '(A)') 'Example 1:'
write (*, '(A)') 'Input: ', arr
result = uniqueOccurrences(arr)
write (*, '(A, L1)') 'Output: ', result
write (*, *)
! Example 2
arr = [1, 2]
write (*, '(A)') 'Example 2:'
write (*, '(A)') 'Input: ', arr
result = uniqueOccurrences(arr)
write (*, '(A, L1)') 'Output: ', result
write (*, *)
! Example 3
arr = [-3, 0, 1, -3, 1, 1, 1, -3, 10, 0]
write (*, '(A)') 'Example 3:'
write (*, '(A)') 'Input: ', arr
result = uniqueOccurrences(arr)
write (*, '(A, L1)') 'Output: ', result
write (*, *)
contains
function uniqueOccurrences(arr) result(result_)
implicit none
integer, dimension(:), intent(in) :: arr
logical :: result_
integer :: i, j
integer :: count
do i = 1, size(arr) - 1
count = 0
do j = i + 1, size(arr)
if (arr(i) == arr(j)) then
count = count + 1
end if
end do
if (count > 0) then
result_ = .false.
return
end if
end do
result_ = .true.
end function uniqueOccurrences
end program main
temp.f95:9:4: 9 | arr = [1, 2, 2, 1, 1, 3] | 1 Error: Different shape for array assignment at (1) on dimension 1 (10 and 6) temp.f95:17:4: 17 | arr = [1, 2] | 1 Error: Different shape for array assignment at (1) on dimension 1 (10 and 2)
module UniqueOccurrences
implicit none
contains
function uniqueOccurrences(arr) result(output)
integer, intent(in) :: arr(:)
logical :: output
integer :: i, count
output = .true.
do i = 1, size(arr)
count = countOccurrences(arr, arr(i))
if (count > 1) then
output = .false.
exit
end if
end do
end function uniqueOccurrences
function countOccurrences(arr, value) result(count)
integer, intent(in) :: arr(:), value
integer :: count
count = count(arr == value)
end function countOccurrences
end module UniqueOccurrences
program test
use UniqueOccurrences
implicit none
integer, parameter :: N = 10
integer :: arr(N)
arr = [1, 2, 2, 1, 1, 3, -1, -1, -1, -1]
if (uniqueOccurrences(arr)) then
print *, "Unique occurrences"
else
print *, "Non-unique occurrences"
end if
arr = [1, 2]
if (uniqueOccurrences(arr)) then
print *, "Unique occurrences"
else
print *, "Non-unique occurrences"
end if
arr = [-3, 0, 1, -3, 1, 1, 1, -3, 10, 0]
if (uniqueOccurrences(arr)) then
print *, "Unique occurrences"
else
print *, "Non-unique occurrences"
end if
end program test
temp.f95:7:26: 7 | function uniqueOccurrences(arr) result(output) | 1 Error: MODULE attribute of ‘uniqueoccurrences’ conflicts with PROCEDURE attribute at (1) temp.f95:9:29: 9 | integer, intent(in) :: arr(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:17: 10 | logical :: output | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:19: 12 | integer :: i, count | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:14:15: 14 | output = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:19: 16 | do i = 1, size(arr) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:18:37: 18 | count = countOccurrences(arr, arr(i)) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:19: 20 | if (count > 1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:22:16: 22 | output = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:24:4: 24 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:26:3: 26 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:3: 28 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:30:3: 30 | end function uniqueOccurrences | 1 Error: Expecting END MODULE statement at (1) temp.f95:45:5: 45 | use UniqueOccurrences | 1 Fatal Error: Cannot open module file ‘uniqueoccurrences.mod’ for reading at (1): No such file or directory compilation terminated.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def delNodes(root, to_delete):
forest = []
to_delete_set = set(to_delete)
helper(root, True, to_delete_set, forest)
return forest
def helper(node, isRoot, to_delete, forest):
if not node:
return None
deleted = node.val in to_delete
if isRoot and not deleted:
forest.append(node)
node.left = helper(node.left, deleted, to_delete, forest)
node.right = helper(node.right, deleted, to_delete, forest)
return None if deleted else node
The algorithm performs a post-order traversal, visiting each node of the tree. It starts with the root of the tree and checks whether it's a root node or not. If it's a root node, it adds the node to the forest; otherwise, it continues traversal. Left and right children are updated for each node. If a node's value is found in the `to_delete` list, this function returns nullptr (Python: None, JavaScript: null, Java: null) otherwise, it returns the current node. This way, the tree nodes will be combined with their new parents after the deletion, and the forest will have the roots of the disjoint trees.
#include <vector>
#include <set>
#include <memory>
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
std::vector<TreeNode*> delNodes(TreeNode* root, std::vector<int>& to_delete) {
std::vector<TreeNode*> forest;
std::set<int> toDelete(to_delete.begin(), to_delete.end());
helper(root, true, toDelete, forest);
return forest;
}
TreeNode* helper(TreeNode* node, bool isRoot, std::set<int>& toDelete, std::vector<TreeNode*>& forest) {
if (!node) return nullptr;
bool deleted = toDelete.count(node->val);
if (isRoot && !deleted) forest.push_back(node);
node->left = helper(node->left, deleted, toDelete, forest);
node->right = helper(node->right, deleted, toDelete, forest);
return deleted ? nullptr : node;
}
The algorithm performs a post-order traversal, visiting each node of the tree. It starts with the root of the tree and checks whether it's a root node or not. If it's a root node, it adds the node to the forest; otherwise, it continues traversal. Left and right children are updated for each node. If a node's value is found in the `to_delete` list, this function returns nullptr (Python: None, JavaScript: null, Java: null) otherwise, it returns the current node. This way, the tree nodes will be combined with their new parents after the deletion, and the forest will have the roots of the disjoint trees.