solving leetcode for fun
47 subscribers
6 photos
3 videos
127 links
Это мой личный канал здесь я делюсь решениями задач leetcode и иногда какими либо мыслями.
Download Telegram
public class Solution {
public double MaxAverageRatio(int[][] classes, int extraStudents) {
var pq = new PriorityQueue<int[], double>(Comparer<double>.Create((x, y) => y.CompareTo(x)));

foreach (var currentClass in classes) {
var a = (double) currentClass[0];
var b = (double) currentClass[1];
var res = ((a+1)/(b+1)) - (a/b);

pq.Enqueue(currentClass, res);
}

for (var i = 0; i < extraStudents; i++) {
var currentClass = pq.Dequeue();

currentClass[0] += 1;
currentClass[1] += 1;
var a = (double) currentClass[0];
var b = (double) currentClass[1];
var res = ((a+1)/(b+1)) - (a/b);

pq.Enqueue(currentClass, res);
}

double result = 0;
while (pq.TryDequeue(out int[] item, out double priority)) {
result += (double)item[0]/item[1];
}

return result / classes.Length;
}
}


https://leetcode.com/problems/maximum-average-pass-ratio/?envType=daily-question&envId=2025-09-01 - daily question

topics: #priority_queue #greedy

level: #medium
🏆5💔3🔥2🍓1💋1
public class Solution {
public string SortVowels(string s) {
var vowelsSet = new HashSet<char>{'A','a','E','e','I','i','O','o','U','u'};
var vowels = new List<char>();

for (var i = 0; i < s.Length; i++) {
if (vowelsSet.Contains(s[i])) {
vowels.Add(s[i]);
}
}
vowels.Sort();
var j = 0;
char[] result = s.ToCharArray();
for (var i = 0; i < s.Length; i++) {
if (vowelsSet.Contains(s[i])) {
result[i] = vowels[j];
j++;
}
}
return new string(result);
}
}


https://leetcode.com/problems/sort-vowels-in-a-string/?envType=daily-question&envId=2025-09-11 - daily question

topics: #string #sorting
level: #medium

TC: O(NlogN)
SC: O(N)
🔥4🐳2💋1
type Router struct {
limit int
queue [][3]int
storage map[[3]int]bool
destTimestampMap map[int][]int
}


func Constructor(memoryLimit int) Router {
return Router{
limit: memoryLimit,
queue: make([][3]int, 0, memoryLimit),
storage: make(map[[3]int]bool),
destTimestampMap: map[int][]int{},
}
}


func (this *Router) AddPacket(source int, destination int, timestamp int) bool {
packet := [3]int{source, destination, timestamp}
if this.storage[packet] {
return false
}

if len(this.queue) >= this.limit {
first := this.queue[0]
this.queue = this.queue[1:]
delete(this.storage, first)

timestamps := this.destTimestampMap[first[1]]
if len(timestamps) > 0 && first[2] == timestamps[0] {
this.destTimestampMap[first[1]] = timestamps[1:]
}
}

this.queue = append(this.queue, packet)
this.destTimestampMap[destination] = append(this.destTimestampMap[destination], timestamp)
this.storage[packet] = true
return true
}


func (this *Router) ForwardPacket() []int {
if len(this.queue) == 0 {
return []int{}
}

first := this.queue[0]
this.queue = this.queue[1:]
timestamps := this.destTimestampMap[first[1]]
if len(timestamps) > 0 && first[2] == timestamps[0] {
this.destTimestampMap[first[1]] = this.destTimestampMap[first[1]][1:]
}
delete(this.storage, [3]int{first[0], first[1], first[2]})
return []int{first[0], first[1], first[2]}
}


func (this *Router) GetCount(destination int, startTime int, endTime int) int {
timestamps, ok := this.destTimestampMap[destination]
if !ok {
return 0
}

return search(timestamps, endTime+1)-search(timestamps, startTime)
}

func search(times []int, val int) int {
l, r := 0, len(times)-1

for l <= r {
mid := l + (r-l)/2
if times[mid] >= val {
r = mid - 1
} else {
l = mid + 1
}
}

return l
}


https://leetcode.com/problems/implement-router/description/

topics: #array #queue #design_system #binary_search
level: #medium
🔥5❤4💋2
public class Solution {
public ListNode MergeInBetween(ListNode list1, int a, int b, ListNode list2) {
ListNode pointA = null;
ListNode pointB = null;

var curr = list1;
var i = 0;
while(curr != null) {
if (i == a-1) pointA = curr;

if (i == b+1) {
pointB = curr;
break;
}

curr = curr.next;
i++;
}

pointA.next = list2;
while(list2 != null & list2.next != null) {
list2 = list2.next;
}
list2.next = pointB;

return list1;
}
}


https://leetcode.com/problems/merge-in-between-linked-lists/ -i've started solving linked list problem, because i had been solving some linked list problems in go, thats why i've decided to solve in c#

topics: #linked_list
level: #medium
❤4🔥2💔1💋1
func getIntersectionNode(headA, headB *ListNode) *ListNode {
dymmyA := &ListNode{
Next: headA,
}
dymmyB := &ListNode{
Next: headB,
}
i := 0
for headA != nil {
headA = headA.Next
i++
}
j := 0
for headB != nil {
headB = headB.Next
j++
}
var (
f *ListNode
s *ListNode
)
k := 0
if j < i {
f = dymmyA.Next
s = dymmyB.Next
k = i-j
} else {
s = dymmyA.Next
f = dymmyB.Next
k = j-i
}

for f != nil && s != nil {
if s == f {
return s
}

if k > 0 {
f = f.Next
k--
} else {
f = f.Next
s = s.Next
}
}


return nil
}


