Notice
Recent Posts
Recent Comments
Link
.Zzumbong
[leetCode/JS] 2256. Minimum Average Difference ๋ณธ๋ฌธ
๋์ด๋ [ ๐ค ] Medium
๋ฌธ์ ์ค๋ช
You are given a 0-indexed integer array nums
of length n
.
The average difference of the index i
is the absolute difference between the average of the first i + 1
elements of nums
and the average of the last n - i - 1
elements. Both averages should be rounded down to the nearest integer.
Return the index with the minimum average difference. If there are multiple such indices, return the smallest one.
Note:
- The absolute difference of two numbers is the absolute value of their difference.
- The average of n elements is the sum of the
n
elements divided (integer division) byn
. - The average of
0
elements is considered to be0
.
์ซ์๊ฐ ๋ค์ด์๋nums
๋ฅผindex
๊ธฐ์ค์ผ๋ก ์ข์ฐ๋ก ๋๋ ์
๊ฐ๊ฐ ํ๊ท ์ ๋ธ ํ ๊ฐ ํ๊ท ์ ์ฐจ์ด๊ฐ ๊ฐ์ฅ ์์index
๋ฅผ ๋ฆฌํดํ๋ ๊ฒ์ด๋ค.
example1 ์ ์ฐจ๊ทผ ์ฐจ๊ทผ ๋ณด๋ฉด ์ดํด๊ฐ ์ฝ๋ค.
์ ์ถ๋ ฅ ์
Example 1:
Input: nums = [2,5,3,9,5,3]
Output: 3
Explanation:
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
The average difference of index 3 is the minimum average difference so return 3.
Example 2:
Input: nums = [0]
Output: 0
Explanation:
The only index is 0 so return 0.
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
Constraints
1 <= nums.length <= 105
0 <= nums[i] <= 105
๋ด ์๋ฃจ์
- for๋ฌธ์ผ๋ก ์ํํ๋ค.
- ์ฒ์์ ์ข์ฐ๋ก ๋๋ ๋ฐฐ์ด a, b๋ฅผ ๊ฐ๊ฐ sum / n ์ผ๋ก ํ๊ท ์ ๊ตฌํ๋๋ฐ, ๋๋ฆฌ๋ค๊ณ ๋น ๊พธ.
- ์๋ก์ด ์์๋ฅผ a์ ๋ํ๊ณ b์ ๋นผ์ฃผ๋ฉด ๋๋ค๋ ๋ก์ง์ ํตํด ๋ก์ง์ ๊ฐ์ํํจ.
var minimumAverageDifference = function(nums) {
let minimun = Infinity;
let answer = null;
let aSum = 0;
let bSum = nums.reduce((acc, cur) => acc + cur, 0);
for(let i = 0; i < nums.length; i++) {
aSum += nums[i];
bSum -= nums[i];
const a = Math.floor(aSum / (i + 1));
const b = Math.floor(bSum / (nums.length - i - 1) || 0);
const min = Math.abs(a - b);
if(minimun > min) {
minimun = min;
answer = i;
}
}
return answer;
};
// ๋๋ฌด ๋๋ ธ์.
// var minimumAverageDifference = function(nums) {
// let minimun = Infinity;
// let answer = null;
// for(let i = 1; i < nums.length+1; i++) {
// const a = Math.floor(nums.slice(0, i).reduce((acc, cur)=>acc+cur,0) / i);
// const b = Math.floor(nums.slice(i).reduce((acc, cur)=>acc+cur,0) / (nums.length - i) || 0);
// const min = Math.abs(a - b);
// if(minimun > min) {
// minimun = min;
// answer = i - 1;
// }
// }
// return answer;
// };
๊ฐ์ํ
- ์ค๋ง์ด๊น? ๊ฐ๋นจ๋๋ค. ์คํํ ๋, ์๋ฒ ์ปจ๋์ ์ด ์ข์๋?
'coding test > leetCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[leetCode/JS] 328. Odd Even Linked List (0) | 2022.12.06 |
---|---|
[leetCode/JS] 876. Middle of the Linked List (0) | 2022.12.05 |
[leetCode/JS] 451. Sort Characters By Frequency (0) | 2022.12.03 |
[leetCode/JS] 1657. Determine if Two Strings Are Close (0) | 2022.12.02 |
[leetCode/JS] 1704. Determine if String Halves Are Alike (0) | 2022.12.01 |
0 Comments