leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit b477c5d538f1fef91dbf906a17411eef32b2ded2
parent 550458b5ef4a31e8c95e4fcd0f22bc6ca59fd46e
Author: Jack Sangdahl <jack@pngu.org>
Date:   Tue, 11 Mar 2025 21:13:37 -0600

2529. maximum count of positive integer and negative integer

Diffstat:
Amax_cnt_pos_neg_int.c | 36++++++++++++++++++++++++++++++++++++
Amax_cnt_pos_neg_int.py | 24++++++++++++++++++++++++
2 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/max_cnt_pos_neg_int.c b/max_cnt_pos_neg_int.c @@ -0,0 +1,36 @@ +// 2529. Maximum Count of Positive Integer and Negative Integer +#include "leetcode.h" + +/* + * given an array 'nums' sorted in non-decreasing order, return the maximum + * between the number of positive integers and he number of negative integers. + * note that '0' is neither positive nor negative. + */ + +int pos(long cnt) { return (int)(cnt >> 32); } + +int neg(long cnt) { return (int)(cnt & 0x0000FFFFL); } + +long sum(int n) { + if (n > 0) + return 1L << 32; + else if (n < 0) + return 1L; + else + return 0L; +} + +int maximumCount(int *nums, int numsSize) { + long cnt = 0; + for (int i = 0; i < numsSize; i++) + cnt += sum(nums[i]); + return fmax(pos(cnt), neg(cnt)); +} + +int main() { + int n1[] = {-2, -1, -1, 1, 2, 3}, n2[] = {-3, -2, -1, 0, 0, 1, 2}, + n3[] = {5, 20, 66, 1314}; + printf("%d\n", maximumCount(n1, ARRAY_SIZE(n1))); // expect: 3 + printf("%d\n", maximumCount(n2, ARRAY_SIZE(n2))); // expect: 3 + printf("%d\n", maximumCount(n3, ARRAY_SIZE(n3))); // expect: 4 +} diff --git a/max_cnt_pos_neg_int.py b/max_cnt_pos_neg_int.py @@ -0,0 +1,24 @@ +# 2529. Maximum Count of Positive Integer and Negative Integer +from bisect import bisect_left + +""" +given an array 'nums' sorted in non-decreasing order, return the maximum +between the number of positive integers and he number of negative integers. +note that '0' is neither positive nor negative. +""" + + +class Solution(object): + def maximumCount(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + return max(bisect_left(nums, 0), len(nums) - bisect_left(nums, 1)) + + +if __name__ == "__main__": + obj = Solution() + print(obj.maximumCount(nums=[-2, -1, -1, 1, 2, 3])) + print(obj.maximumCount(nums=[-3, -2, -1, 0, 0, 1, 2])) + print(obj.maximumCount(nums=[5, 20, 66, 1314]))