프로필사진

Go, Vantage point

가까운 곳을 걷지 않고 서는 먼 곳을 갈 수 없다.


Github | https://github.com/overnew/

Blog | https://everenew.tistory.com/





티스토리 뷰

반응형

문제

https://leetcode.com/problems/maximum-element-sum-of-a-complete-subset-of-indices/description/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

풀이

 

난이도: Hard

 

 

weekly contest의 최종 보스 문제.

 

https://leetcode.com/contest/weekly-contest-363/

 

Contest - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

 

 

 

문제를 파악하는 것도 어려웠는데 이리저리 인수분해 해서 만든 코드도 결국 특정 케이스에서 계속 실패해서 정답을 참고하였다.

 

https://leetcode.com/problems/maximum-element-sum-of-a-complete-subset-of-indices/solutions/4053876/java-c-python-square-factorization-o-n-o-1/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

 

 

 

집합의 구성은 모든 요소들이 서로 한쌍씩 곱할 때 제곱수이어야 한다.

 

제곱수(var*var)를 만드는 정수를 var라고 하자.

var를 1에서 n까지 순서대로 만들면, 제곱수는 1, 4, 9, 16, 25, ... 이런 식으로 이어진다.

이 제곱수들은 서로 한쌍씩 곱하더라도 (var1*var1) x (var2*var2) = (var1*var2) x (var1*var2) 이기 때문에 다시 제곱수가 된다.

 

여기서 base라는 변수를 1에서 n까지 늘려가며 제곱수들에 곱해보자.

base = 1  일때, 제곱수들은 1, 4, 9, 16, 25,...

base = 2  일때, 제곱수들은 2, 8, 18, 32, 50,...  

base = 3  일때, 제곱수들은 3, 12, 27, 48, 75,...

 

즉, (base * var1*var1) x ( base *  var2*var2) = ( base *  var1*var2) x ( base *  var1*var2) 이기 때문에 다시 제곱수가 된다.

 

 

 

 

 

모든 배열의 숫자들은 양수이기 때문에 더해줄수록 값이 커진다.

따라서 base를 늘려가면서 제곱수들의 집합을 만들고, 집합이 가리키는 배열의 모든 수를 더해줘야 정답이다.

 

 

 

코드

 

 

 

반응형
댓글
반응형
인기글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함