LeetCode ๐Ÿ”๏ธ/Array

LeetCode ๐Ÿ”๏ธ/Array

11. Container With Most Water

Description: Solution: Brute force method is not adapted as a solution. We are gonna make the pointer end of both array. Shift l if height[l] is smaller than height[r]. Else: shift r to right.

LeetCode ๐Ÿ”๏ธ/Array

15. 3Sum

Description: Solution: We need to nums.sort() before moving pointer. i is a count, a is the value (enumerate) if i > 0 and a == nums[i-1] => if i is bigger than 0, we make sure not using same value l, r pointers to find the two sum for 0 of three sum. Since we sort the values, if threeSum is bigger than 0, we should move r pointer to left. elif threeSum is smaller than 0, we should move l pointe..

LeetCode ๐Ÿ”๏ธ/Array

33. Search in Rotated Sorted Array

Description: Solution: We are gonna set l, r, m pointers to find in O(logn). If l pointer value is less than m pointer value: target should be between l and m or between m and r. if target is more than m value or target is less than l value, we are gonna see right portion, except left portion. else we are gonna see left portion, except right portion. If r value is less than m value: target can b..

LeetCode ๐Ÿ”๏ธ/Array

153. Find Minimum in Rotated Sorted Array

Description: Solution: I set the right and left pointers to the end of the array. m is the pivot(first set the middle of the array). If m is more than l, then we can throw out the left portion. Now, the new l is the next value of m. Else, we can throw out the right portion, so the new r is the previous value of m. When we get the nums[l] is less than nums[r], we return the res.

LeetCode ๐Ÿ”๏ธ/Array

152. Maximum Product Subarray

Description: Solution: tmp will be used for curMin. Example: [-1, -2, -3] nums[i]= -2: tmp = 2, curMax = 2, curMin = -2 -> res = 2 nums[i] = -3: tmp = -6, curMax = 6, curMin = -6 -> res = 6 โ€ป Think about all negative integers array case!

LeetCode ๐Ÿ”๏ธ/Array

53. Maximum Subarray

Today, I solved "Maximum Subarray." Description: Solution: As we can see, we don't have to include the first negative array. curSum is a cumulative sum of numbers. If curSum is negative, it means that subarray is not for maximum sum. I use max() to compare the curSum and maxSub.

LeetCode ๐Ÿ”๏ธ/Array

238. Product of Array Except Self

Description: If we are in nums[0], then output should be the product of all variables in array except nums[0]. I used the division operation, but the problem ask me to avoid using division operation. In addtion, the time complexity should be less than O(N), and space complexity is O(1). Solution1(not answer): We can use brute force here, but doesn't work for the problem. The reason is that time ..

LeetCode ๐Ÿ”๏ธ/Array

217. Contains Duplicate

Description: Basically, we will look into the array, and then find out if there are same numbers. If there is a duplication in array, then we return "True", or not then "False". Solution1(not Answer): Time Complexity for this algorithms O(N^2). In contrast, space complexity is O(1). When you submit this method, you can see the time complexity exceed for some case! That's definitely doesn't work...

LeetCode ๐Ÿ”๏ธ/Array

121. Best Time to Buy and Sell Stock

Today, I studied the best time to buy and sell stock algorithms problem. Description: Solution: This code is not nested loop, so it is not gonna be O(N^2). Just O(N) time complexity. What is the difference from previous brute force algorithms? I would like to say we store two array while process in loop. We stored two value called profit and minimum price. ex) case 1: price: [7, 1, 5, 3, 6, 4] p..

LeetCode ๐Ÿ”๏ธ/Array

1. Two Sum

The problem is called "Two Sum" with array, so I decided to solve with Python. Description: Solution: When I looked into the each variables, I used nested loop(two for loop) to find out the two variables in the array. And if the sum of two variables equal to the value of target, I return the i and j as a dictionary. Brute Force: Time complexity: O(N^2) Space complexity: O(1) Solution2: Using Has..

KB0129
'LeetCode ๐Ÿ”๏ธ/Array' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก