Skip to content

Commit 890bcb8

Browse files
committed
Feat: add Python solution
1 parent 46215f8 commit 890bcb8

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@
3030
| 344 | Reverse String | [Ruby](./algorithms/ruby/0344-reverse-string.rb) | Easy |
3131
| 387 | First Unique Character in a String | [Ruby](./algorithms/ruby/0387-first-unique-character-in-a-string.rb) | Easy |
3232
| 438 | Find All Anagrams in a String | [Ruby](./algorithms/ruby/0438-find-all-anagrams-in-a-string.rb) | Medium |
33+
| 502 | IPO | [Ruby](./algorithms/ruby/0502-ipo.rb) [Python3](./algorithms/python3/0502-ipo.py) | Hard |
3334
| 509 | Fibonacci Number | [Ruby](./algorithms/ruby/0509-fibonacci-number.rb) | Easy |
3435
| 540 | Single Element in a Sorted Array | [Ruby](./algorithms/ruby/0540-single-element-in-a-sorted-array.rb) | Medium |
3536
| 557 | Reverse Words in a String III | [Ruby](./algorithms/ruby/0557-reverse-words-in-a-string-iii.rb) | Easy |
3637
| 567 | Permutation in String | [Ruby](./algorithms/ruby/0567-permutation-in-string.rb) | Medium |
3738
| 622 | Design Circular Queue | [Ruby](./algorithms/ruby/0622-design-circular-queue.rb) | Medium |
3839
| 733 | Flood Fill | [Ruby](./algorithms/ruby/0733-flood-fill.rb) | Easy |
39-
| 783 | Minimum Distance Between BST Nodes | [Ruby](./algorithms/ruby/0783-minimum-distance-between-bst-nodes.rb) [Python3](0783-minimum-distance-between-bst-nodes.py) | Easy |
40+
| 783 | Minimum Distance Between BST Nodes | [Ruby](./algorithms/ruby/0783-minimum-distance-between-bst-nodes.rb) [Python3](./algorithms/python3/0783-minimum-distance-between-bst-nodes.py) | Easy |
4041
| 904 | Fruit Into Baskets | [Ruby](./algorithms/ruby/0904-fruit-into-baskets:description.rb) | Medium |
4142
| 953 | Verifying an Alien Dictionary | [Ruby](./algorithms/ruby/0953-verifying-an-alien-dictionary.rb) | Easy |
4243
| 989 | Add to Array-Form of Integer | [Ruby](./algorithms/ruby/0989-add-to-array-form-of-integer.rb) | Easy |

algorithms/python3/0502-ipo.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Problem: 502. IPO
2+
# URL: https://leetcode.com/problems/ipo
3+
4+
import heapq
5+
6+
class Solution:
7+
def findMaximizedCapital(self, k: int, w: int, profits: list[int], capital: list[int]) -> int:
8+
n = len(profits)
9+
projects = list(zip(capital, profits))
10+
projects.sort()
11+
12+
q = []
13+
ptr = 0
14+
for i in range(k):
15+
while ptr < n and projects[ptr][0] <= w:
16+
heapq.heappush(q, -projects[ptr][1])
17+
ptr += 1
18+
if not q:
19+
break
20+
w += -heapq.heappop(q)
21+
return w
22+
23+
# ********************#
24+
# TEST #
25+
# ********************#
26+
27+
import unittest
28+
29+
class TestStringMethods(unittest.TestCase):
30+
def test_addBinary(self):
31+
self.assertEqual(Solution.findMaximizedCapital(self, 2, 0, [1, 2, 3], [0, 1, 1]), 4)
32+
self.assertEqual(Solution.findMaximizedCapital(self, 3, 0, [1, 2, 3], [0, 1, 2]), 6)
33+
34+
if __name__ == '__main__':
35+
unittest.main()

algorithms/ruby/0502-ipo.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
# Problem: 502. IPO
4+
# URL: https://leetcode.com/problems/ipo
5+
6+
# @param {Integer} k
7+
# @param {Integer} w
8+
# @param {Integer[]} profits
9+
# @param {Integer[]} capital
10+
# @return {Integer}
11+
def find_maximized_capital(k, w, profits, capital)
12+
end
13+
14+
# ********************#
15+
# TEST #
16+
# ********************#
17+
18+
require "test/unit"
19+
class Test_find_maximized_capital < Test::Unit::TestCase
20+
def test_
21+
assert_equal 4, find_maximized_capital(2, 0, [1, 2, 3], [0, 1, 1])
22+
assert_equal 6, find_maximized_capital(3, 0, [1, 2, 3], [0, 1, 2])
23+
end
24+
end

0 commit comments

Comments
 (0)