Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c847ec1

Browse files
Merge pull request codeisneverodd#15 from prove-ability/main
2022年03月29日 문제 풀이 추가합니다
2 parents a637c9b + 4da5567 commit c847ec1

File tree

2 files changed

+197
-138
lines changed

2 files changed

+197
-138
lines changed

‎level-1/로또의-최고-순위와-최저-순위.js

Lines changed: 110 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,111 +2,138 @@
22
//완벽한 정답이 아닙니다.
33
//정답 1 - codeisneverodd
44
function solution(lottos, win_nums) {
5-
const zeroCount = lottos.filter(e => e === 0).length
6-
const matchCount = win_nums.filter(e => lottos.includes(e)).length
7-
const matchToRank = [6, 6, 5, 4, 3, 2, 1]
8-
const lowRank = matchToRank[matchCount]
9-
const highRank = zeroCount === 6 ? 1 : matchToRank[matchCount + zeroCount]
10-
11-
var answer = [highRank, lowRank];
12-
return answer;
5+
const zeroCount = lottos.filter((e) => e === 0).length
6+
const matchCount = win_nums.filter((e) => lottos.includes(e)).length
7+
const matchToRank = [6, 6, 5, 4, 3, 2, 1]
8+
const lowRank = matchToRank[matchCount]
9+
const highRank = zeroCount === 6 ? 1 : matchToRank[matchCount + zeroCount]
10+
11+
var answer = [highRank, lowRank]
12+
return answer
1313
}
1414

1515
//정답 2 - codeisneverodd
1616
function solution(lottos, win_nums) {
17-
// 0이 없는 경우 > 최저 순위 == 최고 순위
18-
// 0이 있는 경우 > 모두 0인경우 > 1위
19-
// > 0이 아닌 수가 있는 경우 > 최저 순위 - (0의 개수) = 최고순위
20-
// 0이 있는 경우 0만 중복이 가능하므로, 0의 개수를 (배열 길이 - 집합 길이 + 1)를 통해 구함.
21-
// 순위는 7 - hit
22-
// 최종적으로 7위인 경우 6위로 변경
23-
var answer = []
24-
if (lottos.indexOf(0) === -1) {
25-
answer[0] = answer[1] = 7 - hit(lottos, win_nums)
26-
} else {
27-
const zeroCount = lottos.length - [...new Set(lottos)].length + 1
28-
answer[1] = 7 - hit(lottos, win_nums)
29-
zeroCount === 6 ? answer[0] = 1 : answer[0] = answer[1] - zeroCount
30-
}
31-
for (let i = 0; i < 2; i++) {
32-
answer[i] >= 7 ? answer[i] = 6 : null
33-
}
34-
return answer;
17+
// 0이 없는 경우 > 최저 순위 == 최고 순위
18+
// 0이 있는 경우 > 모두 0인경우 > 1위
19+
// > 0이 아닌 수가 있는 경우 > 최저 순위 - (0의 개수) = 최고순위
20+
// 0이 있는 경우 0만 중복이 가능하므로, 0의 개수를 (배열 길이 - 집합 길이 + 1)를 통해 구함.
21+
// 순위는 7 - hit
22+
// 최종적으로 7위인 경우 6위로 변경
23+
var answer = []
24+
if (lottos.indexOf(0) === -1) {
25+
answer[0] = answer[1] = 7 - hit(lottos, win_nums)
26+
} else {
27+
const zeroCount = lottos.length - [...new Set(lottos)].length + 1
28+
answer[1] = 7 - hit(lottos, win_nums)
29+
zeroCount === 6 ? (answer[0] = 1) : (answer[0] = answer[1] - zeroCount)
30+
}
31+
for (let i = 0; i < 2; i++) {
32+
answer[i] >= 7 ? (answer[i] = 6) : null
33+
}
34+
return answer
3535
}
3636

3737
function hit(lottos, win_nums) {
38-
let result = 0
39-
lottos.forEach((element) => {
40-
win_nums.indexOf(element) === -1 ? null : result += 1
41-
})
42-
return result
38+
let result = 0
39+
lottos.forEach((element) => {
40+
win_nums.indexOf(element) === -1 ? null : (result += 1)
41+
})
42+
return result
4343
}
4444

