Shuffle the Array

🏠 ⬅️ ➡️

Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].

Return the array in the form [x1,y1,x2,y2,...,xn,yn].

Example 1:

Input: nums = [2,5,1,3,4,7], n = 3 Output: [2,3,5,4,1,7] Explanation: Since x1\=2, x2\=5, x3\=1, y1\=3, y2\=4, y3\=7 then the answer is [2,3,5,4,1,7].

Example 2:

Input: nums = [1,2,3,4,4,3,2,1], n = 4 Output: [1,4,2,3,3,2,4,1]

Example 3:

Input: nums = [1,1,2,2], n = 2 Output: [1,2,1,2]

Constraints:

  • 1 <= n <= 500
  • nums.length == 2n
  • 1 <= nums[i] <= 10^3

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 3
    integer, dimension(2*n) :: nums = [2,5,1,3,4,7]
    integer, dimension(n) :: result

    call solve(nums, n, result)

    write (*, '(A)') 'Result:'
    write (*, '(A)') result

contains

    subroutine solve(nums, n, result)
        implicit none
        integer, intent(in) :: n
        integer, dimension(:), intent(in) :: nums
        integer, dimension(:), intent(out) :: result
        integer :: i

        do i = 1, n
            result(i) = nums(2*i-1)
            result(i+n) = nums(2*i)
        end do
    end subroutine solve
end program main
Compiled
Executed
Correct
module solve_the_problem
    implicit none
    contains
    subroutine solve(nums, n, result)
        implicit none
        integer, intent(in) :: n
        integer, dimension(2*n), intent(in) :: nums
        integer, dimension(2*n), intent(out) :: result

        ! Local variables
        integer :: i, j

        ! Initialize the result array
        result = 0

        ! Loop through the input array and fill the result array
        do i = 1, 2*n, 2
            result(i) = nums(i)
            result(i+1) = nums(i+1)
        end do

        ! Sort the result array
        call sort(result)

        ! Print the result
        do i = 1, 2*n
            write (*,*) result(i)
        end do
    end subroutine solve
end module solve_the_problem

program test_solve
    use solve_the_problem
    implicit none
    integer, parameter :: n = 3
    integer, dimension(2*n) :: nums = [2,5,1,3,4,7]
    integer, dimension(2*n) :: result

    call solve(nums, n, result)
end program test_solve
🌐 Data from online sources
from collections import defaultdict
from typing import List

class TweetCounts:

    def __init__(self):
        self.tweet_records = defaultdict(lambda: defaultdict(int))

    def recordTweet(self, tweetName: str, time: int) -> None:
        self.tweet_records[tweetName][time] += 1

    def getTweetCountsPerFrequency(self, freq: str, tweetName: str, startTime: int, endTime: int) -> List[int]:
        if freq == "minute":
            interval = 60
        elif freq == "hour":
            interval = 3600
        else:
            interval = 86400

        result = [0] * ((endTime - startTime) // interval + 1)
        for time, count in self.tweet_records[tweetName].items():
            if startTime <= time <= endTime:
                result[(time - startTime) // interval] += count

        return result
The `TweetCounts` class holds a data structure to store the frequency of tweet occurrences at a certain timestamp. This data structure is created as a nested map (unordered_map in C++), where the outer map uses `tweetName` as key, and the inner map uses time (in seconds) as key and count of tweets at that time as value. Whenever a new tweet is recorded, the count is incremented for the corresponding`tweetName` and time.

The getTweetCountsPerFrequency function accepts the frequency (minute, hour or day), tweetName, startTime and endTime as parameters. It calculates the interval in seconds based on the given frequency, and initializes an empty result vector, which will store the count of tweets for each partitioned chunk.

Iterate through the time and count pairs for the given tweetName. If the time lies within startTime and endTime, the count is added to the appropriate index in the result vector.

Finally, return the result vector.

🌐 Data from online sources
#include <unordered_map>
#include <vector>
#include <map>

class TweetCounts {
public:
    void recordTweet(string tweetName, int time) {
        tweetRecords[tweetName][time]++;
    }

    vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
        int interval;
        if(freq == "minute") interval = 60;
        else if(freq == "hour") interval = 3600;
        else interval = 86400;

        vector<int> result((endTime - startTime) / interval + 1);
        for(const auto &timeCount : tweetRecords[tweetName]) {
            int time = timeCount.first, count = timeCount.second;
            if(time >= startTime && time <= endTime) {
                result[(time - startTime) / interval] += count;
            }
        }

        return result;
    }

private:
    unordered_map<string, map<int, int>> tweetRecords;
};
The `TweetCounts` class holds a data structure to store the frequency of tweet occurrences at a certain timestamp. This data structure is created as a nested map (unordered_map in C++), where the outer map uses `tweetName` as key, and the inner map uses time (in seconds) as key and count of tweets at that time as value. Whenever a new tweet is recorded, the count is incremented for the corresponding`tweetName` and time.

The getTweetCountsPerFrequency function accepts the frequency (minute, hour or day), tweetName, startTime and endTime as parameters. It calculates the interval in seconds based on the given frequency, and initializes an empty result vector, which will store the count of tweets for each partitioned chunk.

Iterate through the time and count pairs for the given tweetName. If the time lies within startTime and endTime, the count is added to the appropriate index in the result vector.

Finally, return the result vector.