- Today
- Total
- ์๋ฃ๊ตฌ์กฐ
- MST
- tree
- ๋ฐฑ์๋
- ํ๋ก๊ทธ๋๋จธ์ค
- CS
- ์์์ ๋ ฌ
- ์๋ฐ
- ๊ตฌํ
- ์ธํด
- ์๋ฐ์์ ์
- Graph
- ๋ฒจ๋งํฌ๋
- ๋ค์ต์คํธ๋ผ
- ๋ฐฑ์ค
- BFS
- spring
- OOP
- pytorch
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- leetcode
- ์กธ์ ์ํ
- PS
- Algorithm
- dp
- database
- ๊ทธ๋ฆฌ๋
- array
- java
- ๋ฌธ๋ฒ
Partially Committed
[LEETCODE] 1502. Can Make Arithmetic Progression From Sequence ๋ณธ๋ฌธ
[LEETCODE] 1502. Can Make Arithmetic Progression From Sequence
WonderJay 2022. 10. 12. 15:12
TITLE : 1502. Can Make Arithmetic Progression From Sequence
Description : A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.
Given an array of numbers arr, return true if the array can be rearranged to form an arithmetic progression. Otherwise, return false.
Example 1:
Input: arr = [3,5,1]
Output: true
Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements.
Example 2:
Input: arr = [1,2,4]
Output: false
Explanation: There is no way to reorder the elements to obtain an arithmetic progression.
Constraints:
- 2 <= arr.length <= 1000
- -106 <= arr[i] <= 106
๊ธธ์ด 2 ์ด์์ ์ ์๊ฐ ๋ด๊ธด ๋ฐฐ์ด์ด ์ฃผ์ด์ก์ ๋, ์ด๋ป๊ฒ๋ ์ฐ์ํ๋ ๋ ์์์ ์ฐจ์ด๊ฐ ํญ์ ๋์ผํ๋๋ก ํ ์ ์์ผ๋ฉด Arithmetic progression ํ๋ค๊ณ ์ ์ํ๋ค. ์ฃผ์ด์ง ๋ฐฐ์ด์ ๋ํด์ Arithmetic progression ํ ์ง๋ฅผ true/false ๋ฅผ ๋ฐํํ๋ฉด ๋๋ค. ์ด๋ฅผ ์ํด ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ ๋ค, ๊ฐ๊ฐ์ ์์๋ค๊ฐ์ ์ฐจ์ด๊ฐ ์ผ์ ํ์ง ์ฒดํฌํด์ฃผ๋ฉด ๋๋ค.
[C++]
class Solution {
public:
bool canMakeArithmeticProgression(vector<int>& arr) {
sort(arr.begin(), arr.end());
int diff = 0; int check = arr[1] - arr[0];
for (int i = 1; i < arr.size(); i++) {
diff = arr[i] - arr[i - 1];
if (check != diff) return false;
} return true;
}
};
'๐ฅ Algorithm || ๋ฌธ์ ํ์ด > PS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LEETCODE] 1790. Check if One String Swap Can Make Strings Equal (0) | 2022.10.13 |
---|---|
[LEETCODE] 202. Happy Number (0) | 2022.10.12 |
[LEETCODE] 1822. Sign of the Product of an Array (0) | 2022.10.12 |
[LEETCODE] 1779.Find Nearest Point That Has the Same X or Y Coordinate (0) | 2022.10.11 |
[LEETCODE] 1281. Subtract the Product and Sum of Digits of an Integer (0) | 2022.10.10 |