From 7c60dee83b436e8e8a667bf9f191beced8de4fc3 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Mon, 10 Mar 2025 00:36:13 -0600 Subject: chore: move cpp and rust code to directory --- other/rotate_function.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 other/rotate_function.cpp (limited to 'other/rotate_function.cpp') 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 &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 nums = {4, 3, 2, 6}; + cout << obj.maxRotateFunction(nums); +} -- cgit v1.2.3