File tree Expand file tree Collapse file tree 2 files changed +78
-0
lines changed
minjeong/TwoPointer_SlidingWindow Expand file tree Collapse file tree 2 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 1+ import sys
2+ input = sys .stdin .readline
3+
4+ # N: ์ผ์ ์๋์ด ์, K: ๋จ์ด์ง ๊ฑฐ๋ฆฌ
5+ N , K = map (int , input ().split ())
6+ buckets = []
7+ max_x = 0
8+ for _ in range (N ):
9+ # g: ์ผ์ ์, x: ์๋์ด ์ขํ
10+ g , x = map (int , input ().split ())
11+ buckets .append ((g , x ))
12+ max_x = max (max_x , x )
13+
14+ arr = [0 ] * (max_x + 1 )
15+ for g , x in buckets :
16+ arr [x ] += g
17+
18+ window_size = 2 * K + 1
19+ current = sum (arr [:window_size ])
20+ answer = current
21+ for i in range (window_size , max_x + 1 ):
22+ current += arr [i ] - arr [i - window_size ]
23+ answer = max (answer , current )
24+
25+ print (answer )
Original file line number Diff line number Diff line change 1+ import sys
2+ input = sys .stdin .readline
3+
4+ N = int (input ())
5+ coding = list (map (int , input ().split ()))
6+ coding .sort ()
7+ answer = 0
8+
9+ for i in range (N ):
10+ target = - coding [i ]
11+ left = i + 1
12+ right = N - 1
13+
14+ while left < right :
15+ two_sum = coding [left ] + coding [right ]
16+ if two_sum < target :
17+ left += 1
18+ elif two_sum > target :
19+ right -= 1
20+ else : # two_sum == target
21+ if coding [left ] != coding [right ]:
22+ left_value = coding [left ]
23+ right_value = coding [right ]
24+
25+ # ์ผ์ชฝ์ ๊ฐ์ ๊ฐ(left_value)์ด ์ฐ์์ผ๋ก ๋ช ๋ช
์๋์ง ์ผ๋ค
26+ cnt_left = 1
27+ while left + cnt_left < right and coding [left + cnt_left ] == left_value :
28+ cnt_left += 1
29+
30+ # ์ค๋ฅธ์ชฝ์ ๊ฐ์ ๊ฐ(right_value)์ด ์ฐ์์ผ๋ก ๋ช ๋ช
์๋์ง ์ผ๋ค
31+ cnt_right = 1
32+ while right - cnt_right > left and coding [right - cnt_right ] == right_value :
33+ cnt_right += 1
34+
35+ # ์ด ์กฐํฉ๋ค ๊ฐ๊ฐ์ ์ ๋ถ ์๋ก ๋ค๋ฅธ ํ์ ์กฐํฉ์ด๋ฏ๋ก
36+ # ๊ฒฝ์ฐ์ ์ = cnt_left * cnt_right
37+ answer += cnt_left * cnt_right
38+
39+ # ์ธ์ผ ํ ํ์์ ์ฒ๋ฆฌํ์ผ๋ฏ๋ก, ํฌ์ธํฐ ํ ๋ฒ์ ์ด๋
40+ left += cnt_left
41+ right -= cnt_right
42+
43+ else : # coding[left] == coding[right]
44+ # ์ด ๊ตฌ๊ฐ์ ๋จ์ ํ์ ์
45+ m = right - left + 1
46+
47+ # m๋ช
์ค 2๋ช
์ ๋ฝ๋ ์กฐํฉ ์ C(m, 2)
48+ answer += m * (m - 1 ) // 2
49+
50+ # i์ ๋ํด ์
์กฐํฉ ์ข
๋ฃ
51+ break
52+
53+ print (answer )
You canโt perform that action at this time.
0 commit comments