Skip to content

juneyr.dev

ํ—ท๊ฐˆ๋ฆฌ๋Š” leetcode ๋ฌธ์ œ ์ •๋ฆฌํ•˜๊ธฐ

โ€” 2 min read

152. maximum product array

Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.

์ฃผ์–ด์ง„ integer array nums ์— ๋Œ€ํ•ด์„œ, ๊ณฑ์ด ๊ฐ€์žฅ ํฐ ์—ฐ์†์ ์ธ subarray๋ฅผ ์ฐพ์œผ์‹œ์˜ค. ๋‹ต์€ ๊ณฑ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, array์—๋Š” ์ตœ์†Œ ํ•˜๋‚˜์˜ ์ˆซ์ž๊ฐ€ ์žˆ์Œ

Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6.

์ฒ˜์Œ ์ ‘๊ทผ (ํ†ต๊ณผ๋ชปํ•จ !)

  • maximum sum ๊ณผ ๋น„์Šทํ•  ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ•ด์„œ ๊ณ„์† two pointer ์ƒ๊ฐํ–ˆ์ง€๋งŒ, ์ ‘๊ทผ์ด ์•ˆ๋จ.

  • ๊ฒฐ๊ตญ ์ผ๋ฐ˜๋ก ์œผ๋กœ O(n^3) ์œผ๋กœ ์ ‘๊ทผํ•ด๋ณด์•˜์ง€๋งŒ ์—ญ์‹œ๋‚˜ time limit ์— ๊ฑธ๋ ธ๋‹ค.

Solution ์ ‘๊ทผ

์ฒ˜์Œ์—๋Š” ์ด๊ฒŒ ๋ญ์•ผ? ์‹ถ์ง€๋งŒ ์ฒœ์ฒœํžˆ ๋”ฐ๋ผ๊ฐ€๋ฉด ์กฐ๊ธˆ ๋„์›€์ด ๋œ๋‹ค. ํฌ์ธํŠธ๋Š” ์Œ์ˆ˜์ธ product ๊ฐ’๋„ ๋‹ค์Œ์— ์Œ์ˆ˜๋ฅผ ๋งŒ๋‚˜๋ฉด max๊ฐ’์ด ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋‘๊ฐœ๋ฅผ ๊ฐ™์ด ํŠธ๋ž˜ํ‚นํ•˜๋Š”๊ฒƒ

  • ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •๋„ ์ข‹์•˜๋‹ค. ๋งจ ์•ž์˜ ์ˆซ์ž๋กœ ์„ค์ •.
  • nums[1:] ๋ถ€ํ„ฐ ๋Œ๋ฆฌ๋ฉด ๋˜๋‹ˆ๊นŒ.
  • max, min ์„ ์ •ํ•˜๋Š” ๋ฐฉ์‹. ์ด๋ฒˆ์— ๋งŒ๋‚œ ์ˆซ์ž ํ•˜๋‚˜, ์•„๋‹ˆ๋ฉด ๊ทธ ์ „ min i ํ•œ ๊ฐ’, ๊ทธ ์ „ max i ํ•œ ๊ฐ’ ์ค‘์— ๊ณ ๋ฅด๋ฉด ๋œ๋‹ค.
  • max_prod ๋Š” ์•„์ง ๋ฐ”๋€Œ์ง€ ์•Š์•˜๊ธฐ๋•Œ๋ฌธ์— ํ›„๋ณด์ค‘์— ์ •ํ•ด์ค€๋‹ค. ๊ทธ๋Ÿฐ๋ฐ max_prod์™€ max_curr ๋งŒ ๋น„๊ตํ•˜์ง€ ์•Š๋Š” ์ด์œ ๋Š” ๋ญ˜๊นŒ?

House robber ์‹œ๋ฆฌ์ฆˆ

