leetcode - 2149. Rearrange Array Elements by Sign

Description

You are given a 0-indexed integer array nums of even length consisting of an equal number of positive and negative integers.

You should rearrange the elements of nums such that the modified array follows the given conditions:

  1. Every consecutive pair of integers have opposite signs.
  2. For all integers with the same sign, the order in which they were present in nums is preserved.
  3. The rearranged array begins with a positive integer.

Return the modified array after rearranging the elements to satisfy the aforementioned conditions.

Example 1:

Input: nums = [3,1,-2,-5,2,-4]
Output: [3,-2,1,-5,2,-4]
Explanation:
The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4].
The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4].
Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions.  

Example 2:

Input: nums = [-1,1]
Output: [1,-1]
Explanation:
1 is the only positive integer and -1 the only negative integer in nums.
So nums is rearranged to [1,-1].

Constraints:

2 <= nums.length <= 2 * 10^5
nums.length is even
1 <= |nums[i]| <= 10^5
nums consists of equal number of positive and negative integers.

Solution

A brute force way would be: store all the positive and negative numbers separately, and fill one by one to the result.

The space complexity could be reduced by creating the return list first, then when the current number is positive, insert it to even indexes, otherwise insert it to odd indexes.

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( n ) o(n) o(n)

Code

class Solution:
    def rearrangeArray(self, nums: List[int]) -> List[int]:
        pos_i, neg_i = 0, 1
        res = [0] * len(nums)
        for i in range(len(nums)):
            if nums[i] > 0:
                res[pos_i] = nums[i]
                pos_i += 2
            elif nums[i] < 0:
                res[neg_i] = nums[i]
                neg_i += 2
        return res

相关推荐

  1. leetcode - 2149. Rearrange Array Elements by Sign

    2024-02-16 07:46:02       31 阅读
  2. Leetcode 2949. Count Beautiful Substrings II

    2024-02-16 07:46:02       42 阅读
  3. LeetCode解法汇总2129. 将标题首字母大写

    2024-02-16 07:46:02       19 阅读
  4. leetcode2549--统计桌面上的不同数字

    2024-02-16 07:46:02       19 阅读
  5. 固定区间存在重复元素算法(leetcode219题)

    2024-02-16 07:46:02       39 阅读
  6. LeetCode每日一题[C++]-2129.将标题首字母大写

    2024-02-16 07:46:02       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-16 07:46:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-16 07:46:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-16 07:46:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-16 07:46:02       20 阅读

热门阅读

  1. mac安装docker-compose

    2024-02-16 07:46:02       38 阅读
  2. C语言-----习题

    2024-02-16 07:46:02       29 阅读
  3. 罗马数字转整数

    2024-02-16 07:46:02       37 阅读
  4. 深度学习代码块之计算模型参数量和显存大小

    2024-02-16 07:46:02       36 阅读
  5. 使用深度学习进行序列分类

    2024-02-16 07:46:02       29 阅读
  6. uniapp 读取本地文件

    2024-02-16 07:46:02       40 阅读
  7. 【图论经典题目讲解】CF715B - Complete The Graph

    2024-02-16 07:46:02       33 阅读