Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cSpell.words": [
"leetcode"
"leetcode",
"nums"
],
"go.buildTags": "local",
"go.testTags": "local",
Expand Down
4 changes: 3 additions & 1 deletion easy/2765.longest-alternating-subarray.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package code

/*
* @lc app=leetcode id=2765 lang=golang
*
Expand All @@ -6,7 +8,7 @@

// @lc code=start
func alternatingSubarray(nums []int) int {
return -1
}
// @lc code=end

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/paveg/LeetCode

go 1.24
22 changes: 22 additions & 0 deletions hard/23.merge-k-sorted-lists.go
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions hard/295.find-median-from-data-stream.go
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions hard/type.go
Original file line number Diff line number Diff line change
@@ -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
}
43 changes: 43 additions & 0 deletions medium/1151.minimum-swaps-to-group-all-1-s-together.go
Original file line number Diff line number Diff line change
@@ -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
77 changes: 77 additions & 0 deletions medium/146.lru-cache.go
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions medium/1529.minimum-suffix-flips.go
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions medium/200.number-of-islands.go
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions medium/2134.minimum-swaps-to-group-all-1-s-together-ii.go
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions medium/2554.maximum-number-of-integers-to-choose-from-a-range-i.go
Original file line number Diff line number Diff line change
@@ -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
Loading