-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
๐merge: [230825] PGS(2๊ฐ ๋ ๋ฝ์์ ํ๊ธฐ, ๋ฌธ์์ด ์์ถ) - 2๋ฌธ์ ์๋ฃ (#105)
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
jinny-l/season2/_230825/PGS_๋_๊ฐ_๋ฝ์์_๋ํ๊ธฐ.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |