diff options
-rw-r--r-- | convert_bin_ll_int.c | 30 | ||||
-rw-r--r-- | convert_bin_ll_int.py | 36 | ||||
-rw-r--r-- | max_match_players_trainers.c | 35 | ||||
-rw-r--r-- | max_match_players_trainers.py | 35 |
4 files changed, 136 insertions, 0 deletions
diff --git a/convert_bin_ll_int.c b/convert_bin_ll_int.c new file mode 100644 index 0000000..308b204 --- /dev/null +++ b/convert_bin_ll_int.c @@ -0,0 +1,30 @@ +// 1290. Convert Binary Number in a Linked List to Integer +#include "leetcode.h" + +/* + * given 'head' which is a reference node to a singly linked list. the value of + * each node in the linked list is either 0 or 1. the linked list holds the + * binary representation of a number. return the decimal value of the number in + * the linked list. the most significant bit is at the head of the linked list. + */ + +int getDecimalValue(struct ListNode *head) { + int ans = 0; + while (head) { + ans <<= 1; + ans += head->val; + head = head->next; + } + return ans; +} + +int main() { + struct ListNode *h1 = listnode_create(1); + h1->next = listnode_create(0); + h1->next->next = listnode_create(1); + struct ListNode *h2 = listnode_create(0); + printf("%d\n", getDecimalValue(h1)); // expect: 5 + printf("%d\n", getDecimalValue(h2)); // expect: 0 + listnode_free(h1); + listnode_free(h2); +} diff --git a/convert_bin_ll_int.py b/convert_bin_ll_int.py new file mode 100644 index 0000000..316dd4b --- /dev/null +++ b/convert_bin_ll_int.py @@ -0,0 +1,36 @@ +# 1290. Convert Binary Number in a Linked List to Integer + +""" +given 'head' which is a reference node to a singly linked list. the value of +each node in the linked list is either 0 or 1. the linked list holds the +binary representation of a number. return the decimal value of the number in +the linked list. the most significant bit is at the head of the linked list. +""" + + +# Definition for singly-linked list. +class ListNode(object): + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +class Solution(object): + def getDecimalValue(self, head): + """ + :type head: Optional[ListNode] + :rtype: int + """ + ans = 0 + while head: + ans = 2 * ans + head.val + head = head.next + return ans + + +if __name__ == "__main__": + obj = Solution() + h1 = ListNode(1, ListNode(0, ListNode(1))) + h2 = ListNode(0) + print(obj.getDecimalValue(h1)) + print(obj.getDecimalValue(h2)) diff --git a/max_match_players_trainers.c b/max_match_players_trainers.c new file mode 100644 index 0000000..e612986 --- /dev/null +++ b/max_match_players_trainers.c @@ -0,0 +1,35 @@ +// 2410. Maximum Matching of Players With Trainers +#include "leetcode.h" + +/* + * you are given a 0-indexed integer array 'players' where 'players[i]' + * represents the ability of the i'th player. you are also given a 0-indexed + * integer array 'trainers', where 'trainers[j]' represents the j'th capacity + * of the trainer. the i'th player can match with the j'th trainer if the + * players ability is less than or equal to the trainers training capacity. + * additionally, the i'th player can matchedwith at most one trainer, and the + * j'th trainer can be matched with at most one player. return the maximum + * number of matchings between 'players' and 'trainers' that satisfy these + * conditions. + */ + +int cmp(const void *a, const void *b) { return (*(int *)a) - (*(int *)b); } + +int matchPlayersAndTrainers(int *players, int playersSize, int *trainers, + int trainersSize) { + qsort(players, playersSize, sizeof(int), cmp); + qsort(trainers, trainersSize, sizeof(int), cmp); + int cnt = 0; + for (int i = 0; i < trainersSize && cnt < playersSize; ++i) + cnt += players[cnt] <= trainers[i]; + return cnt; +} + +int main() { + int p1[] = {4, 7, 9}, t1[] = {8, 2, 5, 8}; + int p2[] = {1, 1, 1}, t2[] = {10}; + printf("%d\n", matchPlayersAndTrainers(p1, ARRAY_SIZE(p1), t1, + ARRAY_SIZE(t1))); // expect: 2 + printf("%d\n", matchPlayersAndTrainers(p2, ARRAY_SIZE(p2), t2, + ARRAY_SIZE(t2))); // expect: 1 +} diff --git a/max_match_players_trainers.py b/max_match_players_trainers.py new file mode 100644 index 0000000..63463ff --- /dev/null +++ b/max_match_players_trainers.py @@ -0,0 +1,35 @@ +# 2410. Maximum Matching of Players With Trainers +import heapq + +""" +you are given a 0-indexed integer array 'players' where 'players[i]' +represents the ability of the i'th player. you are also given a 0-indexed +integer array 'trainers', where 'trainers[j]' represents the j'th capacity +of the trainer. the i'th player can match with the j'th trainer if the +players ability is less than or equal to the trainers training capacity. +additionally, the i'th player can matchedwith at most one trainer, and the +j'th trainer can be matched with at most one player. return the maximum +number of matchings between 'players' and 'trainers' that satisfy these +conditions. +""" + + +class Solution(object): + def matchPlayersAndTrainers(self, players, trainers): + """ + :type players: List[int] + :type trainers: List[int] + :rtype: int + """ + heapq.heapify(players) + trainers.sort() + return sum( + bool(players and players[0] <= i and heapq.heappop(players)) + for i in trainers + ) + + +if __name__ == "__main__": + obj = Solution() + print(obj.matchPlayersAndTrainers(players=[4, 7, 9], trainers=[8, 2, 5, 8])) + print(obj.matchPlayersAndTrainers(players=[1, 1, 1], trainers=[10])) |