Excel Sheet Column Title - Leetcode 168 - Python

preview_player
Показать описание
Solving leetcode 168, Excel Sheet Column Title. Todays daily leetcode problem on august 21.

0:00 - Read the problem
0:30 - Drawing Explanation
8:50 - Coding Explanation

leetcode 168

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

how on earth is this question tagged easy?

alexisxander
Автор

Kinda happy to know I wasn't the only one who faced mild difficulty while solving an easy problem! 😅 I came up with a recursive solution but core idea is same.

nishantsrinivas
Автор

Starting a new leetcode subtopic is like opening up a can of worms. But I guess companies want you to know this for some reason.

ExecutionMods
Автор

very good video, i thought i wont understand this until i watched your video

yixiao
Автор

wasn't able to come up with the mathematical solution for this
Thanks NeetCode, also please upload video daily

nikhilgodase
Автор

def convertToTitle(columnTitle: str) -> int:
res = 0
for i, ch in
numForm = (ord(ch)-ord('A')+1)
res += numForm * (26**i)
return res

it's like 1234 -> 1*10^3 + 2*10^2 + 3*10^1 + 4*10^0

sergiofranklin
Автор

Thank you for great contents as always!
The following solution might be more readable for some people:

class Solution:
def convertToTitle(self, columnNumber: int) -> str:
res = ' '
while columnNumber > 0:
columnNumber -= 1 #Adjust for 1-based indexing
remainder = columnNumber % 26
res = chr(65 + remainder) + res
columnNumber = columnNumber // 26 #move to next digit
return res

shakhzod_shermatov
Автор

My leetcode's first question was this. And guess what i couldnt solve it and gave up

acridpie
Автор

I found this more intuitive
```
class Solution:
def convertToTitle(self, columnNumber: int) -> str:
results = ""

while columnNumber > 0:
mod = columnNumber % 26
mod = 26 if mod == 0 else mod
columnNumber -= mod
columnNumber //= 26
results = chr( ord('A') - 1 + mod) + results

return results
```

ShivangiSingh-wcgk
Автор

That third example was so weird. I multiplied 26*26 straight away and I just couldn't get it. Thanks for the explanation.

aadil
Автор

Haven't watched yet. Just venting. I've been working on this for about 5 hours now.

All I can think about is how in 7 days I have a tech interview to do and they want me to complete 2 problems in 45 minutes and they suggested I focus on medium leetcode problems.

I think I'm close. My last attempt solved the 704 out of 1002 testcases. I'm currently accurately solving for any input with one or two places. I can (stupidly) carry out my current logic for the max input length of 7 with a separate if statement for each possible length. If I were in someone else's shoes I wouldn't pass a person for something that clumsy. So, this just isn't good enough.

Close just doesn't matter. I don't want to do the brute force solution and iteratively count up (brute force is the same as failure for these interviews).

I'm trying to look at it like counting in base 26 instead of counting in base 10. Iterating through the input and using a multiplier for position.

zero
Автор

Was waiting for something I could understand

ngneerin
Автор

Recursive solution:
```
import string

def int_to_alpha(n: int) -> str:
i, j = divmod(n, 26)
if i:
return int_to_alpha(i - 1) + string.ascii_uppercase[j]
else:
return string.ascii_uppercase[j]

return int_to_alpha(columnNumber - 1)
```

steeve
Автор

I almost reached the solution, but at the same time, I almost gave up. There's a kind of pressure because of being tagged easy.

alexandersebastiangomezdel
Автор

This is one of the rare problems I stumbled upon the solution super quickly. I always love seeing how you work things out though

RagingAcid
Автор

@NeetCodeIO can you tell if there is inplace solution for lastIdx 2161? maybe some variation of dutch flag algo?

sanchitmishra
Автор

I don't know how to feel atbout the fact that this one is supposed to be "easy" haha

nicolasguillenc
Автор

It is really an easy one .

Sir please add all these daily problems Subsequently in the NeetCode All List 🙏

dk
Автор

Ngl it's pretty unclear as to how you come up with subtracting 1, I've spent several hours on this question and still can't derive that thought process from scratch

hieijaganshi
Автор

Changed the code a little. maybe using a map is easier to understand.

func ConvertToTitle(columnNumber int) string {
pairs := make(map[int]rune)
s := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i, r := range s {
pairs[i+1] = r
}
pairs[0] = 'Z'
// base case
if columnNumber <= 26 {
return string(pairs[columnNumber])
}

// fmt.Println(pairs)
var res []rune
for columnNumber > 0 {
fmt.Println("cn: ", columnNumber)
mod := columnNumber % 26
res = append([]rune{pairs[mod]}, res...)
// 这里必须是(columnNumber - 1) / 26
// 而不是 columnNumber / 2 , 这样会导致重复计算26的倍数。 比如702 = 26 * 26 + 26,前面已经计算了mod=0,所以要减少一个26的倍数
columnNumber = (columnNumber - 1) / 26

}

fmt.Println(string(res))
return string(res)

}

chibata_one