4545
// 정답 3 - jaewon1676
4646
function solution(lottos, win_nums) {
47-
var answer = [];
48-
const correct = lottos.filter(lotto => win_nums.includes(lotto)).length;
49-
// lottos배열을 순회하며 당첨배열에 있는 수를 return 하고 총 개수를 correct에 저장
50-
51-
const zeros = lottos.filter(lotto => lotto === 0).length;
52-
// lottos배열을 순회하며 0인 총 개수를 zeros에 저장
53-
54-
let min = 7-correct >= 6 ? 6 : 7-correct;
55-
56-
let max = min-zeros < 1 ? 1 : min-zeros;
57-
58-
answer = [max,min]
59-
60-
return answer;
47+
var answer = []
48+
const correct = lottos.filter((lotto) => win_nums.includes(lotto)).length
49+
// lottos배열을 순회하며 당첨배열에 있는 수를 return 하고 총 개수를 correct에 저장
50+
51+
const zeros = lottos.filter((lotto) => lotto === 0).length
52+
// lottos배열을 순회하며 0인 총 개수를 zeros에 저장
53+
54+
let min = 7-correct >= 6 ? 6 : 7-correct
55+
56+
let max = min-zeros < 1 ? 1 : min-zeros
57+
58+
answer = [max,min]
59+
60+
return answer
6161
}
6262

