<commit_before>package tokens

func maxScore(tokens []int, p int) int {
	return 0
}<commit_msg>solve 948 use two pointers<commit_after>package tokens

import (
	"sort"

	"github.com/catorpilor/leetcode/utils"
)

func maxScore(tokens []int, p int) int {
	return useTwoPointers(tokens, p)
}

// useTwoPointers time complexity O(N*logN), space complexity O(1)
func useTwoPointers(tokens []int, p int) int {
	n := len(tokens)
	sort.Ints(tokens)
	l, r := 0, n-1
	// this is a greedy approach
	var ans, score int
	for l <= r {
		if p >= tokens[l] {
			score++
			p -= tokens[l]
			l++
			ans = utils.Max(ans, score)
		} else if p < tokens[l] && score > 0 {
			p += tokens[r]
			r--
			score--
		} else {
			break
		}
	}
	return ans
}
{'fraction_non_alphanumeric': 0.14305364511691884, 'fraction_numerical': 0.011004126547455296, 'mean_word_length': 3.1363636363636362, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5801746', 'n_tokens_mistral': 315, 'n_tokens_neox': 283, 'n_words': 84}