-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2366-Minimum_Replacements_to_Sort_the_Array.cpp
More file actions
92 lines (82 loc) · 2.38 KB
/
2366-Minimum_Replacements_to_Sort_the_Array.cpp
File metadata and controls
92 lines (82 loc) · 2.38 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
91
92
/*******************************************************************************
* 2366-Minimum_Replacements_to_Sort_the_Array.cpp
* Billy.Ljm
* 30 August 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/minimum-replacements-to-sort-the-array/
*
* You are given a 0-indexed integer array nums. In one operation you can
* replace any element of the array with any two elements that sum to it.
*
* - For example, consider nums = [5,6,7]. In one operation, we can replace
* nums[1] with 2 and 4 and convert nums to [5,2,4,7].
*
* Return the minimum number of operations to make an array that is sorted in
* non-decreasing order.
*
* ===========
* My Approach
* ===========
* We will want to keep the last element, which will be the maximum element of
* the array. Then, we have to iterate backwards through the array, enforcing
* the non-decreasing order. If the i-th element has to be replaced, we will
* replace it with (element[i] / n) where n is an integer, since it has the
* highest first element while still respecting the order.
*
* This has a time complexity of O(n), and a space complexity of O(1), where n
* is the length of array.
******************************************************************************/
#include <iostream>
#include <vector>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (const auto elem : v) {
os << elem << ",";
}
if (v.size() > 0) os << "\b";
os << "]";
return os;
}
/**
* Solution
*/
class Solution {
public:
long long minimumReplacement(vector<int>& nums) {
long long nmoves = 0; // number of replacement moves
int curr, next = nums.back(); // i-th and (i+1)th element
int tmp; // decompose curr into curr/tmp
// do replacements
for (int i = nums.size() - 2; i >= 0; i--) {
curr = nums[i];
tmp = (curr + next - 1) / next; // round up
nmoves += tmp - 1;
next = curr / tmp;
}
return nmoves;
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
vector<int> nums;
// test case 1
nums = { 3,9,3 };
std::cout << "minimumReplacement(" << nums << ") = ";
std::cout << sol.minimumReplacement(nums) << std::endl;
// test case 2
nums = { 1,2,3,4,5 };
std::cout << "minimumReplacement(" << nums << ") = ";
std::cout << sol.minimumReplacement(nums) << std::endl;
return 0;
}