#๐Ÿ”’ leet code remove k digits

4 messages ยท Page 1 of 1 (latest)

onyx sparrow
#

Let me preface with my idea on how I planned to solve this before getting into my question.

  1. split integer into list

  2. create a for loop that removes index 0 from list, store it, reset, remove index 1, convert both to integers, compare the two, and save the lowest number

  3. repeat step two through the list to find which number I must remove to make the smallest integer.

  4. (haven't done it yet) create a while loop to iterate through the for look "K" number of times to hopefully get the right answer.

I I'm printing my list_num_2 and it's getting smaller as items are deleted so I'm assuming that is the issues I need to solve but can't figure it out. I tried list_num_2 = list_num.copy() however I get an attribute error. My code is as follows:

class Solution(object):
counter = 0
smallest_num = 0

def join_list(self, x):
    return int(''.join(map(str, x)))

def removeKdigits(self, num, k):
    k = 0
    list_num = list(num)
    list_num_2 = list_num[:]
    i = 0
    list_1 = []
    list_2 = []
    while i < len(list_num):
        del list_num[i]
        list_1 = list_num
        list_num = list_num_2
        i += 1
        del list_num[i]
        list_2 = list_num
        list_num = list_num_2
        i += 1
        if list_1 > list_2:
            self.smallest_num = self.join_list(list_2)
        else:
            self.smallest_num = self.join_list(list_1)
    return self.smallest_num
near epochBOT
#

@onyx sparrow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

near epochBOT
#

@onyx sparrow

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.