aboutsummaryrefslogtreecommitdiff
path: root/other/rotate_function.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/rotate_function.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/rotate_function.cpp')
-rw-r--r--other/rotate_function.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/other/rotate_function.cpp b/other/rotate_function.cpp
new file mode 100644
index 0000000..1bfd7f5
--- /dev/null
+++ b/other/rotate_function.cpp
@@ -0,0 +1,29 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ int maxRotateFunction(vector<int> &nums) {
+ long sum = 0, fn = 0;
+ int len = nums.size();
+ for (int i = 0; i < len; i++) {
+ sum += nums[i];
+ fn += (i * nums[i]);
+ }
+ long l = 1, r, newFn = fn;
+ while (l < len) {
+ r = l + len - 1;
+ long removed = (l - 1) * nums[l - 1];
+ long added = r * nums[r % len];
+ newFn -= newFn - removed + added - sum;
+ fn = max(fn, newFn);
+ l++;
+ }
+ return fn;
+ }
+};
+
+int main() {
+ Solution obj;
+ vector<int> nums = {4, 3, 2, 6};
+ cout << obj.maxRotateFunction(nums);
+}