๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm

[Algorithm/Java][LeetCode] 53. Maximum subarray

๋ฐ˜์‘ํ˜•

[LeetCode] 53. Maximum subarray

https://leetcode.com/problems/maximum-subarray/

๋ฌธ์ œ์ ‘๊ทผ

์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ ์—ฐ์†๋˜๋Š” ๊ฐ’์„ ๋”ํ•œ sum์ด ๊ฐ€์žฅ ํฐ subarray์˜ sum์„ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ค.
์ฒ˜์Œ์— ์ด ๋ฌธ์ œ๋ฅผ ๋ณด์ž๋งˆ์ž ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ธ ๊ฒƒ ๊ฐ™์•„์„œ dp๋ผ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  dp[i]๋Š” i๋ฒˆ์งธ ์ธ๋ฑ์Šค๊นŒ์ง€ ์ค‘์— ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ํฐ subarray์˜ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์„œ ํ’€์—ˆ๋‹ค.(๋ฐฉ๋ฒ•1)
๊ทผ๋ฐ ๋ฐฉ๋ฒ•1๋กœ ํ’€๊ณ ๋ณด๋‹ˆ๊นŒ ๊ตณ์ด dp๋ผ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ์ผ์ผ์ด ๋‹ค ์ €์žฅํ•  ํ•„์š” ์—†์ด ํ˜„์žฌ์ธ๋ฑ์Šค ์ „์˜ subarray๊ฐ’(dp[i-1])๋งŒ ์•Œ๋ฉด ๋  ๊ฑฐ ๊ฐ™์•˜๋‹ค. ์ฆ‰ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์ง€ ์•Š๊ณ  dp[i-1]์„ ์ €์žฅํ•˜๋Š” sum์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ dp[i-1] ์ด์ „ ๊ฐ’(dp[i-2] ... dp[0])์˜ ๊ฐ’์€ ์ €์žฅํ•˜์ง€ ์•Š๊ณ  ํ‘ธ๋Š” ๋ฐฉ์‹์„ ์ผ๋‹ค.(๋ฐฉ๋ฒ•2)
ํ•˜์ง€๋งŒ ์ด์ƒํ•˜๊ฒŒ๋„ LeetCode์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” Runtime๊ณผ Memory ๊ฐ’์—์„œ๋Š” ๋ฐฉ๋ฒ• 1์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋” ์ ์—ˆ๋‹ค.(????) (์•„๋ž˜๊ฐ€ ๋ฐฉ๋ฒ•1, ์œ„๊ฐ€ ๋ฐฉ๋ฒ•2)

Code

//๋ฐฉ๋ฒ•1
class Solution {
    public int maxSubArray(int[] nums) {
        int[] dp = nums; // dp = ํ•ด๋‹น ์ธ๋ฑ์Šค์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด
        dp[0] = nums[0];
        int max = dp[0]; // sum์˜ ์ตœ๋Œ“๊ฐ’ ์ €์žฅ
        for(int i = 1; i<nums.length; i++){
            if(dp[i] < dp[i-1] + nums[i]){
                // ํ˜„์žฌ ๊ฐ’๋ณด๋‹ค i-1๋ฒˆ์งธ ์ตœ๋Œ€๊ฐ’ + ํ˜„์žฌ ์ธ๋ฑ์Šค ๊ฐ’์ด ํด ๊ฒฝ์šฐ
                dp[i] = dp[i-1] + nums[i];
            }
            if(max < dp[i])
                    max = dp[i];
        }
        return max;
    }
}
//๋ฐฉ๋ฒ•2
class Solution {
    public int maxSubArray(int[] nums) {
        int max = nums[0]; // ์ตœ๋Œ€๊ฐ’ ์ €์žฅ
        int sum = nums[0]; // ์ง€๊ธˆ๊นŒ์ง€ sum ๊ฐ’
        for(int i = 1; i<nums.length; i++){
            sum = Math.max(sum+nums[i] , nums[i]); //sum + ํ˜„์žฌ๊ฐ’ ๊ณผ ํ˜„์žฌ๊ฐ’ ์ค‘ ํฐ๊ฒƒ์„ sum์— ์ €์žฅ
            max = Math.max(sum,max); // sum์ด ํฐ ์ง€ max๊ฐ€ ํฐ ์ง€ ๋น„๊ต
        }
        return max;
    }
}

์–ด๋ ค์› ๋˜ ์  / ๋ฐฐ์šด ์  / ๋Š๋‚€ ์ 

๋ญ”๊ฐ€ ๋Š๋‚Œ์ด ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด์—ฌ์„œ ํ’€์—ˆ๋Š”๋ฐ ๋งž๋Š”๋“ฏ ์‹ถ๋‹ค.(?)
๋ฐฉ๋ฒ• 2๊ฐ€ ๋ฐฉ๋ฒ• 1 ๋ณด๋‹ค ์‹คํ–‰์‹œ๊ฐ„์€ ๋ชฐ๋ผ๋„ ๋ฉ”๋ชจ๋ฆฌ๋Š” ๋” ํšจ์œจ์ ์ผ ์ค„ ์•Œ์•˜๋Š”๋ฐ ์•„๋‹ˆ์—ฌ์„œ ์ถฉ๊ฒฉ์ ์ด์˜€๋‹ค.
๋‚ด๊ฐ€ ์„ธ์šด ๊ฐ€์„ค์€

  • sum์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋„ฃ์„ ๋•Œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ฆ๊ฐ€ํ•˜๋‚˜?
  • Math.max ๊ฐ€ ๋ฌธ์ œ์ธ๊ฐ€?
    ๊ทธ๋ž˜์„œ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋กœ ์ฝ”๋“œ๋ฅผ ๋ฐ”๊ฟ”๋ณด์•˜๋‹ค.
    //๋ฐฉ๋ฒ•3
    class Solution {
      public int maxSubArray(int[] nums) {
          int max = nums[0]; // ์ตœ๋Œ€๊ฐ’ ์ €์žฅ
          int sum = nums[0]; // ์ง€๊ธˆ๊นŒ์ง€ sum ๊ฐ’
          for(int i = 1; i<nums.length; i++){
              sum = Math.max(sum+nums[i] , nums[i]); //sum + ํ˜„์žฌ๊ฐ’ ๊ณผ ํ˜„์žฌ๊ฐ’ ์ค‘ ํฐ๊ฒƒ์„ sum์— ์ €์žฅ
              max = Math.max(sum,max); // sum์ด ํฐ ์ง€ max๊ฐ€ ํฐ ์ง€ ๋น„๊ต
          }
          return max;
      }
    }
    ๊ฒฐ๋ก ์€ Math.max๊ฐ€ ๋ฌธ์ œ์˜€๋˜ ๊ฒƒ ๊ฐ™๋‹ค! ๐Ÿ‘๐Ÿ‘

์•ž์œผ๋กœ Math.max ๋ณด๋‹ค๋Š” ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ๋‹ค..!!

๋ฐ˜์‘ํ˜•