Skip to content

Commit 0bfee97

Browse files
Merge pull request #1831 from CodingTestStudy2/황은지
[황은지] Day16
2 parents 4247e89 + 16b9f93 commit 0bfee97

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var largestInteger = function (nums, k) {
7+
if (nums.length < k) return -1;
8+
if (nums.length === k) return Math.max(...nums);
9+
10+
const group = {};
11+
let max = -1;
12+
// k가 1이면 중간에 겹치는거 없으면 됨
13+
// 양쪽 끝은 중간에 숫자가 없으면 안겹침
14+
15+
for (let i = 0; i < nums.length; i++) {
16+
if (k !== 1 && (i == 0 || i == nums.length - 1)) continue;
17+
group[nums[i]] = (group[nums[i]] || 0) + 1;
18+
}
19+
20+
if (k !== 1) {
21+
const left = nums[0];
22+
const right = nums[nums.length - 1];
23+
if (left === right) return -1;
24+
if (group[left] === undefined) max = Math.max(max, left);
25+
if (group[right] === undefined) max = Math.max(max, right);
26+
} else {
27+
for (const [key, value] of Object.entries(group)) {
28+
if (value === 1) max = Math.max(key);
29+
}
30+
}
31+
return max;
32+
};

0 commit comments

Comments
 (0)