File tree Expand file tree Collapse file tree 2 files changed +86
-0
lines changed
Expand file tree Collapse file tree 2 files changed +86
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.BufferedReader ;
3+ import java.io.IOException ;
4+ import java.io.InputStreamReader ;
5+ import java.util.Arrays ;
6+
7+ public class Main {
8+
9+ static long diff;
10+ static StringBuilder sb = new StringBuilder ();
11+
12+
13+ public static void main (String [] args ) throws IOException {
14+ init();
15+ process();
16+ print();
17+ }
18+
19+ private static void init () throws IOException {
20+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));;
21+ diff = Integer . parseInt(br. readLine());
22+ }
23+
24+ private static void process () {
25+
26+ long pre = 1l ;
27+ boolean isEmpty = true ;
28+ for (long i = 2 ; i <= 500000 ; i++ ) {
29+ long num = i* i;
30+ if (num - pre > diff) break ;
31+ pre = num;
32+
33+ long target = num - diff;
34+ if (target == 0 ) continue ;
35+ long sqrt = (long ) Math . sqrt(target);
36+ if (sqrt* sqrt != target) continue ;
37+ isEmpty = false ;
38+ sb. append(i). append(" \n " );
39+
40+
41+
42+ }
43+
44+ if (isEmpty)sb. append(" -1" );
45+
46+ }
47+
48+
49+
50+ private static void print () {
51+ System . out. println(sb. toString());
52+ }
53+ }
54+
55+
56+ ```
Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ class Solution {
6+ public int [] solution (int [] numbers ) {
7+ int n = numbers. length;
8+ int [] answer = new int [n];
9+ Stack<Integer > stack = new Stack<> ();
10+
11+ for (int i = n - 1 ; i >= 0 ; i-- ) {
12+ // 현재 원소보다 작거나 같은 스택의 원소들을 제거
13+ while (! stack. isEmpty() && stack. peek() <= numbers[i]) {
14+ stack. pop();
15+ }
16+
17+ if (stack. isEmpty()) {
18+ answer[i] = - 1 ;
19+ } else {
20+ // 스택의 맨 위가 뒷 큰수
21+ answer[i] = stack. peek();
22+ }
23+
24+ stack. push(numbers[i]);
25+ }
26+
27+ return answer;
28+ }
29+ }
30+ ```
You can’t perform that action at this time.
0 commit comments