Skip to content

Commit 9f91bc9

Browse files
committed
leetcode
1 parent d27c937 commit 9f91bc9

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
203203
- [**1071.** Greatest Common Divisor of Strings](GreatestCommonDivisorOfStrings/greatest_common_divisor_of_strings.dart)
204204
- [**953.** Verifying an Alien Dictionary](VerifyingAnAlienDictionary/verifying_an_alien_dictionary.dart)
205205
- [**6.** Zigzag Conversion](ZigzagConversion/zigzag_conversion.dart)
206+
- [**1470.** Shuffle the Array](ShuffleTheArray/shuffle_the_array.dart)
206207

207208
## Reach me via
208209

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
3+
4+
-* 1470. Shuffle the Array *-
5+
6+
7+
8+
9+
Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].
10+
11+
Return the array in the form [x1,y1,x2,y2,...,xn,yn].
12+
13+
14+
15+
Example 1:
16+
17+
Input: nums = [2,5,1,3,4,7], n = 3
18+
Output: [2,3,5,4,1,7]
19+
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
20+
Example 2:
21+
22+
Input: nums = [1,2,3,4,4,3,2,1], n = 4
23+
Output: [1,4,2,3,3,2,4,1]
24+
Example 3:
25+
26+
Input: nums = [1,1,2,2], n = 2
27+
Output: [1,2,1,2]
28+
29+
30+
Constraints:
31+
32+
1 <= n <= 500
33+
nums.length == 2n
34+
1 <= nums[i] <= 10^3
35+
36+
37+
38+
*/
39+
40+
List<int> shuffle(List<int> nums, int n) {
41+
return [];
42+
}
43+
44+
class A {
45+
List<int> shuffle(List<int> nums, int n) {
46+
nums[0] *= -1;
47+
48+
for (int i = 1; i < nums.length; i++) {
49+
int j = i;
50+
int currentNumber = nums[i];
51+
while (nums[j] > 0) {
52+
int target;
53+
if (j < n) {
54+
target = j * 2;
55+
} else {
56+
target = (j - n) * 2 + 1;
57+
}
58+
int temp = nums[target];
59+
nums[target] = currentNumber;
60+
currentNumber = temp;
61+
nums[j] *= -1;
62+
j = target;
63+
}
64+
}
65+
for (int i = 0; i < nums.length; i++) {
66+
nums[i] = (nums[i]).abs();
67+
}
68+
69+
return nums;
70+
}
71+
}
72+
73+
class B {
74+
List<int> shuffle(List<int> nums, int n) {
75+
List<int> ans = List.filled(2 * n, 0);
76+
for (int i = 0; i < n; i++) {
77+
ans[2 * i] = nums[i];
78+
ans[2 * i + 1] = nums[i + n];
79+
}
80+
return ans;
81+
}
82+
}
83+
84+
class c {
85+
List<int> shuffle(List<int> nums, int n) {
86+
int first = 0, second = n, max = 1001;
87+
for (int i = 0; i < 2 * n; i++) {
88+
if (i % 2 == 0)
89+
nums[i] = (nums[first++] % max) * max + nums[i];
90+
else
91+
nums[i] = (nums[second++] % max) * max + nums[i];
92+
}
93+
for (int i = 0; i < 2 * n; i++) nums[i] ~/= max;
94+
return nums;
95+
}
96+
}

ShuffleTheArray/shuffle_the_array.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import "math"
4+
5+
func shuffle(nums []int, n int) []int {
6+
nums[0] *= -1
7+
for i := 1; i < len(nums); i++ {
8+
9+
var j int = i
10+
var currentNumber int = nums[i]
11+
for nums[j] > 0 {
12+
var target int
13+
if j < n {
14+
target = j * 2
15+
} else {
16+
target = (j-n)*2 + 1
17+
}
18+
var temp int = nums[target]
19+
nums[target] = currentNumber
20+
currentNumber = temp
21+
nums[j] *= -1
22+
j = target
23+
}
24+
}
25+
for i := 0; i < len(nums); i++ {
26+
nums[i] = int(math.Abs(float64(nums[i])))
27+
}
28+
return nums
29+
}

ShuffleTheArray/shuffle_the_array.md

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# 🔥 3 Approaches 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Intuition
4+
5+
Reshuffle the given array nums of length 2n into a new array such that the first element of the new array is the first element of the original array, the second element of the new array is the (n+1)th element of the original array, and so on.
6+
7+
## Approach
8+
9+
The solution uses a loop to iterate n times, and in each iteration, it pushes the ith element of nums and the (i + n) the Element of nums into the ans array. After the loop, it returns the ans array as the final result.
10+
11+
## Complexity
12+
13+
Time complexity: O(n)
14+
Space complexity: O(n)
15+
16+
## Code - 1
17+
18+
```dart
19+
class Solution {
20+
List<int> shuffle(List<int> nums, int n) {
21+
List<int> ans = List.filled(2 * n, 0);
22+
for (int i = 0; i < n; i++) {
23+
ans[2 * i] = nums[i];
24+
ans[2 * i + 1] = nums[i + n];
25+
}
26+
return ans;
27+
}
28+
}
29+
```
30+
31+
## Intuition
32+
33+
Moves each element in the array to its target position. Repeat the process for the replaced element.
34+
35+
## Approach
36+
37+
For each element in the array, move it to its final position, then repeat the process for the replaced element. Multiply the value in the position by -1 so we know when to stop the process (completed the cycle in the graph).
38+
39+
Then iterate over each element to restore the original value.
40+
41+
## Complexity
42+
43+
Time complexity: O(n)
44+
45+
Space complexity: O(1)
46+
47+
## Code - 2
48+
49+
```dart
50+
class Solution {
51+
List<int> shuffle(List<int> nums, int n) {
52+
nums[0] *= -1;
53+
54+
for (int i = 1; i < nums.length; i++) {
55+
int j = i;
56+
int currentNumber = nums[i];
57+
while (nums[j] > 0) {
58+
int target;
59+
if (j < n) {
60+
target = j * 2;
61+
} else {
62+
target = (j - n) * 2 + 1;
63+
}
64+
int temp = nums[target];
65+
nums[target] = currentNumber;
66+
currentNumber = temp;
67+
nums[j] *= -1;
68+
j = target;
69+
}
70+
}
71+
for (int i = 0; i < nums.length; i++) {
72+
nums[i] = (nums[i]).abs();
73+
}
74+
75+
return nums;
76+
}
77+
}
78+
```
79+
80+
## Code - 3 Two Pointer
81+
82+
```dart
83+
class Solution {
84+
List<int> shuffle(List<int> nums, int n) {
85+
int first = 0, second = n, max = 1001;
86+
for (int i = 0; i < 2 * n; i++) {
87+
if (i % 2 == 0)
88+
nums[i] = (nums[first++] % max) * max + nums[i];
89+
else
90+
nums[i] = (nums[second++] % max) * max + nums[i];
91+
}
92+
for (int i = 0; i < 2 * n; i++) nums[i] ~/= max;
93+
return nums;
94+
}
95+
}
96+
```
97+
98+
### Disclaimer:-
99+
100+
This Solution is not available in DART Programing language with is a bummer. Hurts my feeling. But as a man we should implement it no matter what. We are not bunch of wussies who gonna skip it if it's not available in one language we love. Instead we will conquer the sea and rivers and cross the mountains so see what's lies beyond our horizons.

0 commit comments

Comments
 (0)