198. House robber - easy

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. ๋‹น์‹ ์€ ์ „๋ฌธํ„ธ์ด๋ฒ”์ž…๋‹ˆ๋‹ค ๐Ÿ˜ˆ ๊ฐ ์ง‘์€ ์ผ์ •ํ•œ ๋ˆ์ด ์Œ“์—ฌ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ์ ‘ํ•œ ์ง‘์€ ๋ณด์•ˆ์‹œ์Šคํ…œ์ด ์—ฐ๊ฒฐ๋˜์–ด์žˆ์–ด์„œ, ์ธ์ ‘ํ•œ ๋‘ ์ง‘์„ ์—ฐ์†์œผ๋กœ ํ„ธ๋ฉด ์ž๋™์œผ๋กœ ๊ฒฝ์ฐฐ์„ ๋ถ€๋ฅด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police. ๊ฐ ์ง‘์˜ ๋ˆ์„ ์˜๋ฏธํ•˜๋Š” ์ž์—ฐ์ˆ˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฒฝ์ฐฐ์—๊ฒŒ ๋“คํ‚ค์ง€ ์•Š๊ณ  ํ„ธ์ˆ˜์žˆ๋Š” ์ตœ๋Œ€ ๋ˆ์„ ๊ตฌํ•˜์‹œ์˜ค.

Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.

์ฃผ์˜ํ•  ์ 

house rob ๋ฅผ ๊ณ„์† ์ด๋ ‡๊ฒŒ ์ ‘๊ทผํ•˜๋Š”๋ฐ.. ์ง์ˆ˜๋งŒ ํ„ธ๊ณ  ํ™€์ˆ˜๋งŒ ํ„ธ์–ด์„œ max๋ฅผ ๊ตฌํ•˜๋Š” ๊ฑด ์•ˆ๋จนํžŒ๋‹ค. ๋‹ค์Œ๊ฑธ ํ„ธ์—ˆ์„ ๋•Œ vs ํ„ธ์ง€ ์•Š์•˜์„ ๋•Œ์˜ ์ˆ˜๋กœ ๋‚˜๋ˆ ์„œ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ํ˜„๋ช…ํ•˜๋‹ค.

๋‚˜์˜ ์ ‘๊ทผ๋ฒ•

hop์œผ๋กœ ๋›ด๋‹ค. memo๋กœ ์ €์žฅํ•œ๋‹ค.

  • ๋ฆฌ์ŠคํŠธ ๊ธธ์ด๊ฐ€ 1๊ฐœ์ผ๋• ์—ฌ๊ธฐ๋งŒ ํ„ธ๋ฉด๋œ๋‹ค.
  • 2๊ฐœ์ผ๋•Œ๋Š” ๋‘˜์ค‘ ํ•˜๋‚˜๋งŒ ํ„ธ์–ด์•ผํ•œ๋‹ค.
  • 3๊ฐœ ์ผ๋•Œ๋Š” ์ฒซ๋ฒˆ์งธ, ์„ธ๋ฒˆ์งธ๋ฅผ ํ„ธ๊ธฐ vs ๋‘๋ฒˆ์งธ๋ฅผ ํ„ธ๊ธฐ
  • 4๊ฐœ์ผ ๋•Œ๋Š” (1,3) / (2,4) / (1,4) ์ค‘์— ํฐ๊ฒƒ

์ด๋ฅผ ์กฐ๊ธˆ ์ผ๋ฐ˜ํ™”ํ•˜๋ฉด, 1๊ฐœ ๋–จ์–ด์ง„๊ฑธ ๊ฐ€๋Š” ๊ฒฝ์šฐ or ๋‘๊ฐœ๋ฅผ ๋–จ์–ด์ง„ ๊ฑธ ๊ฐ€๋Š” ๊ฒฝ์šฐ๋ฅผ ์ž˜ ์„ ํƒํ•ด์„œ ๊ฐ€๋ฉด ๋œ๋‹ค.

์ด๊ฒŒ ์ž˜ ๋ชฐ๋ž๋Š”๋ฐ ์ „ํ˜•์ ์ธ DP ๋ฌธ์ œ๋ผ๊ณ  ํ•˜๋”๋ผ๊ตฌ. DP๋ฅผ ๋‹ค์‹œ ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ๋‹ค.

Solution

์ถœ์ฒ˜