6363
// 정답 4 - jaewon1676
6464
function solution(lottos, win_nums) {
65-
var answer = [];
66-
let max = 7;
67-
let min = 7;
68-
console.log(lottos)
69-
console.log(win_nums)
70-
for(let i=0; i<6; i++){
71-
if (lottos.includes(win_nums[i])){
72-
max--;
73-
}
74-
}
75-
min = max;
76-
for(let i=0; i<6; i++){
77-
if (lottos[i] == 0) min--;
65+
var answer = []
66+
let max = 7
67+
let min = 7
68+
console.log(lottos)
69+
console.log(win_nums)
70+
for (let i = 0; i < 6; i++) {
71+
if (lottos.includes(win_nums[i])) {
72+
max--
7873
}
79-
if (max == 7) max = 6
80-
if (min == 7) min = 6
81-
answer = [min, max]
82-
return answer;
74+
}
75+
min = max
76+
for (let i = 0; i < 6; i++) {
77+
if (lottos[i] == 0) min--
78+
}
79+
if (max == 7) max = 6
80+
if (min == 7) min = 6
81+
answer = [min, max]
82+
return answer
8383
}
8484

8585
//정답 5 - yongchanson
8686
function solution(lottos, win_nums) {
87-
//최고당첨개수 : maxPoint + basicPoint
88-
//최저당첨개수 : basicPoint
89-
90-
let basicPoint = 0;
91-
let maxPoint = 0;
92-
let answer = [];
93-
94-
lottos.forEach(function (lottos_item) {
95-
win_nums.forEach(function (win_nums_item) {
96-
if(lottos_item==win_nums_item) {
97-
basicPoint++;
98-
}
99-
})
100-
})
87+
//최고당첨개수 : maxPoint + basicPoint
88+
//최저당첨개수 : basicPoint
89+
90+
let basicPoint = 0
91+
let maxPoint = 0
92+
let answer = []
10193

102-
lottos.forEach(function (item) {
103-
if(item==0) {
104-
maxPoint++;
105-
}
94+
lottos.forEach(function (lottos_item) {
95+
win_nums.forEach(function (win_nums_item) {
96+
if (lottos_item == win_nums_item) {
97+
basicPoint++
98+
}
10699
})
100+
})
101+
102+
lottos.forEach(function (item) {
103+
if (item == 0) {
104+
maxPoint++
105+
}
106+
})
107+
108+
maxPoint + basicPoint >= 2
109+
? answer.push(7 - maxPoint - basicPoint)
110+
: answer.push(6)
111+
basicPoint >= 2 ? answer.push(7 - basicPoint) : answer.push(6)
107112

108-
maxPoint+basicPoint >= 2 ? answer.push(7- maxPoint - basicPoint) : answer.push(6);
109-
basicPoint >= 2 ? answer.push(7- basicPoint) : answer.push(6);
113+
return answer
114+
}
115+
116+
//정답 6 - prove-ability
117+
function solution(lottos, win_nums) {
118+
// 맞춘 수와 0의 갯수 활용할 변수 0으로 초기화
119+
let winCount = 0
120+
let zeroCount = 0
121+
// 내 로또 번호 하니씩 접근
122+
lottos.forEach((num) => {
123+
// 번호가 0이 아니고 당첨 번호라면
124+
if (num !== 0 && win_nums.includes(num)) {
125+
winCount++
126+
// 번호가 0이라면
127+
} else if (num === 0) {
128+
zeroCount++
129+
}
130+
})
131+
// 일치한 수와 등수는 반비례하기 때문에 빼기 7
132+
// 이때 등수를 벗어나면 낙첨(6) 으로 고정
133+
let max = 7 - (winCount + zeroCount)
134+
if (max > 5) max = 6
135+
let min = 7 - winCount
136+
if (min > 5) min = 6
110137

111-
return answer;
138+
return [max,min]
112139
}

‎level-1/신고-결과-받기.js

Lines changed: 87 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,100 @@
22
//완벽한 정답이 아닙니다.
33
//정답 1 - codeisneverodd
44
function solution(id_list, report, k) {
5-
//report를 set을 이용하여 중복제거, 각 id 당 신고당한 횟수 reportedCount에 저장,
6-
//각 id를 신고한 사람 array를 reportedBy에 저장,
7-
//k번 이상 신고당한 id를 신고한 id가 받을 메일 수를 mailCount에 저장
8-
//answer에 mailCount에 저장된 값을 id_list와 같은 id 순서로 저장.
9-
const reportSet = new Set(report)
10-
const reportedCount = {}//{"id": Number(count)}
11-
const reportedBy = {}//{"id":[]}
12-
const mailCount = {}//{"id":Number(count)}
13-
id_list.forEach((element) => {//Initialization
14-
reportedCount[element]=0
15-
mailCount[element] = 0
16-
reportedBy[element] = []
17-
})
18-
reportSet.forEach((element)=>{
19-
const$id=(element.split(" "))[0]
20-
const $reported = (element.split(" "))[1]
21-
reportedCount[$reported]+=1
22-
reportedBy[$reported].push($id)
23-
})
24-
for(constreportedIdinreportedCount){
25-
if(reportedCount[reportedId]>=k) {
26-
reportedBy[reportedId].forEach((reporter)=> {
27-
mailCount[reporter]+=1;
28-
})
29-
}
5+
//report를 set을 이용하여 중복제거, 각 id 당 신고당한 횟수 reportedCount에 저장,
6+
//각 id를 신고한 사람 array를 reportedBy에 저장,
7+
//k번 이상 신고당한 id를 신고한 id가 받을 메일 수를 mailCount에 저장
8+
//answer에 mailCount에 저장된 값을 id_list와 같은 id 순서로 저장.
9+
const reportSet = new Set(report)
10+
const reportedCount = {}//{"id": Number(count)}
11+
const reportedBy = {}//{"id":[]}
12+
const mailCount = {}//{"id":Number(count)}
13+
id_list.forEach((element) => {
14+
//Initialization
15+
reportedCount[element] = 0
16+
mailCount[element] = 0
17+
reportedBy[element]=[]
18+
})
19+
reportSet.forEach((element)=>{
20+
const $id = element.split(" ")[0]
21+
const$reported=element.split(" ")[1]
22+
reportedCount[$reported]+=1
23+
reportedBy[$reported].push($id)
24+
})
25+
for(constreportedIdinreportedCount) {
26+
if(reportedCount[reportedId]>=k) {
27+
reportedBy[reportedId].forEach((reporter)=>{
28+
mailCount[reporter]+=1
29+
})
3030
}
31-
var answer = []
32-
id_list.forEach((element) => {
33-
answer.push(mailCount[element])
34-
})
31+
}
32+
var answer = []
33+
id_list.forEach((element) => {
34+
answer.push(mailCount[element])
35+
})
3536

36-
return answer
37+
return answer
3738
}
3839

3940
//정답 2 - jaewon1676
4041
function solution(id_list, report, k) {
41-
42-
let answer = new Array(id_list.length).fill(0)
43-
let report_list = {} // 신고당한 ID
44-
45-
// key, value 형식의 report_list 객체를 만든다.
46-
id_list.map((user)=>{
42+
let answer = new Array(id_list.length).fill(0)
43+
let report_list = {} // 신고당한 ID
44+
45+
// key, value 형식의 report_list 객체를 만든다.
46+
id_list.map((user) => {
4747
report_list[user] = [] //key = userid , value = 빈 배열을 가지는 객체
48-
})
49-
// report_list { muzi: [], frodo: [], apeach: [], neo: [] }
48+
})
49+
// report_list { muzi: [], frodo: [], apeach: [], neo: [] }
5050

51-
// 유저가 신고한 ID를 report_list 객체에 넣어주기 위해 순회한다.
52-
report.map((user)=>{
53-
const [user_id, report_id] = user.split(' ')
54-
// report 값에서 띄어쓰기로 구분된 문자열을 자르고 user_id, report_id로 각각 넣어준다.
55-
if(!report_list[report_id].includes(user_id)){
56-
report_list[report_id].push(user_id)
57-
// 배열에 포함하는지 여부를 확인하여 포함하지 않을때 신고자의 이름을 추가하였다.
58-
}
51+
// 유저가 신고한 ID를 report_list 객체에 넣어주기 위해 순회한다.
52+
report.map((user) => {
53+
const [user_id, report_id] = user.split(" ")
54+
// report 값에서 띄어쓰기로 구분된 문자열을 자르고 user_id, report_id로 각각 넣어준다.
55+
if (!report_list[report_id].includes(user_id)) {
56+
report_list[report_id].push(user_id)
57+
// 배열에 포함하는지 여부를 확인하여 포함하지 않을때 신고자의 이름을 추가하였다.
58+
}
59+
})
60+
for (const key in report_list) {
61+
// report_list의 index 순회
62+
if (report_list[key].length >= k) {
63+
report_list[key].map((user) => {
64+
answer[id_list.indexOf(user)] += 1
65+
})
66+
}
67+
}
68+
return answer
69+
}
5970

60-
})
61-
for(const key in report_list){ // report_list의 index 순회
62-
if(report_list[key].length >= k){
63-
report_list[key].map((user)=>{
64-
answer[id_list.indexOf(user)] += 1
65-
})
66-
}
71+
//정답 3 - prove-ability
72+
function solution(id_list, report, k) {
73+
// 사용자 길이의 빈 배열을 만든다 0
74+
var answer = new Array(id_list.length).fill(0)
75+
// 신고된 내용 정리할 객체 선언
76+
const obj = {}
77+
// 신고된 내용 정리할 객체 사용자 아이디로 초기화
78+
id_list.forEach((id) => {
79+
obj[id] = new Set()
80+
})
81+
// 신고 리스트를 조회하며 신고 내용 객체에 정리
82+
// set.add 를 활용해 같은 사용자의 중복 신고 제거
83+
report.forEach((str) => {
84+
const [reporter, target] = str.split(" ")
85+
obj[target].add(reporter)
86+
})
87+
// obj value 하나씩 접근
88+
for (const value of Object.values(obj)) {
89+
// 신고당한 횟수가 정지 기준에 적합하다면
90+
if (value.size >= k) {
91+
// 해당 유저를 신고한 사람들
92+
for (let item of value) {
93+
// 인덱스 구해서 증가
94+
let index = id_list.indexOf(item)
95+
answer[index]++
96+
}
6797
}
68-
return answer
69-
}
98+
}
99+
100+
return answer
101+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /