Skip to content

Commit

Permalink
Merge branch 'DaleStudy:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaedie authored Jan 21, 2025
2 parents 7cbc714 + 89edeec commit b0c8ddf
Show file tree
Hide file tree
Showing 47 changed files with 1,694 additions and 0 deletions.
12 changes: 12 additions & 0 deletions best-time-to-buy-and-sell-stock/thispath98.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def maxProfit(self, prices: List[int]) -> int:
minimum = prices[0]
answer = 0
for i in range(1, len(prices)):
if minimum > prices[i]:
minimum = prices[i]
else:
diff = prices[i] - minimum
if answer < diff:
answer = diff
return answer
59 changes: 59 additions & 0 deletions container-with-most-water/EcoFriendlyAppleSu.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package leetcode_study

/*
* ์ฃผ์–ด์ง„ ๋†’์ด์—์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ๋ฉด์ ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ.
* Brute force๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋”ฐ์ ธ๊ฐ€๋ฉฐ ์ตœ๋Œ€ ๋ฉด์ ์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•.
* ์ฃผ์–ด์ง„ ๋†’์ด(n)์˜ ๊ฐœ์ˆ˜๋Š” 2 ๋ณด๋‹ค ํฌ๊ฑฐ๊ฐ€ ๊ฐ™๊ณ  10^4 ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์Œ.
* ์ด์ค‘ Loop์œผ๋กœ ํ•ด๊ฒฐํ•  ๊ฒฝ์šฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฐœ์ƒ (10^8 ์ดํ•˜๋กœ ํ•ด๊ฒฐํ•ด์•ผ ์ œํ•œ ์‹œ๊ฐ„ ์•ˆ์œผ๋กœ ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ)
*
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n^2)
* -> ๋‘ ๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๋†’์ด๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ ์‹คํ–‰: O(n^2)
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
* -> ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ์—†์Œ.
* */
fun maxArea01(height: IntArray): Int {
var maxValue = 0
for (i in 0 until height.size) {
for (j in i + 1 until height.size) {
// ๋„ˆ๋น„๋Š” ๋‘ ์„ ๋ถ„ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ
val width = j - i
// ๋†’์ด๋Š” ๋‘ ์„ ๋ถ„ ์ค‘ ์ž‘์€ ๊ฐ’
val containerHeight = Math.min(height[i], height[j])
// ๋ฉด์  ๊ณ„์‚ฐ
val area = width * containerHeight
// ์ตœ๋Œ€๊ฐ’ ๊ฐฑ์‹ 
maxValue = Math.max(maxValue, area)
}
}
return maxValue
}

/*
* ์ด์ค‘ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ ๋ฌธ์ œ ํ’€์ด.
* ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ์กฐ๊ฑด๊ณผ ์š”์†Œ
* -> ๋†’์ด์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์™ผ์ชฝ๊ฐ’๊ณผ ์˜ค๋ฅธ์ชฝ ๊ฐ’์—์„œ ๋‘ ๊ฐ’ ์ค‘ ์ž‘์€ ๊ฐ’์ด ๋†’์ด๊ฐ€ ๋  ์ˆ˜ ์žˆ์Œ.
* -> ์˜ค๋ฅธ์ชฝ์˜ ๊ฐ’์€ ์™ผ์ชฝ ๊ฐ’๋ณด๋‹ค ์ž‘์„ ์ˆ˜ ์—†์Œ.
* -> ๋„ˆ๋น„ ๊ฐ’์€ ์˜ค๋ฅธ์ชฝ ์ธ๋ฑ์Šค์—์„œ ์™ผ์ชฝ ์ธ๋ฑ์Šค๋ฅผ ๋บ€ ๊ฐ’์ž„.
*
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
* -> ์ฃผ์–ด์ง„ ๋†’์ด ๋ฐฐ์—ด์—์„œ ์–‘์ชฝ ๋ ๊ฐ’์„ ์ฆ๊ฐ/๊ฐ€๊ฐ ํ•ด๊ฐ€๋ฉฐ ๋ฐ˜๋ณต ์ง„ํ–‰: O(n)
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
* -> ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ์—†์Œ.
* */
fun maxArea02(height: IntArray): Int {
var maxValue = 0
var left = 0
var right = height.size - 1
while (left <= right) {
val width = right - left
val containerHeight = Math.min(height[left], height[right])
val area = width * containerHeight
maxValue = Math.max(maxValue, area)
if (height[left] < height[right]) {
left++
} else {
right--
}
}
return maxValue
}
45 changes: 45 additions & 0 deletions container-with-most-water/GangBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Solution {
public int maxArea(int[] height) {
/**
1. understanding
- with two pair of line, each can contain "min(line1,line2) * (distance)" mount of water
- find maximum amount of water can contain
2. strategy
- brute force
- for each pair of lines, calculate amount and update maximum amount.
- it can takes O(N), where N is the count of lines.
- N is 10^5 at most, so it can takes 10^10, which can takes about 10 seconds
- you need to swap out unnecessary calculation
- so, let's find a unnecessary calculation in this problem.
3. complexity
- time: O(N)
- space: O(1)
*/
int l = 0;
int r = height.length - 1;
int maxAmount = amountOf(height, l, r); // Math.min(height[l], height[r], r-l);
while (l < r) { // O(N)
maxAmount = Math.max(maxAmount, amountOf(height, l, r));
if (height[l] < height[r]) {
l++;
} else if (height[l] > height[r]) {
r--;
} else {
int nextLeftAmount = amountOf(height, l+1, r);
int nextRightAmount = amountOf(height, l, r-1);
if (nextLeftAmount < nextRightAmount) {
r--;
} else {
l++;
}
}
}

return maxAmount;
}

private int amountOf(int[] height, int left, int right) {
return (Math.min(height[left], height[right]) * (right - left));
}
}

