[python ๊ธฐ์ดˆ] Code-kata week2-5

EMMAยท2022๋…„ 3์›” 25์ผ
0

[wecode] Code-kata

๋ชฉ๋ก ๋ณด๊ธฐ
10/12

๐Ÿ–ฅ Code-kata week2-5


๋ฌธ์ œ
์ธ์ž์ธ height๋Š” ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.๊ทธ๋ž˜ํ”„๋กœ ์ƒ๊ฐํ•œ๋‹ค๋ฉด y์ถ•์˜ ๊ฐ’์ด๊ณ , ๋†’์ด ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜์˜ ๊ทธ๋ž˜ํ”„๋ผ๋ฉด height ๋ฐฐ์—ด์€ [1, 8, 6, 2, 5, 4, 8, 3, 7] ์ž…๋‹ˆ๋‹ค.

์ € ๊ทธ๋ž˜ํ”„์— ๋ฌผ์„ ๋‹ด๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ณ , ๋ฌผ์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๋„“์€ ๋ฉด์ ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ์„ธ์š”.


ํ’€์ด

์‚ฌ์‹ค ์ด ๋ฌธ์ œ๋Š”, code kata ์‹œ๊ฐ„ ๋•Œ ๋‹ค๋ฅธ ๋™๊ธฐ๋“ค์ด ํ’€์—ˆ๋˜ ๋ฐฉ๋ฒ•์„ ์ž˜ ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜๋‹ค๊ฐ€ ์ง‘์—์™€์„œ ๋‹ค์‹œ ๋ณด๋‹ˆ ์ข€ ๋” ๋ฌธ์ œ๊ฐ€ ์ž˜ ๋ณด์˜€๋‹ค.

๊ฐ€์žฅ ๋‹จ์ˆœํ•˜๊ฒŒ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” brute force ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด:

  • ๋จผ์ € ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์žก๋Š”๋‹ค.
  • ๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ถ€ํ„ฐ ๊ณฑํ•œ๋‹ค: height[0] x height[1], height[0] x height[2] ...
  • ์ด๋ฒˆ์—” ๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋˜ ๋ˆ๋‹ค: height[1] x height[2], height[2] x height[3]...
  • ๊ทธ ์ค‘ ์ตœ๋Œ€๊ฐ’์„ return ํ•œ๋‹ค
def get_max_area(height):
	result = 0 
    for i in range(0,len(height)-1):
    	for j in range(i+1, len(height)):
        	x = j - i 
            y = min(height[i], height[j]) 
            area = x * y 
        	result = max(result, area)
        
    return result 
profile
์˜ˆ๋น„ ๊ฐœ๋ฐœ์ž์˜ ๊ธฐ์ˆ  ๋ธ”๋กœ๊ทธ | explore, explore and explore

0๊ฐœ์˜ ๋Œ“๊ธ€