и посмотреть медиа
Golang | LeetCode
и посмотреть медиа
LeetCode tasks in Golang. Solutions with code and explanations.
LeetCode tasks in Golang. Solutions with code and explanations.
AI bot for text-to-speech and voice cloning in Telegram. Create audio using neural network quickly and easily.
Channel with solutions to popular LeetCode tasks in Go. Focus on optimized algorithms, efficient memory usage, and idiomatic code. Helps prepare for technical interviews in companies that use Golang.
Задача: 1262. Greatest Sum Divisible by Three Сложность: medium Если задан целочисленный массив nums, верните максимально возможную сумму элементов массива, которая делится на три. Пример: Input: nums = [3,6,5,1,8] Output: 18 👨💻 Алгоритм: 1⃣Найдите сумму всех элементов массива. 2⃣Если сумма делится на 3, то она и есть ответ. 3⃣Если сумма при делении на 3 дает остаток 1, удалите один элемент с остатком 1 или два элемента с остатком 2 (если их сумма равна 2). Если сумма при делении на 3 дает остаток 2, удалите один элемент с остатком 2 или два элемента с остатком 1 (если их сумма равна 2). 😎 Решение: import ( "sort" ) func maxSumDivThree(nums []int) int { totalSum := 0 for _, num := range nums { totalSum += num } if totalSum % 3 == 0 { return totalSum } mod1 := []int{} mod2 := []int{} for _, num := range nums { if num % 3 == 1 { mod1 = append(mod1, num) } else if num % 3 == 2 { mod2 = append(mod2, num) } } sort.Ints(mod1) sort.Ints(mod2) if totalSum % 3 == 1 { remove1 := int(^uint(0) >> 1) // maximum int if len(mod1) > 0 { remove1 = mod1[0] } remove2 := int(^uint(0) >> 1) if len(mod2) >= 2 { remove2 = mod2[0] + mod2[1] } if remove1 < remove2 { return totalSum - remove1 } else { return totalSum - remove2 } } else { remove2 := int(^uint(0) >> 1) if len(mod2) > 0 { remove2 = mod2[0] } remove1 := int(^uint(0) >> 1) if len(mod1) >= 2 { remove1 = mod1[0] + mod1[1] } if remove2 < remove1 { return totalSum - remove2 } else { return totalSum - remove1 } } } Ставь 👍 и забирай 📚 Базу знаний
Открыть канал и посмотреть медиаOnly registered users can share their opinion.
Be the first to share your impression of this resource!