๋ฌธ์ )
https://school.programmers.co.kr/learn/courses/30/lessons/161990
ํ์ด1) -> ์ฑ๋ฅ์ด ๋ ์ข์ ์ฝ๋ ( ์ข ๋ ํจ์จ์ )
class Solution {
public int[] solution(String[] wallpaper) {
int lux = wallpaper.length;
int luy = wallpaper[0].length();
int rdx = 0;
int rdy = 0;
for(int i=0; i<wallpaper.length; i++){
for(int j=0; j<wallpaper[i].length(); j++){
if(wallpaper[i].charAt(j) == '#'){
lux = Math.min(lux, i);
luy = Math.min(luy, j);
rdx = Math.max(rdx, i);
rdy = Math.max(rdy, j);
}
}
}
return new int[]{lux, luy, rdx+1, rdy+1};
}
}
ํ์ด2) -> ์ฑ๋ฅ์ ๋น์ทํ๊ฑฐ๋ StreamAPI์ฌ์ฉ์ ๋ด๋ถ์ ์ผ๋ก ๋ฉ์๋ ํธ์ถ ๋น์ฉ์ด ์ถ๊ฐ๋์ด ์กฐ๊ธ ๋ ๋จ์ด์ง ์ ์์ but, ๊ฐ๋ ์ฑ ํฅ์๊ณผ ์ต์, ์ต๋๊ฐ ์ฐพ์ ๋ ํจ์จ์
import java.util.stream.IntStream;
class Solution {
public int[] solution(String[] wallpaper) {
int lux = IntStream.range(0, wallpaper.length)
.filter(i -> wallpaper[i].contains("#"))
.findFirst().orElse(0); // ์ต์๊ฐ findFirst()
int rdx = IntStream.range(0, wallpaper.length)
.filter(i -> wallpaper[i].contains("#"))
.reduce((first, second) -> second).orElse(0); // ์ต๋๊ฐ reduce()
int luy = IntStream.range(0, wallpaper[0].length())
.filter(j -> IntStream.range(0, wallpaper.length).anyMatch(i -> wallpaper[i].charAt(j) == '#'))
.findFirst().orElse(0);
int rdy = IntStream.range(0, wallpaper[0].length())
.filter(j -> IntStream.range(0, wallpaper.length).anyMatch(i -> wallpaper[i].charAt(j) == '#'))
.reduce((first, second) -> second).orElse(0);
return new int[]{lux, luy, rdx + 1, rdy + 1};
}
}
'์๊ณ ๋ฆฌ์ฆ > ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ์ด' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค) ๋ฌ๋ฆฌ๊ธฐ ๊ฒฝ์ฃผ ๋ฌธ์ ํ์ด (0) | 2025.02.21 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค) ๊ฐ์ธ์ ๋ณด ์์ง ์ ํจ๊ธฐ๊ฐ ๋ฌธ์ ํ์ด (0) | 2025.02.20 |
ํ๋ก๊ทธ๋๋จธ์ค) ์ฑ๊ฒฝ ์ ํ ๊ฒ์ฌํ๊ธฐ (0) | 2025.02.18 |
ํ๋ก๊ทธ๋๋จธ์ค) ํ๋ฒ๊ฑฐ ๋ง๋ค๊ธฐ (0) | 2025.02.17 |
ํ๋ก๊ทธ๋๋จธ์ค : ๋์ถฉ๋ง๋ ์ํ(56/Lv3) (0) | 2025.02.12 |