-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2460-ApplyOperationsToAnArray.go
More file actions
90 lines (77 loc) · 3.47 KB
/
2460-ApplyOperationsToAnArray.go
File metadata and controls
90 lines (77 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
// 2460. Apply Operations to an Array
// You are given a 0-indexed array nums of size n consisting of non-negative integers.
// You need to apply n - 1 operations to this array where, in the ith operation (0-indexed),
// you will apply the following on the ith element of nums:
// If nums[i] == nums[i + 1], then multiply nums[i] by 2 and set nums[i + 1] to 0.
// Otherwise, you skip this operation.
// After performing all the operations, shift all the 0's to the end of the array.
// For example, the array [1,0,2,0,0,1] after shifting all its 0's to the end, is [1,2,1,0,0,0].
// Return the resulting array.
// Note that the operations are applied sequentially, not all at once.
// Example 1:
// Input: nums = [1,2,2,1,1,0]
// Output: [1,4,2,0,0,0]
// Explanation: We do the following operations:
// - i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
// - i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,4,0,1,1,0].
// - i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
// - i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,2,0,0].
// - i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,0,0].
// After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].
// Example 2:
// Input: nums = [0,1]
// Output: [1,0]
// Explanation: No operation can be applied, we just shift the 0 to the end.
// Constraints:
// 2 <= nums.length <= 2000
// 0 <= nums[i] <= 1000
import "fmt"
func applyOperations(nums []int) []int {
res, n := []int{}, len(nums)
for i := 1; i < n; i++ {
if nums[i] == nums[i-1] {
nums[i - 1], nums[i] = 2 * nums[i - 1], 0
}
}
for _, v := range nums {
if v != 0 { res = append(res, v) }
}
for _, v := range nums {
if v == 0 { res = append(res, v) }
}
return res
}
func applyOperations1(nums []int) []int {
j, n := 0, len(nums)
for i := 0; i < n; i++ {
if i + 1 < n && nums[i] == nums[i+1] {
nums[i], nums[i + 1] = 2 * nums[i], 0
}
if nums[i] != 0 {
nums[i], nums[j] = nums[j], nums[i]
j++
}
}
return nums
}
func main() {
// Example 1:
// Input: nums = [1,2,2,1,1,0]
// Output: [1,4,2,0,0,0]
// Explanation: We do the following operations:
// - i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
// - i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,4,0,1,1,0].
// - i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
// - i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,2,0,0].
// - i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,0,0].
// After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].
fmt.Println(applyOperations([]int{1,2,2,1,1,0})) // [1,4,2,0,0,0]
// Example 2:
// Input: nums = [0,1]
// Output: [1,0]
// Explanation: No operation can be applied, we just shift the 0 to the end.
fmt.Println(applyOperations([]int{0,1})) // [1,0]
fmt.Println(applyOperations1([]int{1,2,2,1,1,0})) // [1,4,2,0,0,0]
fmt.Println(applyOperations1([]int{0,1})) // [1,0]
}