diff --git a/.vscode/settings.json b/.vscode/settings.json index 6fbb278..f4b9902 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "cSpell.words": [ - "leetcode" + "leetcode", + "nums" ], "go.buildTags": "local", "go.testTags": "local", diff --git a/easy/2765.longest-alternating-subarray.go b/easy/2765.longest-alternating-subarray.go index a5cd094..3c08f33 100644 --- a/easy/2765.longest-alternating-subarray.go +++ b/easy/2765.longest-alternating-subarray.go @@ -1,3 +1,5 @@ +package code + /* * @lc app=leetcode id=2765 lang=golang * @@ -6,7 +8,7 @@ // @lc code=start func alternatingSubarray(nums []int) int { - + return -1 } // @lc code=end diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e72a5e2 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/paveg/LeetCode + +go 1.24 diff --git a/hard/23.merge-k-sorted-lists.go b/hard/23.merge-k-sorted-lists.go new file mode 100644 index 0000000..070564c --- /dev/null +++ b/hard/23.merge-k-sorted-lists.go @@ -0,0 +1,22 @@ +package code + +/* + * @lc app=leetcode id=23 lang=golang + * + * [23] Merge k Sorted Lists + */ + +// TODO: implement +// @lc code=start +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func mergeKLists(lists []*ListNode) *ListNode { + return &ListNode{} +} + +// @lc code=end diff --git a/hard/295.find-median-from-data-stream.go b/hard/295.find-median-from-data-stream.go new file mode 100644 index 0000000..cde0b76 --- /dev/null +++ b/hard/295.find-median-from-data-stream.go @@ -0,0 +1,32 @@ +package code + +/* + * @lc app=leetcode id=295 lang=golang + * + * [295] Find Median from Data Stream + */ + +// TODO: Implement +// @lc code=start +type MedianFinder struct { +} + +func Constructor() MedianFinder { + return MedianFinder{} +} + +func (this *MedianFinder) AddNum(num int) { + +} + +func (this *MedianFinder) FindMedian() float64 { + return 0.0 +} + +/** + * Your MedianFinder object will be instantiated and called as such: + * obj := Constructor(); + * obj.AddNum(num); + * param_2 := obj.FindMedian(); + */ +// @lc code=end diff --git a/hard/type.go b/hard/type.go new file mode 100644 index 0000000..a917525 --- /dev/null +++ b/hard/type.go @@ -0,0 +1,16 @@ +//go:build local +// +build local + +package code + +// ローカル専用の ListNode 定義(LeetCode 側では読み込まれない) +type ListNode struct { + Val int + Next *ListNode +} + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} diff --git a/medium/1151.minimum-swaps-to-group-all-1-s-together.go b/medium/1151.minimum-swaps-to-group-all-1-s-together.go new file mode 100644 index 0000000..cc390a8 --- /dev/null +++ b/medium/1151.minimum-swaps-to-group-all-1-s-together.go @@ -0,0 +1,43 @@ +package code + +/* + * @lc app=leetcode id=1151 lang=golang + * + * [1151] Minimum Swaps to Group All 1's Together + */ + +// @lc code=start +func minSwaps(data []int) int { + // Count the number of 1's in the array + ones := 0 + for _, v := range data { + ones += v + } + // If there are no 1's, no swaps are needed, so return 0 + // If there is only one 1, no swaps are needed, so return 0 + if ones == 0 || ones == 1 { + return 0 + } + zeros := 0 + for i := 0; i < ones; i++ { + if data[i] == 0 { + zeros++ + } + } + minZeros := zeros + + for i := ones; i < len(data); i++ { + if data[i] == 0 { + zeros++ + } + if data[i-ones] == 0 { + zeros-- + } + if zeros < minZeros { + minZeros = zeros + } + } + return minZeros +} + +// @lc code=end diff --git a/medium/146.lru-cache.go b/medium/146.lru-cache.go new file mode 100644 index 0000000..b2a9071 --- /dev/null +++ b/medium/146.lru-cache.go @@ -0,0 +1,77 @@ +//go:build ignore + +package code + +/* + * @lc app=leetcode id=146 lang=golang + * + * [146] LRU Cache + */ + +// @lc code=start +type Node struct { + key, val int + prev, next *Node +} + +type LRUCache struct { + cap int + cache map[int]*Node + head, tail *Node +} + +func Constructor(capacity int) LRUCache { + head := &Node{} + tail := &Node{} + head.next = tail + tail.prev = head + + return LRUCache{cap: capacity, cache: make(map[int]*Node, capacity), head: head, tail: tail} +} + +func (l *LRUCache) remove(node *Node) { + node.prev.next = node.next + node.next.prev = node.prev +} + +func (l *LRUCache) insertHead(node *Node) { + node.next = l.head.next + node.prev = l.head + l.head.next.prev = node + l.head.next = node +} + +func (this *LRUCache) Get(key int) int { + if node, ok := this.cache[key]; ok { + this.remove(node) + this.insertHead(node) + return node.val + } + return -1 +} + +func (this *LRUCache) Put(key int, value int) { + if node, ok := this.cache[key]; ok { + node.val = value + this.remove(node) + this.insertHead(node) + return + } + + node := &Node{key: key, val: value} + this.cache[key] = node + this.insertHead(node) + if len(this.cache) > this.cap { + lru := this.tail.prev + this.remove(lru) + delete(this.cache, lru.key) + } +} + +/** + * Your LRUCache object will be instantiated and called as such: + * obj := Constructor(capacity); + * param_1 := obj.Get(key); + * obj.Put(key,value); + */ +// @lc code=end diff --git a/medium/1529.minimum-suffix-flips.go b/medium/1529.minimum-suffix-flips.go new file mode 100644 index 0000000..4e25ca5 --- /dev/null +++ b/medium/1529.minimum-suffix-flips.go @@ -0,0 +1,23 @@ +package code + +/* + * @lc app=leetcode id=1529 lang=golang + * + * [1529] Minimum Suffix Flips + */ + +// @lc code=start +func minFlips(target string) int { + flips := 0 + current := byte('0') + + for i := 0; i < len(target); i++ { + if target[i] != current { + flips++ + current = target[i] + } + } + return flips +} + +// @lc code=end diff --git a/medium/200.number-of-islands.go b/medium/200.number-of-islands.go new file mode 100644 index 0000000..d46d068 --- /dev/null +++ b/medium/200.number-of-islands.go @@ -0,0 +1,45 @@ +package code + +/* + * @lc app=leetcode id=200 lang=golang + * + * [200] Number of Islands + */ + +// @lc code=start +func numIslands(grid [][]byte) int { + land := byte('1') + water := byte('0') + + y := len(grid) + if y == 0 { + return 0 + } + x := len(grid[0]) + islandsCount := 0 + + var dfs func(i, j int) + dfs = func(i, j int) { + if i < 0 || i >= y || j < 0 || j >= x || grid[i][j] == water { + return + } + grid[i][j] = water + // Explore the four adjacent cells + dfs(i-1, j) + dfs(i+1, j) + dfs(i, j-1) + dfs(i, j+1) + } + + for i := 0; i < y; i++ { + for j := 0; j < x; j++ { + if grid[i][j] == land { + islandsCount++ + dfs(i, j) + } + } + } + return islandsCount +} + +// @lc code=end diff --git a/medium/2134.minimum-swaps-to-group-all-1-s-together-ii.go b/medium/2134.minimum-swaps-to-group-all-1-s-together-ii.go new file mode 100644 index 0000000..3313edb --- /dev/null +++ b/medium/2134.minimum-swaps-to-group-all-1-s-together-ii.go @@ -0,0 +1,47 @@ +//go:build ignore + +package code + +import "fmt" + +/* + * @lc app=leetcode id=2134 lang=golang + * + * [2134] Minimum Swaps to Group All 1's Together II + */ + +// @lc code=start +func minSwaps(nums []int) int { + ones := 0 + for _, v := range nums { + ones += v + } + fmt.Println(ones) + if ones == 0 { + return 0 + } + + n := len(nums) + zeros := 0 + for i := 0; i < ones; i++ { + if nums[i] == 0 { + zeros++ + } + } + minZeros := zeros + + for i := 0; i < n; i++ { + if nums[(i+ones)%n] == 0 { + zeros++ + } + if nums[i] == 0 { + zeros-- + } + if zeros < minZeros { + minZeros = zeros + } + } + return minZeros +} + +// @lc code=end diff --git a/medium/2554.maximum-number-of-integers-to-choose-from-a-range-i.go b/medium/2554.maximum-number-of-integers-to-choose-from-a-range-i.go new file mode 100644 index 0000000..4807695 --- /dev/null +++ b/medium/2554.maximum-number-of-integers-to-choose-from-a-range-i.go @@ -0,0 +1,30 @@ +package code + +/* + * @lc app=leetcode id=2554 lang=golang + * + * [2554] Maximum Number of Integers to Choose From a Range I + */ + +// @lc code=start +func maxCount(banned []int, n int, maxSum int) int { + mc := 0 + + bannedSet := make(map[int]struct{}) + for _, b := range banned { + bannedSet[b] = struct{}{} + } + + sum := 0 + // [1, 2, 3, ..., n] + for i := 1; i <= n; i++ { + _, banned := bannedSet[i] + if !banned && sum+i <= maxSum { + sum += i + mc++ + } + } + return mc +} + +// @lc code=end diff --git a/medium/438.find-all-anagrams-in-a-string.go b/medium/438.find-all-anagrams-in-a-string.go new file mode 100644 index 0000000..85b60bf --- /dev/null +++ b/medium/438.find-all-anagrams-in-a-string.go @@ -0,0 +1,35 @@ +package code + +/* + * @lc app=leetcode id=438 lang=golang + * + * [438] Find All Anagrams in a String + */ + +// @lc code=start +func findAnagrams(s string, p string) []int { + if len(s) < len(p) { + return nil + } + + result := []int{} + var sCount, pCount [26]int + for i := 0; i < len(p); i++ { + pCount[p[i]-'a']++ + sCount[s[i]-'a']++ + } + if sCount == pCount { + result = append(result, 0) + } + + for i := len(p); i < len(s); i++ { + sCount[s[i]-'a']++ + sCount[s[i-len(p)]-'a']-- + if sCount == pCount { + result = append(result, i-len(p)+1) + } + } + return result +} + +// @lc code=end diff --git a/medium/452.minimum-number-of-arrows-to-burst-balloons.go b/medium/452.minimum-number-of-arrows-to-burst-balloons.go new file mode 100644 index 0000000..6914eb1 --- /dev/null +++ b/medium/452.minimum-number-of-arrows-to-burst-balloons.go @@ -0,0 +1,35 @@ +package code + +import "sort" + +/* + * @lc app=leetcode id=452 lang=golang + * + * [452] Minimum Number of Arrows to Burst Balloons + */ + +// @lc code=start +func findMinArrowShots(points [][]int) int { + // [10,16],[2,8],[1,6],[7,12] + // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 + // ==================== + // ============= + // =========== + // ============== + result := 0 + sort.Slice(points, func(i, j int) bool { + return points[i][1] < points[j][1] + }) + result++ + xEnd := points[0][1] + for i := 1; i < len(points); i++ { + if points[i][0] > xEnd { + result++ + xEnd = points[i][1] + } + } + + return result +} + +// @lc code=end diff --git a/medium/452.minimum-number-of-arrows-to-burst-balloons.py b/medium/452.minimum-number-of-arrows-to-burst-balloons.py new file mode 100644 index 0000000..283821d --- /dev/null +++ b/medium/452.minimum-number-of-arrows-to-burst-balloons.py @@ -0,0 +1,12 @@ +# +# @lc app=leetcode id=452 lang=python3 +# +# [452] Minimum Number of Arrows to Burst Balloons +# + +# @lc code=start +class Solution: + def findMinArrowShots(self, points: List[List[int]]) -> int: + +# @lc code=end +