<style> html, body, .ui-content { background: #222222; color: #00BFFF; } /* 設定 code 模板 */ .markdown-body code, .markdown-body tt { background-color: #ffffff36; } .markdown-body .highlight pre, .markdown-body pre { color: #ddd; background-color: #00000036; } .hljs-tag { color: #ddd; } .token.operator { background-color: transparent; } /* 設定連結 */ a, .open-files-container li.selected a { color: #89FFF8; } a:hover, .open-files-container li.selected a:hover { color: #89FFF890; } </style> ###### tags: `Leetcode` # 6275. Minimum Operations to Make Array Equal II ###### Link : https://leetcode.com/contest/biweekly-contest-96/problems/minimum-operations-to-make-array-equal-ii/ ## 題目 回傳讓nums1變成nums2所需動作次數 ## 程式碼 ```cpp= class Solution { public: long long minOperations(vector<int>& nums1, vector<int>& nums2, int k) { if(k == 0) { if(nums1 == nums2) return 0; else return -1; } long long ans = 0; int cnt = 0; for(int i = 0;i < nums1.size();i++){ long long temp = nums1[i] - nums2[i]; cnt = cnt + (temp / k); if(temp % k != 0) return -1; ans += abs(temp); } if(ans % (2 * k) != 0 || cnt!=0) return -1; ans /= (k*2); return ans; } }; ``` ## Date ### 2023/1/21
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up