Skip to content
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
.DS_Store
105 changes: 105 additions & 0 deletions problems/121.best-time-to-buy-and-sell-stock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#
# @lc app=leetcode id=121 lang=python3
#
# [121] Best Time to Buy and Sell Stock
#
# https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
#
# algorithms
# Easy (46.66%)
# Total Accepted: 469.1K
# Total Submissions: 1M
# Testcase Example: '[7,1,5,3,6,4]'
#
# Say you have an array for which the i^th element is the price of a given
# stock on day i.
#
# If you were only permitted to complete at most one transaction (i.e., buy one
# and sell one share of the stock), design an algorithm to find the maximum
# profit.
#
# Note that you cannot sell a stock before you buy one.
#
# Example 1:
#
#
# Input: [7,1,5,3,6,4]
# Output: 5
# Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit =
# 6-1 = 5.
# Not 7-1 = 6, as selling price needs to be larger than buying price.
#
#
# Example 2:
#
#
# Input: [7,6,4,3,1]
# Output: 0
# Explanation: In this case, no transaction is done, i.e. max profit = 0.
#
#
#


from typing import List

class Solution:
def maxProfit(self, prices: List[int]) -> int:
maxValue = 0
lowIdx = 0
highIdx = 0
for idx, price in enumerate(prices):
lowPrice = prices[lowIdx]
highPrice = prices[highIdx]
if price < lowPrice:
maxValue = max(maxValue, highPrice - lowPrice)
lowIdx = idx
highIdx = idx
elif prices[idx] > highPrice:
highIdx = idx
return max(maxValue, prices[highIdx] - prices[lowIdx]) if len(prices) else 0


"""
# Another approach: Kadane's Algorithm
class Solution:
def maxProfit(self, prices: List[int]) -> int:
currentProfit = 0
mostProfit = 0
for idx in range(1, len(prices)):
currentProfit = max(0, currentProfit + prices[idx] - prices[idx-1])
mostProfit = max(mostProfit, currentProfit)
return mostProfit
"""

"""
import unittest

class TestMaxProfit(unittest.TestCase):
def test_emptyList(self):
self.assertEqual(Solution().maxProfit([]), 0)

def test_onlyOneDay(self):
self.assertEqual(Solution().maxProfit([2]), 0)

def test_allTheSamePrice(self):
self.assertEqual(Solution().maxProfit([2, 2, 2]), 0)

def test_positiveExample1(self):
self.assertEqual(Solution().maxProfit([7, 1, 5, 3, 6, 4]), 5)

def test_positiveExample2(self):
self.assertEqual(Solution().maxProfit([8, 4, 6, 2, 10]), 8)

def test_positiveExample3(self):
self.assertEqual(Solution().maxProfit([8, 4, 6, 2, 1]), 2)

def test_positiveExample4(self):
self.assertEqual(Solution().maxProfit([1, 2, 3, 4, 5]), 4)

def test_negativeExample(self):
self.assertEqual(Solution().maxProfit([7, 6, 4, 3, 1]), 0)

if __name__ == '__main__':
unittest.main()
"""
50 changes: 50 additions & 0 deletions problems/122.best-time-to-buy-and-sell-stock-ii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# @lc app=leetcode id=122 lang=python3
#
# [122] Best Time to Buy and Sell Stock II
#

from typing import List

class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
for i in range(1, len(prices)):
if prices[i] > prices[i-1]:
profit += prices[i] - prices[i-1]

return profit


# import unittest

# class TestMaxProfit(unittest.TestCase):
# def test_emptyList(self):
# self.assertEqual(Solution().maxProfit([]), 0)

# def test_onlyOneDay(self):
# self.assertEqual(Solution().maxProfit([2]), 0)

# def test_allTheSamePrice(self):
# self.assertEqual(Solution().maxProfit([2, 2, 2]), 0)

# def test_positiveExample1(self):
# self.assertEqual(Solution().maxProfit([7, 1, 5, 3, 6, 4]), 7)

# def test_positiveExample2(self):
# self.assertEqual(Solution().maxProfit([8, 4, 6, 2, 10]), 10)

# def test_positiveExample3(self):
# self.assertEqual(Solution().maxProfit([8, 4, 6, 2, 1]), 2)

# def test_positiveExample4(self):
# self.assertEqual(Solution().maxProfit([1, 2, 3, 4, 5]), 4)

# def test_negativeExample(self):
# self.assertEqual(Solution().maxProfit([7, 6, 4, 3, 1]), 0)

# if __name__ == '__main__':
# unittest.main()



