728x90
๐ 23 - ๋ด์ฐ๋ฆฌ
์์ ์ ์ํ์ข์ฐ ์ซ์๋ณด๋ค ํฐ ์ซ์์ ๊ฐ์๋ฅผ ์ฐพ์ผ๋ฉด ๋๋๋ฐ, ์ํ์ข์ฐ๋ฅผ ํ๋ณํ ๋๋ dx, dy
์ ๊ฐ์ด ๋ฐฉํฅ ๋ฒกํฐ๋ฅผ ์ค์ ํด์ฃผ๋ฉด ํ์ธํ๊ธฐ ํธํ๋ค. ๋๋ ์ด๋ ๊ฒ ํ์๋ค.
- ์, ํ, ์ข, ์ฐ์ ์ขํ๋ฅผ ํ๋์ฉ ํ์ํ๊ณ ์ต๋๊ฐ์ ๊ฐฑ์ ํ๋ค.
max
๋ก ๊ฐฑ์ ๋ ์ฃผ๋ณ ์ขํ์ ์๋ ๋์ ์ขํ์ ๋น๊ตํ๋ค์ ์๋ ๋์ ์ขํ๊ฐ ๋ ํฌ๋ฉดcnt++
ํด์ค๋ค.
๊ฐ์ฌ๋์ ์ด๋ ๊ฒ ํธ์ จ๋ค.
flag
๋ฅผ ์ค์ ํ๋ค.- ์๋ ๋์ ์ขํ๋ณด๋ค ์ฃผ๋ณ์ขํ๊ฐ ํฌ๋ฉด
flag
๋ฅผ 0์ผ๋ก ๋ฐ๊พผ๋ค. - ์ฃผ๋ณ ์ขํ์ ํ์์ด ๋๋ฌ๋๋ฐ๋
flag
๊ฐ 1์ด๋ฉด ์๋ ๋์ ์ขํ๊ฐ ํฐ ๊ฒ์ด๋ฏ๋กcnt++
ํด์ค๋ค.
let n = 5;
let arr = [[5, 3, 7, 2, 3], [3, 7, 1, 6, 1], [7, 2, 5, 3, 4], [4, 3, 6, 4, 1], [8, 7, 3, 5, 2]];
console.log(solution(n, arr));
// ๋์ ์ฝ๋
function solution(n, arr){
let cnt = 0;
let dx = [-1, 0, 1, 0], dy = [0, 1, 0, -1];
for (let x=0; x<n; x++){
for(let y=0; y<n; y++){
let surround = 0;
for (let i=0; i<4; i++){
let nx = x + dx[i];
let ny = y + dy[i];
if (nx<0 || ny<0 || nx>=n || ny>=n) continue
if (surround < arr[nx][ny]) surround = arr[nx][ny]
}
if (arr[x][y] > surround) cnt++;
}
}
return cnt;
}
let n = 5;
let arr = [[5, 3, 7, 2, 3], [3, 7, 1, 6, 1], [7, 2, 5, 3, 4], [4, 3, 6, 4, 1], [8, 7, 3, 5, 2]];
console.log(solution(n, arr));
// ๊ฐ์ ์ฝ๋
function solution(n, arr){
let cnt = 0;
let dx = [-1, 0, 1, 0], dy = [0, 1, 0, -1];
for (let x=0; x<n; x++){
for(let y=0; y<n; y++){
let flag = 1;
for (let i=0; i<4; i++){
let nx = x + dx[i];
let ny = y + dy[i];
if (nx<0 || ny<0 || nx>=n || ny>=n) continue
if (arr[nx][ny] > arr[x][y]){
flag = 0;
break;
}
}
if (flag) cnt ++;
}
}
return cnt;
}
๋ฐ์ํ
'Algorithm > ์ธํ๋ฐ(inflearn)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 25 - ์ ํจํ ํฐ๋ฆฐ๋๋กฌ (0) | 2021.08.17 |
---|---|
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 24 - ํ๋ฌธ ๋ฌธ์์ด (0) | 2021.08.17 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 22 - ๊ฒฉ์ํ ์ต๋ํฉ (0) | 2021.08.17 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 21 - ๋ฑ์ ๊ตฌํ๊ธฐ (0) | 2021.08.13 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 20 - ์ ์ ๊ณ์ฐ (0) | 2021.08.13 |
๋๊ธ