-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
17-kangrae-jo #61
base: main
Are you sure you want to change the base?
17-kangrae-jo #61
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ง ๋๋ฌด๋ ์ด๋ ค์ด ๋ฌธ์ ๋ค์.. ์ด๊ฒ ๋ ๋ฒจ 3์ด๋ผ๋๊ฒ ๋ฏฟ๊ธฐ์ง๊ฐ ์์ ์ ๋๋ก..
์ ๋ 4์๊ฐ์ ๋๊ฒ ๊ฑธ๋ฆฐ๊ฒ ๊ฐ์์. 3์๊ฐ ์ ๋ ํ๋ค๊ฐ ์ค๊ณ๊ฐ ์๋ชป๋์๋ค๋ ๊ฑธ ๊นจ๋ซ๊ณ ๊ฐ๋๋ ์ฝ๋๋ณด๊ณ ์ ์ ์๋ชป๋ ์ค๊ณ๋ฅผ ๋์น์ฑ ํ ๋ค์ ์ค๊ณ๋ถํฐ ์์ํ์ต๋๋ค.
๊ทธ๋ฌ๋ค ๋ณด๋ ๊ฐ๋๋ ์ฝ๋์ ๋น์ทํ๊ฒ ์ขํ๋ฅผ key
๋ก ํ๊ณ ์ถฉ๋๋๋ ๋ก๋ด์ value
๋ก ํ๋ map
์ ์ฌ์ฉํ์๊ณ ์์ฐ์ค๋ time
์ด๋ผ๋ ์ซ์ ๊ฐ์ index๋ก ๊ตฌํํ์ต๋๋ค.
์์ฑํ๊ณ ๋๋ ์ ์ฝ๋๊ฐ ๊ฐ์ฅ ์ฌ์๋ณด์๋๋ฐ ๊ฐ๋๋์ ์ฝ๋๋ฅผ ๋ณด๊ณ ๋ ํ ๋ถํ์ํ ๊ตฌ์กฐ์ฒด๋ก ์ธํด ๋ ๋ณต์กํด์ง ๊ฒ ๊ฐ์ ์์ฝ๋ค์. ์ค์ ์ฝ๋๋ฅผ ๋ณด๋ฉด ๊ตฌ์กฐ์ฒด๊ฐ ํ์์์ด๋ ๋ ๋ฏ ํฉ๋๋ค. (์ฌ์ค set์ ์ฌ์ฉํ๊ธฐ ์ํด์ ๊ตฌ์กฐ์ฒด์ ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ์ ์ฌ์ฉํ์๋๋ฐ ์๋ฏธ๊ฐ ์์ด์ก๋ค๋๊ฑธ ์์ด๋ฒ๋ ธ์๋ค์.)
code
#include <string>
#include <vector>
#include <map>
using namespace std;
struct Coord {
int r, c;
Coord(vector<int> point) : r(point[0] - 1), c(point[1] - 1) {}
bool operator==(const Coord &other) const {return r==other.r && c==other.c;}
bool operator!=(const Coord &other) const { return !(*this == other); }
bool operator<(const Coord &other) const {
return (r < other.r) || (r == other.r && c < other.c);
}
string str() const { return "(" + to_string(r) + "," + to_string(c) + ")"; }
};
void move(Coord &start, Coord &end) {
if (start.r < end.r) start.r++;
else if (start.r > end.r) start.r--;
else if (start.c < end.c) start.c++;
else if (start.c > end.c) start.c--;
}
int solution(vector<vector<int> > points, vector<vector<int> > routes) {
vector<map<string, int> > report(20000);
for (auto &route: routes) {
int time = 0;
for (int i = 1; i < route.size(); i++) {
int s = route[i - 1]; //์ถ๋ฐ์ง
int e = route[i]; //๋ชฉ์ ์ง
Coord start = Coord(points[s - 1]);
Coord end = Coord(points[e - 1]);
if (time == 0) report[time++][start.str()]++; //์ถ๋ฐ์ง๋ฅผ ๊ธฐ๋ก
while (start != end) {
//move
move(start, end);
//write
report[time][start.str()]++;
time++;
}
}
}
int danger = 0;
for (int time = 0; time < 20000; time++) {
if (report[time].empty()) break;
for (auto &[coord, dup]: report[time])
if (dup >= 2) danger++;
}
return danger;
}
if (y1 != y2) y1 += (y2 > y1 ? 1 : -1); | ||
else x1 += (x2 > x1 ? 1 : -1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ก๋ด์ด ์์ฃผ ์์ ์ ์ผ๋ก ์์ง์ด๋ ๊ตฐ์. ์ ๋ง ๊ฐ๋จํ๊ฒ ์ ๊ตฌํํ์ จ๋ค์ ๐
int answer = 0; | ||
|
||
// ๊ฐ ์์ ์์ ๋ก๋ด๋ค์ ์์น๋ฅผ ๊ธฐ๋กํ ๋งต (์๊ฐ, ์ขํ) -> ๋ก๋ด ์ | ||
vector<map<pair<int, int>, int>> history(100005); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ์๊ฐ์ ์ต๋๋ 20000์ผ๋ก ์ก์์ต๋๋ค. ๋ก๋ด์ด ๋์๋ค๋๋ ์ง๋์ size๋ ์ต๋ 100x100 ์ด์ด์ ์ต๋ ๊ฑฐ๋ฆฌ๋ 200์ด๋ผ ์๊ฐํ๊ณ ์ด๊ฒ์ด ๋ฌธ์ ์ ์กฐ๊ฑด์ ์ํด ์ต๋ 100๋ฒ ๋ฐ๋ณต๋ ์ ์์ด์ 20000์ผ๋ก ํ์ต๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ถํ์ํ ๊ตฌ์กฐ์ฒด๋ก ์ธํด ๋ ๋ณต์กํด์ง ๊ฒ ๊ฐ์ ์์ฝ๋ค์.
๋ง์์ ์ ๋ ๋๋ฒ์งธ ์๋๋ก ๊ตฌ์กฐ์ฒด๊ฐ ๋ ์ฌ๋๋๋ฐ ๋น๊ต ์ฐ์ฐ ์ ์ํ๋๊ฒ ๊ฝค ํฐ์์ ์ด๋ผ ์๊ฐํด์ ํฌ๊ธฐํ์ต๋๋ค...
์ ๋ ์๊ฐ์ ์ต๋๋ 20000์ผ๋ก ์ก์์ต๋๋ค. ๋ก๋ด์ด ๋์๋ค๋๋ ์ง๋์ size๋ ์ต๋ 100x100 ์ด์ด์ ์ต๋ ๊ฑฐ๋ฆฌ๋ 200์ด๋ผ ์๊ฐํ๊ณ ์ด๊ฒ์ด ๋ฌธ์ ์ ์กฐ๊ฑด์ ์ํด ์ต๋ 100๋ฒ ๋ฐ๋ณต๋ ์ ์์ด์ 20000์ผ๋ก ํ์ต๋๋ค.
์ ๊ทธ๋ฐ๊ฐ์? ๋ณด๋์ ๋ชจ๋ ์นธ์ ๊ฒฝ์ ํ๋ค๊ณ ํ์๋ 10_000 ์ฏค ์ด๋ผ๊ณ ์๊ฐํ๋๋ฐ .....
์ด์ ๋ณด๋๊น 100_005 ๋ผ๊ณ 10๋ฐฐ๋ ํฌ๊ฒ ์ก์๋ฒ๋ ธ์๋ค์ ใ
ใ
ใ
ใ
history[time][{y1, x1}]++; | ||
time++; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
time์ ์ธ๋ฑ์ค๋ก ์ขํ๋ฅผ ์ ์ฅํ๋ ์ปจํ
์ด๋๋ฅผ ๋ ์ฌ๋ฆด ์ ์๋ค๋
๋๋จํฉ๋๋ค..
์ ๋ ๋ฌธ์ ์ดํด์๋ง ์ค๋ ์๊ฐ์ด ๊ฑธ๋ ธ๊ณ
๊ตฌํ๋ ๊ฒฐ๊ตญ ์ง์ ์ ์ฐธ๊ณ ๋ง ํด์ ์ ์ฝ๋๊ฐ ์๋๋ค์...
๋ค๋ง ์ฐธ๊ณ ํ๋ฉด์ ๋ฐฐ์ด ์ ์
์๋ฐ์์๋ Map์ ์ฌ์ฉํ์ฌ ๊ฐ ์ขํ์ ์์๋ฅผ ๋ฉ๊ธธ ์ ์์์ต๋๋ค.
Map<Integer, int[]> pointMap = new HashMap<>();
for (int i = 0; i < points.length; i++) {
pointMap.put(i + 1, points[i]);
}
์ด๋ฐ ์์ผ๋ก ์ฌ๊ธฐ์ points = [[3,2], [6,4]]๋ฅผ ์
๋ ฅํ๋ฉด
pointMap = {1 -> [3,2], 2 -> [6,4]}๊ฐ ์ ์ฅ๋ฉ๋๋ค.
๋ค์ ๊ธฐํ์ ํด๋น ๋ฌธ์ ๋ค์ ํ์ด๋ณด๊ณ ์ฌ๊ธฐ์ผ์ ํ์ฌ PCCP ์ฌ๋์ ํด๋ณด๊ฒ ์ต๋๋ค.
์๊ณ ํ์
จ์ต๋๋ค!
๐ ๋ฌธ์ ๋งํฌ
[์ถฉ๋ ์ํ ์ฐพ๊ธฐ]
โ๏ธ ์์๋ ์๊ฐ
3h ์ด์...
(๋ฆฌํฉํ ๋ง ํฌํจ)
โจ ์๋ ์ฝ๋
PCCP ๊ธฐ์ถ 3๋ฒ ๋ฌธ์ ์ ๋๋ค (Lv.2)
์ต๊ทผ ํ์๋ ๋ฌธ์ ์ค์ ๊ฐ์ฅ ํ๊ธฐ ํ๋ค์๋ ๋ฌธ์ ์ธ ๊ฒ ๊ฐ๋ค์.
๋ฌธ์ ์ ํ์
์๋ฎฌ๋ ์ด์
์ ๋ฃ์ด๋จ์ง๋ง๊ตฌํ
์ ๋๋์ด ๊ฐํฉ๋๋ค.์ ๋ ์ด ๋ฌธ์ ์ฝ๊ณ ์ดํดํ๋ ๋ฐ๋ง 15๋ถ์ด ๋๊ฒ ๊ฑธ๋ฆฐ ๊ฒ ๊ฐ์์.
์๊ฐ๋ณด๋ค ๊ณ ๋ คํด์ผํ ์ฌํญ์ด ๋ง๋๋ผ๊ตฌ์.
๊ทธ๋์ ์ ๊ฐ ๋ช์๊ฐ ๋์ ์๊ฐํ ํ์ด์ ํ๋ฆ ์์๋๋ก ์๋ ค๋๋ฆด๊ฒ์.
์ฐธ๊ณ ๋ก ๋๋ฒ์งธ ๋ฐฉ์๋ถํฐ๋ ์งํผํฐ์ ํจ๊ปํ์ต๋๋ค.
์ฒซ ๋ฒ์งธ๋ก ๋ ์ค๋ฅธ ๋ฐฉ์
( ์คํจ, ๋ ผ๋ฆฌ์ ์ค๋ฅ๊ฐ ์์, 1์๊ฐ ์ด์ ์์ )
์ ์ผ ์ฒ์์๋ 2์ฐจ์ ๋ฐฐ์ด์ ๋ง๋ค์ด์ ๋ก๋ด์ ํ๋์ฉ ํฌ์ธํธ๋ก ์์ง์ด๊ณ , ์์ง์ธ ๋ก๋ด์ด ๋ค๋ ๊ฐ ์๋ฆฌ๋ 1, 2, 3, .. ์ด๋ ๊ฒ
ํ์
์ ๋จ๊ธฐ๊ณ ๊ฐ๋ ์ฝ๋๋ฅผ ์์ฑํ์ต๋๋ค.์ด๋ ๊ฒ ํ๋ฉด ๊ฐ์ ์ซ์์
ํ์
์ ๋จ๊ธด ๋ก๋ด์ ๊ฐ์ ์๊ฐ์ ๊ฒน์ณค๋ค๋ ์๋ฏธ๊ฐ ๋๊ฒ ์ฃ ?์ด๋ ์นด์ดํธ๋ฅผ +1 ํ๋ฉด ๋ฉ๋๋ค.
๊ทธ๋ฐ๋ฐ ์ด ๋ฐฉ์์ ์๋ชป๋ ๋ฐฉ์์ ํ์ด์ ๋๋ค.
๋ฐ๋ก๋ฅผ ๋ค์ด๋ณด๊ฒ ์ต๋๋ค. ์์ 3๋ฒ์ ์ผ์ด์ค ์ฒ๋ผ ๋ก๋ด์ด ์์ง์ผ ๋, ์ด์ ์ ์์ง์ธ ๋ก๋ด์
ํ์
์ ํ์ฌ ์์ง์ด๋ ๋ก๋ด์ด ์์ ์ํ์
์ผ๋ก ๋ฎ์ด ์ฐ๊ฒ ๋ฉ๋๋ค!์ด ์ค๋ฅ๋ฅผ ์๊ฐํ๋๋ฐ ์์ฒญ ์๊ฐ์ด ๋ค์๋ค์...
๋ ๋ฒ์งธ๋ก ๋ ์ค๋ฅธ ๋ฐฉ์
( ์คํจ, ๊ตฌํ์ ์ด๋ ค์ 1์๊ฐ ์ด์ ์์ )
๊ทธ๋์ ๋ ์ค๋ฅธ ๋ฐฉ๋ฒ์ ๋ก๋ด์ ์์ง์(ํ์ )์ ๊ธฐ๋กํ๋ 2์ฐจ์ ๋ฐฐ์ด์ ๋ง๋ค์ง ๋ง์ ์ ๋๋ค.
๋์ , ์ถฉ๋ ๋น๊ต ์ ์ฌ์ฉ๋๋
x ์ขํ, y ์ขํ, time
์ผ๋ก ๊ตฌ๋ถ๋๋ ๋ฐฐ์ด์ ๋ง๋ค์ด์ผ ๊ฒ ๋ค๊ณ ์๊ฐํ์ต๋๋ค.๋ฐ๋ก ๋ ์ค๋ฅธ ๊ฒ์ ๊ตฌ์กฐ์ฒด๋ฅผ ํ์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ํ์ฉํ๋ ๊ฒ์ด์์ต๋๋ค.
๊ทธ๋ฐ๋ฐ ๋ฌธ์ ๋ ์ฌ์ฉ์๊ฐ ์ ์ํ ๊ตฌ์กฐ์ฒด๋ฅผ
key
๋ก ์ฌ์ฉํ ๊ฒฝ์ฐ==
operation์ ์ ์ํด์ค์ผํ๊ณ , ํด์ฌ ํจ์๋ ์ ์ํด์ผํ๋ค๋ ์ ์ ๋๋ค...์ด๊ฑด ์๋๋ค ์ถ์ด์ ๋ค์ ๋ฐฉ๋ฒ์ผ๋ก ๋์ด๊ฐ๋๋ค.
์ธ ๋ฒ์งธ๋ก ๋ ์ค๋ฅธ ๋ฐฉ์
( ์ฑ๊ณต, ์ปจํ ์ด๋์ ์๋ฏธ๋ง ์ดํดํ๋ฉด ์ฌ์ )
๊ทธ ๋ค์ ๋ ์ค๋ฅธ ๋ฐฉ์์
time
์ ์ธ๋ฑ์ค๋ก ๊ฐ์ง๋ ์ปจํ ์ด๋์x์ขํ, y์ขํ
๋ฅผ ๋ฃ๋ ๊ฒ์ ๋๋ค.์ด๋ฅผ ๊ตฌํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ปจํ ์ด๋๊ฐ ๋ฉ๋๋ค!
์ด๊ฑฐ๋ฅผ ์ ๊ฐ ๋ค์ ๊ตฌํํ ์ ์์์ง ๋ชจ๋ฅด๊ฒ ๋ค์...
(์ดํดํ๊ธฐ ์ด๋ ต๋ค๋ฉด
์๊ฐ
๊ณผ์ขํ
๋ฅผ ์ฌ์ฉํ 2์ฐจ์ ๋ฐฐ์ด... ์ฒ๋ผ ์๊ฐํด๋ ๋ ๋ฏ ํฉ๋๋ค?)์ด๋ ๊ฒ ํ๋ฉด ๊ฐ์ ์๊ฐ๋ง๋ค ๋ก๋ด์ ์์น๋ฅผ ์ ์ ์๊ณ , ๊ทธ ์ค ๊ฐ์
x์ขํ, y์ขํ
๋ฅผ ๊ฐ์ง ๋ก๋ด์ ์ถฉ๋์ํ์ด ์๋ ๊ฒ์ด ๋์ฃ !์ฐธ๊ณ ๋ก
history
์ ํฌ๊ธฐ๋100005
๋ก ์์์ ์ต๋๊ฐ์ผ๋ก ์ค์ ํ์ต๋๋ค.์ต๋๊ฐ์ด๋ผ๋ ๊ทผ๊ฑฐ๋
r๊ณผ c์ ์ต๋๊ฐ์ 100
์ด๊ณ , ์ต๋ ์ด๋ ๊ฑฐ๋ฆฌ๋100 * 100
์ด๋๊น ์ต๋ ์ด๋ ์๊ฐ๋ ํ100000
์ด๊ฒ ์ฃ ?๋๋จธ์ง ๊ตฌํ ์ฝ๋๋ ์ ์ฝ์ด๋ณด๋ฉด ์ดํด๋ ๊ฑฐ์์.
์ถ๊ฐ๋ก ์ฃผ์ํ ์ ์ด ์๊ธดํฉ๋๋ค.
์ด ์ ๋๊ฐ ์๊ฒ ๋ค์
์ฐธ๊ณ ๋ก
๋ค๋ฅธ ์ฌ๋์ ํ์ด
๋ฑ ๋ค๋ฅธ ์ ๋ต์ง ๋ณด๋ค ์ ํ์ด๊ฐ ์ ์ผ ์ฝ์ต๋๋ค. ( ์๋ง๋์.. )์ ๊ฐ ๋ช ์๊ฐ ๋์ ํ ๊ณ ๋ฏผ์ด ๋ค ๋ด๊ฒผ๋์ง ๋ชจ๋ฅด๊ฒ ๋ค์...
์ง๋ฌธ๋ ๋ง์ด ํด์ฃผ์ธ์
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์ฌ๋ฌ ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋์ผ์ฑ ๋น๊ต๋ฅผ ํ๊ณ ์ถ์ ๋๋ ์ปจํ ์ด๋๋ฅผ ์ ํ์ฉํ์!