126 changes: 126 additions & 0 deletions problems/134.gas-station.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#
# @lc app=leetcode id=134 lang=python3
#
# [134] Gas Station
#
# https://leetcode.com/problems/gas-station/description/
#
# algorithms
# Medium (33.21%)
# Total Accepted: 136.3K
# Total Submissions: 410.4K
# Testcase Example: '[1,2,3,4,5]\n[3,4,5,1,2]'
#
# There are N gas stations along a circular route, where the amount of gas at
# station i is gas[i].
#
# You have a car with an unlimited gas tank and it costs cost[i] of gas to
# travel from station i to its next station (i+1). You begin the journey with
# an empty tank at one of the gas stations.
#
# Return the starting gas station's index if you can travel around the circuit
# once in the clockwise direction, otherwise return -1.
#
# Note:
#
#
# If there exists a solution, it is guaranteed to be unique.
# Both input arrays are non-empty and have the same length.
# Each element in the input arrays is a non-negative integer.
#
#
# Example 1:
#
#
# Input:
# gas = [1,2,3,4,5]
# cost = [3,4,5,1,2]
#
# Output: 3
#
# Explanation:
# Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 +
# 4 = 4
# Travel to station 4. Your tank = 4 - 1 + 5 = 8
# Travel to station 0. Your tank = 8 - 2 + 1 = 7
# Travel to station 1. Your tank = 7 - 3 + 2 = 6
# Travel to station 2. Your tank = 6 - 4 + 3 = 5
# Travel to station 3. The cost is 5. Your gas is just enough to travel back to
# station 3.
# Therefore, return 3 as the starting index.
#
#
# Example 2:
#
#
# Input:
# gas = [2,3,4]
# cost = [3,4,3]
#
# Output: -1
#
# Explanation:
# You can't start at station 0 or 1, as there is not enough gas to travel to
# the next station.
# Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 =
# 4
# Travel to station 0. Your tank = 4 - 3 + 2 = 3
# Travel to station 1. Your tank = 3 - 3 + 3 = 3
# You cannot travel back to station 2, as it requires 4 unit of gas but you
# only have 3.
# Therefore, you can't travel around the circuit once no matter where you
# start.
#
#
#

from typing import List
from operator import sub


class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
maxAccuGas = -1
maxIndex = 0
sumGas = 0
restGasList = list(map(sub, gas, cost))

for idx in range(len(restGasList)-1, -1, -1):
restGas = restGasList[idx]
sumGas += restGas
if sumGas > maxAccuGas:
maxAccuGas = sumGas
maxIndex = idx

return -1 if sumGas < 0 else maxIndex


"""
import unittest


class TestCanCompleteCircuit(unittest.TestCase):
def test_canCompleteCase1(self):
gas = [1, 2]
cost = [2, 1]
self.assertEqual(Solution().canCompleteCircuit(gas, cost), 1)

def test_canCompleteCase2(self):
gas = [1, 2, 3, 4, 5]
cost = [3, 4, 5, 1, 2]
self.assertEqual(Solution().canCompleteCircuit(gas, cost), 3)

def test_canCompleteCase3(self):
gas = [5, 8, 2, 8]
cost = [6, 5, 6, 6]
self.assertEqual(Solution().canCompleteCircuit(gas, cost), 3)

def test_canNotComplete(self):
gas = [2, 3, 4]
cost = [3, 4, 4]
self.assertEqual(Solution().canCompleteCircuit(gas, cost), -1)


if __name__ == '__main__':
unittest.main()
"""
110 changes: 110 additions & 0 deletions problems/189.rotate-array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#
# @lc app=leetcode id=189 lang=python3
#
# [189] Rotate Array
#
# https://leetcode.com/problems/rotate-array/description/
#
# algorithms
# Easy (29.32%)
# Total Accepted: 277K
# Total Submissions: 944.7K
# Testcase Example: '[1,2,3,4,5,6,7]\n3'
#
# Given an array, rotate the array to the right by k steps, where k is
# non-negative.
#
# Example 1:
#
#
# Input: [1,2,3,4,5,6,7] and k = 3
# Output: [5,6,7,1,2,3,4]
# Explanation:
# rotate 1 steps to the right: [7,1,2,3,4,5,6]
# rotate 2 steps to the right: [6,7,1,2,3,4,5]
# rotate 3 steps to the right: [5,6,7,1,2,3,4]
#
#
# Example 2:
#
#
# Input: [-1,-100,3,99] and k = 2
# Output: [3,99,-1,-100]
# Explanation:
# rotate 1 steps to the right: [99,-1,-100,3]
# rotate 2 steps to the right: [3,99,-1,-100]
#
#
# Note:
#
#
# Try to come up as many solutions as you can, there are at least 3 different
# ways to solve this problem.
# Could you do it in-place with O(1) extra space?
#
#

from typing import List


class Solution:
def reverseNumsInPlace(self, nums, startIdx, stopIdx):
sliceLength = (stopIdx - startIdx + 1) // 2
for i in range(sliceLength):
nums[startIdx + i], nums[stopIdx - i] = nums[stopIdx - i], nums[startIdx + i]

def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if not nums:
return None

n = k % len(nums)
if not n:
return None

self.reverseNumsInPlace(nums, 0, len(nums) - 1)
self.reverseNumsInPlace(nums, 0, n - 1)
self.reverseNumsInPlace(nums, n, len(nums) - 1)


"""
import unittest


class TestReverseNumsInPlace(unittest.TestCase):
def test_case_1(self):
nums = [1, 2, 3, 4, 5]
Solution().reverseNumsInPlace(nums, 0, 2)
self.assertEqual(nums, [3, 2, 1, 4, 5])

def test_case_2(self):
nums = [1, 2, 3, 4, 5]
Solution().reverseNumsInPlace(nums, 0, 3)
self.assertEqual(nums, [4, 3, 2, 1, 5])

class TestRotate(unittest.TestCase):
def test_emptyList(self):
nums = []
Solution().rotate(nums, 1)
self.assertEqual(nums, nums)

def test_rotateZero(self):
nums = [1, 2]
Solution().rotate(nums, 0)
self.assertEqual(nums, nums)

def test_rotateMoreThanLength(self):
nums = [1, 2, 3]
Solution().rotate(nums, 4)
self.assertEqual(nums, [3, 1, 2])

def test_oridinaryCase(self):
nums = [1, 2, 3, 4]
Solution().rotate(nums, 2)
self.assertEqual(nums, [3, 4, 1, 2])

if __name__ == '__main__':
unittest.main()
"""
Loading