-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3041-MaximizeConsecutiveElementsInAnArrayAfterModification.go
More file actions
71 lines (62 loc) · 2.59 KB
/
3041-MaximizeConsecutiveElementsInAnArrayAfterModification.go
File metadata and controls
71 lines (62 loc) · 2.59 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
package main
// 3041. Maximize Consecutive Elements in an Array After Modification
// You are given a 0-indexed array nums consisting of positive integers.
// Initially, you can increase the value of any element in the array by at most 1.
// After that, you need to select one or more elements from the final array such that those elements are consecutive when sorted in increasing order.
// For example, the elements [3, 4, 5] are consecutive while [3, 4, 6] and [1, 1, 2, 3] are not.
// Return the maximum number of elements that you can select.
// Example 1:
// Input: nums = [2,1,5,1,1]
// Output: 3
// Explanation: We can increase the elements at indices 0 and 3. The resulting array is nums = [3,1,5,2,1].
// We select the elements [3,1,5,2,1] and we sort them to obtain [1,2,3], which are consecutive.
// It can be shown that we cannot select more than 3 consecutive elements.
// Example 2:
// Input: nums = [1,4,7,10]
// Output: 1
// Explanation: The maximum consecutive elements that we can select is 1.
// Constraints:
// 1 <= nums.length <= 10^5
// 1 <= nums[i] <= 10^6
import "fmt"
import "sort"
func maxSelectedElements(nums []int) int {
sort.Ints(nums)
//This will store what is the maximum size we may achieve ending on this element
// for example, values would be like
// 7 -> 5, which means that the there exist a consecutive sequqnce ending on 7 with size 5 {3,4,5,6,7}
res, until := 1, make(map[int]int)
until[nums[0]], until[nums[0]+1] = 1, 1
max := func (x, y int) int { if x > y { return x; }; return y; }
for i := 1; i < len(nums); i++ {
curr := nums[i]
incr := curr + 1
if val, ok := until[incr-1]; ok {
until[incr] = val + 1
res = max(res, val + 1)
} else {
until[incr] = 1
}
if val, ok := until[curr-1]; ok {
until[curr] = val+1
res = max(res, val + 1)
} else {
until[curr] = 1
}
}
return res
}
func main() {
// Example 1:
// Input: nums = [2,1,5,1,1]
// Output: 3
// Explanation: We can increase the elements at indices 0 and 3. The resulting array is nums = [3,1,5,2,1].
// We select the elements [3,1,5,2,1] and we sort them to obtain [1,2,3], which are consecutive.
// It can be shown that we cannot select more than 3 consecutive elements.
fmt.Println(maxSelectedElements([]int{2,1,5,1,1})) // 3
// Example 2:
// Input: nums = [1,4,7,10]
// Output: 1
// Explanation: The maximum consecutive elements that we can select is 1.
fmt.Println(maxSelectedElements([]int{1,4,7,10})) // 1
}