17 changes: 17 additions & 0 deletions container-with-most-water/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(n)
# ๊ณต๊ฐ„ ๋ณต์žก๋„ : O(1)
# ๋ฌธ์ œ ์œ ํ˜• : Two Pointer
class Solution:
def maxArea(self, height: List[int]) -> int:
left, right = 0, len(height) - 1
max_area = 0

while left < right:
max_area = max(max_area, (right - left) * min(height[left], height[right]))
if height[left] < height[right]:
left += 1
else:
right -= 1

return max_area

40 changes: 40 additions & 0 deletions container-with-most-water/Jay-Mo-99.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
๏ปฟ๏ปฟ #ํ•ด์„
#s๋Š” start index, e๋Š” ending index๋กœ ํ• ๋‹นํ•œ๋‹ค.
#area ์— (e-s)*min(height[e], height[s]) ๋กœ ๋ฉด์ ์˜ ๋„“์ด๋ฅผ ๊ตฌํ•œ๋‹ค.
#๋งŒ์•ฝ height[s]๊ฐ€ height[e] ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด, ํ˜„ area๋ณด๋‹ค ๋” ํฐ ๊ฒฐ๊ด๊ฐ’์„ ์œ„ํ•ด ๋ณ€ํ™”๋ฅผ ์ค€๋‹ค.
# (e-s)์—์„œ e๋ฅผ ์ค„์–ด๋“ค๋ฉด ํ•„์—ฐ์ ์œผ๋กœ area ๊ฐ’์ด ๊ธฐ์กด๋ณด๋‹ค ์ ์–ด์ง„๋‹ค. ๋”ฐ๋ผ์„œ s์— 1์„ ๋”ํ•ด ์ธ๋ฑ์Šค๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™์‹œ์ผœ height[s] ์— ๋ณ€ํ™”๋ฅผ ์ค€๋‹ค.
#๊ทธ ์™ธ์˜ ์ƒํ™ฉ์—๋Š” height[e]๋ฅผ ๋ณ€ํ™”์‹œํ‚ค๊ธฐ ์œ„ํ•ด e์— 1๋ฅผ ๋นผ ์™ผ์ชฝ ์ธ๋ฑ์Šค๋กœ ์ด๋™์‹œํ‚จ๋‹ค.
#ํ•ด๋‹น ๋ฃจํ”„๋Š” s๊ฐ€ e๋ณด๋‹ค ์ž‘์„๋•Œ ์ž‘์šฉ๋œ๋‹ค. ๋งŒ์•ฝ s์˜ ์ฆ๊ฐ€์™€ e์˜ ๊ฐ์†Œ๋กœ ๋‘ ๋ณ€์ˆ˜๊ฐ€ ๋งˆ์ฃผ์น˜๋ฉด ์ข…๋ฃŒํ•œ ํ›„ max_area๋ฅผ return์‹œํ‚จ๋‹ค.



#Big O
#- N: height์˜ element ๊ฐฏ์ˆ˜

