Skip to content

Commit

Permalink
๐Ÿ”€merge: [230825] PGS(2๊ฐœ ๋” ๋ฝ‘์•„์„œ ํ•˜๊ธฐ, ๋ฌธ์ž์—ด ์••์ถ•) - 2๋ฌธ์ œ ์™„๋ฃŒ (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinny-l committed Aug 25, 2023
1 parent 6486a5d commit 5a84784
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package season2._230825;

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

public class PGS_๋‘_๊ฐœ_๋ฝ‘์•„์„œ_๋”ํ•˜๊ธฐ {

public static void main(String[] args) {
System.out.println(Arrays.toString(solution(new int[]{5,0,2,7})));
}

public static int[] solution(int[] numbers) {
Set<Integer> answer = new TreeSet<>();

for (int i = 0; i < numbers.length; i++) {
for (int j = i + 1; j < numbers.length; j++) {
answer.add(numbers[i] + numbers[j]);
}
}

return answer.stream().mapToInt(Integer::intValue).toArray();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package season2._230825;

public class PGS_๋ฌธ์ž์—ด_์••์ถ• {

public static void main(String[] args) {
System.out.println(solution("a" ));
}

public static int solution(String s) {
int min = Integer.MAX_VALUE;
int count = 0;
String prev = "";
String tmp = "";
StringBuilder sb = new StringBuilder();

for (int i = 1; i <= s.length() / 2; i++) {
for (int j = 0; j < s.length(); j += i) {

if ((j + i) > s.length()) { // ๋‚จ์€ ๋ฌธ์ž์—ด์ด ์ž๋ฅด๋Š” ๊ธธ์ด๋ณด๋‹ค ์งง์„ ๋•Œ ์ฒ˜๋ฆฌ
tmp = s.substring(j);
} else {
tmp = s.substring(j, j + i);
}

if (!tmp.equals(prev)) { // ์•ž์— ๋ฌธ์ž์—ด์ด ๋‹ค๋ฅด๋ฉด
if (count == 1 || prev.equals("")) { // ๊ฐœ์ˆ˜๊ฐ€ 1์ด๋ฉด ๋ฌธ์ž์—ด๋งŒ ๋ถ™์ด๊ณ 
sb.append(prev);
} else {
sb.append(count).append(prev); // ์•„๋‹ˆ๋ฉด ์ˆซ์ž์™€ ๋ฌธ์ž์—ด ๋ถ™์ธ๋‹ค.
}
prev = tmp;
count = 0;
}
count++;
}

// ๋ฌธ์ž์—ด ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธ
if (!prev.equals("")) {
if (count == 1) { // ๊ฐœ์ˆ˜๊ฐ€ 1์ด๋ฉด ๋ฌธ์ž์—ด๋งŒ ๋ถ™์ด๊ณ 
sb.append(prev);
} else {
sb.append(count).append(prev); // ์•„๋‹ˆ๋ฉด ์ˆซ์ž์™€ ๋ฌธ์ž์—ด ๋ถ™์ธ๋‹ค.
}
prev = "";
count = 0;
}

min = Math.min(min, sb.length());
sb = new StringBuilder();
}
if (s.length() == 1) {
return 1;
}
return min;
}

}

0 comments on commit 5a84784

Please sign in to comment.