You are given an integer array nums
. The unique elements of an array are the elements that appear exactly once in the array.
Return the sum of all the unique elements of nums
.
Example 1:
Input: nums = [1,2,3,2] Output: 4 Explanation: The unique elements are [1,3], and the sum is 4.
Example 2:
Input: nums = [1,1,1,1,1] Output: 0 Explanation: There are no unique elements, and the sum is 0.
Example 3:
Input: nums = [1,2,3,4,5] Output: 15 Explanation: The unique elements are [1,2,3,4,5], and the sum is 15.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
program main
implicit none
integer :: nums(5)
nums = [1, 2, 3, 2, 1]
print '(I0)', sum_unique_elements(nums)
nums = [1, 1, 1, 1, 1]
print '(I0)', sum_unique_elements(nums)
nums = [1, 2, 3, 4, 5]
print '(I0)', sum_unique_elements(nums)
contains
function sum_unique_elements(nums) result(sum)
implicit none
integer, intent(in) :: nums(:)
integer :: sum, i, count
sum = 0
do i = 1, size(nums)
count = 0
do j = 1, size(nums)
if (nums(i) == nums(j)) then
count = count + 1
end if
end do
if (count == 1) then
sum = sum + nums(i)
end if
end do
end function sum_unique_elements
end program main
temp.f95:25:16: 25 | do j = 1, size(nums) | 1 Error: Symbol ‘j’ at (1) has no IMPLICIT type
module SumUniqueElements
implicit none
private
public :: sumUniqueElements
contains
function sumUniqueElements(nums) result(sum)
integer, intent(in) :: nums(:)
integer :: sum
integer :: i, j, count
logical :: found
sum = 0
do i = 1, size(nums)
found = .false.
do j = 1, i - 1
if (nums(i) == nums(j)) then
found = .true.
exit
end if
end do
if (.not. found) then
count = 1
do j = i + 1, size(nums)
if (nums(i) == nums(j)) then
count = count + 1
end if
end do
if (count == 1) then
sum = sum + nums(i)
end if
end if
end do
end function sumUniqueElements
end module SumUniqueElements
program test_sumUniqueElements
use SumUniqueElements
implicit none
integer, parameter :: nums1(4) = [1, 2, 3, 2]
integer, parameter :: nums2(5) = [1, 1, 1, 1, 1]
integer, parameter :: nums3(5) = [1, 2, 3, 4, 5]
integer :: sum
sum = sumUniqueElements(nums1)
write (*,*) "Sum of unique elements in nums1:", sum
sum = sumUniqueElements(nums2)
write (*,*) "Sum of unique elements in nums2:", sum
sum = sumUniqueElements(nums3)
write (*,*) "Sum of unique elements in nums3:", sum
end program test_sumUniqueElements
temp.f95:4:31: 4 | public :: sumUniqueElements | 1 Error: PUBLIC attribute applied to MODULE sumuniqueelements at (1) temp.f95:8:30: 8 | function sumUniqueElements(nums) result(sum) | 1 Error: MODULE attribute of ‘sumuniqueelements’ conflicts with PROCEDURE attribute at (1) temp.f95:9:38: 9 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:22: 10 | integer :: sum | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:30: 11 | integer :: i, j, count | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:24: 12 | logical :: found | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:14:15: 14 | sum = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:28: 15 | do i = 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:16:27: 16 | found = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:27: 17 | do j = 1, i - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:18:44: 18 | if (nums(i) == nums(j)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:19:34: 19 | found = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:24: 20 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:21:19: 21 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:15: 22 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:23:33: 23 | if (.not. found) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:24:25: 24 | count = 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:25:40: 25 | do j = i + 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:26:48: 26 | if (nums(i) == nums(j)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:27:41: 27 | count = count + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:28:23: 28 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:29:19: 29 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:30:36: 30 | if (count == 1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:31:39: 31 | sum = sum + nums(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:32:19: 32 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:33:15: 33 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:34:11: 34 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:35:7: 35 | end function sumUniqueElements | 1 Error: Expecting END MODULE statement at (1) temp.f95:39:9: 39 | use SumUniqueElements | 1 Fatal Error: Cannot open module file ‘sumuniqueelements.mod’ for reading at (1): No such file or directory compilation terminated.
def bestTeamScore(scores, ages):
players = sorted(zip(ages, scores))
n = len(players)
dp = [0] * n
best_score = 0
for i in range(n):
dp[i] = players[i][1]
for j in range(i):
if players[i][1] >= players[j][1]:
dp[i] = max(dp[i], dp[j] + players[i][1])
best_score = max(best_score, dp[i])
return best_score
1. First, we create a new array/list (`players`) of pairs containing the players' ages and scores.
players
array/list by age, followed by sorting by scores within the same age.dp
array/list of length n
(number of players) initializing it with 0.players
array/list:
a. Set dp[i]
to the current player's score.
b. Iterate through all previous players (0 to i-1) and check if the current player's score is greater or equal to the previous player's score. If true, set dp[i]
to the maximum value between existing dp[i]
and the sum of that previous player's DP value plus the current player's score.
c. Update the best_score
with the maximum of its current value and dp[i]
for each player.best_score
which represents the highest overall score of all possible basketball teams without conflicts.#include <vector>
#include <algorithm>
using namespace std;
int bestTeamScore(vector<int>& scores, vector<int>& ages) {
int n = scores.size();
vector<pair<int, int>> players(n);
for (int i = 0; i < n; ++i) {
players[i] = {ages[i], scores[i]};
}
sort(players.begin(), players.end());
vector<int> dp(n);
int best_score = 0;
for (int i = 0; i < n; ++i) {
dp[i] = players[i].second;
for (int j = 0; j < i; ++j) {
if (players[i].second >= players[j].second) {
dp[i] = max(dp[i], dp[j] + players[i].second);
}
}
best_score = max(best_score, dp[i]);
}
return best_score;
}
1. First, we create a new array/list (`players`) of pairs containing the players' ages and scores.
players
array/list by age, followed by sorting by scores within the same age.dp
array/list of length n
(number of players) initializing it with 0.players
array/list:
a. Set dp[i]
to the current player's score.
b. Iterate through all previous players (0 to i-1) and check if the current player's score is greater or equal to the previous player's score. If true, set dp[i]
to the maximum value between existing dp[i]
and the sum of that previous player's DP value plus the current player's score.
c. Update the best_score
with the maximum of its current value and dp[i]
for each player.best_score
which represents the highest overall score of all possible basketball teams without conflicts.