#Time Complexity: O(N)
#- while : s์™€ e๊ฐ€ ๋งŒ๋‚ ๋•Œ๊นŒ์ง€ ์ตœ๋Œ€ N๋ฒˆ ๋ฐ˜๋ณต๋œ๋‹ค. ๊ฐ ๋ฐ˜๋ณต์—์„œ์˜ ์—ฐ์‚ฐ๋“ค์€ O(1)์— ํ•ด๋‹น๋œ๋‹ค. -> O(N)


#Space Complexity: O(1)
#- s,e,max_area: ๋ณ€์ˆ˜๋Š” ์ƒ์ˆ˜๋กœ ์ž‘์šฉ๋œ๋‹ค -> O(1)
####
#
#
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
max_area = 0 #Saving for answer
s,e=0,len(height)-1 #Assign the first index and last index
while s<e:
area = (e-s) * min(height[s],height[e]) #Current area using e,s
max_area = max(area, max_area) #Re-assing the max_area comparing with area
if height[s]< height[e]:
s+=1
else:
e -=1
return max_area


15 changes: 15 additions & 0 deletions container-with-most-water/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def maxArea(self, height: List[int]) -> int:
right = len(height) - 1
left = 0
max_size = 0
for line in range(len(height)):
if left >= right:
break
cur_size = (right - left) * min(height[left], height[right])
if height[left] < height[right]: # ์™ผ์ชฝ์ด ์ž‘์œผ๋ฉด left ์ด๋™
left += 1
else:
right -= 1
max_size = max(max_size, cur_size)
return max_size
28 changes: 28 additions & 0 deletions container-with-most-water/higeuni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param {number[]} height
* @return {number}
*
* complexity
* time: O(n)
* space: O(1)
*/

var maxArea = function(height) {
let answer = 0;
let l = 0, r = height.length - 1;

while(l < r) {
const area = (r - l) * Math.min(height[l], height[r]);

answer = area > answer ? area : answer;

if(height[l] < height[r]) {
l += 1;
}else {
r -= 1;
}
}

return answer;
};