https://leetcode.com/problems/intersection-of-two-linked-lists/

topics: #linked_list #two_pointers
level: #easy

note: I've solved it with O(1) memory, that why it is becoming medium problem
💋7❤1🔥1
I have achieved this badge, so it is not bad, but it is only beginning of journey ;)
🔥10❤2💘1
func minimumPushes(word string) int {
m := make([]int, 26)
for i := 0; i < len(word); i++ {
m[word[i]-97]++
}

result := 0
n := 0
for i, v := range m {
if m[i] == 0 { continue }
if n < 8 {
result += v
} else if n >= 8 && n < 16 {
result += (v*2)
} else if n >= 16 && n < 24 {
result += (v*3)
} else {
result += (v*4)
}
n++
}
return result
}


https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i/?envType=daily-question&envId=2026-07-30

topics: #string #math
level: #easy
❤4❤‍🔥3💘1
func smallestPalindrome(s string) string {
s = sortString(s)
m := map[byte]int{}
for i := 0; i < len(s); i++ {
m[s[i]]++
}
var oddChar byte
hasOdd := false
for k, v := range m {
if v % 2 != 0 {
oddChar = k
hasOdd = true
}
}

result := make([]byte, len(s))
if hasOdd {
result[len(s)/2] = oddChar
m[oddChar]--
}

j := len(s)-1
k := 0
i := 0
for i < len(s) {
if s[i] == oddChar && hasOdd {
i++
hasOdd = false
} else {
result[k], result[j] = s[i], s[i]
k++
j--
i+=2
}
}
return string(result)
}

func sortString(w string) string {
s := strings.Split(w, "")
sort.Strings(s)
return strings.Join(s, "")
}

https://leetcode.com/problems/smallest-palindromic-rearrangement-i/?envType=daily-question&envId=2026-07-28

https://leetcode.com/problems/smallest-palindromic-rearrangement-i/?envType=daily-question&envId=2026-07-28

topics: #sorting #math
level: #medium
❤3
public class Solution {
public int MaxArea(int[] height) {
var l = 0;
var r = height.Length-1;
var result = 0;

while (l < r) {
result = Math.Max(result, Math.Min(height[l], height[r])*(r-l));
if (height[l] < height[r]) l++;
else r--;
}

return result;
}
}


https://leetcode.com/problems/container-with-most-water/ - One of the classic problems at leetcode. Best problem to understanding the two pointers pattern.

topics: #array #two_pointers
level: #medium
❤2
func sortColors(nums []int) {
m := map[int]int{}
for _, n := range nums { m[n]++ }
currColor := 0
for i := 0; i < len(nums); i++ {
for m[currColor] == 0 {
currColor++
}
nums[i] = currColor
m[currColor]--
}
}

https://leetcode.com/problems/sort-colors/

TC: O(n)
SC: O(1) - map at most has 3 values

topics: #array #sorting
level: #medium
approach: The pretty straight forward solution used counting sort
❤1🔥1
🤣3🎉2🔥1👏1😁1
Forwarded from Programmer & IT Memes
Любая компания в 26 году

🖥️ IT Memes
Please open Telegram to view this post
VIEW IN TELEGRAM
😁1💯1
solving leetcode for fun
Photo
func shortestSubarray(nums []int, k int) int {
result := math.MaxInt
prefix := make([]int, len(nums)+1)
prefix[0] = 0
for i := 1; i < len(prefix); i++ {
prefix[i] = prefix[i-1]+nums[i-1]
}

maxdeq := []int{}
for r := 0; r < len(prefix); r++ {
for len(maxdeq) > 0 && prefix[r] < prefix[maxdeq[len(maxdeq) - 1]] {
maxdeq = maxdeq[:len(maxdeq) - 1]
}
maxdeq = append(maxdeq, r)

for len(maxdeq) > 0 && prefix[r] - prefix[maxdeq[0]] >= k {
result = min(result, r-maxdeq[0])
maxdeq = maxdeq[1:]
}

}
if result == math.MaxInt { return -1 }
return result
}

https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/

P.S. Obviously that I couldn't solve this problem. But I figured out to use sliding window technique+prefix sum. I've got remaining part of solution(max deque) from solutions.
👍2🔥2💋2
Time Complexity: O(1)
Space Complexity: O(1)
Brain Complexity: O(0)
😱2🍓2💋2👍1🔥1
func minimumTime(n int, relations [][]int, time []int) int {
graph := map[int][][2]int{}
for _, rel := range relations {
graph[rel[0]] = append(graph[rel[0]], [2]int{rel[1], time[rel[1]-1]})
}

memo := make([]int, n+1)
var fn func(node int) int
fn = func(node int) int {
if memo[node] != 0 {
return memo[node]
}

maxi := 0
for _, ngb := range graph[node] {
if memo[ngb[0]] != 0 {
maxi = max(memo[ngb[0]], maxi )
}
maxi = max(fn(ngb[0]), maxi)
}

memo[node] = time[node-1]+maxi
return memo[node]
}

result := 0
for i := 1; i <= n; i++ {
result = max(result, fn(i))
}

return result
}


https://leetcode.com/problems/parallel-courses-iii/

topics: #graph #memoization #dfs
level: #hard
🔥3❤1👍1