19 changes: 19 additions & 0 deletions container-with-most-water/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// ํˆฌํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•œ ์‹œ๊ฐ„๋ณต์žก๋„ O(N)์œผ๋กœ ํ’€๋ฉด๋œ๋‹ค.
// ์ค‘์š”ํ•œ๊ฑด ์–ธ์ œ ์ด๋™ํ•˜๋ƒ?๋ฅผ ์ •์˜ํ•˜๋Š”๊ฑด๋ฐ, ์ „์ฒด ๋ฌผ์–‘์ด ์ค„์–ด๋“ค๋ฉด ์ƒ๋Œ€์ ์œผ๋กœ ์ž‘์€ ์ชฝ์„ ์ด๋™์‹œํ‚ค๋ฉด๋œ๋‹ค.
class Solution {
public int maxArea(int[] height) {
int left = 0;
int right = height.length-1;
int maxWater = 0;
while(left < right) {
int max = Math.min(height[left], height[right]) * (right - left);
maxWater = Math.max(max,maxWater);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxWater;
}
}
13 changes: 13 additions & 0 deletions container-with-most-water/jeldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
# O(n), n = len(height)
def maxArea(self, height: list[int]) -> int:
max_amount = 0
l, r = 0, len(height) - 1
while l < r:
amount = min(height[l], height[r]) * (r - l)
max_amount = max(max_amount, amount)
if height[l] < height[r]:
l += 1
else:
r -= 1
return max_amount
41 changes: 41 additions & 0 deletions container-with-most-water/lledellebell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/***
*
* @problem
* - ๋ฐฐ์—ด์—์„œ ๋‘ ์„ ์„ ์„ ํƒํ•˜์—ฌ ์ตœ๋Œ€ ๋ฌผ์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋ฉด์ ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
*
* @constraints
* - ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” 2 ์ด์ƒ 10^5 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
* - ๊ฐ ๋†’์ด๋Š” 0 ์ด์ƒ 10^4 ์ดํ•˜์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.
*
* @example
* - ์ž…๋ ฅ: height = [1,8,6,2,5,4,8,3,7]
* - ์ถœ๋ ฅ: 49
* (๋†’์ด 8๊ณผ 7์„ ์„ ํƒํ•˜์—ฌ ๋„“์ด = min(8, 7) * (8 - 1) = 49)
*
* @description
* - ๋ฐฐ์—ด์˜ ์–‘ ๋์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๋‘ ๊ฐœ์˜ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
* - ํ˜„์žฌ ํฌ์ธํ„ฐ ์œ„์น˜์—์„œ์˜ ๋„“์ด๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ์ตœ๋Œ€๊ฐ’์„ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.
* - ๋” ์ž‘์€ ๋†’์ด๋ฅผ ๊ฐ€์ง„ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™ํ•˜์—ฌ ๋” ํฐ ๋„“์ด๋ฅผ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
*
* @complexity
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
* (๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ๋งŒ ์ˆœํšŒํ•˜๋ฉฐ ์ตœ๋Œ€ ๋„“์ด๋ฅผ ๊ณ„์‚ฐ)
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
* (์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ์—†์ด ํฌ์ธํ„ฐ๋งŒ ์‚ฌ์šฉ)
*
*/
function maxArea(height: number[]): number {
let left = 0,
right = height.length - 1,
maxArea = 0;

while (left < right) {
// ํ˜„์žฌ ๋„“์ด ๊ณ„์‚ฐ ๋ฐ ์ตœ๋Œ€๊ฐ’ ๊ฐฑ์‹ 
maxArea = Math.max(maxArea, Math.min(height[left], height[right]) * (right - left));

// ๋” ์ž‘์€ ๋†’์ด์˜ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™
height[left] < height[right] ? left++ : right--;
}

return maxArea;
}
40 changes: 40 additions & 0 deletions container-with-most-water/neverlish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

package main

import "testing"

func TestMaxArea(t *testing.T) {
result1 := maxArea([]int{1, 8, 6, 2, 5, 4, 8, 3, 7})

if result1 != 49 {
t.Errorf("Expected 49, but got %v", result1)
}

result2 := maxArea([]int{1, 1})

if result2 != 1 {
t.Errorf("Expected 1, but got %v", result2)
}
}

func maxArea(height []int) int {
result := 0

pointer_left := 0
pointer_right := len(height) - 1

for pointer_left < pointer_right {
area := (pointer_right - pointer_left) * min(height[pointer_left], height[pointer_right])
result = max(result, area)

if height[pointer_left] < height[pointer_right] {
pointer_left++
} else {
pointer_right--
}
}

return result
}
26 changes: 26 additions & 0 deletions container-with-most-water/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'''
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
- ๋‘ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™ํ•˜๋ฉด์„œ ๋ฐฐ์—ด์„ ํ•œ ๋ฒˆ๋งŒ ์ˆœํšŒํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ž…๋‹ˆ๋‹ค.
๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
- ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ O(1)์ž…๋‹ˆ๋‹ค.
'''

from typing import List


class Solution:
def maxArea(self, height: List[int]) -> int:
max_area = 0
left, right = 0, len(height) - 1

while left < right:
current_area = (right - left) * min(height[left], height[right])
max_area = max(current_area, max_area)

if height[left] < height[right]:
left += 1
else:
right -= 1

return max_area
26 changes: 26 additions & 0 deletions container-with-most-water/sungjinwi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
ํ’€์ด :
left, right ์ปค์„œ๋ฅผ ์–‘ ๋์— ๋†“๊ณ  ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฐ˜๋Œ€ํŽธ์œผ๋กœ ์ด๋™์‹œํ‚ค๋ฉฐ ๋ฌผ ์–‘ ๋น„๊ต
๋‘˜ ์ค‘ ๋” ๋†’์€ ๋†’์ด๋ฅผ ์ด๋™์‹œํ‚ค๋ฉด ๋ฌด์กฐ๊ฑด ๋ฌผ์˜ ์–‘์ด ์ค„์–ด๋“ค๊ธฐ ๋•Œ๋ฌธ์— ๋” ๋‚ฎ๊ฑฐ๋‚˜ ๊ฐ™์€ ์ปค์„œ๋ฅผ ์ด๋™์‹œํ‚ค๋ฉฐ ์—…๋ฐ์ดํŠธ
๋‘˜์ด ๋งŒ๋‚˜๋ฉด ๋น„๊ต ์ข…๋ฃŒ
len(height) : N
TC : O(N)
l, r์˜ ์ด๋™์ด ์ „์ฒด height ๊ฐœ์ˆ˜๋งŒํผ ์ผ์–ด๋‚˜๋ฏ€๋กœ
SC : O(1)
"""

class Solution:
def maxArea(self, height: List[int]) -> int:
l = 0
r = len(height) - 1
max_area = 0
while (l != r) :
cur_area = (r - l) * min(height[l], height[r])
max_area = max(cur_area, max_area)
if (height[l] >= height[r]) :
r -= 1
else :
l += 1
return max_area
Loading

0 comments on commit b0c8ddf

Please sign in to comment.